UNPKG

piral-cli

Version:

The standard CLI for creating and building a Piral instance or a Pilet.

72 lines 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runScript = runScript; exports.runCommand = runCommand; const child_process_1 = require("child_process"); const path_1 = require("path"); const log_1 = require("./log"); const info_1 = require("./info"); const MemoryStream_1 = require("./MemoryStream"); function resolveWinPath(specialFolder, subPath) { const basePath = process.env[specialFolder]; if (basePath) { return (0, path_1.resolve)(basePath, subPath); } return undefined; } function runScript(script, cwd = process.cwd(), output = process.stdout) { const bin = (0, path_1.resolve)(cwd, './node_modules/.bin'); const sep = info_1.isWindows ? ';' : ':'; const env = Object.assign({}, process.env); (0, log_1.log)('generalDebug_0003', `Running "${script}" in "${cwd}" ("${bin}").`); if (info_1.isWindows) { // on windows we sometimes may see a strange behavior, // see https://github.com/smapiot/piral/issues/192 const newPaths = [ resolveWinPath('AppData', 'npm'), resolveWinPath('ProgramFiles', 'nodejs'), resolveWinPath('ProgramFiles(x86)', 'nodejs'), ...(env.Path || env.PATH || '').split(';'), ]; env.PATH = newPaths.filter((path) => path && path.length > 0).join(sep); } env.PATH = `${bin}${sep}${env.PATH}`; return new Promise((resolve, reject) => { const error = new MemoryStream_1.MemoryStream(); const opt = { end: false }; const cp = (0, child_process_1.exec)(script, { cwd, env, }); cp.stdout.pipe(output, opt); cp.stderr.pipe(error, opt); cp.on('error', () => reject(new Error(error.value))); cp.on('close', (code) => (code === 0 ? resolve() : reject(new Error(error.value)))); }); } function runCommand(exe, args, cwd, output) { const npmCommand = info_1.isWindows ? `${exe}.cmd` : exe; const sanitizedArgs = sanitizeCmdArgs(args); const cmd = [npmCommand, ...sanitizedArgs].join(' '); (0, log_1.log)('generalDebug_0003', `Applying cmd "${cmd}" in directory "${cwd}".`); return runScript(cmd, cwd, output); } function sanitizeCmdArgs(args) { // Introduced for fixing https://github.com/smapiot/piral/issues/259. // If an arg contains a whitespace, it can be incorrectly interpreted as two separate arguments. // For the moment, it's fixed by simply wrapping each arg in OS specific quotation marks. const quote = info_1.isWindows ? '"' : "'"; return args.map((arg) => { let result = arg.trim(); if (/\s/.test(result)) { if (!result.startsWith(quote)) { result = `${quote}${result}`; } if (!result.endsWith(quote)) { result = `${result}${quote}`; } } return result; }); } //# sourceMappingURL=scripts.js.map