Skip to content
This repository has been archived by the owner on Sep 29, 2017. It is now read-only.

Commit

Permalink
general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Oct 22, 2015
1 parent abcd9c2 commit 89bb08f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 69 deletions.
63 changes: 31 additions & 32 deletions bin/digs-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

'use strict';

let yargs = require('yargs');
let path = require('path');
let execute = require('../lib');
const yargs = require('yargs');
const execute = require('../lib');
const _ = require('lodash');
const pkg = require('../package.json');
const emoji = require('node-emoji');
const chalk = require('chalk');

// note to self: "var" is intended here
// not a mistake
/* eslint no-var:0 */
var argv;

Expand All @@ -19,48 +22,44 @@ function commandYargs(y, name, options) {
}

argv = yargs
.usage('Usage: $0 <command>')
.usage(`Usage: $0 <command>`)
.command('symlink',
`Symlink digs-dev's dotfiles into current or target directory`,
function symlink(y) {
argv = commandYargs(y, 'symlink');
})
`Symlink digs-dev's dotfiles into current or target directory`,
_.partialRight(commandYargs, 'symlink'))
.command('upgrade',
`Install/upgrades digs-dev's dependencies into current or target directory.` +
`(will modify your "package.json" if the directory is under Git VC)`,
function upgrade(y) {
argv = commandYargs(y, 'upgrade');
})
`Install/upgrade digs-dev's devDependencies into current or target ` +
`directory (will modify your "package.json" if the directory is under VCS)`,
_.partialRight(commandYargs, 'upgrade'))
.command('install',
`Performs an "upgrade", "symlink" & "gitignore"`,
function install(y) {
argv = commandYargs(y, 'install');
})
`Performs an "upgrade", "symlink" & "gitignore"`,
_.partialRight(commandYargs, 'install'))
.command('gitignore',
`Updates .gitignore to include all symlinked files`,
function gitignore(y) {
argv = commandYargs(y, 'gitignore');
})
`Updates .gitignore to include all symlinked files`,
_.partialRight(commandYargs, 'gitignore'))
.option('help', {
alias: 'h',
describe: 'Show this help, or specify a command for details',
example: '$0 --help [command]',
type: 'string'
})
.version(function getVersion() {
return require(path.join(__dirname, '..', 'package.json')).version;
})
.showHelpOnFail(true)
.check(function check(finalArgv) {
if (finalArgv.hasOwnProperty('help')) {
yargs.showHelp();
.version(() => pkg.version)
.check((_argv) => {
if (_.has(_argv, 'help')) {
yargs.showHelp('log');
return true;
}
if (execute[finalArgv._[0]]) {
return true;
if (_.isEmpty(_argv._)) {
throw new Error(`${emoji.get('exclamation')} Command is required`);
}
const command = _.first(_argv._);
if (!_.has(execute, command)) {
throw new Error(`${emoji.get('exclamation')} Unknown command ` +
`"${command}"`);
}
throw new Error('Command is required!');
return true;
})
.epilog(`${emoji.get('bug')} Problems? ` +
chalk.underline(`https://github.com/digsjs/digs-dev/issues/`))
.argv;

execute(argv._[0], argv);
8 changes: 4 additions & 4 deletions lib/commands.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

let log = require('./logger');
let pkg = require('../package.json');
let _ = require('lodash');
const log = require('./logger');
const pkg = require('../package.json');
const _ = require('lodash');

function uniquifyArgs(argv) {
return _.pick(argv, (v, k) => {
Expand All @@ -12,10 +12,10 @@ function uniquifyArgs(argv) {

function execute(command, argv, grunt) {
const args = uniquifyArgs(argv);
log.info(`digs-dev@v${pkg.version} executing command "${command}"`);
if (arguments.length < 3) {
return execute[command](argv._[1], args);
}
log.info(`digs-dev [${pkg.version}] executing command "${command}"`);
return execute[command](args, grunt.log);
}

Expand Down
33 changes: 17 additions & 16 deletions lib/gitignore.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

let gitignoreParser = require('gitignore-parser');
let Promise = require('bluebird');
let fs = Promise.promisifyAll(require('graceful-fs'));
let logger = require('./logger');
let pkg = require('../package.json');
let path = require('path');
let _ = require('lodash');
let utils = require('./utils');
const gitignoreParser = require('gitignore-parser');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('graceful-fs'));
const logger = require('./logger');
const pkg = require('../package.json');
const path = require('path');
const _ = require('lodash');
const utils = require('./utils');

const GITIGNORE = '.gitignore';

Expand All @@ -17,10 +17,11 @@ function updateGitignore(cwd, opts, log) {
ignore: []
});
opts.ignore = [].concat(opts.ignore);
const isIgnored = _.partial(_.contains, opts.ignore);
const isIgnored = _.partial(_.has, opts.ignore);
cwd = cwd || process.cwd();

return utils.isGit(cwd)
.then(function maybeUpdate(result) {
.then((result) => {
if (result) {
const symlinks = pkg.config['digs-dev'].symlink;
const gitignorePath = path.join(cwd, GITIGNORE);
Expand All @@ -46,21 +47,21 @@ function updateGitignore(cwd, opts, log) {
entries: _.reject(symlinks, isIgnored)
};
})
.then(function getInfo(data) {
.then((data) => {
const contents = data.contents;
const entries = data.entries;

if (entries.length) {
if (!_.isEmpty(entries)) {
let beforeLen = 0;
let afterLen = 0;

return {
contents: _(contents)
.tap(function getBeforeLines(lines) {
.tap((lines) => {
beforeLen = lines.length;
})
.concat(entries)
.tap(function getAfterLines(lines) {
.tap((lines) => {
afterLen = lines.length;
})
.join('\n'),
Expand All @@ -69,10 +70,10 @@ function updateGitignore(cwd, opts, log) {
}
log.ok(`${gitignorePath} up-to-date; nothing to do`);
})
.then(function write(obj) {
.then((obj) => {
if (obj) {
return fs.writeFileAsync(gitignorePath, obj.contents)
.then(function report() {
.then(() => {
log.ok(`Appended ${obj.appended} entries to` +
`${gitignorePath}`);
});
Expand Down
16 changes: 8 additions & 8 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';

let symlink = require('./symlink');
let upgrade = require('./upgrade');
let logger = require('./logger');
let gitignore = require('./gitignore');
const symlink = require('./symlink');
const upgrade = require('./upgrade');
const logger = require('./logger');
const gitignore = require('./gitignore');

function install(cwd, opts, log) {
log = log || logger;
return upgrade(cwd, log)
.then(function execSymlink() {
.then(() => {
return symlink(cwd, log);
})
.then(function execGitignore(failures) {
.then((ignore) => {
return gitignore(cwd, {
ignore: failures
ignore: ignore
}, log);
})
.then(function report() {
.then(() => {
log.ok('Install complete.');
});
}
Expand Down
19 changes: 10 additions & 9 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

let EventEmitter = require('events').EventEmitter;
let emoji = require('node-emoji');
let _ = require('lodash');
let format = require('util').format;
let chalk = require('chalk');
const EventEmitter = require('events').EventEmitter;
const emoji = require('node-emoji');
const _ = require('lodash');
const format = require('util').format;
const chalk = require('chalk');

let log;

const LEVELS = {
Expand Down Expand Up @@ -46,10 +47,10 @@ class Logger extends EventEmitter {

this.level = logLevel || 'log';

_.each(LEVELS, function createMethod(settings, name) {
_.each(LEVELS, (settings, name) => {
this[name] = function logMethod() {
let level = LEVELS[name];
let args = [
const level = LEVELS[name];
const args = [
emoji.get(level.emoji),
' ',
level.color.apply(null, arguments)
Expand All @@ -59,7 +60,7 @@ class Logger extends EventEmitter {
}
this.emit(level, format(arguments));
};
}, this);
});

/* eslint consistent-this:0 */
log = this;
Expand Down

0 comments on commit 89bb08f

Please sign in to comment.