Last active
October 24, 2022 12:57
-
-
Save Ivannnnn/0ec934cb723156967f7f18d86fd26dca 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
function commandarize(methods) { | |
const args = process.argv.slice(2).map((arg) => { | |
const isJson = /^\[.+\]$|^\{.+\}$/.test(arg); | |
return isJson ? JSON.parse(arg) : arg; | |
}); | |
const [methodName, ...params] = args; | |
if (methodName) { | |
const method = | |
methods[methodName] || methods[methodName.replace(/^-*/, "")]; | |
if (!method) { | |
return console.log( | |
[ | |
"", | |
`Method "${methodName}" does not exists! Existing methods are:`, | |
``, | |
`${Object.keys(methods).join("\n")}`, | |
].join("\n") | |
); | |
} | |
console.log(method(...params)); | |
} else { | |
methods.default?.(...params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment