|
| 1 | +import { mkdir, Stats, stat, readFile, writeFile } from 'fs'; |
| 2 | +import * as glob from 'glob'; |
| 3 | +import { ncp } from 'ncp'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as pify from 'pify'; |
| 6 | +import { ChildProcess, ForkOptions, fork, SpawnOptions, spawn } from 'child_process'; |
| 7 | +import * as once from 'once'; |
| 8 | +import * as tmp from 'tmp'; |
| 9 | + |
| 10 | +export const BUILD_DIRECTORY = 'build'; |
| 11 | + |
| 12 | +export const globP: (pattern: string) => Promise<string[]> = pify(glob); |
| 13 | +export const mkdirP: (dir: string) => Promise<void> = pify(mkdir); |
| 14 | +export const ncpP: (src: string, dest: string) => Promise<void> = pify(ncp); |
| 15 | +export const readFileP: (path: string, encoding?: string) => Promise<Buffer|string> = pify(readFile); |
| 16 | +export const writeFileP: (path: string, data: Buffer|string, encoding?: string) => Promise<void> = pify(writeFile); |
| 17 | +export const statP: (path: string) => Promise<Stats> = pify(stat); |
| 18 | +export const tmpDirP: () => Promise<string> = pify(tmp.dir); |
| 19 | + |
| 20 | +export function nodule(nodule: string) { |
| 21 | + return path.relative(BUILD_DIRECTORY, `node_modules/${nodule}`); |
| 22 | +} |
| 23 | + |
| 24 | +export function flatten<T>(arr: Array<Array<T>>): Array<T> { |
| 25 | + return arr.reduce((acc, e) => acc.concat(e), []); |
| 26 | +} |
| 27 | + |
| 28 | +export function existsP(path: string): Promise<boolean> { |
| 29 | + return statP(path).then( |
| 30 | + () => Promise.resolve(true), |
| 31 | + () => Promise.resolve(false) |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function promisifyChildProcess(childProcess: ChildProcess): Promise<void> { |
| 36 | + return new Promise((resolve, reject) => { |
| 37 | + const exit = (err?: Error) => once(() => err ? reject(err) : resolve())(); |
| 38 | + childProcess.on('error', exit); |
| 39 | + childProcess.on('close', (code) => { |
| 40 | + if (code === 0) { |
| 41 | + exit(); |
| 42 | + } else { |
| 43 | + exit(new Error(`Process ${childProcess.pid} exited with code ${code}.`)); |
| 44 | + } |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +export function spawnP(command: string, args?: string[], options?: SpawnOptions): Promise<void> { |
| 50 | + const stringifiedCommand = `\`${command}${args ? (' ' + args.join(' ')) : ''}\``; |
| 51 | + console.log(`> Running: ${stringifiedCommand}`); |
| 52 | + return promisifyChildProcess(spawn(command, args, Object.assign({ |
| 53 | + stdio: 'inherit', |
| 54 | + shell: true |
| 55 | + }, options))); |
| 56 | +} |
| 57 | + |
| 58 | +export function forkP(moduleName: string, args?: string[], options?: ForkOptions): Promise<void> { |
| 59 | + const stringifiedCommand = `\`${moduleName}${args ? (' ' + args.join(' ')) : ''}\``; |
| 60 | + console.log(`> Running: ${stringifiedCommand}`); |
| 61 | + return promisifyChildProcess(fork(moduleName, args, Object.assign({ |
| 62 | + stdio: 'inherit' |
| 63 | + }, options))); |
| 64 | +} |
0 commit comments