Last active
June 25, 2020 18:44
-
-
Save dominykas/396b3279eceb3ad5ffeddacf30fd35e7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 args = process.argv.slice(2); | |
const [modfn, ...params] = args; | |
const [mod, fn] = modfn.split('.'); | |
const Mod = require(mod); | |
const options = {}; | |
for (let i = 0; i < params.length; ++i) { | |
let param = params[i]; | |
let key, value; | |
if (param.startsWith('--')) { | |
param = param.slice(2); | |
key = param; | |
value = true; | |
} | |
if (param.includes(':')) { | |
const [k, v] = param.split(':'); | |
key = k; | |
value = v; | |
} | |
if (param.includes('=')) { | |
const [k, v] = param.split('='); | |
key = k; | |
value = v; | |
} | |
if (key) { | |
if (value === 'true') { | |
value = true; | |
} | |
options[key] = value; | |
delete params[i]; | |
} | |
} | |
console.log({ mod, fn, params, options }); | |
Mod[fn](...params.filter(p => p), options, (err, result) => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
console.log(result); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment