ci-sf-plugin
Version:
Set of commands making CI and dev's life easier.
88 lines • 4.13 kB
JavaScript
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as os from 'os';
import * as util from 'util';
import * as childProcess from 'child_process';
import { readFileSync } from 'fs';
import { SfCommand, Flags, requiredOrgFlagWithDeprecations } from '@salesforce/sf-plugins-core';
import { Messages, SfError } from '@salesforce/core';
import { ux } from '@oclif/core';
// promisify child process
const execSync = util.promisify(childProcess.exec);
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages('ci-sf-plugin', 'user.assign.permset');
export default class Permset extends SfCommand {
static description = messages.getMessage('commandDescription');
static examples = messages.getMessage('examples').split(os.EOL);
static flags = {
'target-org': requiredOrgFlagWithDeprecations,
// flag with a value (-n, --name=VALUE)
'permset-name': Flags.string({
char: 'n',
description: messages.getMessage('permsetnameFlagDescription')
}),
'ci-config-file': Flags.file({
char: 'x',
default: 'ciconfig.json',
description: messages.getMessage('ciconfigfileFlagDescription')
})
};
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
static requiresProject = false;
async run() {
try {
const { flags } = await this.parse(Permset); // fetch needed flags
const targetOrgUsername = flags['target-org'].getUsername();
const permsetNameFlag = flags['permset-name'];
const ciConfigFileFlag = flags['ci-config-file'];
ux.action.start(messages.getMessage('infoAssigningPermsets', [targetOrgUsername]), 'in progress', { stdout: true });
// load configuration
let permsets = [];
if (permsetNameFlag) {
permsets.push(permsetNameFlag);
}
else {
const config = JSON.parse(readFileSync(ciConfigFileFlag, 'utf-8'));
if (!config.permsets || !Array.isArray(config.permsets)) {
throw new Error(`'permsets' property is missing in the '${ciConfigFileFlag}' file, or is not an array.`);
}
permsets = config.permsets;
}
const output = { stdout: [], stderr: [] };
for (const permset of permsets) {
// build the command, without --json to see details
const commandStr = `sf org assign permset --name ${permset} --target-org ${targetOrgUsername}`;
// execute and print output
const commandPromise = execSync(commandStr, { encoding: 'utf8', maxBuffer: 1024 * 1024 });
commandPromise.child.stdout.on('data', (data) => {
if (data?.trim()) {
process.stdout.write(data);
}
});
commandPromise.child.stderr.on('data', (data) => {
if (data?.trim()) {
process.stderr.write(data.trim());
}
});
const { stdout, stderr } = await commandPromise;
output.stdout.push(stdout);
output.stderr.push(stderr);
}
ux.action.stop('done');
// Return an object
return { output };
}
catch (error) {
ux.action.stop('failed');
throw new SfError(messages.getMessage('errorAssignmentFailed', [JSON.stringify(error, null, 2)]));
}
}
}
//# sourceMappingURL=permset.js.map