|
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +import sane from 'sane'; |
| 4 | +import { resolve as resolvePath } from 'path'; |
| 5 | +import { spawn } from 'child_process'; |
| 6 | + |
| 7 | +process.env.PATH += ':./node_modules/.bin'; |
| 8 | + |
| 9 | +var cmd = resolvePath(__dirname); |
| 10 | +var srcDir = resolvePath(cmd, '../src'); |
| 11 | + |
| 12 | +function exec(command, options) { |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + var child = spawn(command, options, { |
| 15 | + cmd: cmd, |
| 16 | + env: process.env, |
| 17 | + stdio: 'inherit' |
| 18 | + }); |
| 19 | + child.on('exit', code => { |
| 20 | + if (code === 0) { |
| 21 | + resolve(true); |
| 22 | + } else { |
| 23 | + reject(new Error('Error code: ' + code)); |
| 24 | + } |
| 25 | + }); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +var watcher = sane(srcDir, { glob: ['**/*.js', '**/*.graphql'] }) |
| 30 | + .on('ready', startWatch) |
| 31 | + .on('add', changeFile) |
| 32 | + .on('delete', deleteFile) |
| 33 | + .on('change', changeFile); |
| 34 | + |
| 35 | +process.on('SIGINT', () => { |
| 36 | + console.log(CLEARLINE + yellow(invert('stopped watching'))); |
| 37 | + watcher.close(); |
| 38 | + process.exit(); |
| 39 | +}); |
| 40 | + |
| 41 | +var isChecking; |
| 42 | +var needsCheck; |
| 43 | +var toCheck = {}; |
| 44 | +var timeout; |
| 45 | + |
| 46 | +function startWatch() { |
| 47 | + process.stdout.write(CLEARSCREEN + green(invert('watching...'))); |
| 48 | +} |
| 49 | + |
| 50 | +function changeFile(filepath, root, stat) { |
| 51 | + if (!stat.isDirectory()) { |
| 52 | + toCheck[filepath] = true; |
| 53 | + debouncedCheck(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function deleteFile(filepath) { |
| 58 | + delete toCheck[filepath]; |
| 59 | + debouncedCheck(); |
| 60 | +} |
| 61 | + |
| 62 | +function debouncedCheck() { |
| 63 | + needsCheck = true; |
| 64 | + clearTimeout(timeout); |
| 65 | + timeout = setTimeout(guardedCheck, 250); |
| 66 | +} |
| 67 | + |
| 68 | +function guardedCheck() { |
| 69 | + if (isChecking || !needsCheck) { |
| 70 | + return; |
| 71 | + } |
| 72 | + isChecking = true; |
| 73 | + var filepaths = Object.keys(toCheck); |
| 74 | + toCheck = {}; |
| 75 | + needsCheck = false; |
| 76 | + checkFiles(filepaths).then(() => { |
| 77 | + isChecking = false; |
| 78 | + process.nextTick(guardedCheck); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function checkFiles(filepaths) { |
| 83 | + console.log('\u001b[2J'); |
| 84 | + |
| 85 | + return runTests(filepaths) |
| 86 | + // parseFiles(filepaths) |
| 87 | + // .then(() => runTests(filepaths)) |
| 88 | + // .then(testSuccess => lintFiles(filepaths) |
| 89 | + // .then(lintSuccess => testSuccess && lintSuccess)) |
| 90 | + .catch(() => false) |
| 91 | + .then(success => { |
| 92 | + process.stdout.write( |
| 93 | + '\n' + (success ? '' : '\x07') + green(invert('watching...')) |
| 94 | + ); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +// Checking steps |
| 99 | + |
| 100 | +function parseFiles(filepaths) { |
| 101 | + console.log('Checking Syntax'); |
| 102 | + |
| 103 | + return Promise.all(filepaths.map(filepath => { |
| 104 | + if (isJS(filepath) && !isTest(filepath)) { |
| 105 | + return exec('babel', [ |
| 106 | + '--optional', 'runtime', |
| 107 | + '--out-file', '/dev/null', |
| 108 | + srcPath(filepath) |
| 109 | + ]); |
| 110 | + } |
| 111 | + })); |
| 112 | +} |
| 113 | + |
| 114 | +function runTests(filepaths) { |
| 115 | + console.log('\nRunning Tests'); |
| 116 | + const cmd = [ |
| 117 | + './node_modules/.bin/_mocha', |
| 118 | + '--compilers', 'js:babel-core/register', |
| 119 | + // '--reporter', 'progress', |
| 120 | + '--reporter', 'dot', |
| 121 | + '--require', './resources/mocha-bootload', |
| 122 | + ].concat( |
| 123 | + allTests(filepaths) ? |
| 124 | + filepaths.map(srcPath) : |
| 125 | + ['src/**/__tests__/**/*-test.js'] |
| 126 | + ); |
| 127 | + // console.log(cmd); |
| 128 | + return exec('node', cmd).catch(() => false); |
| 129 | +} |
| 130 | + |
| 131 | +function lintFiles(filepaths) { |
| 132 | + console.log('Linting Code\n'); |
| 133 | + |
| 134 | + return filepaths.reduce((prev, filepath) => prev.then(prevSuccess => { |
| 135 | + if (isJS(filepath)) { |
| 136 | + process.stdout.write(' ' + filepath + ' ...'); |
| 137 | + return exec('eslint', [srcPath(filepath)]) |
| 138 | + .catch(() => false) |
| 139 | + .then(success => { |
| 140 | + console.log(CLEARLINE + ' ' + (success ? CHECK : X) |
| 141 | + + ' ' + filepath); |
| 142 | + return prevSuccess && success; |
| 143 | + }); |
| 144 | + } |
| 145 | + return prevSuccess; |
| 146 | + }), Promise.resolve(true)); |
| 147 | +} |
| 148 | + |
| 149 | +// Filepath |
| 150 | + |
| 151 | +function srcPath(filepath) { |
| 152 | + return resolvePath(srcDir, filepath); |
| 153 | +} |
| 154 | + |
| 155 | +// Predicates |
| 156 | + |
| 157 | +function isJS(filepath) { |
| 158 | + return filepath.indexOf('.js') === filepath.length - 3; |
| 159 | +} |
| 160 | + |
| 161 | +function allTests(filepaths) { |
| 162 | + return filepaths.length > 0 && filepaths.every(isTest); |
| 163 | +} |
| 164 | + |
| 165 | +var TEST_PATH_RX = /^(?:.*?\/)?__tests__\/.+?-test\.js$/; |
| 166 | + |
| 167 | +function isTest(filepath) { |
| 168 | + return TEST_PATH_RX.test(filepath); |
| 169 | +} |
| 170 | + |
| 171 | +// Print helpers |
| 172 | + |
| 173 | +var CLEARSCREEN = '\u001b[2J'; |
| 174 | +var CLEARLINE = '\r\x1B[K'; |
| 175 | +var CHECK = green('\u2713'); |
| 176 | +var X = red('\u2718'); |
| 177 | + |
| 178 | +function invert(str) { |
| 179 | + return `\u001b[7m ${str} \u001b[27m`; |
| 180 | +} |
| 181 | + |
| 182 | +function red(str) { |
| 183 | + return `\x1B[K\u001b[1m\u001b[31m${str}\u001b[39m\u001b[22m`; |
| 184 | +} |
| 185 | + |
| 186 | +function green(str) { |
| 187 | + return `\x1B[K\u001b[1m\u001b[32m${str}\u001b[39m\u001b[22m`; |
| 188 | +} |
| 189 | + |
| 190 | +function yellow(str) { |
| 191 | + return `\x1B[K\u001b[1m\u001b[33m${str}\u001b[39m\u001b[22m`; |
| 192 | +} |
0 commit comments