|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +/* eslint-disable no-console */ |
| 5 | +const child_process = require('child_process'); |
| 6 | +const mkdirSync = require('fs').mkdirSync; |
| 7 | +const chalk = require('chalk'); |
| 8 | +const blue = chalk.blue; |
| 9 | +const yellow = chalk.yellow; |
| 10 | + |
| 11 | +function exec(options, cmd, args) { |
| 12 | + let stdout = ''; |
| 13 | + let stderr = ''; |
| 14 | + const cwd = process.cwd(); |
| 15 | + console.log( |
| 16 | + `==========================================================================================` |
| 17 | + ); |
| 18 | + |
| 19 | + args = args.filter(x => x !== undefined); |
| 20 | + const flags = [ |
| 21 | + options.silent && 'silent', |
| 22 | + options.waitForMatch && `matching(${options.waitForMatch})` |
| 23 | + ] |
| 24 | + .filter(x => !!x) // Remove false and undefined. |
| 25 | + .join(', ') |
| 26 | + .replace(/^(.+)$/, ' [$1]'); // Proper formatting. |
| 27 | + |
| 28 | + console.log(blue(`Running \`${cmd} ${args.map(x => `"${x}"`).join(' ')}\`${flags}...`)); |
| 29 | + console.log(blue(`CWD: ${cwd}`)); |
| 30 | + const spawnOptions = {cwd}; |
| 31 | + |
| 32 | + if (process.platform.startsWith('win')) { |
| 33 | + args.unshift('/c', cmd); |
| 34 | + cmd = 'cmd.exe'; |
| 35 | + spawnOptions['stdio'] = 'pipe'; |
| 36 | + } |
| 37 | + |
| 38 | + const childProcess = child_process.spawn(cmd, args, spawnOptions); |
| 39 | + childProcess.stdout.on('data', (data) => { |
| 40 | + stdout += data.toString('utf-8'); |
| 41 | + if (options.silent) { |
| 42 | + return; |
| 43 | + } |
| 44 | + data.toString('utf-8') |
| 45 | + .split(/[\n\r]+/) |
| 46 | + .filter(line => line !== '') |
| 47 | + .forEach(line => console.log(' ' + line)); |
| 48 | + }); |
| 49 | + childProcess.stderr.on('data', (data) => { |
| 50 | + stderr += data.toString('utf-8'); |
| 51 | + if (options.silent) { |
| 52 | + return; |
| 53 | + } |
| 54 | + data.toString('utf-8') |
| 55 | + .split(/[\n\r]+/) |
| 56 | + .filter(line => line !== '') |
| 57 | + .forEach(line => console.error(yellow(' ' + line))); |
| 58 | + }); |
| 59 | + |
| 60 | + _processes.push(childProcess); |
| 61 | + |
| 62 | + // Create the error here so the stack shows who called this function. |
| 63 | + const err = new Error(`Running "${cmd} ${args.join(' ')}" returned error code `); |
| 64 | + return new Promise((resolve, reject) => { |
| 65 | + childProcess.on('exit', (error) => { |
| 66 | + _processes = _processes.filter(p => p !== childProcess); |
| 67 | + |
| 68 | + if (!error) { |
| 69 | + resolve({ stdout }); |
| 70 | + } else { |
| 71 | + err.message += `${error}...\n\nSTDOUT:\n${stdout}\n`; |
| 72 | + reject(err); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + if (options.waitForMatch) { |
| 77 | + childProcess.stdout.on('data', (data) => { |
| 78 | + if (data.toString().match(options.waitForMatch)) { |
| 79 | + resolve({ stdout, stderr }); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | +- install CLI via master |
| 88 | +- new new foo |
| 89 | +- cd foo |
| 90 | +- ng build -prod (to force AOT) |
| 91 | +- if successful push yarn.lock to a separate repo tagged w/ master's hash? |
| 92 | +*/ |
| 93 | + |
| 94 | +Promise.resolve() |
| 95 | + .then(() => mkdirSync('tmp')) |
| 96 | + .then(() => exec({}, 'ng', ['new', '--'])); |
0 commit comments