Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Last active October 24, 2022 12:57
Show Gist options
  • Save Ivannnnn/0ec934cb723156967f7f18d86fd26dca to your computer and use it in GitHub Desktop.
Save Ivannnnn/0ec934cb723156967f7f18d86fd26dca to your computer and use it in GitHub Desktop.
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