'use strict';
const chalk = require('chalk');
const {spawn} = require('child_process');
const fs = require('fs');
require('./createFlowConfigs');
async function runFlow(renderer, args) {
return new Promise(resolve => {
let cmd = __dirname + '/../../node_modules/.bin/flow';
if (process.platform === 'win32') {
cmd = cmd.replace(/\//g, '\\') + '.cmd';
}
const srcPath =
process.cwd() + '/scripts/flow/' + renderer + '/.flowconfig';
const srcStat = fs.statSync(__dirname + '/config/flowconfig');
const destPath = './.flowconfig';
if (fs.existsSync(destPath)) {
const oldConfig = String(fs.readFileSync(destPath));
const newConfig = String(fs.readFileSync(srcPath));
if (oldConfig !== newConfig) {
const destStat = fs.statSync(destPath);
if (destStat.mtimeMs - srcStat.mtimeMs > 1) {
console.error(
chalk.red(
'Detected manual changes to .flowconfig, which is a generated ' +
'file. These changes have been discarded.\n\n' +
'To change the Flow config, edit the template in ' +
'scripts/flow/config/flowconfig. Then run this command again.\n',
),
);
}
fs.unlinkSync(destPath);
fs.copyFileSync(srcPath, destPath);
fs.utimesSync(destPath, srcStat.atime, srcStat.mtime);
}
} else {
fs.copyFileSync(srcPath, destPath);
fs.utimesSync(destPath, srcStat.atime, srcStat.mtime);
}
console.log(
'Running Flow on the ' + chalk.yellow(renderer) + ' renderer...',
);
spawn(cmd, args, {
stdio: 'inherit',
}).on('close', function (code) {
if (code !== 0) {
console.error(
'Flow failed for the ' + chalk.red(renderer) + ' renderer',
);
console.log();
process.exit(code);
} else {
console.log(
'Flow passed for the ' + chalk.green(renderer) + ' renderer',
);
resolve();
}
});
});
}
module.exports = runFlow;