-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·472 lines (423 loc) · 14.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env node
/**
* Copyright (c) 2018 Jundat95.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// rn-redux-cli 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 rn-redux-cli is to init the repository and then
// forward all the commands to the local version of rn-redux.
//
// If you need to add a new command, please add it to local-cli/.
//
// The only reason to modify this file is to add more warnings and
// troubleshooting information for the `rn-redux init` command.
//
// To allow for graceful failure on older node versions, this file should
// retain ES5 compatibility.
//
// Do not make breaking changes! We absolutely don't want to have to
// tell people to update their global version of rn-redux-cli.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'use strict';
var fs = require('fs');
var path = require('path');
var execSync = require('child_process').execSync;
var chalk = require('chalk');
var prompt = require('prompt');
var semver = require('semver');
var copydir = require('copy-dir');
var vnrm = require('vnrm');
var initProject = require('./ultis/init-project');
var selectTemplates = require('./ultis/select-templates');
var customTemplate = require('./ultis/custom-template');
var getFolder = require('./ultis/get-folder');
/**
* Used arguments:
* -v --version - to print current version of rn-redux-cli and rn-redux dependency
* if you are in a RN app folder
* init - to create a new project and npm install it
* --verbose - to print logs while init
* --template - name of the template to use, e.g. --template navigation
* --version <alternative rn-redux package> - override default (https://registry.npmjs.org/rn-redux@latest),
*/
var options = require('minimist')(process.argv.slice(2));
var CLI_MODULE_PATH = function () {
return path.resolve(
process.cwd(),
'node_modules',
'react-native',
'cli.js'
);
};
var REACT_NATIVE_PACKAGE_JSON_PATH = function () {
return path.resolve(
process.cwd(),
'node_modules',
'react-native',
'package.json'
);
};
if (options._.length === 0 && (options.v || options.version)) {
printVersionsAndExit(REACT_NATIVE_PACKAGE_JSON_PATH());
}
// Use Yarn if available, it's much faster than the npm client.
// Return the version of yarn installed on the system, null if yarn is not available.
function getYarnVersionIfAvailable() {
var yarnVersion;
try {
// execSync returns a Buffer -> convert to string
if (process.platform.startsWith('win')) {
yarnVersion = (execSync('yarn --version 2> NUL').toString() || '').trim();
} else {
yarnVersion = (execSync('yarn --version 2>/dev/null').toString() || '').trim();
}
} catch (error) {
return null;
}
// yarn < 0.16 has a 'missing manifest' bug
try {
if (semver.gte(yarnVersion, '0.16.0')) {
return yarnVersion;
} else {
return null;
}
} catch (error) {
console.error('Cannot parse yarn version: ' + yarnVersion);
return null;
}
}
var cli;
var cliPath = CLI_MODULE_PATH();
if (fs.existsSync(cliPath)) {
cli = require(cliPath);
}
var commands = options._;
if (cli) {
cli.run();
} else {
if (options._.length === 0 && (options.h || options.help)) {
console.log([
'',
' Usage: rn-redux [command] [options]',
'',
'',
' Commands:',
'',
' init <ProjectName> [options] generates a new project and installs its dependencies',
'',
' Options:',
'',
' -h, --help output usage information',
' -v, --version use a specific version of React Native',
' --template use an app template. Use --template to see available templates.',
'',
].join('\n'));
process.exit(0);
}
if (commands.length === 0) {
console.error(
'You did not pass any commands, run `rn-redux --help` to see a list of all available commands.'
);
process.exit(1);
}
switch (commands[0]) {
case 'init':
if (!commands[1]) {
console.error(
'Usage: rn-redux init <ProjectName> [--verbose]'
);
process.exit(1);
} else {
init(commands[1], options);
}
break;
default:
console.error(
'Command `%s` unrecognized. ' +
'Make sure that you have run `npm install` and that you are inside a react-native project.',
commands[0]
);
process.exit(1);
break;
}
}
function validateProjectName(name) {
if (!String(name).match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
console.error(
'"%s" is not a valid name for a project. Please use a valid identifier ' +
'name (alphanumeric).',
name
);
process.exit(1);
}
if (name === 'React') {
console.error(
'"%s" is not a valid name for a project. Please do not use the ' +
'reserved word "React".',
name
);
process.exit(1);
}
}
/**
* @param name Project name, e.g. 'AwesomeApp'.
* @param options.verbose If true, will run 'npm install' in verbose mode (for debugging).
* @param options.version Version of React Native to install, e.g. '0.38.0'.
* @param options.npm If true, always use the npm command line client,
* don't use yarn even if available.
*/
function init(name, options) {
validateProjectName(name);
if (fs.existsSync(name)) {
createAfterConfirmation(name, options);
} else {
createProject(name, options);
}
}
function createAfterConfirmation(name, options) {
prompt.start();
var property = {
name: 'yesno',
message: 'Directory ' + name + ' already exists. Continue?',
validator: /y[es]*|n[o]?/,
warning: 'Must respond yes or no',
default: 'no'
};
prompt.get(property, function (err, result) {
if (result.yesno[0] === 'y') {
createProject(name, options);
} else {
console.log('Project initialization canceled');
process.exit();
}
});
}
function createProject(name, options) {
var root = path.resolve(name);
var projectName = path.basename(root);
console.log(
'This will walk you through creating a new React Native project in',
root
);
if (!fs.existsSync(root)) {
fs.mkdirSync(root);
}
var packageJson = {
name: projectName,
version: '0.0.1',
private: true,
scripts: {
start: 'node node_modules/react-native/local-cli/cli.js start',
ios: 'react-native run-ios',
android: 'react-native run-android',
}
};
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
process.chdir(root);
run(root, projectName, options);
}
function getInstallPackage(rnPackage) {
var packageToInstall = 'react-native';
var isValidSemver = semver.valid(rnPackage);
if (isValidSemver) {
packageToInstall += '@' + isValidSemver;
} else if (rnPackage) {
// for tar.gz or alternative paths
packageToInstall = rnPackage;
}
return packageToInstall;
}
async function run(root, projectName, options) {
var rnPackage = options.version; // e.g. '0.38' or '/path/to/archive.tgz'
var forceNpmClient = options.npm;
var yarnVersion = (!forceNpmClient) && getYarnVersionIfAvailable();
var installCommand;
if (options.installCommand) {
// In CI environments it can be useful to provide a custom command,
// to set up and use an offline mirror for installing dependencies, for example.
installCommand = options.installCommand;
} else {
if (yarnVersion) {
console.log('Using yarn v' + yarnVersion);
console.log('Installing ' + getInstallPackage(rnPackage) + '...');
installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact';
if (options.verbose) {
installCommand += ' --verbose';
}
} else {
console.log('Installing ' + getInstallPackage(rnPackage) + '...');
if (!forceNpmClient) {
console.log('Consider installing yarn to make this faster: https://yarnpkg.com');
}
installCommand = 'npm install --save --save-exact ' + getInstallPackage(rnPackage);
if (options.verbose) {
installCommand += ' --verbose';
}
}
}
try {
execSync(installCommand, { stdio: 'inherit' });
} catch (err) {
console.error(err);
console.error('Command `' + installCommand + '` failed.');
process.exit(1);
}
checkNodeVersion();
cli = require(CLI_MODULE_PATH());
cli.init(root, projectName);
// Set up new project
initProject((userSelect) => {
if (!userSelect) return;
selectTemplates(temp => {
if (temp !== 'temp0') {
var pathTemplates = __dirname + '/templates/' + temp;
moveProject(pathTemplates, root, () => {
});
} else {
customTemplate(res => {
if (res === 'local') {
getFolder('enter folder: ', folder => {
var pathTemplates = folder;
if (fs.existsSync(pathTemplates)) {
moveProject(pathTemplates, root, () => {
// copy project success
});
} else {
console.log(`folder don't exists`);
}
});
} else {
getFolder('enter url github: ', url => {
cloneGithub(url, root);
});
}
});
}
});
});
}
async function cloneGithub(url, root) {
var temp = url.split('/');
var name = temp[temp.length - 1].split('.')[0];
if (fs.existsSync(name))
return console.log('folder ' + name + ' is exists');
await execSync('git clone ' + url, { stdio: 'inherit' });
await moveProject(root + '/' + name, root, () => {
vnrm(root + '/' + name, err => {
if (err)
console.log(err);
});
});
}
/**
* moveProject
* @param {path folder copy to project} pathTemplates
* @param {path project current} root
*/
async function moveProject(pathTemplates, root, callback) {
console.log('\nCopy templates to new project...');
// install package
await installPackage(pathTemplates, root);
copydir(
pathTemplates,
root,
function (stat, filepath, filename) {
// Don't copy file .json in rootFolder to new project
if (stat === 'file' && path.extname(filepath) === '.json' && path.relative(filepath, pathTemplates + "/" + filename) === '') {
return false;
}
if (stat === 'directory' && filename === '.json') {
return false;
}
return true;
},
function (err) {
if (err) {
console.error(err);
callback();
} else {
// console.log('copy file success');
fs.unlink(root + '/App.js', function (error) {
if (error) {
console.error(error);
callback();
}
// console.log('deleted App.js');
console.log('\nCopy templates success\n');
callback();
});
}
});
}
/**
*
* @param {path templates} pathTemplates
* @param {path root} root
*/
async function installPackage(pathTemplates, root) {
var forceNpmClient = options.npm;
var yarnVersion = (!forceNpmClient) && getYarnVersionIfAvailable();
var packagePath = pathTemplates + '/package.json';
var packageJson = null;
if (fs.existsSync(packagePath)) {
packageJson = require(packagePath);
}
if (packageJson) {
if (packageJson.dependencies) {
for (var item in packageJson.dependencies) {
// console.log(item + '-' + package.dependencies[item]);
if (yarnVersion) {
await execSync('yarn add ' + item + '@' + packageJson.dependencies[item], { stdio: 'inherit' });
} else {
await execSync('npm i --save ' + item + '@' + packageJson.dependencies[item], { stdio: 'inherit' });
}
}
}
if (packageJson.devDependencies) {
for (var item in packageJson.devDependencies) {
// console.log(item + '-' + package.devDependencies[item]);
if (yarnVersion) {
await execSync('yarn add --dev ' + item + '@' + packageJson.devDependencies[item], { stdio: 'inherit' });
} else {
await execSync('npm i --save-dev ' + item + '@' + packageJson.devDependencies[item], { stdio: 'inherit' });
}
}
}
}
}
function checkNodeVersion() {
var packageJson = require(REACT_NATIVE_PACKAGE_JSON_PATH());
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 React Native requires %s. ' +
'Please use a supported version of Node.\n'
),
process.version,
packageJson.engines.node);
}
}
function printVersionsAndExit(reactNativePackageJsonPath) {
console.log('rn-redux-cli: ' + require('./package.json').version);
try {
console.log('rn-redux: ' + require(reactNativePackageJsonPath).version);
} catch (e) {
console.log('rn-redux: n/a - not inside a React Native project directory');
}
process.exit();
}