UNPKG

ci-sf-plugin

Version:

Set of commands making CI and dev's life easier.

101 lines 5.02 kB
/* * 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', 'package.install'); export default class Install 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) 'security-type': Flags.string({ char: 's', options: ['AllUsers', 'AdminsOnly'], default: 'AdminsOnly', description: messages.getMessage('securitytypeFlagDescription') }), 'package': Flags.string({ char: 'p', description: messages.getMessage('packageFlagDescription') }), '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(Install); // fetch needed flags const targetOrgUsername = flags['target-org'].getUsername(); const securityTypeFlag = flags['security-type']; const packageFlag = flags['package']; const ciConfigFileFlag = flags['ci-config-file']; // TODO commented out as package:install has changed behavior // ux.action.start(messages.getMessage('infoInstallingPackages', [targetOrgUsername]), 'in progress', {stdout: true}); // load configuration let packageIds = []; if (packageFlag) { packageIds.push({ id: packageFlag }); } else { const config = JSON.parse(readFileSync(ciConfigFileFlag, 'utf-8')); if (!config.packageIds || !Array.isArray(config.packageIds)) { throw new Error(`'packageIds' property is missing in the '${ciConfigFileFlag}' file, or is not an array.`); } packageIds = config.packageIds; } const output = { stdout: [], stderr: [] }; for (const packageIdObj of packageIds) { if (!packageIdObj.id) { throw new Error(`'packageIds' array item '${packageIdObj}' doesn't have required 'id' property.`); } const packageInfo = packageIdObj.id + (packageIdObj.name ? ', name: ' + packageIdObj.name : '') + (packageIdObj.versionNumber ? ', version number: ' + packageIdObj.versionNumber : ''); process.stdout.write(`Installing package: ${packageInfo}.\n`); // build the command, without --json to see details const commandStr = `sf package install --package ${packageIdObj.id} --wait 60 --security-type ${securityTypeFlag} --no-prompt --target-org ${targetOrgUsername}`; // execute and print output const commandPromise = execSync(commandStr, { encoding: 'utf8', maxBuffer: Infinity }); 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); } }); 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('errorInstallationFailed', [JSON.stringify(error, null, 2)])); } } } //# sourceMappingURL=install.js.map