-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
455 additions
and
153 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,146 +1,12 @@ | ||
#!/usr/bin/env node | ||
|
||
var path = require('path'); | ||
var fs = require('fs'); | ||
var program = require('commander'); | ||
var mkdirp = require('mkdirp'); | ||
var debug = require('debug')('donejs-cli:binary'); | ||
var isWindows = require('os').platform() === 'win32'; | ||
|
||
var cli = require('../lib/cli'); | ||
var utils = require('../lib/utils'); | ||
var log = utils.log; | ||
|
||
var mypkg = require(path.join(__dirname, '..', 'package.json')); | ||
var commandList = ['init', 'generate']; | ||
|
||
program.version(mypkg.version) | ||
.description('The DoneJS command line utility lets you run generators, NPM scripts and binaries local to your project.'); | ||
|
||
debug('Loading CLI, version is', mypkg.version); | ||
|
||
utils.projectRoot().then(function (root) { | ||
debug('Found project root', root); | ||
var actions = { | ||
init: function (folder, options) { | ||
debug('Initializing new application', folder); | ||
if (folder) { | ||
var appDir = path.join(process.cwd(), folder); | ||
if (fs.existsSync(appDir)) { | ||
console.error('Folder `' + folder + '` already exists.'); | ||
return; | ||
} | ||
|
||
console.log('Creating folder ' + folder); | ||
mkdirp.sync(appDir); | ||
process.chdir(appDir); | ||
} | ||
|
||
var nodeModules = path.join(process.cwd(), 'node_modules'); | ||
|
||
if(!fs.existsSync(nodeModules)) { | ||
nodeModules = path.join(root, 'node_modules'); | ||
} | ||
|
||
debug('Generating application in folder', process.cwd()); | ||
|
||
var type = options.type || 'app'; | ||
var genVersion = mypkg.donejs.dependencies['generator-donejs']; | ||
var genInstallPromise = utils.installIfMissing(nodeModules, | ||
'generator-donejs', | ||
genVersion)(); | ||
|
||
var generatePromise = genInstallPromise.then(function () { | ||
return utils.generate(nodeModules, 'generator-donejs', [type, { | ||
version: mypkg.version, | ||
packages: mypkg.donejs, | ||
skipInstall: options.skipInstall | ||
} | ||
]); | ||
}); | ||
log(generatePromise); | ||
}, | ||
generate: function (type, options) { | ||
debug('Generating', 'generator-donejs', type, options); | ||
log(utils.generate(path.join(root, 'node_modules'), 'generator-donejs', [[type].concat(options)])); | ||
}, | ||
catchAll: function (command) { | ||
console.error('Could not run `' + command + '`'); | ||
program.help(); | ||
}, | ||
add: function(name, params) { | ||
var generators = require(path.join(root, 'node_modules', 'generator-donejs')); | ||
if(generators[name]) { | ||
debug('add called but running generate instead', name, params); | ||
return actions.generate(name, params); | ||
} | ||
|
||
debug('add', name, params); | ||
log(utils.add(path.join(root, 'node_modules'), name, params)); | ||
} | ||
}; | ||
// Lists all NPM scripts of the package.json in the current folder | ||
var curpkg = {}; | ||
try { | ||
curpkg = require(path.join(root, 'package.json')); | ||
} catch (e) { | ||
// otherwise no local package.json | ||
debug('Could not load local package.json'); | ||
} | ||
|
||
// donejs init | ||
program.command('init [folder]') | ||
.option('-S, --skip-install') | ||
.option('-T, --type [type]') | ||
.description('Initialize a new DoneJS application or plugin (--type plugin) in a new folder or the current one') | ||
.action(actions.init); | ||
|
||
// donejs generate | ||
program.command('generate <name> [options...]') | ||
.description('Run a generator.') | ||
.action(actions.generate); | ||
|
||
// catchall | ||
program.command('*') | ||
.description('') | ||
.action(actions.catchAll); | ||
|
||
program.command('add <name> [params...]') | ||
.description('Add functionality to your project') | ||
.action(actions.add); | ||
|
||
Object.keys(curpkg.scripts || {}).forEach(function (script) { | ||
if (commandList.indexOf(script) === -1) { | ||
commandList.push(script); | ||
program.command(script + ' [...args]') | ||
.description('`' + curpkg.scripts[script] + '` (package.json)') | ||
.action(function (args) { | ||
debug('Running script', script, args); | ||
log(utils.runScript(script, args)); | ||
}); | ||
} | ||
utils.projectRoot() | ||
.then(function(root) { | ||
cli(root, process.argv); | ||
}) | ||
.catch(function(err) { | ||
throw err; | ||
}); | ||
|
||
// List all other local binaries | ||
var binPath = path.join(path.join(root, "node_modules"), '.bin'); | ||
if (fs.existsSync(binPath)) { | ||
fs.readdirSync(binPath).forEach(function (name) { | ||
if (commandList.indexOf(name) === -1) { | ||
commandList.push(name); | ||
program.command(name + ' [...args]') | ||
.description('') | ||
.action(function (args) { | ||
var filename = name + (isWindows ? ".cmd": ""); | ||
debug('Running command', name, args); | ||
log(utils.runCommand(path.join(binPath, filename), args)); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
program.parse(process.argv); | ||
if (!program.args.length) { | ||
program.help(); | ||
} | ||
}); | ||
|
||
exports.program = 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,16 @@ | ||
var path = require('path'); | ||
var add = require('../utils').add; | ||
var generate = require('./cmd-generate'); | ||
var debug = require('debug')('donejs-cli:binary'); | ||
|
||
module.exports = function(root, name, params) { | ||
var generators = require(path.join(root, 'node_modules', 'generator-donejs')); | ||
|
||
if (generators[name]) { | ||
debug('add called but running generate instead', name, params); | ||
return generate(root, name, params); | ||
} | ||
|
||
debug('add', name, params); | ||
return add(path.join(root, 'node_modules'), name, params); | ||
}; |
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,13 @@ | ||
var path = require('path'); | ||
var generate = require('../utils').generate; | ||
var debug = require('debug')('donejs-cli:binary'); | ||
|
||
module.exports = function(root, type, options) { | ||
debug('Generating', 'generator-donejs', type, options); | ||
|
||
return generate( | ||
path.join(root, 'node_modules'), | ||
'generator-donejs', | ||
[[type].concat(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,45 @@ | ||
var Q = require('q'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var mkdirp = require('mkdirp'); | ||
var debug = require('debug')('donejs-cli:binary'); | ||
|
||
var utils = require('../utils'); | ||
var generate = utils.generate; | ||
var installIfMissing = utils.installIfMissing; | ||
|
||
module.exports = function(root, mypkg, folder, options) { | ||
debug('Initializing new application', folder); | ||
|
||
if (folder) { | ||
var appDir = path.join(process.cwd(), folder); | ||
|
||
if (fs.existsSync(appDir)) { | ||
return Q.reject(new Error('Folder `' + folder + '` already exists.')); | ||
} | ||
|
||
console.log('Creating folder ' + folder); | ||
mkdirp.sync(appDir); | ||
process.chdir(appDir); | ||
} | ||
|
||
var nodeModules = path.join(process.cwd(), 'node_modules'); | ||
|
||
if(!fs.existsSync(nodeModules)) { | ||
nodeModules = path.join(root, 'node_modules'); | ||
} | ||
|
||
debug('Generating application in folder', process.cwd()); | ||
|
||
var type = options.type || 'app'; | ||
var genVersion = mypkg.donejs.dependencies['generator-donejs']; | ||
|
||
return installIfMissing(nodeModules, 'generator-donejs', genVersion)() | ||
.then(function() { | ||
return generate(nodeModules, 'generator-donejs', [type, { | ||
version: mypkg.version, | ||
packages: mypkg.donejs, | ||
skipInstall: options.skipInstall | ||
}]); | ||
}); | ||
}; |
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,13 @@ | ||
var fs = require('fs'); | ||
|
||
module.exports = function(binPath) { | ||
var result = []; | ||
|
||
if (fs.existsSync(binPath)) { | ||
result = fs.readdirSync(binPath).map(function(name) { | ||
return name; | ||
}); | ||
} | ||
|
||
return result; | ||
}; |
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,13 @@ | ||
var path = require('path'); | ||
var debug = require('debug')('donejs-cli:binary'); | ||
|
||
module.exports = function(root) { | ||
try { | ||
var curpkg = require(path.join(root, 'package.json')); | ||
return curpkg.scripts || {}; | ||
} | ||
catch (e) { | ||
// otherwise no local package.json | ||
debug('Could not load local package.json'); | ||
} | ||
}; |
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,96 @@ | ||
var path = require('path'); | ||
var utils = require('../utils'); | ||
var program = require('commander'); | ||
var getLocalBins = require('./get-local-bins'); | ||
var getNpmScripts = require('./get-npm-scripts'); | ||
var debug = require('debug')('donejs-cli:binary'); | ||
var isWindows = require('os').platform() === 'win32'; | ||
|
||
// commands | ||
var add = require('./cmd-add'); | ||
var init = require('./cmd-init'); | ||
var generate = require('./cmd-generate'); | ||
var commandList = ['init', 'generate', 'add']; | ||
|
||
module.exports = function(root, argv) { | ||
var mypkg = require(path.join(root, 'package.json')); | ||
|
||
debug('mypkg', mypkg); | ||
|
||
program.version(mypkg.version) | ||
.description( | ||
'The DoneJS command line utility lets you run generators, NPM scripts ' + | ||
'and binaries local to your project.' | ||
); | ||
|
||
// donejs init | ||
program.command('init [folder]') | ||
.option('-S, --skip-install') | ||
.option('-T, --type [type]') | ||
.description( | ||
'Initialize a new DoneJS application or plugin (--type plugin) in ' + | ||
'a new folder or the current one' | ||
) | ||
.action(function(folder, options) { | ||
utils.log(init(root, mypkg, folder, options)); | ||
}); | ||
|
||
// donejs generate | ||
program.command('generate <name> [options...]') | ||
.description('Run a generator.') | ||
.action(function(type, options) { | ||
utils.log(generate(root, type, options)); | ||
}); | ||
|
||
// donejs add | ||
program.command('add <name> [params...]') | ||
.description('Add functionality to your project') | ||
.action(function(name, params) { | ||
utils.log(add(root, name, params)); | ||
}); | ||
|
||
// add package npm scripts as commands | ||
var scripts = getNpmScripts(root); | ||
Object.keys(scripts).forEach(function(script) { | ||
if (commandList.indexOf(script) === -1) { | ||
commandList.push(script); | ||
program.command(script + ' [...args]') | ||
.description('`' + scripts[script] + '` (package.json)') | ||
.action(function(args) { | ||
debug('Running script', script, args); | ||
utils.log(utils.runScript(script, args)); | ||
}); | ||
} | ||
}); | ||
|
||
// add all other local binaries | ||
var binPath = path.join(path.join(root, 'node_modules'), '.bin'); | ||
getLocalBins(binPath).forEach(function(name) { | ||
if (commandList.indexOf(name) === -1) { | ||
commandList.push(name); | ||
|
||
program.command(name + ' [...args]') | ||
.description('') | ||
.action(function(args) { | ||
var filename = name + (isWindows ? '.cmd': ''); | ||
debug('Running command', name, args); | ||
utils.log(utils.runCommand(path.join(binPath, filename), args)); | ||
}); | ||
} | ||
}); | ||
|
||
// catchall | ||
program.command('*') | ||
.description('') | ||
.action(function(command) { | ||
console.error('Could not run `' + command + '`'); | ||
program.help(); | ||
}); | ||
|
||
program.parse(argv); | ||
|
||
// show help if no args provided | ||
if (!program.args.length) { | ||
program.help(); | ||
} | ||
}; |
Oops, something went wrong.