UNPKG

@commercelayer/cli

Version:
69 lines (68 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable @typescript-eslint/no-misused-promises */ const cli_core_1 = require("@commercelayer/cli-core"); const core_1 = require("@oclif/core"); const child_process_1 = require("child_process"); // import { detect as detectPackageManager } from 'detect-package-manager' class CliUpdate extends core_1.Command { static description = 'Update Commerce Layer CLI'; static aliases = ['upgrade', 'latest']; static examples = [ '<%= config.bin %> <%= command.id %> --version=<version-or-tag>', ]; static flags = { version: core_1.Flags.string({ char: 'v', description: 'update CLI to a specific version or tag', required: false, }) }; async run() { const { flags } = await this.parse(CliUpdate); const version = flags.version ? `@${flags.version}` : ''; // await detectPackageManager().then(pm => this.log) this.log(); core_1.ux.action.start('Updating Commerce Layer CLI'); const cp = (0, child_process_1.exec)(`npm install --location=global @commercelayer/cli${version}`, async (error, stdout, stderr) => { const errorMessage = this.getErrorMessage(error, stdout, stderr); if (errorMessage) { core_1.ux.action.stop('failed'); this.log(errorMessage); this.error('CLI update failed.'); } else { core_1.ux.action.stop(); await this.showCliVersion(); } }); cp.on('exit', async (code, signal) => { if (code || signal) { this.log(cli_core_1.clColor.msg.error('Unexpected error occurred')); this.log(); this.log(`Code: ${code}`); this.log(`Signal: ${signal}`); this.log(); process.exit(); } }); } getErrorMessage(error, stdout, stderr) { let errorMessage = ''; if (error) errorMessage = error.message; if (stderr && !['Reshimming asdf nodejs', 'WARN', 'warn', 'npm'].some(str => stderr.includes(str))) errorMessage = String(stderr); return errorMessage; } async showCliVersion() { try { const verCmd = await this.config.findCommand('cli:version')?.load(); await verCmd?.run([]); // without [] the command uses args of command 'version' and it fails } catch (err) { this.log(); } } } exports.default = CliUpdate;