|
| 1 | +// @flow |
| 2 | +import fs from 'fs'; |
| 3 | +import os from 'os'; |
| 4 | +import path from 'path'; |
| 5 | +import {createDirectory} from 'jest-util'; |
| 6 | +import rimraf from 'rimraf'; |
| 7 | +import execa from 'execa'; |
| 8 | +import {Writable} from 'readable-stream'; |
| 9 | + |
| 10 | +const CLI_PATH = path.resolve(__dirname, '../packages/cli/build/bin.js'); |
| 11 | + |
| 12 | +type RunOptions = { |
| 13 | + nodeOptions?: string, |
| 14 | + nodePath?: string, |
| 15 | + timeout?: number, // kill the process after X milliseconds |
| 16 | +}; |
| 17 | + |
| 18 | +export function run( |
| 19 | + dir: string, |
| 20 | + args?: Array<string>, |
| 21 | + options: RunOptions = {}, |
| 22 | +) { |
| 23 | + return spawnCli(dir, args, options); |
| 24 | +} |
| 25 | + |
| 26 | +// Runs cli until a given output is achieved, then kills it with `SIGTERM` |
| 27 | +export async function runUntil( |
| 28 | + dir: string, |
| 29 | + args: Array<string> | void, |
| 30 | + text: string, |
| 31 | + options: RunOptions = {}, |
| 32 | +) { |
| 33 | + const spawnPromise = spawnCliAsync(dir, args, {timeout: 30000, ...options}); |
| 34 | + |
| 35 | + spawnPromise.stderr.pipe( |
| 36 | + new Writable({ |
| 37 | + write(chunk, _encoding, callback) { |
| 38 | + const output = chunk.toString('utf8'); |
| 39 | + |
| 40 | + if (output.includes(text)) { |
| 41 | + spawnPromise.kill(); |
| 42 | + } |
| 43 | + |
| 44 | + callback(); |
| 45 | + }, |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | + return spawnPromise; |
| 50 | +} |
| 51 | + |
| 52 | +export const makeTemplate = ( |
| 53 | + str: string, |
| 54 | +): ((values?: Array<any>) => string) => (values?: Array<any>) => |
| 55 | + str.replace(/\$(\d+)/g, (_match, number) => { |
| 56 | + if (!Array.isArray(values)) { |
| 57 | + throw new Error('Array of values must be passed to the template.'); |
| 58 | + } |
| 59 | + return values[number - 1]; |
| 60 | + }); |
| 61 | + |
| 62 | +export const cleanup = (directory: string) => rimraf.sync(directory); |
| 63 | + |
| 64 | +/** |
| 65 | + * Creates a nested directory with files and their contents |
| 66 | + * writeFiles( |
| 67 | + * '/home/tmp', |
| 68 | + * { |
| 69 | + * 'package.json': '{}', |
| 70 | + * 'dir/file.js': 'module.exports = "x";', |
| 71 | + * } |
| 72 | + * ); |
| 73 | + */ |
| 74 | +export const writeFiles = ( |
| 75 | + directory: string, |
| 76 | + files: {[filename: string]: string}, |
| 77 | +) => { |
| 78 | + createDirectory(directory); |
| 79 | + Object.keys(files).forEach(fileOrPath => { |
| 80 | + const dirname = path.dirname(fileOrPath); |
| 81 | + |
| 82 | + if (dirname !== '/') { |
| 83 | + createDirectory(path.join(directory, dirname)); |
| 84 | + } |
| 85 | + fs.writeFileSync( |
| 86 | + path.resolve(directory, ...fileOrPath.split('/')), |
| 87 | + files[fileOrPath], |
| 88 | + ); |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +export const copyDir = (src: string, dest: string) => { |
| 93 | + const srcStat = fs.lstatSync(src); |
| 94 | + if (srcStat.isDirectory()) { |
| 95 | + if (!fs.existsSync(dest)) { |
| 96 | + fs.mkdirSync(dest); |
| 97 | + } |
| 98 | + fs.readdirSync(src).map(filePath => |
| 99 | + copyDir(path.join(src, filePath), path.join(dest, filePath)), |
| 100 | + ); |
| 101 | + } else { |
| 102 | + fs.writeFileSync(dest, fs.readFileSync(src)); |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +export const getTempDirectory = (name: string) => |
| 107 | + path.resolve(os.tmpdir(), name); |
| 108 | + |
| 109 | +function spawnCli(dir: string, args?: Array<string>, options: RunOptions = {}) { |
| 110 | + const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options}); |
| 111 | + |
| 112 | + return execa.sync(process.execPath, spawnArgs, spawnOptions); |
| 113 | +} |
| 114 | + |
| 115 | +function spawnCliAsync( |
| 116 | + dir: string, |
| 117 | + args?: Array<string>, |
| 118 | + options: RunOptions = {}, |
| 119 | +) { |
| 120 | + const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options}); |
| 121 | + |
| 122 | + return execa(process.execPath, spawnArgs, spawnOptions); |
| 123 | +} |
| 124 | + |
| 125 | +function getCliArguments({dir, args, options}) { |
| 126 | + const isRelative = !path.isAbsolute(dir); |
| 127 | + |
| 128 | + if (isRelative) { |
| 129 | + dir = path.resolve(__dirname, dir); |
| 130 | + } |
| 131 | + |
| 132 | + const env = Object.assign({}, process.env, {FORCE_COLOR: '0'}); |
| 133 | + |
| 134 | + if (options.nodeOptions) { |
| 135 | + env.NODE_OPTIONS = options.nodeOptions; |
| 136 | + } |
| 137 | + if (options.nodePath) { |
| 138 | + env.NODE_PATH = options.nodePath; |
| 139 | + } |
| 140 | + |
| 141 | + const spawnArgs = [CLI_PATH, ...(args || [])]; |
| 142 | + const spawnOptions = { |
| 143 | + cwd: dir, |
| 144 | + env, |
| 145 | + reject: false, |
| 146 | + timeout: options.timeout || 0, |
| 147 | + }; |
| 148 | + return {spawnArgs, spawnOptions}; |
| 149 | +} |
0 commit comments