qforce
Version:
Commands to help with salesforce development.
89 lines (88 loc) • 4.4 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');
const csvjson = require('csvjson');
class Query extends command_1.Command {
async run() {
cli_ux_1.default.action.start('Querying data');
const { flags } = this.parse(Query);
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.queryFilePath || 'query.soql';
const resultPath = flags.result || settings.queryResultsPath || 'query.csv';
if (resultPath.includes('/')) {
let resultPathArray = resultPath.split('/');
resultPathArray.pop();
if (!fs.existsSync(path.join(process.cwd(), ...resultPathArray))) {
fs.mkdirSync(path.join(process.cwd(), ...resultPathArray), { recursive: true });
}
}
let targetusername = flags.username || settings.targetusername || sfdxConfig.defaultusername;
let queryString = flags.query || fs.readFileSync(utility_1.getAbsolutePath(filePath), 'utf8');
if (queryString.startsWith('//')) {
const firstLine = queryString.split(/\n/, 1)[0];
targetusername = firstLine.substring(2).trim();
queryString = queryString.substring(queryString.toLowerCase().indexOf('\n'));
}
queryString = queryString.trim();
if (!queryString.toLowerCase().includes('select')) {
let sobjectMapPath = utility_1.getAbsolutePath('.qforce/definitions/sobjectsByPrefix.json');
if (!fs.existsSync(sobjectMapPath)) {
cli_ux_1.default.action.stop('Mapping file does not exist.');
return;
}
let sobjectByPrefix = JSON.parse(fs.readFileSync(sobjectMapPath));
let sobject = sobjectByPrefix[queryString.substring(0, 3)];
queryString = 'SELECT * FROM ' + sobject + ' WHERE ID = \'' + queryString + '\'';
}
if (queryString.includes('*')) {
try {
queryString = await utility_1.getQueryAll(queryString, targetusername, false);
this.log('Complete Query: \n' + queryString);
}
catch (err) {
cli_ux_1.default.action.stop('Receied error: ' + err);
return;
}
}
let options = {};
options.json = true;
options.query = queryString;
if (targetusername)
options.targetusername = targetusername;
options.json = true;
options._rejectOnError = true;
sfdx.data.soqlQuery(options).then((queryResult) => {
fs.writeFileSync(utility_1.getAbsolutePath(resultPath.replace(/\.csv$/, '.json')), JSON.stringify(queryResult, null, 4), { encoding: 'utf-8' });
queryResult.records.map(utility_1.prepJsonForCsv);
fs.writeFileSync(utility_1.getAbsolutePath(resultPath), csvjson.toCSV(queryResult.records, { headers: 'relative', wrap: true }), { encoding: 'utf-8' });
cli_ux_1.default.action.stop();
}).catch((error) => {
//let errorLog = JSON.parse(error)
cli_ux_1.default.action.stop(error[0].message);
});
}
}
exports.default = Query;
Query.description = 'Run a SOQL and save results to csv.';
Query.aliases = ['query', 'q', 'dx:query'];
Query.examples = [
`$ qforce dx:query`,
];
Query.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 query file in unix format.' }),
query: command_1.flags.string({ char: 'q', description: 'SOQL query as string.' }),
result: command_1.flags.string({ char: 'r', description: 'Relative path to save results of query.' })
};