Skip to content

Commit

Permalink
More ES2015ification
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored and SBoudrias committed Mar 5, 2017
1 parent d535bac commit 6553965
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 373 deletions.
8 changes: 4 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ gulp.task('nsp', cb => {
nsp({package: path.resolve('package.json')}, cb);
});

gulp.task('pre-test', () => {
return gulp.src([
gulp.task('pre-test', () =>
gulp.src([
'lib/**/*.js'
])
.pipe(istanbul({includeUntested: true}))
.pipe(istanbul.hookRequire());
});
.pipe(istanbul.hookRequire())
);

gulp.task('test', ['pre-test'], cb => {
let mochaErr;
Expand Down
38 changes: 17 additions & 21 deletions lib/actions/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const table = require('text-table');
const pathExists = require('path-exists');

/**
* @mixin
Expand All @@ -13,12 +12,12 @@ const help = module.exports;

/**
* Tries to get the description from a USAGE file one folder above the
* source root otherwise uses a default description.
* source root otherwise uses a default description
*/

help.help = function () {
const filepath = path.join(this.sourceRoot(), '../USAGE');
const exists = pathExists.sync(filepath);
const filepath = path.resolve(this.sourceRoot(), '../USAGE');
const exists = fs.existsSync(filepath);

let out = [
'Usage:',
' ' + this.usage(),
Expand Down Expand Up @@ -52,31 +51,30 @@ help.help = function () {
};

function formatArg(config) {
let arg = '<' + config.name + '>';
let arg = `<${config.name}>`;

if (!config.required) {
arg = '[' + arg + ']';
arg = `[${arg}]`;
}

return arg;
}

/**
* Output usage information for this given generator, depending on its arguments
* or options.
* or options
*/

help.usage = function () {
const options = Object.keys(this._options).length ? '[options]' : '';
let name = ' ' + this.options.namespace;
let name = this.options.namespace;
let args = '';

if (this._arguments.length > 0) {
args = this._arguments.map(formatArg).join(' ');
}

name = name.replace(/^yeoman:/, '');
let out = 'yo' + name + ' ' + options + ' ' + args;
let out = `yo ${name} ${options} ${args}`;

if (this.description) {
out += '\n\n' + this.description;
Expand Down Expand Up @@ -105,9 +103,9 @@ help.argumentsHelp = function () {
return [
'',
config.name ? config.name : '',
config.description ? '# ' + config.description : '',
config.type ? 'Type: ' + config.type.name : '',
'Required: ' + config.required
config.description ? `# ${config.description}` : '',
config.type ? `Type: ${config.type.name}` : '',
`Required: ${config.required}`
];
});

Expand All @@ -119,17 +117,15 @@ help.argumentsHelp = function () {
* @returns {String} Text of options in formatted table
*/
help.optionsHelp = function () {
const options = _.reject(this._options, el => {
return el.hide;
});
const options = _.reject(this._options, x => x.hide);

const rows = options.map(opt => {
return [
'',
opt.alias ? '-' + opt.alias + ', ' : '',
'--' + opt.name,
opt.description ? '# ' + opt.description : '',
(opt.default !== undefined && opt.default !== null && opt.default !== '') ? 'Default: ' + opt.default : ''
opt.alias ? `-${opt.alias}, ` : '',
`--${opt.name}`,
opt.description ? `# ${opt.description}` : '',
(opt.default !== undefined && opt.default !== '') ? 'Default: ' + opt.default : ''
];
});

Expand Down
20 changes: 10 additions & 10 deletions lib/actions/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ install.runInstall = function (installer, paths, options, cb, spawnOptions) {

options = options || {};
spawnOptions = spawnOptions || {};
cb = cb || function () {};
cb = cb || (() => {});
paths = Array.isArray(paths) ? paths : (paths && paths.split(' ')) || [];

let args = ['install'].concat(paths).concat(dargs(options));

// Yarn uses the `add` command to specifically add a package to a project.
// Yarn uses the `add` command to specifically add a package to a project
if (installer === 'yarn' && paths.length > 0) {
args[0] = 'add';
}
Expand All @@ -48,14 +48,14 @@ install.runInstall = function (installer, paths, options, cb, spawnOptions) {
args = args.concat(['--cache-min', 24 * 60 * 60]);
}

// Return early if we're skipping installation.
// Return early if we're skipping installation
if (this.options.skipInstall) {
cb();
return this;
}

this.env.runLoop.add('install', done => {
this.emit(installer + 'Install', paths);
this.emit(`${installer}Install`, paths);
this.spawnCommand(installer, args, spawnOptions)
.on('error', err => {
console.log(chalk.red('Could not finish installation. \n') +
Expand All @@ -65,11 +65,14 @@ install.runInstall = function (installer, paths, options, cb, spawnOptions) {
cb(err);
})
.on('exit', err => {
this.emit(installer + 'Install:end', paths);
this.emit(`${installer}Install:end`, paths);
cb(err);
done();
});
}, {once: installer + ' ' + args.join(' '), run: false});
}, {
once: installer + ' ' + args.join(' '),
run: false
});

return this;
};
Expand All @@ -94,7 +97,6 @@ install.runInstall = function (installer, paths, options, cb, spawnOptions) {
* @param {Boolean} [options.skipMessage=false] - whether to log the used commands
* @param {Function} [options.callback] - call once all commands have run
*/

install.installDependencies = function (options) {
options = options || {};
const commands = [];
Expand Down Expand Up @@ -157,7 +159,6 @@ install.installDependencies = function (options) {
* @param {Function} [cb]
* @param {Object} [spawnOptions] Options to pass `child_process.spawn`.
*/

install.bowerInstall = function (cmpnt, options, cb, spawnOptions) {
return this.runInstall('bower', cmpnt, options, cb, spawnOptions);
};
Expand All @@ -172,10 +173,10 @@ install.bowerInstall = function (cmpnt, options, cb, spawnOptions) {
* @param {Function} [cb]
* @param {Object} [spawnOptions] Options to pass `child_process.spawn`.
*/

install.npmInstall = function (pkgs, options, cb, spawnOptions) {
return this.runInstall('npm', pkgs, options, cb, spawnOptions);
};

/**
* Receives a list of `packages` and an `options` object to install through npm.
*
Expand All @@ -186,7 +187,6 @@ install.npmInstall = function (pkgs, options, cb, spawnOptions) {
* @param {Function} [cb]
* @param {Object} [spawnOptions] Options to pass `child_process.spawn`.
*/

install.yarnInstall = function (pkgs, options, cb, spawnOptions) {
return this.runInstall('yarn', pkgs, options, cb, spawnOptions);
};
4 changes: 2 additions & 2 deletions lib/actions/spawn-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const spawnCommand = module.exports;
* @param {Array} args
* @param {object} [opt]
*/
spawnCommand.spawnCommand = function (command, args, opt) {
spawnCommand.spawnCommand = (command, args, opt) => {
opt = opt || {};
return spawn(command, args, _.defaults(opt, {stdio: 'inherit'}));
};
Expand All @@ -27,7 +27,7 @@ spawnCommand.spawnCommand = function (command, args, opt) {
* @param {Array} args
* @param {object} [opt]
*/
spawnCommand.spawnCommandSync = function (command, args, opt) {
spawnCommand.spawnCommandSync = (command, args, opt) => {
opt = opt || {};
return spawn.sync(command, args, _.defaults(opt, {stdio: 'inherit'}));
};
23 changes: 10 additions & 13 deletions lib/actions/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
const shell = require('shelljs');
const githubUsername = require('github-username');

const nameCache = {};
const emailCache = {};
const nameCache = new Map();
const emailCache = new Map();

/**
* @mixin
Expand All @@ -18,17 +18,16 @@ user.github = {};
* Retrieves user's name from Git in the global scope or the project scope
* (it'll take what Git will use in the current context)
*/

user.git.name = function () {
let name = nameCache[process.cwd()];
user.git.name = () => {
let name = nameCache.get(process.cwd());

if (name) {
return name;
}

if (shell.which('git')) {
name = shell.exec('git config --get user.name', {silent: true}).stdout.trim();
nameCache[process.cwd()] = name;
nameCache.set(process.cwd(), name);
}

return name;
Expand All @@ -38,27 +37,25 @@ user.git.name = function () {
* Retrieves user's email from Git in the global scope or the project scope
* (it'll take what Git will use in the current context)
*/

user.git.email = function () {
let email = emailCache[process.cwd()];
user.git.email = () => {
let email = emailCache.get(process.cwd());

if (email) {
return email;
}

if (shell.which('git')) {
email = shell.exec('git config --get user.email', {silent: true}).stdout.trim();
emailCache[process.cwd()] = email;
emailCache.set(process.cwd(), email);
}

return email;
};

/**
* Retrieves GitHub's username from the GitHub API.
* Retrieves GitHub's username from the GitHub API
*/

user.github.username = function (cb) {
user.github.username = cb => {
const promise = githubUsername(user.git.email());

if (cb) {
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const util = require('util');
const path = require('path');
const events = require('events');
const EventEmitter = require('events');
const assert = require('assert');
const _ = require('lodash');
const findUp = require('find-up');
Expand Down Expand Up @@ -59,7 +59,7 @@ const EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';
*/

const Base = function (args, options) {
events.EventEmitter.call(this);
EventEmitter.call(this);

if (!Array.isArray(args)) {
options = args;
Expand Down Expand Up @@ -142,7 +142,7 @@ const Base = function (args, options) {
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));
};

util.inherits(Base, events.EventEmitter);
util.inherits(Base, EventEmitter);

// Mixin the actions modules
_.extend(Base.prototype, require('./actions/install'));
Expand Down
58 changes: 28 additions & 30 deletions lib/util/binary-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,38 @@ const dateFormat = require('dateformat');
const prettyBytes = require('pretty-bytes');
const Table = require('cli-table');

module.exports = {
isBinary(existingFilePath, newFileContents) {
const existingHeader = readChunk.sync(existingFilePath, 0, 512);
return istextorbinary.isBinarySync(undefined, existingHeader) || istextorbinary.isBinarySync(undefined, newFileContents);
},
exports.isBinary = (existingFilePath, newFileContents) => {
const existingHeader = readChunk.sync(existingFilePath, 0, 512);
return istextorbinary.isBinarySync(undefined, existingHeader) || istextorbinary.isBinarySync(undefined, newFileContents);
};

diff(existingFilePath, newFileContents) {
const existingStat = fs.statSync(existingFilePath);
const table = new Table({
head: ['', 'Existing', 'Replacement', 'Diff']
});
exports.diff = (existingFilePath, newFileContents) => {
const existingStat = fs.statSync(existingFilePath);
const table = new Table({
head: ['', 'Existing', 'Replacement', 'Diff']
});

let sizeDiff;
let sizeDiff;

if (existingStat.size > newFileContents.length) {
sizeDiff = '-';
} else {
sizeDiff = '+';
}
if (existingStat.size > newFileContents.length) {
sizeDiff = '-';
} else {
sizeDiff = '+';
}

sizeDiff += prettyBytes(Math.abs(existingStat.size - newFileContents.length));
sizeDiff += prettyBytes(Math.abs(existingStat.size - newFileContents.length));

table.push([
'Size',
prettyBytes(existingStat.size),
prettyBytes(newFileContents.length),
sizeDiff
], [
'Last modified',
dateFormat(existingStat.mtime),
'',
''
]);
table.push([
'Size',
prettyBytes(existingStat.size),
prettyBytes(newFileContents.length),
sizeDiff
], [
'Last modified',
dateFormat(existingStat.mtime),
'',
''
]);

return table.toString();
}
return table.toString();
};
Loading

0 comments on commit 6553965

Please sign in to comment.