Last active
April 17, 2020 22:37
-
-
Save y0n1/9639eb5f92e5ca3f5cd05b2ce97ad70a to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
} |
This file contains 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
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