This repository was archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathindex.js
222 lines (199 loc) · 6.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env node
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// create-react-app is installed globally on people's computers. This means
// that it is extremely difficult to have them upgrade the version and
// because there's only one global version installed, it is very prone to
// breaking changes.
//
// The only job of create-react-app is to init the repository and then
// forward all the commands to the local version of create-react-app.
//
// If you need to add a new command, please add it to the scripts/ folder.
//
// The only reason to modify this file is to add more warnings and
// troubleshooting information for the `create-react-app` command.
//
// Do not make breaking changes! We absolutely don't want to have to
// tell people to update their global version of create-react-app.
//
// Also be careful with new language features.
// This file must work on Node 0.10+.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'use strict';
var fs = require('fs');
var path = require('path');
var spawn = require('cross-spawn');
var chalk = require('chalk');
var semver = require('semver');
var argv = require('minimist')(process.argv.slice(2));
var pathExists = require('path-exists');
/**
* Arguments:
* --version - to print current version
* --verbose - to print logs while init
* --scripts-version <alternative package>
* Example of valid values:
* - a specific npm version: "0.22.0-rc1"
* - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"
* - a package prepared with `tasks/clean_pack.sh`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"
*/
var commands = argv._;
if (commands.length === 0) {
if (argv.version) {
console.log('create-react-app version: ' + require('./package.json').version);
process.exit();
}
console.error(
'Usage: create-react-app <project-directory> [--verbose]'
);
process.exit(1);
}
createApp(commands[0], argv.verbose, argv['scripts-version']);
function createApp(name, verbose, version) {
var root = path.resolve(name);
var appName = path.basename(root);
checkAppName(appName);
if (!pathExists.sync(name)) {
fs.mkdirSync(root);
} else if (!isSafeToCreateProjectIn(root)) {
console.log('The directory `' + name + '` contains file(s) that could conflict. Aborting.');
process.exit(1);
}
console.log(
'Creating a new React app in ' + root + '.'
);
console.log();
var packageJson = {
name: appName,
version: '0.1.0',
private: true,
};
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
var originalDirectory = process.cwd();
process.chdir(root);
console.log('Installing packages. This might take a couple minutes.');
console.log('Installing react-scripts from npm...');
console.log();
run(root, appName, version, verbose, originalDirectory);
}
function run(root, appName, version, verbose, originalDirectory) {
var installPackage = getInstallPackage(version);
var packageName = getPackageName(installPackage);
var args = [
'install',
verbose && '--verbose',
'--save-dev',
'--save-exact',
installPackage,
].filter(function(e) { return e; });
var proc = spawn('npm', args, {stdio: 'inherit'});
proc.on('close', function (code) {
if (code !== 0) {
console.error('`npm ' + args.join(' ') + '` failed');
return;
}
checkNodeVersion(packageName);
var scriptsPath = path.resolve(
process.cwd(),
'node_modules',
packageName,
'scripts',
'init.js'
);
var init = require(scriptsPath);
init(root, appName, verbose, originalDirectory);
});
}
function getInstallPackage(version) {
var packageToInstall = 'react-scripts';
var validSemver = semver.valid(version);
if (validSemver) {
packageToInstall += '@' + validSemver;
} else if (version) {
// for tar.gz or alternative paths
packageToInstall = version;
}
return packageToInstall;
}
// Extract package name from tarball url or path.
function getPackageName(installPackage) {
if (~installPackage.indexOf('.tgz')) {
return installPackage.match(/^.+\/(.+)-.+\.tgz$/)[1];
} else if (~installPackage.indexOf('@')) {
return installPackage.split('@')[0];
}
return installPackage;
}
function checkNodeVersion(packageName) {
var packageJsonPath = path.resolve(
process.cwd(),
'node_modules',
packageName,
'package.json'
);
var packageJson = require(packageJsonPath);
if (!packageJson.engines || !packageJson.engines.node) {
return;
}
if (!semver.satisfies(process.version, packageJson.engines.node)) {
console.error(
chalk.red(
'You are currently running Node %s but create-react-app requires %s.' +
' Please use a supported version of Node.\n'
),
process.version,
packageJson.engines.node
);
process.exit(1);
}
}
function checkAppName(appName) {
// TODO: there should be a single place that holds the dependencies
var dependencies = ['react', 'react-dom'];
var devDependencies = ['react-scripts'];
var allDependencies = dependencies.concat(devDependencies).sort();
if (allDependencies.indexOf(appName) >= 0) {
console.error(
chalk.red(
'We cannot create a project called `' + appName + '` because a dependency with the same name exists.\n' +
'Due to the way npm works, the following names are not allowed:\n\n'
) +
chalk.cyan(
allDependencies.map(function(depName) {
return ' ' + depName;
}).join('\n')
) +
chalk.red('\n\nPlease choose a different project name.')
);
process.exit(1);
}
}
// If project only contains files generated by GH, it’s safe.
// We also special case IJ-based products .idea because it integrates with CRA:
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-243446094
function isSafeToCreateProjectIn(root) {
var validFiles = [
'.DS_Store', 'Thumbs.db', '.git', '.gitignore', '.idea', 'README.md', 'LICENSE'
];
return fs.readdirSync(root)
.every(function(file) {
return validFiles.indexOf(file) >= 0;
});
}