Skip to content

Commit eae3d82

Browse files
author
Maël Nison
committed
Avoids cra from crashing when initializing react-scripts
1 parent 7c362b0 commit eae3d82

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

packages/create-react-app/createReactApp.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,19 +340,25 @@ function run(
340340
() => packageName
341341
);
342342
})
343-
.then(packageName => {
343+
.then(async packageName => {
344344
checkNodeVersion(packageName);
345345
setCaretRangeForRuntimeDeps(packageName);
346346

347-
const scriptsPath = path.resolve(
348-
process.cwd(),
349-
'node_modules',
350-
packageName,
351-
'scripts',
352-
'init.js'
347+
const pnpPath = path.resolve(process.cwd(), '.pnp.js');
348+
349+
const nodeArgs = fs.existsSync(pnpPath) ? ['--require', pnpPath] : [];
350+
351+
await executeNodeScript(
352+
{
353+
cwd: process.cwd(),
354+
args: nodeArgs,
355+
},
356+
[root, appName, verbose, originalDirectory, template],
357+
`
358+
var init = require('${packageName}/scripts/init.js');
359+
init.apply(null, JSON.parse(process.argv[1]));
360+
`
353361
);
354-
const init = require(scriptsPath);
355-
init(root, appName, verbose, originalDirectory, template);
356362

357363
if (version === '[email protected]') {
358364
console.log(
@@ -799,3 +805,23 @@ function checkIfOnline(useYarn) {
799805
});
800806
});
801807
}
808+
809+
function executeNodeScript({ cwd, args }, data, source) {
810+
return new Promise((resolve, reject) => {
811+
const child = spawn(
812+
process.execPath,
813+
[...args, '-e', source, '--', JSON.stringify(data)],
814+
{ stdio: 'inherit' }
815+
);
816+
817+
child.on('close', code => {
818+
if (code !== 0) {
819+
reject({
820+
command: `${command} ${args.join(' ')}`,
821+
});
822+
return;
823+
}
824+
resolve();
825+
});
826+
});
827+
}

0 commit comments

Comments
 (0)