ci-sf-plugin
Version:
Set of commands making CI and dev's life easier.
95 lines • 4.23 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 { SfCommand, Flags, requiredOrgFlagWithDeprecations } from '@salesforce/sf-plugins-core';
import { Messages, SfError } from '@salesforce/core';
import { readFileSync, writeFileSync } from 'fs';
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', 'source.push');
export default class Push 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)
'ci': Flags.boolean({
default: false,
description: messages.getMessage('ciFlagDescription')
}),
'ci-config-file': Flags.file({
char: 'x',
default: 'ciconfig.json',
description: messages.getMessage('ciconfigfileFlagDescription')
})
};
async run() {
try {
const { flags } = await this.parse(Push);
const targetOrgUsername = flags['target-org'].getUsername();
const ciConfigFileFlag = flags['ci-config-file'];
ux.action.start(messages.getMessage('infoPushing', [targetOrgUsername]), 'in progress', { stdout: true });
if (flags.ci) {
const config = JSON.parse(readFileSync(ciConfigFileFlag, 'utf-8'));
if (!config.ciForceignore || !Array.isArray(config.ciForceignore)) {
throw new Error(`'ciForceignore' property is missing in the '${ciConfigFileFlag}' file, or is not an array.`);
}
updateForceignoreFile(config.ciForceignore);
}
// build the command, without --json to see details
const commandStr = `sf project deploy start --target-org ${targetOrgUsername} --ignore-warnings --ignore-conflicts`;
// 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.trim());
}
});
const { stdout, stderr } = await commandPromise;
ux.action.stop('done');
// Return an object
return { stdout, stderr };
}
catch (error) {
ux.action.stop('failed');
throw new SfError(messages.getMessage('errorPushingFailed', [JSON.stringify(error, null, 2)]));
}
}
}
/**
* Update '.forceignore'. Add additional lines (files to ignore) from 'ciconfig.json' file.
*
* @param {any} pathsToIgnore - array of relative/absolute paths to ignore
*
* @author Kamil Malecek, BearingPoint
* @date 2022-09-15
*/
function updateForceignoreFile(pathsToIgnore) {
const forceignoreFilename = '.forceignore';
const forceignore = readFileSync(forceignoreFilename, 'utf-8')?.split(/\r?\n/);
if (!Array.isArray(forceignore)) {
throw new Error(`'.forceignore' file is not available.`);
}
// add new paths to be ignored
pathsToIgnore.forEach(path => {
forceignore.push(path);
});
writeFileSync(forceignoreFilename, forceignore.join('\n'));
}
//# sourceMappingURL=push.js.map