|
8 | 8 | 'use strict';
|
9 | 9 |
|
10 | 10 | const chalk = require('chalk');
|
11 |
| -const spawn = require('child_process').spawn; |
| 11 | +const {spawn} = require('child_process'); |
| 12 | +const fs = require('fs'); |
12 | 13 |
|
| 14 | +// TODO: This generates all the renderer configs at once. Originally this was |
| 15 | +// to allow the possibility of running multiple Flow processes in parallel, but |
| 16 | +// that never happened. If we did, we'd probably do this in CI, anyway, and run |
| 17 | +// on multiple machines. So instead we could remove this intermediate step and |
| 18 | +// generate only the config for the specified renderer. |
13 | 19 | require('./createFlowConfigs');
|
14 | 20 |
|
15 | 21 | async function runFlow(renderer, args) {
|
16 | 22 | return new Promise(resolve => {
|
17 |
| - console.log( |
18 |
| - 'Running Flow on the ' + chalk.yellow(renderer) + ' renderer...', |
19 |
| - ); |
20 | 23 | let cmd = __dirname + '/../../node_modules/.bin/flow';
|
21 | 24 | if (process.platform === 'win32') {
|
22 | 25 | cmd = cmd.replace(/\//g, '\\') + '.cmd';
|
23 | 26 | }
|
| 27 | + |
| 28 | + // Copy renderer flowconfig file to the root of the project so that it |
| 29 | + // works with editor integrations. This means that the Flow config used by |
| 30 | + // the editor will correspond to the last renderer you checked. |
| 31 | + const srcPath = |
| 32 | + process.cwd() + '/scripts/flow/' + renderer + '/.flowconfig'; |
| 33 | + const srcStat = fs.statSync(__dirname + '/config/flowconfig'); |
| 34 | + const destPath = './.flowconfig'; |
| 35 | + if (fs.existsSync(destPath)) { |
| 36 | + const oldConfig = fs.readFileSync(destPath) + ''; |
| 37 | + const newConfig = fs.readFileSync(srcPath) + ''; |
| 38 | + if (oldConfig !== newConfig) { |
| 39 | + // Use the mtime to detect if the file was manually edited. If so, |
| 40 | + // log an error. |
| 41 | + const destStat = fs.statSync(destPath); |
| 42 | + if (destStat.mtimeMs - srcStat.mtimeMs > 1) { |
| 43 | + console.error( |
| 44 | + chalk.red( |
| 45 | + 'Detected manual changes to .flowconfig, which is a generated ' + |
| 46 | + 'file. These changes have been discarded.\n\n' + |
| 47 | + 'To change the Flow config, edit the template in ' + |
| 48 | + 'scripts/flow/config/flowconfig. Then run this command again.\n', |
| 49 | + ), |
| 50 | + ); |
| 51 | + } |
| 52 | + fs.unlinkSync(destPath); |
| 53 | + fs.copyFileSync(srcPath, destPath); |
| 54 | + // Set the mtime of the copied file to be same as the original file, |
| 55 | + // so that the ahove check works. |
| 56 | + fs.utimesSync(destPath, srcStat.atime, srcStat.mtime); |
| 57 | + } |
| 58 | + } else { |
| 59 | + fs.copyFileSync(srcPath, destPath); |
| 60 | + fs.utimesSync(destPath, srcStat.atime, srcStat.mtime); |
| 61 | + } |
| 62 | + |
| 63 | + console.log( |
| 64 | + 'Running Flow on the ' + chalk.yellow(renderer) + ' renderer...', |
| 65 | + ); |
| 66 | + |
24 | 67 | spawn(cmd, args, {
|
25 | 68 | // Allow colors to pass through:
|
26 | 69 | stdio: 'inherit',
|
27 |
| - // Use a specific renderer config: |
28 |
| - cwd: process.cwd() + '/scripts/flow/' + renderer + '/', |
29 | 70 | }).on('close', function(code) {
|
30 | 71 | if (code !== 0) {
|
31 | 72 | console.error(
|
|
0 commit comments