Skip to content

Commit

Permalink
null values and es6 cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
openhoat committed Jul 27, 2016
1 parent 11814b7 commit e89e03f
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 143 deletions.
39 changes: 38 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ env:
es6: true
node: true
mocha: true
extends: 'eslint:recommended'
extends: ['eslint:recommended', 'nodejs']
plugins:
- 'you-dont-need-lodash-underscore'
rules:
indent:
- error
Expand All @@ -28,3 +30,38 @@ rules:
arrow-parens: ["warn", "as-needed"]
arrow-body-style: ["warn", "as-needed"]
prefer-const: warn
'you-dont-need-lodash-underscore/concat': warn
'you-dont-need-lodash-underscore/fill': warn
'you-dont-need-lodash-underscore/find': warn
'you-dont-need-lodash-underscore/detect': warn
'you-dont-need-lodash-underscore/find-index': warn
'you-dont-need-lodash-underscore/index-of': warn
'you-dont-need-lodash-underscore/join': warn
'you-dont-need-lodash-underscore/last-index-of': warn
'you-dont-need-lodash-underscore/reverse': warn
'you-dont-need-lodash-underscore/each': warn
'you-dont-need-lodash-underscore/for-each': warn
'you-dont-need-lodash-underscore/every': warn
'you-dont-need-lodash-underscore/all': warn
'you-dont-need-lodash-underscore/filter': warn
'you-dont-need-lodash-underscore/select': warn
'you-dont-need-lodash-underscore/includes': warn
'you-dont-need-lodash-underscore/contains': warn
'you-dont-need-lodash-underscore/map': warn
'you-dont-need-lodash-underscore/collect': warn
'you-dont-need-lodash-underscore/reduce': warn
'you-dont-need-lodash-underscore/inject': warn
'you-dont-need-lodash-underscore/foldl': warn
'you-dont-need-lodash-underscore/reduce-right': warn
'you-dont-need-lodash-underscore/foldr': warn
'you-dont-need-lodash-underscore/size': warn
'you-dont-need-lodash-underscore/some': warn
'you-dont-need-lodash-underscore/any': warn
'you-dont-need-lodash-underscore/is-na-n': warn
'you-dont-need-lodash-underscore/extend-own': warn
'you-dont-need-lodash-underscore/assign': warn
'you-dont-need-lodash-underscore/keys': warn
'you-dont-need-lodash-underscore/repeat': warn
'you-dont-need-lodash-underscore/to-lower': warn
'you-dont-need-lodash-underscore/to-upper': warn
'you-dont-need-lodash-underscore/trim': warn
210 changes: 111 additions & 99 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
const _ = require('lodash')
, fs = require('fs')
, path = require('path')
, p = require('bluebird')
, chalk = require('chalk')
, coveralls = require('gulp-coveralls')
, debug = require('gulp-debug')
, del = require('del')
, gulp = require('gulp')
, gulpif = require('gulp-if')
, gutil = require('gulp-util')
, istanbul = require('gulp-istanbul')
, eslint = require('gulp-eslint')
, licenseFinder = require('gulp-license-finder')
, minimist = require('minimist')
, mkdirp = require('mkdirp')
, mocha = require('gulp-mocha')
, notify = require('gulp-notify')
, f = require('util').format.bind(null)
, pkg = require('./package')
, jenkins = !!process.env['JENKINS_URL'];
'use strict';

const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const Promise = require('bluebird');
const chalk = require('chalk');
const coveralls = require('gulp-coveralls');
const debug = require('gulp-debug');
const del = require('del');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const istanbul = require('gulp-istanbul');
const eslint = require('gulp-eslint');
const licenseFinder = require('gulp-license-finder');
const minimist = require('minimist');
const mkdirp = require('mkdirp');
const mocha = require('gulp-mocha');
const notify = require('gulp-notify');
const f = (...args) => require('util').format(...args);
const pkg = require('./package');
const jenkins = !!process.env['JENKINS_URL'];

let taskSpecs;

p.promisifyAll(fs);
Promise.promisifyAll(fs);
process.env['NODE_ENV'] = 'test';

const toRelativePath = (file, baseDir) => {
Expand All @@ -38,7 +40,8 @@ const toArray = (s, sep, format) => s.split(sep || ',').map(item => f(format ||
const rm = src => del([src], {dryRun: cmdOpt['dry-run']})
.then(files => {
if (cmdOpt.verbose) {
gutil.log(files && files.length ? $.yellow(f('Files and folders deleted :', toRelativePath(files).join(', '))) : $.yellow(f('Nothing deleted')));
gutil.log(files && files.length ? $.yellow(f('Files and folders deleted :', toRelativePath(files)
.join(', '))) : $.yellow(f('Nothing deleted')));
}
});

Expand Down Expand Up @@ -67,6 +70,9 @@ if (cmdOpt['log-level']) {
const $ = new chalk.constructor({enabled: cmdOpt.color});
process.env['HW_LOG_COLORS'] = cmdOpt.color;

/**
* Configuration
*/
const config = {
distDir: 'dist',
reportDir: 'dist/reports',
Expand Down Expand Up @@ -167,7 +173,7 @@ taskSpecs = {
log($.bold('Tasks'));
const tasks = [];
_.forIn(taskSpecs, (taskSpec, taskSpecName) => {
const task = _.omit(taskSpec, 'task');
const task = _.pick(taskSpec, ['name', 'desc', 'deps', 'config', 'providesFn']);
l = Math.max(taskSpecName.length, l);
task.name = taskSpecName;
task.providesFn = typeof taskSpec.task === 'function';
Expand All @@ -176,7 +182,7 @@ taskSpecs = {
tasks.forEach(task => {
log(false, ' %s : %s', $.cyan(_.padEnd(task.name, l)), task.desc);
if (task.deps) {
log(false, ' %s', $.yellow(f('[%s]', task.deps.join(', '))));
log(false, ' %s', $.yellow(f('[%s]', (Array.isArray(task.deps) ? task.deps : [task.deps]).join(', '))));
}
log(false, ' ');
if (task.config) {
Expand All @@ -200,7 +206,7 @@ taskSpecs = {
config: {
src: [config.distDir, config.reportDir, config.lintReportDir, config.testReportDir]
},
task: t => p.each(t.config.src, dir => p.fromNode(mkdirp.bind(mkdirp, dir))
task: t => Promise.each(t.config.src, dir => Promise.fromNode(mkdirp.bind(mkdirp, dir))
.then(dir => {
if (cmdOpt.verbose) {
gutil.log(dir ? $.yellow(f('Directory created :', toRelativePath(dir))) : $.yellow(f('Nothing created')));
Expand Down Expand Up @@ -270,18 +276,18 @@ taskSpecs = {
desc: 'Find licenses in node project and dependencies',
task: () => {
const dest = path.join(config.distDir, 'licenses.csv');
return licenseFinder(path.basename(dest),
{
csv: true,
depth: 1
})
.once('finish', function () {
if (cmdOpt.verbose) {
gutil.log($.yellow(f('Created license report : %s', dest)));
}
this.emit('end');
})
.pipe(gulp.dest(path.dirname(dest)));
const lf = licenseFinder(path.basename(dest), {
csv: true,
depth: 1
});
lf.once('finish', () => {
if (cmdOpt.verbose) {
gutil.log($.yellow(f('Created license report : %s', dest)));
}
lf.emit('end');
});
lf.pipe(gulp.dest(path.dirname(dest)));
return lf;
}
},
sources: {
Expand All @@ -299,72 +305,78 @@ taskSpecs = {
}
};

const initTasks = () => {
const taskSpecTransformer = baseNs => (result, taskSpec, taskSpecName) => {
const isTaskGroup = () => !Object.keys(_.pick(taskSpec, ['deps', 'task', 'desc'])).length;
const dryRun = () => {
if (cmdOpt['dry-run']) {
if (_.get(item, 'config.src')) {
return gulp.src(item.config.src)
.pipe(debug({title: ns}));
}
return true;
const taskSpecTransformer = baseNs => (result, taskSpec, taskSpecName) => {
const isTaskGroup = () => !Object.keys(_.pick(taskSpec, ['deps', 'task', 'desc'])).length;
const dryRun = () => {
if (cmdOpt['dry-run']) {
if (_.get(item, 'config.src')) {
return gulp.src(item.config.src)
.pipe(debug({title: ns}));
}
};
const ns = baseNs ? (taskSpecName === (_.get(config, 'taskSpecs.defaultGroupTask') || 'default') ? baseNs : path.join(baseNs, taskSpecName)) : taskSpecName;
if (isTaskGroup()) {
_.transform(taskSpec, taskSpecTransformer(ns), result);
return;
return true;
}
const item = result[ns] = _.omit(taskSpec, ['desc', 'deps', 'task', 'config']);
if (taskSpec.desc) {
item.desc = typeof taskSpec.desc === 'function' ? taskSpec.desc(taskSpecName, taskSpec) : taskSpec.desc;
}
if (taskSpec.deps) {
item.deps = [];
(Array.isArray(taskSpec.deps) ? taskSpec.deps : [taskSpec.deps]).forEach(dep => {
if (dep.indexOf('/') === 0) {
item.deps.push(dep.substring(1));
} else {
item.deps.push(baseNs ? path.join(baseNs, dep) : dep);
};
const ns = baseNs ?
(
taskSpecName === (_.get(config, 'taskSpecs.defaultGroupTask') || 'default') ?
baseNs :
path.join(baseNs, taskSpecName)
) :
taskSpecName;
const group = isTaskGroup();
if (group) {
_.transform(taskSpec, taskSpecTransformer(ns), result);
return;
}
const item = result[ns] = _.omit(taskSpec, ['desc', 'deps', 'task', 'config']);
if (taskSpec.desc) {
item.desc = typeof taskSpec.desc === 'function' ? taskSpec.desc(taskSpecName, taskSpec) : taskSpec.desc;
}
if (taskSpec.deps) {
item.deps = [];
(Array.isArray(taskSpec.deps) ? taskSpec.deps : [taskSpec.deps]).forEach(dep => {
if (dep.indexOf('/') === 0) {
item.deps.push(dep.substring(1));
} else {
item.deps.push(baseNs ? path.join(baseNs, dep) : dep);
}
});
}
if (typeof taskSpec.task === 'function') {
item.task = cb => {
if (dryRun(item.config)) {
if (!_.get(item, 'config.continueOnDryRun')) {
return cb();
}
}
return taskSpec.task(_.omit(item, 'task'), (err, data) => {
if (cmdOpt.verbose && data) {
gutil.log($.yellow('Task result :', data));
}
cb(err);
});
};
}
if (taskSpec.config) {
item.config = taskSpec.config;
}
};

taskSpecs = _.transform(taskSpecs, taskSpecTransformer(), {});

const registerTasks = taskSpecs => {
_.forIn(taskSpecs, (taskSpec, taskSpecName) => {
const args = [taskSpecName];
if (!taskSpec.desc && taskSpec.deps && taskSpec.deps.length === 1) {
taskSpec.desc = taskSpecs[_.first(taskSpec.deps)].desc;
}
if (typeof taskSpec.task === 'function') {
item.task = cb => {
if (dryRun(item.config)) {
if (!_.get(item, 'config.continueOnDryRun')) {
return cb();
}
}
return taskSpec.task.call(this, _.omit(item, 'task'), (err, data) => {
if (cmdOpt.verbose && data) {
gutil.log($.yellow('Task result :', data));
}
cb(err);
});
};
if (taskSpec.deps) {
args.push(taskSpec.deps);
}
if (taskSpec.config) {
item.config = taskSpec.config;
if (taskSpec.task) {
args.push(taskSpec.task);
}
};
const registerTasks = () => {
_.forIn(taskSpecs, (taskSpec, taskSpecName) => {
const args = [taskSpecName];
if (!taskSpec.desc && taskSpec.deps && taskSpec.deps.length === 1) {
taskSpec.desc = taskSpecs[_.first(taskSpec.deps)].desc;
}
if (taskSpec.deps) {
args.push(taskSpec.deps);
}
if (taskSpec.task) {
args.push(taskSpec.task);
}
gulp.task(...args);
});
};
taskSpecs = _.transform(taskSpecs, taskSpecTransformer(), {});
registerTasks();
gulp.task(...args);
});
};
initTasks();
registerTasks(taskSpecs);
20 changes: 16 additions & 4 deletions lib/entity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const Promise = require('bluebird');
const util = require('util');
const _ = require('lodash');
Expand Down Expand Up @@ -48,7 +50,11 @@ class Entity {
const type = clazz.type;
return Promise.resolve()
.then(() => {
logger.enabledLevels.debug && log.debug('searching %s entity ID with index "%s" matching "%s"', type, name, value);
logger.enabledLevels.debug && log.debug('searching %s entity ID with index "%s" matching "%s"',
type,
name,
value
);
const index = clazz.indexes && clazz.indexes[name];
if (!index) {
logger.enabledLevels.debug && log.debug('no index "%s" for entity "%s"', name, type);
Expand All @@ -67,7 +73,11 @@ class Entity {
if (result) {
return result;
}
logger.enabledLevels.debug && log.debug('searching %s entity ID with link "%s" matching "%s"', type, name, value);
logger.enabledLevels.debug && log.debug('searching %s entity ID with link "%s" matching "%s"',
type,
name,
value
);
const link = clazz.links && clazz.links[name];
if (!link) {
logger.enabledLevels.debug && log.debug('no link "%s" for entity "%s"', name, type);
Expand Down Expand Up @@ -170,7 +180,9 @@ class Entity {
return ohm.exec('exists', k).then(result => {
let nameValue;
if (result) {
nameValue = name.indexOf(',') !== -1 ? name.split(',').map(item => entity.value[item]).join(',') : entity.value[name];
nameValue = name.indexOf(',') !== -1 ?
name.split(',').map(item => entity.value[item]).join(',') :
entity.value[name];
ohm.e.throwEntityConflict({type, attrName: name, attrValue: nameValue});
}
});
Expand Down Expand Up @@ -616,4 +628,4 @@ class Entity {
}
}

exports = module.exports = Entity;
exports = module.exports = Entity;

0 comments on commit e89e03f

Please sign in to comment.