Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- "9"
- "8"
- "6"
21 changes: 17 additions & 4 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ const argv = yargs
.env(true)
.argv;

module.exports.argv = argv;
// if run from command line:
if (!module.parent) {
main({}, argv);
if (argv.config) {
process.env.TASKKIT_CONFIG = argv.config;
}

if (argv.env) {
process.env.NODE_ENV = argv.config;
}

let task = '';
const cmd = argv._;
if (cmd.length === 0) {
task = 'default';
} else if (cmd.length === 1) {
task = argv._[0];
} else {
task = cmd;
}
main(task);
3 changes: 3 additions & 0 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion example/taskkit.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
crashOnError: true
tasks:
ls: 'taskkit-shell'
watch: 'taskkit-watcher'
disabled: 'taskkit-shell2'
default:
- 'ls'
- 'disabled'
- 'help'
- 'config'
- 'watch'
#- 'disabled'
fail: 'taskkit-shell'
ls:
command: 'ls'
bad:
command: 'asdasdasd'
disabled:
enabled: false
watch:
debug: true
ignore:
- '.git'
- 'node_modules'
files:
'**/*': 'ls'

98 changes: 12 additions & 86 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,18 @@
#! /usr/bin/env node
'use strict';
const path = require('path');
const Logr = require('logr');
const loadTasks = require('./lib/load-tasks');
const async = require('async');
const LoadConfig = require('./lib/load-config');
const fs = require('fs');
const log = Logr.createLogger({
reporters: {
cliFancy: {
reporter: require('logr-cli-fancy')
},
bell: {
reporter: require('logr-reporter-bell')
}
}
});

const main = (options, argv) => {
// if caller didn't pass their own argv, get it for them:
if (!argv) {
argv = require('./bin.js').argv;
}
options = options || {};
const configPaths = options.configPaths || [];
const context = options.context || {};
const name = options.name || 'taskkit';
const configPath = argv.config || path.join(process.cwd(), name);
const env = argv.env;
const getTasks = require('./lib/getTasks');
const getConfig = require('./lib/getConfig');
const main = async (task = 'default') => {
const config = await getConfig();

configPaths.push(configPath);
configPaths.push({
env: argv.env,
path: process.cwd(),
prefix: name
});
context.CONFIGDIR = configPath;
const runner = getTasks(config);

const start = new Date().getTime();
async.autoInject({
version(done) {
if (options.version) {
return done(null, options.version);
}
fs.exists('./package.json', (exists) => {
if (!exists) {
return done(null, 'unknown');
}
return done(null, require('./package.json').version);
});
},
loadConfig(version, done) {
log([name], `Using config directory: ${configPath}, environment is "${env}", version is ${version}`);
const config = new LoadConfig(name, env, configPaths, context);
done(null, config);
},
config(loadConfig, done) {
loadConfig.get(done);
},
task(done) {
let task = '';
const cmd = argv._;
if (cmd.length === 0) {
task = 'default';
} else if (cmd.length === 1) {
task = argv._[0];
} else {
task = cmd;
}
done(null, task);
},
runner(config, loadConfig, done) {
loadTasks(config, log, loadConfig, done);
},
runTask(runner, task, done) {
log([name], `Running ${task}...`);
runner.run(task, done);
}
}, (err, results) => {
if (err) {
if (results.config && results.config.crashOnError === true) {
throw err;
}
try {
await runner.run(task);
} catch (err) {
// if crashOnError then throw the error up the chain:
if (config && config.crashOnError === true) {
throw err;
}
const end = new Date().getTime();
const duration = (end - start) / 1000;
log([name], `Finished all ${duration} seconds`);
});
}
};

module.exports = main;
29 changes: 29 additions & 0 deletions lib/getConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const confi = require('confi');
const path = require('path');

module.exports = function() {
const prefix = process.env.TASKKIT_PREFIX || 'taskkit';
const configPaths = [];

const env = process.env.NODE_ENV || 'dev';
const primaryConfig = process.env.TASKKIT_CONFIG || path.join(process.cwd(), prefix);

if (process.env.TASKKIT_BASECONFIG) {
configPaths.push(process.env.TASKKIT_BASECONFIG);
}

configPaths.push(primaryConfig);
configPaths.push({
env,
path: process.cwd(),
prefix
});

return confi({
path: configPaths,
context: {
CONFIGDIR: primaryConfig
}
});
};
56 changes: 56 additions & 0 deletions lib/getTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const path = require('path');
const runshell = require('runshell');
const Logr = require('logr');
const log = Logr.createLogger({
reporters: {
cliFancy: {
reporter: require('logr-cli-fancy')
},
bell: {
reporter: require('logr-reporter-bell')
}
}
});

module.exports = function(name, task, runner) {
return async function() {
log([name], `Running ${name}...`);
const start = new Date().getTime();
try {
const { results } = await runshell(`${path.join(__dirname)}/runner.js`, {
env: {
TASKKIT_NAME: name,
TASKKIT_TASK: task,
FORCE_COLOR: 1
},
onMessage(msg) {
if (msg.type === 'run') {
runner.run(msg.task, (e) => {
log([msg.task], 'error', e);
});
}
},
logger(tags, msg) {
if (typeof tags === 'string') {
msg = tags;
tags = [];
}
msg = msg.replace(/\n$/, '');
//TODO: remove when all tasks are on latest taskkit-task
if (msg.indexOf('::') !== -1) {
console.log(msg); //eslint-disable-line no-console
} else {
log([name, ...tags], msg);
}
}
});
const end = new Date().getTime();
const duration = (end - start) / 1000;
log([name], `Finished in ${duration} seconds`);
return results;
} catch (err) {
log([name, 'error'], err);
throw err;
}
};
};
20 changes: 20 additions & 0 deletions lib/getTasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const RunTask = require('runtask');
const getTask = require('./getTask');

module.exports = function(config) {
const runner = new RunTask();
config.tasks.help = `${__dirname}/../tasks/help.js`;
config.tasks.config = `${__dirname}/../tasks/config.js`;
Object.keys(config.tasks).forEach(name => {
const task = config.tasks[name];
let taskFn;
if (Array.isArray(task)) { //task maps to other tasks
taskFn = task;
} else { // task is wrapped by a function that will lazy-load/cache it:
taskFn = getTask(name, task, runner);
}
runner.register(name, taskFn);
});
return runner;
};
51 changes: 0 additions & 51 deletions lib/load-config.js

This file was deleted.

Loading