Skip to content

Commit 9484ef9

Browse files
committed
Port cra.sh development task to javascript
1 parent 4bec877 commit 9484ef9

File tree

3 files changed

+112
-85
lines changed

3 files changed

+112
-85
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"scripts": {
44
"build": "node packages/react-scripts/scripts/build.js",
55
"changelog": "lerna-changelog",
6-
"create-react-app": "tasks/cra.sh",
6+
"create-react-app": "node tasks/cra.js",
77
"e2e": "tasks/e2e-simple.sh",
88
"e2e:docker": "tasks/local-test.sh",
99
"postinstall": "node bootstrap.js && cd packages/react-error-overlay/ && npm run build:prod",

Diff for: tasks/cra.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
const fs = require('fs');
12+
const path = require('path');
13+
const cp = require('child_process');
14+
15+
const cleanup = () => {
16+
console.log('Cleaning up.');
17+
// Uncomment when snapshot testing is enabled by default:
18+
// rm ./template/src/__snapshots__/App.test.js.snap
19+
};
20+
21+
const handleExit = () => {
22+
cleanup();
23+
console.log('Exiting without error.');
24+
process.exit();
25+
};
26+
27+
const handleError = e => {
28+
console.error('ERROR! An error was encountered while executing\n', e);
29+
cleanup();
30+
console.log('Exiting with error.');
31+
process.exit(1);
32+
};
33+
34+
process.on('SIGINT', handleExit);
35+
process.on('uncaughtException', handleError);
36+
37+
// ******************************************************************************
38+
// Pack react- scripts so we can verify they work.
39+
// ******************************************************************************
40+
41+
const rootDir = path.join(__dirname, '..');
42+
const reactScriptsDir = path.join(rootDir, 'packages', 'react-scripts');
43+
const packageJsonPath = path.join(reactScriptsDir, 'package.json');
44+
const packageJsonOrigPath = path.join(reactScriptsDir, 'package.json.orig');
45+
46+
// Install all our packages
47+
const lernaPath = path.join(rootDir, 'node_modules', '.bin', 'lerna');
48+
cp.execSync(`${lernaPath} bootstrap`, {
49+
cwd: rootDir,
50+
stdio: 'inherit',
51+
});
52+
53+
// Save package.json because we're going to touch it
54+
fs.writeFileSync(packageJsonOrigPath, fs.readFileSync(packageJsonPath));
55+
56+
// Replace own dependencies (those in the`packages` dir) with the local paths
57+
// of those packages
58+
const replaceOwnDepsPath = path.join(__dirname, 'replace-own-deps.js');
59+
cp.execSync(`node ${replaceOwnDepsPath}`, { stdio: 'inherit' });
60+
61+
// Finally, pack react-scripts
62+
// Don't redirect stdio as we want to capture the output that will be returned
63+
// from execSync(). In this case it will be the .tgz filename.
64+
const scriptsFileName = cp
65+
.execSync(`npm pack`, { cwd: reactScriptsDir })
66+
.toString()
67+
.trim();
68+
// CRA can run into issues if the .tgz path is absolute (especially on Windows)
69+
// Use a relative path to be safe
70+
const scriptsPath = path.join(
71+
'..',
72+
'packages',
73+
'react-scripts',
74+
scriptsFileName
75+
);
76+
77+
// Restore package.json
78+
fs.unlinkSync(packageJsonPath);
79+
fs.writeFileSync(packageJsonPath, fs.readFileSync(packageJsonOrigPath));
80+
fs.unlinkSync(packageJsonOrigPath);
81+
82+
// ******************************************************************************
83+
// Now that we have packed them, call the global CLI.
84+
// ******************************************************************************
85+
86+
// If Yarn is installed, clean its cache because it may have cached react-scripts
87+
try {
88+
cp.execSync('yarn cache clean');
89+
} catch (e) {
90+
// We can safely ignore this as the user doesn't have yarn installed
91+
}
92+
93+
const args = process.argv.slice(2);
94+
95+
// Now run the CRA command
96+
const craScriptPath = path.join(
97+
rootDir,
98+
'packages',
99+
'create-react-app',
100+
'index.js'
101+
);
102+
cp.execSync(
103+
`node ${craScriptPath} --scripts-version="${scriptsPath}" ${args.join(' ')}`,
104+
{
105+
cwd: rootDir,
106+
stdio: 'inherit',
107+
}
108+
);
109+
110+
// Cleanup
111+
handleExit();

Diff for: tasks/cra.sh

-84
This file was deleted.

0 commit comments

Comments
 (0)