Skip to content

Instantly share code, notes, and snippets.

@VonHeikemen
Created May 19, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VonHeikemen/638740d6fdd2a6407f0038fae381fedf to your computer and use it in GitHub Desktop.
Save VonHeikemen/638740d6fdd2a6407f0038fae381fedf to your computer and use it in GitHub Desktop.
minimal, no dependencies task runner for deno.
// Command line usage examples:
// 1: deno run --allow-run ./Taskfile.js start
// 2: deno run --allow-run ./Taskfile.js echo "what?"
// 3: deno run --allow-run ./Taskfile.js do-stuff one two three
run(Deno.args, {
start() {
exec.cmd('deno run ./src/index.js');
},
echo(str) {
return exec(['echo', str ,"\nThis is javascript, y'all"]);
},
async ['do-stuff'](...args) {
if(true) {
await this.echo(`Testing ${args.join(' ')}`);
}
}
});
function run([name, ...args], tasks) {
name in tasks
? tasks[name](...args)
: console.log(`Task "${name}" not found`);
}
async function exec(cmd) {
const proc = await Deno.run({ cmd }).status();
if(proc.success == false) {
Deno.exit(proc.code);
}
return proc;
}
exec.cmd = function(raw_cmd) {
return exec(split(raw_cmd));
};
// taken from: https://github.com/gpasq/deno-exec
function split(command) {
var myRegexp = /[^\s"]+|"([^"]*)"/gi;
var splits = [];
do {
//Each call to exec returns the next regex match as an array
var match = myRegexp.exec(command);
if (match != null) {
//Index 1 in the array is the captured group if it exists
//Index 0 is the matched text, which we use if no captured group exists
splits.push(match[1] ? match[1] : match[0]);
}
} while (match != null);
return splits;
}
@VonHeikemen
Copy link
Author

VonHeikemen commented May 19, 2020

This is not meant to be a module you download, but something you would write in the root of your project to execute external commands or automate some simple tasks.

These are proper task runner for deno:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment