33 lines
1002 B
JavaScript
33 lines
1002 B
JavaScript
let params = process.argv.slice(2)
|
|
|
|
const moduleStr = params.shift()
|
|
const [ esmodule, exported ] = (moduleStr || '').split(':')
|
|
|
|
const optionReg = /^-{1,2}(.*?)([:=](.*?))?$/
|
|
const args = params.filter( s=> optionReg.test(s) )
|
|
.reduce( (opt,str)=> {
|
|
let [ , prop, , value ] = optionReg.exec(str)
|
|
return Object.assign( opt, {[prop]: value || true} )
|
|
// return hasValue ? { ...opt, [prop]: value || true }
|
|
// : opt
|
|
},
|
|
params.filter( s=> !optionReg.test(s) )
|
|
)
|
|
//console.log( process.argv, esmodule, exported, args )
|
|
|
|
let mod =
|
|
require('esm')( module, {await: true} )
|
|
( `./${esmodule || require('./package.json').module}` )
|
|
|
|
let toCall = mod[ exported || 'default' ]
|
|
let returned =
|
|
typeof toCall == 'function'
|
|
? toCall( args )
|
|
: toCall
|
|
//console.log( 'type' , typeof returned, returned instanceof Promise )
|
|
returned instanceof Promise
|
|
? returned.then( ret=> console.log(ret||''), e=>console.error(e) )
|
|
: console.log( returned || '' )
|
|
|
|
module.exports = mod
|