Last active
November 4, 2020 10:27
-
-
Save ricardobeat/bd41bcdc385b20d09cafa935b9c49359 to your computer and use it in GitHub Desktop.
minimal task runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const _tasks = {} | |
function task (name, fn) { | |
_tasks[name] = fn | |
} | |
task.run = async function (name, wait) { | |
if (!name) return console.warn(`Usage: node task [name]`) | |
if (!_tasks[name]) return console.warn(`No task named ${name}`) | |
console.info(`> Running task: ${name}`) | |
return wait ? await _tasks[name].call() : _tasks[name].call() | |
} | |
task.parallel = function (tasks) { | |
for (const t of tasks) task.run(t) | |
} | |
task.series = async function (tasks) { | |
for (const t of tasks) await task.run(t) | |
} | |
const say = (str, time) => new Promise(resolve => setTimeout(_ => resolve(console.log(str)), time)) | |
task('scripts', async function () { | |
await say('A', 200) | |
}) | |
task('styles', async function () { | |
await say('B', 100) | |
}) | |
task('xxx', async function () { | |
await say('C', 300) | |
}) | |
task('yyy', async function () { | |
await say('D', 100) | |
}) | |
task('build', async function () { | |
await task.run('scripts') // A | |
await task.run('styles') // B | |
await task.parallel(['scripts', 'styles']) // B, A | |
await task.series(['xxx', 'yyy']) // C, D | |
}) | |
task.run(process.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment