Skip to content
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 6 commits into from
Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .editorconfig
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
6 changes: 4 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"describe": true,
"before": true,
"after": true,
"exports": true
"exports": true,
"beforeEach": true,
"afterEach": true
}
}
}
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ notifications:
- matthew@bitovi.com
on_success: change
on_failure: change
script: npm run jshint && npm run document && npm run test-guides && npm run coverage:upload
94 changes: 5 additions & 89 deletions bin/donejs
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();
}
23 changes: 23 additions & 0 deletions lib/cli/cmd-add.js
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) {
Copy link
Contributor

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.

Copy link
Contributor

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 :)

Copy link
Contributor

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.

Copy link
Contributor

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:

function doSomeStuff(){
  var a = 'foo';
  var b = 'bar';

  return doOtherStuff();

  function doOtherStuff() {
    // this is a super long function
  }
}

var types = ['app', 'plugin', 'generator'];
return types.indexOf(type) !== -1;
}
5 changes: 5 additions & 0 deletions lib/cli/cmd-help.js
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']);
};
50 changes: 50 additions & 0 deletions lib/cli/cmd-init.js
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);
}
64 changes: 64 additions & 0 deletions lib/cli/index.js
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;
25 changes: 25 additions & 0 deletions lib/cli/run-binary.js
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);
});
};
3 changes: 3 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
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
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
"test-quickstart": "guide guides/guide/test.js --local",
"test-pmo": "guide guides/place-my-order/test.js --local",
"jshint": "jshint lib/. bin/donejs test/. --config",
"mocha": "mocha test/ --timeout 120000",
"mocha": "mocha test/test --timeout 120000",
"verify": "echo \"Script ran\"",
"document:watch": "documentjs --watch",
"document": "documentjs -f && cp docs/theme/static/favicon.ico site/favicon.ico",
"publish": "git push origin && git push origin --tags",
"release:prerelease": "npm version prerelease && npm publish",
"release:patch": "npm version patch && npm publish",
"release:minor": "npm version minor && npm publish",
"release:major": "npm version major && npm publish"
"release:major": "npm version major && npm publish",
"coverage": "istanbul cover _mocha -- test/test --timeout 120000",
"coverage:upload": "istanbul cover _mocha --report lcovonly -- test/ --timeout 600000 && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"repository": {
"type": "git",
Expand All @@ -38,17 +40,23 @@
},
"homepage": "https://github.com/donejs/donejs",
"devDependencies": {
"coveralls": "^2.11.9",
"documentjs": "^0.4.3",
"dotdotdot": "^1.7.0",
"es6-promise": "^3.0.2",
"guide-automation": "^0.2.0",
"is-appveyor": "^1.0.0",
"istanbul": "^0.4.2",
"jshint": "^2.8.0",
"mocha": "^2.2.5",
"mocha-lcov-reporter": "^1.2.0",
"mockery": "^1.4.1",
"node-fetch": "^1.3.3",
"rimraf": "^2.5.2",
"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