diff --git a/packages/react-scripts/bin/react-scripts.js b/packages/react-scripts/bin/react-scripts.js index cb52fe71b09..56ed5a739d2 100755 --- a/packages/react-scripts/bin/react-scripts.js +++ b/packages/react-scripts/bin/react-scripts.js @@ -19,9 +19,12 @@ switch (script) { case 'eject': case 'start': case 'test': { + const nodeOptions = getNodeOptionsIfPresent(script); const result = spawn.sync( 'node', - [require.resolve('../scripts/' + script)].concat(args), + nodeOptions + .concat([require.resolve('../scripts/' + script)]) + .concat(args), { stdio: 'inherit' } ); if (result.signal) { @@ -51,3 +54,30 @@ switch (script) { ); break; } + +function getNodeOptionsIfPresent(script) { + const nameOne = 'NODE_COMMAND_LINE'; + const nameTwo = `${nameOne}_${script.toUpperCase()}`; + const { readFileSync } = require('fs'); + const { parse } = require('dotenv'); + const path = '.env'; + const encoding = 'utf8'; + const getArgList = val => { + return val.split('--').filter(v => v !== '').map(v => `--${v}`); + }; + let nodeOptions = []; + try { + const parsedObj = parse(readFileSync(path, { encoding: encoding })); + nodeOptions = Object.entries(parsedObj).reduce((lst, [key, val]) => { + if (key === nameTwo) { + lst = getArgList(val); + } else if (key === nameOne && lst.length === 0) { + lst = getArgList(val); + } + return lst; + }, []); + } catch (e) { + // File does not exit or could not be read + } + return nodeOptions; +}