-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating a new app in the current directory #368
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ 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: | ||
|
@@ -67,21 +68,23 @@ if (commands.length === 0) { | |
createApp(commands[0], argv.verbose, argv['scripts-version']); | ||
|
||
function createApp(name, verbose, version) { | ||
if (fs.existsSync(name)) { | ||
console.log('The directory `' + name + '` already exists. Aborting.'); | ||
var root = path.resolve(name); | ||
if (!pathExists.sync(name)) { | ||
fs.mkdirSync(root); | ||
} | ||
// Check if GitHub boilerplate compatible | ||
// https://github.com/facebookincubator/create-react-app/pull/368#issuecomment-237875655 | ||
else if (!isGitHubBoilerplate(root)) { | ||
console.log('The directory `' + name + '` contains file(s) that could conflict. Aborting.'); | ||
process.exit(1); | ||
} | ||
|
||
var root = path.resolve(name); | ||
var appName = path.basename(root); | ||
|
||
console.log( | ||
'Creating a new React app in ' + root + '.' | ||
); | ||
console.log(); | ||
|
||
fs.mkdirSync(root); | ||
|
||
var packageJson = { | ||
name: appName, | ||
version: '0.0.1', | ||
|
@@ -166,3 +169,13 @@ function checkNodeVersion() { | |
process.exit(1); | ||
} | ||
} | ||
|
||
function isGitHubBoilerplate(root) { | ||
var validFiles = [ | ||
'.DS_Store', 'Thumbs.db', '.git', '.gitignore', 'README.md', 'LICENSE' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add 'CNAME' to the the validFiles array? https://help.github.com/articles/adding-or-removing-a-custom-domain-for-your-github-pages-site/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you open a new issue with this request please? It's a little difficult to track in an issue like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this essentially makes it impossible to use another collaboration/versioning product eg subversion, or some other IDE such as eclipse to register the project. I am trying to co-locate my project in eclipse and use subversion for collaboration. Eclipse creates a ".project" file, and subversion creates ".svn" directories. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please open another issue to discuss this. Comments on old issues aren't very actionable. |
||
]; | ||
return fs.readdirSync(root) | ||
.every(function (file) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: no space after function |
||
return validFiles.indexOf(file) >= 0; | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
var fs = require('fs-extra'); | ||
var path = require('path'); | ||
var spawn = require('cross-spawn'); | ||
var pathExists = require('path-exists'); | ||
var chalk = require('chalk'); | ||
|
||
module.exports = function(appPath, appName, verbose, originalDirectory) { | ||
var ownPath = path.join(appPath, 'node_modules', 'react-scripts'); | ||
|
@@ -43,12 +45,31 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { | |
JSON.stringify(appPackage, null, 2) | ||
); | ||
|
||
var readmeExists = pathExists.sync(path.join(appPath, 'README.md')); | ||
if (readmeExists) { | ||
fs.renameSync(path.join(appPath, 'README.md'), path.join(appPath, 'README.old.md')); | ||
} | ||
|
||
// Copy the files for the user | ||
fs.copySync(path.join(ownPath, 'template'), appPath); | ||
|
||
// Rename gitignore after the fact to prevent npm from renaming it to .npmignore | ||
// See: https://github.com/npm/npm/issues/1862 | ||
fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), []); | ||
fs.move(path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), [], function (err) { | ||
if (err) { | ||
// Append if there's already a `.gitignore` file there | ||
if (err.code === 'EEXIST') { | ||
fs.readFile(path.join(appPath, 'gitignore'), (err, data) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use readFileSync, otherwise we may run into race conditions as the rest of the file is going to run concurrently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we do nothing with that file later, so I thought it would be ok. Changing it. |
||
if (err) throw err; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: always use {} and put the throw on its own line |
||
fs.appendFileSync(path.join(appPath, '.gitignore'), data); | ||
fs.unlinkSync(path.join(appPath, 'gitignore')); | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: } on the same line as else { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sigh! sorry for these silly mistakes. I thought |
||
else { | ||
throw err; | ||
} | ||
} | ||
}); | ||
|
||
// Run another npm install for react and react-dom | ||
console.log('Installing react and react-dom from npm...'); | ||
|
@@ -88,6 +109,10 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { | |
console.log(); | ||
console.log(' cd', cdpath); | ||
console.log(' npm start'); | ||
if (readmeExists) { | ||
console.log(); | ||
console.log(chalk.yellow('You had a `README.md` file, we renamed it to `README.old.md`')); | ||
} | ||
console.log(); | ||
console.log('Happy hacking!'); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: put } before else if. Otherwise skimming through the file we may read as if that's the only condition