Skip to content

Instantly share code, notes, and snippets.

@y0n1
Last active April 17, 2020 22:37
Show Gist options
  • Save y0n1/9639eb5f92e5ca3f5cd05b2ce97ad70a to your computer and use it in GitHub Desktop.
Save y0n1/9639eb5f92e5ca3f5cd05b2ce97ad70a to your computer and use it in GitHub Desktop.
interface MainFuncAsync {
(args: string[]): Promise<number|void>
}
class ProgramEntryPoint {
static main: MainFuncAsync;
}
interface IProgramEntryPoint {
new(): ProgramEntryPoint
main: MainFuncAsync;
}
export function Main<T extends IProgramEntryPoint>(ctor: T) {
const errorHandler = (error: Error & { exitCode?: number }) => {
console.error(error);
process.exit(error.exitCode);
}
const exitProcessHandler = (exitCode: number | void) => {
process.exit(exitCode || 0);
}
ctor.main(process.argv)
.then(exitProcessHandler)
.catch(errorHandler)
}
import { Main } from "./Decorators"
@Main
export default class Program {
static async main(args: string[]): Promise<number> {
const [arg1, arg2, fail] = args;
return fail ? 1 : 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment