1+ import type { Base } from "./core/base.js" ;
2+ import type { Context , OverrideConfig } from "./types/index.js" ;
13import process from "node:process" ;
24import { Command } from "commander" ;
35import pkg from "../package.json" with { type : "json" } ;
@@ -26,11 +28,11 @@ async function main() {
2628 . description ( "Build the plugin" )
2729 . option ( "--dev" , "Builds the plugin in dev mode" )
2830 . option ( "--dist <dir>" , "The relative path for the new output directory (default: build)" )
29- . action ( ( options ) => {
31+ . action ( async ( options ) => {
3032 process . env . NODE_ENV = options . dev ? "development" : "production" ;
31- Config . loadConfig ( {
33+ await runCommand ( Build , {
3234 dist : options . dist ,
33- } ) . then ( ctx => new Build ( ctx ) . run ( ) ) ;
35+ } ) ;
3436 } ) ;
3537
3638 cli
@@ -45,32 +47,29 @@ async function main() {
4547 // "--only-start",
4648 // "skip building website before deploy it (default: false)",
4749 // )
48- . action ( ( _options ) => {
49- Config . loadConfig ( { } ) . then ( ctx => new Serve ( ctx ) . run ( ) ) ;
50+ . action ( async ( _options ) => {
51+ await runCommand ( Serve , { } ) ;
5052 } ) ;
5153
5254 cli . command ( "test" )
5355 . description ( "Run tests" )
5456 . option ( "--abort-on-fail" , "Abort the test suite on first failure" )
5557 . option ( "--exit-on-finish" , "Exit the test suite after all tests have run" )
5658 . option ( "--no-watch" , "Exit the test suite after all tests have run" )
57- . action ( ( options ) => {
59+ . action ( async ( options ) => {
5860 process . env . NODE_ENV = "test" ;
59-
60- Config . loadConfig ( {
61+ await runCommand ( Test , {
6162 test : {
6263 abortOnFail : options . abortOnFail ,
6364 watch : ! options . exitOnFinish && options . watch ,
6465 } ,
65- } ) . then ( ( ctx ) => {
66- new Test ( ctx ) . run ( ) ;
6766 } ) ;
6867 } ) ;
6968
7069 cli
7170 . command ( "create" )
7271 . description ( "Create the plugin template" )
73- . action ( ( _options : any ) => {
72+ . action ( async ( _options : any ) => {
7473 logger . error ( "The create not yet implemented" ) ;
7574 // new Create().run();
7675 } ) ;
@@ -83,15 +82,15 @@ async function main() {
8382 . option ( "-y, --yes" , "Skip confirmation" )
8483 . action ( async ( version , options ) => {
8584 process . env . NODE_ENV = "production" ;
86- Config . loadConfig ( {
85+ await runCommand ( Release , {
8786 release : {
8887 bumpp : {
8988 release : version ,
9089 preid : options . preid ,
9190 confirm : ! options . yes ,
9291 } ,
9392 } ,
94- } ) . then ( ctx => new Release ( ctx ) . run ( ) ) ;
93+ } ) ;
9594 } ) ;
9695
9796 cli . arguments ( "<command>" ) . action ( ( cmd ) => {
@@ -100,7 +99,18 @@ async function main() {
10099 } ) ;
101100
102101 cli . parse ( ) ;
103- // globalOpts = cli.optsWithGlobals();
102+ }
103+
104+ type Constructor < T > = new ( ctx : Context ) => T ;
105+
106+ export async function runCommand < T extends Base > (
107+ CommandClass : Constructor < T > ,
108+ config : OverrideConfig ,
109+ ) {
110+ const ctx = await Config . loadConfig ( config ) ;
111+ const instance = new CommandClass ( ctx ) ;
112+ process . on ( "SIGINT" , instance . exit . bind ( instance ) ) ;
113+ await instance . run ( ) ;
104114}
105115
106116export default async function mainWithErrorHandler ( ) {
0 commit comments