-
Notifications
You must be signed in to change notification settings - Fork 164
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
Initial implementation of general donejs add
.
#603
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
357e6d6
Add code coverage and refactor cli.
13685b8
Make sure guide tests run in Travis.
e5a605e
Fix run-binary tests in windows.
80c956e
Initial implementation of general `donejs add`.
e14055f
Support original "donejs add" command.
5fbc1c1
Break up travis scripts into multiple lines.
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
; Unix-style newlines | ||
[*] | ||
indent_size = 2 | ||
end_of_line = LF | ||
indent_style = space | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,9 @@ | ||
#!/usr/bin/env node | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var program = require('commander'); | ||
|
||
var utils = require('../lib/utils'); | ||
var mypkg = require(path.join(__dirname, '..', 'package.json')); | ||
var program = require('../lib/cli'); | ||
|
||
program.version(mypkg.version); | ||
program.parse(process.argv); | ||
|
||
utils.projectRoot().then(function(root) { | ||
var doneScript = process.platform === 'win32' ? 'donejs.cmd' : 'donejs'; | ||
var donejsBinary = path.join(root, 'node_modules', '.bin', doneScript); | ||
var runBinary = function(args) { | ||
if(!fs.existsSync(donejsBinary)) { | ||
console.error('Could not find local DoneJS binary (' + donejsBinary + ')'); | ||
return; | ||
} | ||
|
||
return utils.spawn(donejsBinary, args, { cwd: root }); | ||
}; | ||
var runInit = function(folder, opts) { | ||
var promise = utils.mkdirp(folder).then(function(fullPath) { | ||
var options = { cwd: fullPath }; | ||
|
||
console.log('Initializing new DoneJS application at', fullPath); | ||
console.log('Installing donejs-cli'); | ||
|
||
var cliArg = 'donejs-cli@' + utils.versionRange(mypkg.version); | ||
|
||
return utils.spawn('npm', [ 'install', cliArg, '--loglevel', 'error' ], options) | ||
.then(function() { | ||
var args = [ 'node_modules', 'donejs-cli', 'bin', 'donejs' ]; | ||
var binary = path.join.apply(path, [fullPath].concat(args)); | ||
|
||
if(!fs.existsSync(binary)) { | ||
// If donejs-cli wasn't installed in this folder, it is the root | ||
binary = path.join.apply(path, [root].concat(args)); | ||
} | ||
var initArgs = [binary, 'init']; | ||
|
||
if(opts.skipInstall) { | ||
initArgs.push('--skip-install'); | ||
} | ||
|
||
if(opts.type) { | ||
initArgs.push('--type', opts.type); | ||
} | ||
|
||
return utils.spawn('node', initArgs, options); | ||
}); | ||
}); | ||
|
||
utils.log(promise); | ||
}; | ||
|
||
// donejs init | ||
program.command('init [folder]') | ||
.option('-S, --skip-install') | ||
.option('-T, --type [type]') | ||
.description('Initialize a new DoneJS application in a new folder or the current one') | ||
.action(runInit); | ||
|
||
// DoneJS plugin | ||
program.command('plugin [folder]') | ||
.option('-S, --skip-install') | ||
.description('Initialize a new DoneJS plugin in a new folder or the current one') | ||
.action(function(folder, opts) { | ||
opts.type = 'plugin'; | ||
runInit(folder, opts); | ||
}); | ||
|
||
program.command('help') | ||
.description('Show all DoneJS commands available for this application') | ||
.action(function() { | ||
runBinary(['--help']); | ||
}); | ||
|
||
// donejs <anything else> | ||
program.command('*') | ||
.description('Run DoneJS commands using the current DoneJS application') | ||
.action(function() { | ||
runBinary(program.rawArgs.slice(2)); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
if (!program.args.length) { | ||
program.help(); | ||
} | ||
}); | ||
|
||
exports.program = program; | ||
if (!program.args.length) { | ||
program.help(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var Q = require('q'); | ||
var runBinary = require('./run-binary'); | ||
|
||
// `donejs add app [folder]` => `donejs-cli init [folder]` | ||
// `donejs add plugin [folder]` => `donejs-cli init [folder] --type=plugin` | ||
// `donejs add generator [name]` => `donejs-cli init [folder] --type=generator` | ||
module.exports = function(type, folder, options) { | ||
if (!isValidType(type)) { | ||
var msg = 'Invalid [type] value `' + type + '`'; | ||
return Q.reject(new Error(msg)); | ||
} | ||
|
||
if (type !== 'app') { | ||
options.type = type; | ||
} | ||
|
||
return runBinary(['init', folder], options); | ||
}; | ||
|
||
function isValidType(type) { | ||
var types = ['app', 'plugin', 'generator']; | ||
return types.indexOf(type) !== -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var runBinary = require('./run-binary'); | ||
|
||
module.exports = function() { | ||
return runBinary(['--help']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var utils = require('../utils'); | ||
var runBinary = require('./run-binary'); | ||
var mypkg = require(path.join(__dirname, '..', '..', 'package.json')); | ||
|
||
module.exports = function(folder, opts) { | ||
return utils.mkdirp(folder) | ||
.then(function(folderPath) { | ||
var folderModules = path.join(folderPath, 'node_modules'); | ||
|
||
// create an empty node_modules inside the target `folder`, this | ||
// will prevent npm to install the dependencies in any node_modules | ||
// folder but the one inside `folder`. | ||
if (!fs.existsSync(folderModules)) { | ||
fs.mkdirSync(folderModules); | ||
} | ||
|
||
console.log('Initializing new DoneJS application at', folderPath); | ||
console.log('Installing donejs-cli'); | ||
|
||
return installCli(mypkg.version, { cwd: folderPath }) | ||
.then(function() { | ||
return runCliInit(folderPath, opts); | ||
}); | ||
}); | ||
}; | ||
|
||
// install donejs-cli | ||
function installCli(version, options) { | ||
var pkg = 'donejs-cli@' + utils.versionRange(version); | ||
var npmArgs = [ 'install', pkg, '--loglevel', 'error' ]; | ||
return utils.spawn('npm', npmArgs, options); | ||
} | ||
|
||
// run donejs-cli init | ||
function runCliInit(folderPath, options) { | ||
var initArgs = ['init']; | ||
|
||
if (options.skipInstall) { | ||
initArgs.push('--skip-install'); | ||
} | ||
|
||
if (options.type) { | ||
initArgs.push('--type', options.type); | ||
} | ||
|
||
options.cwd = folderPath; | ||
return runBinary(initArgs, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var program = require('commander'); | ||
var log = require('../utils').log; | ||
var chalk = require('chalk'); | ||
|
||
// commands | ||
var add = require('./cmd-add'); | ||
var init = require('./cmd-init'); | ||
var help = require('./cmd-help'); | ||
|
||
// donejs add | ||
program.command('add <type> [folder]') | ||
.option('-S, --skip-install') | ||
.usage(cmdAddUsage()) | ||
.action(function(type, folder, options) { | ||
log(add(type, folder, options)); | ||
}); | ||
|
||
// DEPRECATED: donejs init | ||
program.command('init [folder]') | ||
.option('-S, --skip-install') | ||
.option('-T, --type [type]') | ||
.description('Initialize a new DoneJS application in a new folder or the current one') | ||
.action(function(folder, options) { | ||
deprecationNotice('donejs init [folder]', 'donejs add app [folder]'); | ||
log(init(folder, options)); | ||
}); | ||
|
||
// DEPRECATED: donejs plugin | ||
program.command('plugin [folder]') | ||
.option('-S, --skip-install') | ||
.description('Initialize a new DoneJS plugin in a new folder or the current one') | ||
.action(function(folder, opts) { | ||
deprecationNotice('donejs plugin [folder]', 'donejs add plugin [folder]'); | ||
opts.type = 'plugin'; | ||
log(init(folder, opts)); | ||
}); | ||
|
||
program.command('help') | ||
.description('Show all DoneJS commands available for this application') | ||
.action(function() { | ||
log(help()); | ||
}); | ||
|
||
function deprecationNotice(deprecated, instead) { | ||
console.log(); | ||
console.log(chalk.yellow('DEPRECATION NOTICE:')); | ||
console.log(); | ||
console.log(' ' + chalk.gray(deprecated) + ' is deprecated'); | ||
console.log(); | ||
console.log(' Use ' + chalk.inverse(instead) + ' instead'); | ||
console.log(); | ||
} | ||
|
||
function cmdAddUsage() { | ||
var usage = | ||
'[options] <type> [folder] \n\n' + | ||
' Types: \n\n' + | ||
' app, Initializes a new app\n' + | ||
' plugin, Initializes a new plugin\n' + | ||
' generator, Initializes a basic generator'; | ||
return usage; | ||
} | ||
|
||
module.exports = program; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var Q = require('q'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var utils = require('../utils'); | ||
|
||
module.exports = function(args, options) { | ||
options = options || {}; | ||
|
||
return utils.projectRoot() | ||
.then(function(root) { | ||
var doneScript = process.platform === 'win32' ? 'donejs.cmd' : 'donejs'; | ||
var donejsBinary = path.join(root, 'node_modules', '.bin', doneScript); | ||
|
||
if (!fs.existsSync(donejsBinary)) { | ||
var msg = 'Could not find local DoneJS binary (' + donejsBinary + ')'; | ||
return Q.reject(new Error(msg)); | ||
} | ||
|
||
if (!options.cwd) { | ||
options.cwd = root; | ||
} | ||
|
||
return utils.spawn(donejsBinary, args, options); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
JSHint will complain if functions are used before they are declared. We probably want to move this one to the top.
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.
I'd say jshint is wrong then :)
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.
It's a pet peeve of mine, too. Imagine if this would be 1000 lines of code and I'd skim through it I have no idea what this function does until I find it somewhere later on.
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.
See, I like this style for that reason, if you have a really long function I like seeing the
return
early in the function body, I think it makes it easier to understand how it flows. Like: