UNPKG

cross-os

Version:

Allow to add OS-specific scripts in your package.json!

56 lines (55 loc) 1.77 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var child_process_1 = require("child_process"); var path = require("path"); var platform = process.platform; /** * Grab package.json */ var pipeline = new Promise(function (resolve) { child_process_1.exec('npm prefix --no-workspaces').stdout.on('data', function (root) { resolve(require(path.resolve(root.toString('utf8').trim(), 'package.json'))); }); }).then(function (config) { /** * Check if the desired script exists */ var script, property = 'scripts'; var params = []; if (process.argv.length > 3) { script = process.argv[2]; params = process.argv.slice(3, process.argv.length); if (params.indexOf('--') === 0) { params.shift(); } } else { script = process.argv.pop(); } if (!config[property][script] || typeof config[property][script] !== 'object') { property = 'cross-os'; } try { return Promise.resolve({ command: config[property][script][platform], params: params, script: script }); } catch (e) { throw script; } }).then(function (_a) { var command = _a.command, params = _a.params, script = _a.script; /** * Execute the script */ if (command) { var proc = child_process_1.spawn(command, params, { stdio: 'inherit', shell: true }); /** * Propagate child exit code */ proc.on('exit', function (code, signal) { return process.exit(code); }); return proc; } throw script; }).catch(function (error) { console.log('\x1b[33m', "script: '" + error + "' not found for the current platform: " + platform + "\n", '\x1b[39m'); });