| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | const chalk = require('chalk'); |
| 11 | const {spawn} = require('child_process'); |
| 12 | const fs = require('fs'); |
| 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. |
| 19 | require('./createFlowConfigs'); |
| 20 | |
| 21 | async function runFlow(renderer, args) { |
| 22 | return new Promise(resolve => { |
| 23 | let cmd = __dirname + '/../../node_modules/.bin/flow'; |
| 24 | if (process.platform === 'win32') { |
| 25 | cmd = cmd.replace(/\//g, '\\') + '.cmd'; |
| 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 = String(fs.readFileSync(destPath)); |
| 37 | const newConfig = String(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 above 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 | |
| 67 | spawn(cmd, args, { |
| 68 | // Allow colors to pass through: |
| 69 | stdio: 'inherit', |
| 70 | }).on('close', function (code) { |
| 71 | if (code !== 0) { |
| 72 | console.error( |
| 73 | 'Flow failed for the ' + chalk.red(renderer) + ' renderer', |
| 74 | ); |
| 75 | console.log(); |
| 76 | process.exit(code); |
| 77 | } else { |
| 78 | console.log( |
| 79 | 'Flow passed for the ' + chalk.green(renderer) + ' renderer', |
| 80 | ); |
| 81 | resolve(); |
| 82 | } |
| 83 | }); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | module.exports = runFlow; |