Skip to content

Commit 47c97b2

Browse files
committed
Initial version of the global cli
Test Plan: ``` npm pack node global-cli/index.js testing --scripts-version /Users/vjeux/random/react-getting-started/create-react-app-scripts-0.0.1.tgz ``` make sure it shows ``` Creating the app testing at /Users/vjeux/random/react-getting-started/testing ```
1 parent 0a5377a commit 47c97b2

File tree

5 files changed

+177
-1
lines changed

5 files changed

+177
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
build
33
.DS_Store
4+
*.tgz

global-cli/index.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Copyright (c) 2015-present, Facebook, Inc.
5+
* All rights reserved.
6+
*
7+
* This source code is licensed under the BSD-style license found in the
8+
* LICENSE file in the root directory of this source tree. An additional grant
9+
* of patent rights can be found in the PATENTS file in the same directory.
10+
*/
11+
12+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
// /!\ DO NOT MODIFY THIS FILE /!\
14+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
//
16+
// create-react-app is installed globally on people's computers. This means
17+
// that it is extremely difficult to have them upgrade the version and
18+
// because there's only one global version installed, it is very prone to
19+
// breaking changes.
20+
//
21+
// The only job of create-react-app is to init the repository and then
22+
// forward all the commands to the local version of create-react-app.
23+
//
24+
// If you need to add a new command, please add it to local-cli/.
25+
//
26+
// The only reason to modify this file is to add more warnings and
27+
// troubleshooting information for the `react init` command.
28+
//
29+
// Do not make breaking changes! We absolutely don't want to have to
30+
// tell people to update their global version of create-react-app.
31+
//
32+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33+
// /!\ DO NOT MODIFY THIS FILE /!\
34+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
36+
'use strict';
37+
38+
var fs = require('fs');
39+
var path = require('path');
40+
var exec = require('child_process').exec;
41+
var spawn = require('child_process').spawn;
42+
var chalk = require('chalk');
43+
var semver = require('semver');
44+
/**
45+
* Used arguments:
46+
* -v --version - to print current version of create-react-app and create-react-app-scripts dependency
47+
* --verbose - to print logs while init
48+
* --scripts-version <alternative create-react-app-scripts package> - override default (https://registry.npmjs.org/create-react-app-scripts@latest),
49+
* package to install, examples:
50+
* - "0.22.0-rc1" - A new app will be created using a specific version of React CLI from npm repo
51+
* - "https://registry.npmjs.org/create-react-app-scripts/-/create-react-app-scripts-0.20.0.tgz" - a .tgz archive from any npm repo
52+
* - "/Users/home/create-react-app/create-react-app-scripts-0.22.0.tgz" - for package prepared with `npm pack`, useful for e2e tests
53+
*/
54+
var argv = require('minimist')(process.argv.slice(2));
55+
56+
var commands = argv._;
57+
if (commands.length === 0) {
58+
console.error(
59+
'Usage: create-react-app <project-name> [--verbose]'
60+
);
61+
process.exit(1);
62+
}
63+
64+
if (argv.v || argv.version) {
65+
console.log('create-react-app: ' + require('./package.json').version);
66+
process.exit();
67+
}
68+
69+
createApp(commands[0], argv.verbose, argv['scripts-version']);
70+
71+
function createApp(name, verbose, version) {
72+
if (fs.existsSync(name)) {
73+
console.log('Directory `' + name + '` already exists. Aborting.');
74+
process.exit();
75+
}
76+
77+
var root = path.resolve(name);
78+
var appName = path.basename(root);
79+
80+
console.log(
81+
'This will walk you through creating a new React app in',
82+
root
83+
);
84+
85+
fs.mkdirSync(root);
86+
87+
var packageJson = {
88+
name: appName,
89+
version: '0.0.1',
90+
private: true,
91+
};
92+
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
93+
process.chdir(root);
94+
95+
console.log('Installing create-react-app-scripts package from npm...');
96+
97+
run(root, appName, version, verbose);
98+
}
99+
100+
function run(root, appName, version, verbose) {
101+
var args = [
102+
'install',
103+
verbose && '--verbose',
104+
'--save',
105+
'--save-exact',
106+
getInstallPackage(version),
107+
].filter(function(e) { return e; });
108+
var proc = spawn('npm', args, {stdio: 'inherit'});
109+
proc.on('close', function (code) {
110+
if (code !== 0) {
111+
console.error('`npm ' + args.join(' ') + '` failed');
112+
return;
113+
}
114+
115+
var scriptsPath = path.resolve(
116+
process.cwd(),
117+
'node_modules',
118+
'create-react-app-scripts',
119+
'init.js'
120+
);
121+
var init = require(scriptsPath);
122+
init(root, appName);
123+
});
124+
}
125+
126+
function getInstallPackage(version) {
127+
var packageToInstall = 'create-react-app-scripts';
128+
var validSemver = semver.valid(version);
129+
if (validSemver) {
130+
packageToInstall += '@' + validSemver;
131+
} else if (version) {
132+
// for tar.gz or alternative paths
133+
packageToInstall = version;
134+
}
135+
return packageToInstall;
136+
}
137+
138+
function checkNodeVersion() {
139+
var packageJsonPath = path.resolve(
140+
process.cwd(),
141+
'node_modules',
142+
'create-react-app-scripts',
143+
'package.json'
144+
);
145+
var packageJson = require(packageJsonPath);
146+
if (!packageJson.engines || !packageJson.engines.node) {
147+
return;
148+
}
149+
if (!semver.satisfies(process.version, packageJson.engines.node)) {
150+
console.error(
151+
chalk.red(
152+
'You are currently running Node %s but React CLI requires %s. ' +
153+
'Please use a supported version of Node.\n'
154+
),
155+
process.version,
156+
packageJson.engines.node
157+
);
158+
}
159+
}

global-cli/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "create-react-app",
3+
"version": "1.0.0",
4+
"bin": {
5+
"create-react-app": "index.js"
6+
},
7+
"dependencies": {
8+
"chalk": "^1.1.1",
9+
"minimist": "^1.2.0",
10+
"semver": "^5.0.3"
11+
}
12+
}

init.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(root, appName) {
2+
console.log('Creating the app', appName, 'at', root);
3+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"name": "react-getting-started",
2+
"name": "create-react-app-scripts",
3+
"version": "0.0.1",
34
"scripts": {
45
"start": "cross-env NODE_ENV=development node devServer.js",
56
"build": "rimraf build && cross-env NODE_ENV=production webpack --config webpack.config.prod.js"

0 commit comments

Comments
 (0)