@salesforce/plugin-release-management
Version:
A plugin for preparing and publishing npm packages
135 lines • 5.17 kB
JavaScript
"use strict";
/*
* 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
*/
/* eslint-disable @typescript-eslint/no-unused-vars */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginCommand = void 0;
const os_1 = require("os");
const path = require("path");
const fs = require("fs");
const npm_run_path_1 = require("npm-run-path");
const shelljs = require("shelljs");
const core_1 = require("@salesforce/core");
const kit_1 = require("@salesforce/kit");
class PluginCommand extends kit_1.AsyncCreatable {
constructor(options) {
super(options);
this.options = options;
}
async init() {
this.logger = await core_1.Logger.child(this.constructor.name);
this.pkgPath = require.resolve(path.join(this.options.npmName, 'package.json'), { paths: [this.options.cliRoot] });
this.pkg = JSON.parse(fs.readFileSync(this.pkgPath, 'utf8'));
this.nodeExecutable = this.findNode(this.options.cliRoot);
this.bin = this.getBin();
}
runPluginCmd(options) {
const command = `"${this.nodeExecutable}" "${this.bin}" ${options.command} ${options.parameters
.map((p) => `"${p}"`)
.join(' ')}`;
delete options.command;
delete options.parameters;
this.logger.debug(`Running plugin command ${command}`);
const results = shelljs.exec(command, {
...options,
silent: true,
fatal: true,
async: false,
env: npm_run_path_1.default.env({ env: process.env }),
});
if (results.code !== 0) {
this.logger.debug(`Plugin command ${command} failed`, results.stderr);
throw new core_1.SfError(results.stderr, 'ShellExecError');
}
this.logger.debug(`Plugin command ${command} succeeded`, results);
try {
if (options.parameters?.includes('--json')) {
return JSON.parse(results.stdout);
}
else {
return results;
}
}
catch (error) {
const sfError = new core_1.SfError(error, 'JsonParseError');
this.logger.debug(`Plugin command ${command} threw eception`, sfError);
throw sfError;
}
}
packagePath() {
return this.pkgPath;
}
/**
* Returns the path to the defined bin file in this package's node_modules
*
* @private
*/
getBin() {
const pkgPath = this.packagePath();
const prjPath = pkgPath.substring(0, pkgPath.lastIndexOf(path.sep));
if (!this.pkg.bin[this.options.commandBin]) {
const sfdxError = new core_1.SfError(`Could not locate commandBin ${this.options.commandBin} in package at path ${pkgPath}`);
this.logger.debug(sfdxError);
throw sfdxError;
}
return path.join(prjPath, this.pkg.bin[this.options.commandBin]);
}
/**
* Locate node executable and return its absolute path
* First it tries to locate the node executable on the root path passed in
* If not found then tries to use whatever 'node' resolves to on the user's PATH
* If found return absolute path to the executable
* If the node executable cannot be found, an error is thrown
*
* @private
*/
findNode(root = undefined) {
const isExecutable = (filepath) => {
if ((0, os_1.type)() === 'Windows_NT')
return filepath.endsWith('node.exe');
try {
if (filepath.endsWith('node')) {
// This checks if the filepath is executable on Mac or Linux, if it is not it errors.
fs.accessSync(filepath, fs.constants.X_OK);
return true;
}
}
catch {
return false;
}
return false;
};
if (root) {
const sfdxBinDirs = this.findSfdxBinDirs(root);
if (sfdxBinDirs.length > 0) {
// Find the node executable
const node = shelljs.find(sfdxBinDirs).filter((file) => isExecutable(file))[0];
if (node) {
return fs.realpathSync(node);
}
}
}
// Check to see if node is installed
const nodeShellString = shelljs.which('node');
if (nodeShellString?.code === 0 && nodeShellString?.stdout)
return nodeShellString.stdout;
throw new core_1.SfError('Cannot locate node executable.', 'CannotFindNodeExecutable');
}
/**
* Finds the bin directory in the sfdx installation root path
*
* @param sfdxPath
* @private
*/
findSfdxBinDirs(sfdxPath) {
return sfdxPath
? [path.join(sfdxPath, 'bin'), path.join(sfdxPath, 'client', 'bin')].filter((p) => fs.existsSync(p))
: [];
}
}
exports.PluginCommand = PluginCommand;
//# sourceMappingURL=pluginCommand.js.map