Skip to content

Commit e26b8d9

Browse files
torifatvjeux
authored andcommitted
Creating a new app in the current directory (#368)
* Creating a new app in the current directory * Fixed style mistakes
1 parent db5f874 commit e26b8d9

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

global-cli/index.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var spawn = require('cross-spawn');
4141
var chalk = require('chalk');
4242
var semver = require('semver');
4343
var argv = require('minimist')(process.argv.slice(2));
44+
var pathExists = require('path-exists');
4445

4546
/**
4647
* Arguments:
@@ -67,21 +68,20 @@ if (commands.length === 0) {
6768
createApp(commands[0], argv.verbose, argv['scripts-version']);
6869

6970
function createApp(name, verbose, version) {
70-
if (fs.existsSync(name)) {
71-
console.log('The directory `' + name + '` already exists. Aborting.');
71+
var root = path.resolve(name);
72+
if (!pathExists.sync(name)) {
73+
fs.mkdirSync(root);
74+
} else if (!isGitHubBoilerplate(root)) {
75+
console.log('The directory `' + name + '` contains file(s) that could conflict. Aborting.');
7276
process.exit(1);
7377
}
7478

75-
var root = path.resolve(name);
7679
var appName = path.basename(root);
77-
7880
console.log(
7981
'Creating a new React app in ' + root + '.'
8082
);
8183
console.log();
8284

83-
fs.mkdirSync(root);
84-
8585
var packageJson = {
8686
name: appName,
8787
version: '0.0.1',
@@ -166,3 +166,15 @@ function checkNodeVersion() {
166166
process.exit(1);
167167
}
168168
}
169+
170+
// Check if GitHub boilerplate compatible
171+
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-237875655
172+
function isGitHubBoilerplate(root) {
173+
var validFiles = [
174+
'.DS_Store', 'Thumbs.db', '.git', '.gitignore', 'README.md', 'LICENSE'
175+
];
176+
return fs.readdirSync(root)
177+
.every(function(file) {
178+
return validFiles.indexOf(file) >= 0;
179+
});
180+
}

global-cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"chalk": "^1.1.1",
2121
"cross-spawn": "^4.0.0",
2222
"minimist": "^1.2.0",
23+
"path-exists": "^3.0.0",
2324
"semver": "^5.0.3"
2425
}
2526
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"jest": "14.1.0",
6767
"json-loader": "0.5.4",
6868
"opn": "4.0.2",
69+
"path-exists": "^3.0.0",
6970
"postcss-loader": "0.9.1",
7071
"promise": "7.1.1",
7172
"recursive-readdir": "2.0.0",

scripts/init.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
var fs = require('fs-extra');
1111
var path = require('path');
1212
var spawn = require('cross-spawn');
13+
var pathExists = require('path-exists');
14+
var chalk = require('chalk');
1315

1416
module.exports = function(appPath, appName, verbose, originalDirectory) {
1517
var ownPath = path.join(appPath, 'node_modules', 'react-scripts');
@@ -43,12 +45,28 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
4345
JSON.stringify(appPackage, null, 2)
4446
);
4547

48+
var readmeExists = pathExists.sync(path.join(appPath, 'README.md'));
49+
if (readmeExists) {
50+
fs.renameSync(path.join(appPath, 'README.md'), path.join(appPath, 'README.old.md'));
51+
}
52+
4653
// Copy the files for the user
4754
fs.copySync(path.join(ownPath, 'template'), appPath);
4855

4956
// Rename gitignore after the fact to prevent npm from renaming it to .npmignore
5057
// See: https://github.com/npm/npm/issues/1862
51-
fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), []);
58+
fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), [], function (err) {
59+
if (err) {
60+
// Append if there's already a `.gitignore` file there
61+
if (err.code === 'EEXIST') {
62+
var data = fs.readFileSync(path.join(appPath, 'gitignore'));
63+
fs.appendFileSync(path.join(appPath, '.gitignore'), data);
64+
fs.unlinkSync(path.join(appPath, 'gitignore'));
65+
} else {
66+
throw err;
67+
}
68+
}
69+
});
5270

5371
// Run another npm install for react and react-dom
5472
console.log('Installing react and react-dom from npm...');
@@ -88,6 +106,10 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
88106
console.log();
89107
console.log(' cd', cdpath);
90108
console.log(' npm start');
109+
if (readmeExists) {
110+
console.log();
111+
console.log(chalk.yellow('You had a `README.md` file, we renamed it to `README.old.md`'));
112+
}
91113
console.log();
92114
console.log('Happy hacking!');
93115
});

0 commit comments

Comments
 (0)