Skip to content

Commit

Permalink
Initial implementation of general donejs add.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Mujica committed Mar 31, 2016
1 parent e5a605e commit 80c956e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lib/cli/cmd-add.js
@@ -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;
}
36 changes: 34 additions & 2 deletions lib/cli/index.js
@@ -1,24 +1,36 @@
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 init
// 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));
});

// DoneJS plugin
// 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));
});
Expand All @@ -29,4 +41,24 @@ program.command('help')
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;
3 changes: 3 additions & 0 deletions lib/utils.js
Expand Up @@ -73,7 +73,10 @@ exports.log = function(promise) {
return promise.then(function() {
process.exit(0);
}, function(error) {
console.log();
console.error(error.stack || error.message || error);
console.log();

process.exit(1);
});
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"stream-when": "^1.0.0"
},
"dependencies": {
"chalk": "^1.1.3",
"commander": "^2.8.1",
"cross-spawn-async": "^2.0.0",
"q": "^1.4.1"
Expand Down
75 changes: 75 additions & 0 deletions test/cmd-add-test.js
@@ -0,0 +1,75 @@
var Q = require('q');
var assert = require('assert');
var mockery = require('mockery');

describe('cmd add test', function() {
var add;
var binaryArgs;

beforeEach(function() {
var cmdAddPath = '../lib/cli/cmd-add';

mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});

mockery.registerAllowable(cmdAddPath);

mockery.registerMock('./run-binary', function() {
binaryArgs = arguments;
return Q(true);
});

add = require(cmdAddPath);
});

afterEach(function() {
mockery.disable();
mockery.deregisterAll();
});

it('rejects when [type] value is invalid', function(done) {
var folder;

add('foo', folder, {})
.then(function() {
assert(false, 'should have failed!');
done();
})
.catch(function(err) {
assert(err.message, 'Invalid [type] value `foo`');
done();
});
});

it('when type is "app", runs binary with the right args', function() {
var folder = 'my-awesome-app';

return add('app', folder, {})
.then(function() {
assert.deepEqual(binaryArgs[0], ['init', folder]);
});
});

it('when type is "plugin", runs binary with the right args', function() {
var folder = 'my-awesome-app';

return add('plugin', folder, {})
.then(function() {
assert.deepEqual(binaryArgs[0], ['init', folder]);
assert.deepEqual(binaryArgs[1].type, 'plugin');
});
});

it('when type is "generator", runs binary with the right args', function() {
var name = 'donejs-jshint';

return add('generator', name, {})
.then(function() {
assert.deepEqual(binaryArgs[0], ['init', name]);
assert.deepEqual(binaryArgs[1].type, 'generator');
});
});

});
1 change: 1 addition & 0 deletions test/test.js
@@ -1,5 +1,6 @@
require('./run-binary-test');
require('./cmd-init-test');
require('./cmd-help-test');
require('./cmd-add-test');

require('./utils-test');

0 comments on commit 80c956e

Please sign in to comment.