qforce
Version:
Commands to help with salesforce development.
54 lines (53 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@oclif/command");
const cli_ux_1 = require("cli-ux");
const utility_1 = require("../../helper/utility");
const sfdx = require('sfdx-node');
const path = require('path');
const fs = require('fs');
class Exe extends command_1.Command {
async run() {
cli_ux_1.default.action.start('Executing anonymous script');
const { flags } = this.parse(Exe);
let settings, sfdxConfig;
if (fs.existsSync(path.join(process.cwd(), '.qforce', 'settings.json'))) {
settings = JSON.parse(fs.readFileSync(path.join(process.cwd(), '.qforce', 'settings.json')));
}
if (fs.existsSync(path.join(process.cwd(), '.sfdx', 'sfdx-config.json'))) {
sfdxConfig = JSON.parse(fs.readFileSync(path.join(process.cwd(), '.sfdx', 'sfdx-config.json')));
}
const filePath = flags.file || settings.exeFilePath || 'exe.cls';
const resultPath = flags.result || settings.exeResultsPath || 'exe.log';
let targetusername = flags.username || settings.targetusername || sfdxConfig.defaultusername;
const fileContent = fs.readFileSync(utility_1.getAbsolutePath(filePath), 'utf8');
const firstLine = fileContent.split(/\n/, 1)[0];
if (firstLine.startsWith('//')) {
targetusername = firstLine.substring(2).trim();
}
let options = {};
options._rejectOnError = true;
options._quiet = false;
options.apexcodefile = utility_1.getAbsolutePath(filePath);
if (targetusername)
options.targetusername = targetusername;
sfdx.apex.execute(options).then((result) => {
fs.writeFileSync(utility_1.getAbsolutePath(resultPath), result.logs, { encoding: 'utf-8' });
cli_ux_1.default.action.stop();
}).catch((error) => {
cli_ux_1.default.action.stop();
});
}
}
exports.default = Exe;
Exe.description = 'Execute anonymous apex.';
Exe.aliases = ['exe', 'dx:exe'];
Exe.examples = [
`$ q dx:exe`,
];
Exe.flags = {
help: command_1.flags.help({ char: 'h' }),
username: command_1.flags.string({ char: 'u' }),
file: command_1.flags.string({ char: 'f', description: 'Relative path of apex file in unix format.' }),
result: command_1.flags.string({ char: 'r', description: 'Relative path to save results.' })
};