@@ -6,101 +6,113 @@ dotenv.config({
66import { Command } from "commander" ;
77
88import { runBuild } from "./commands/build" ;
9- import { ALL_PACKAGES , CATEGORY_VM , PCKG } from "./common/cli.constants" ;
9+ import { ALL_PACKAGES , CATEGORY_VM } from "./common/cli.constants" ;
1010import { startDeleteFlow } from "./commands/delete" ;
11+ import { Options_Cli , Schema_Options_Cli } from "./common/cli.types" ;
1112import { log } from "./common/cli.utils" ;
12- import { Options_Cli } from "./common/cli.types" ;
1313
14- const createProgram = ( ) => {
15- const program = new Command ( ) ;
16- program . option (
17- `-e, --environment [${ CATEGORY_VM . STAG } | ${ CATEGORY_VM . PROD } ]` ,
18- "specify environment"
19- ) ;
20- program . option ( "-f, --force" , "forces operation, no cautionary prompts" ) ;
21- program . option (
22- "-u, --user [id | email]" ,
23- "specifies which user to run script for"
24- ) ;
14+ class CompassCli {
15+ private program : Command ;
16+ private options : Options_Cli ;
2517
26- program
27- . command ( "build" )
28- . description ( "build compass package(s)" )
29- . argument (
30- `[${ ALL_PACKAGES . join ( " | " ) } ]` ,
31- "package(s) to build, separated by comma"
32- )
33- . option ( "--skip-env" , "skips copying env files to build" ) ;
18+ constructor ( args : string [ ] ) {
19+ this . program = this . createProgram ( ) ;
20+ this . program . parse ( args ) ;
21+ this . options = this . getCliOptions ( ) ;
22+ }
3423
35- program
36- . command ( "delete" )
37- . description ( "delete user data from compass database" ) ;
38- return program ;
39- } ;
24+ private createProgram ( ) : Command {
25+ const program = new Command ( ) ;
26+ program . option (
27+ `-e, --environment [${ CATEGORY_VM . STAG } | ${ CATEGORY_VM . PROD } ]` ,
28+ "specify environment"
29+ ) ;
30+ program . option ( "-f, --force" , "force operation, no cautionary prompts" ) ;
31+ program . option (
32+ "-u, --user [id | email]" ,
33+ "specify which user to run script for"
34+ ) ;
4035
41- const exitHelpfully = ( program : Command , msg ?: string ) => {
42- msg && log . error ( msg ) ;
43- console . log ( program . helpInformation ( ) ) ;
44- process . exit ( 1 ) ;
45- } ;
36+ program
37+ . command ( "build" )
38+ . description ( "build compass package(s)" )
39+ . argument (
40+ `[${ ALL_PACKAGES . join ( " | " ) } ]` ,
41+ "package(s) to build, separated by comma"
42+ )
43+ . option ( "--skip-env" , "skip copying env files to build" ) ;
4644
47- const getCliOptions = ( program : Command ) : Options_Cli => {
48- const _options = program . opts ( ) ;
49- const packages = program . args [ 1 ] ?. split ( "," ) ;
45+ program
46+ . command ( "delete" )
47+ . description ( "delete user data from compass database" ) ;
48+ return program ;
49+ }
5050
51- const options = {
52- ..._options ,
53- packages,
54- force : _options [ "force" ] === true ,
55- user : _options [ "user" ] as string ,
56- } ;
51+ private getCliOptions ( ) : Options_Cli {
52+ const _options = this . program . opts ( ) ;
53+ const packages = this . program . args [ 1 ] ?. split ( "," ) ;
54+ const options : Options_Cli = {
55+ ..._options ,
56+ force : _options [ "force" ] === true ,
57+ packages,
58+ } ;
5759
58- return options ;
59- } ;
60+ const { data, error } = Schema_Options_Cli . safeParse ( options ) ;
61+ if ( error ) {
62+ log . error ( `Invalid CLI options: ${ JSON . stringify ( error . format ( ) ) } ` ) ;
63+ process . exit ( 1 ) ;
64+ }
6065
61- const validatePackages = ( packages : string [ ] | undefined ) => {
62- if ( ! packages ) {
63- log . error ( "Packages must be defined" ) ;
66+ return data ;
6467 }
65- if ( ! packages ?. includes ( PCKG . NODE ) && ! packages ?. includes ( PCKG . WEB ) ) {
66- log . error (
67- `One or more of these pckgs isn't supported: ${ (
68- packages as string [ ]
69- ) ?. toString ( ) } `
70- ) ;
7168
72- process . exit ( 1 ) ;
69+ private validatePackages ( packages : string [ ] | undefined ) {
70+ if ( ! packages ) {
71+ log . error ( "Packages must be defined" ) ;
72+ process . exit ( 1 ) ;
73+ }
74+ const unsupportedPackages = packages . filter (
75+ ( pkg ) => ! ALL_PACKAGES . includes ( pkg )
76+ ) ;
77+ if ( unsupportedPackages . length > 0 ) {
78+ log . error (
79+ `One or more of these packages isn't supported: ${ unsupportedPackages . toString ( ) } `
80+ ) ;
81+ process . exit ( 1 ) ;
82+ }
7383 }
74- } ;
75-
76- const runScript = async ( ) => {
77- const program = createProgram ( ) ;
78- program . parse ( process . argv ) ;
7984
80- const options = getCliOptions ( program ) ;
81- const { user, force } = options ;
85+ public async run ( ) {
86+ const { user, force, packages } = this . options ;
87+ const cmd = this . program . args [ 0 ] ;
8288
83- const cmd = program . args [ 0 ] ;
84- switch ( true ) {
85- case cmd === "build" : {
86- validatePackages ( options . packages ) ;
87- await runBuild ( options ) ;
88- break ;
89- }
90- case cmd === "delete" : {
91- if ( ! user || typeof user !== "string" ) {
92- exitHelpfully ( program , "You must supply a user" ) ;
89+ switch ( true ) {
90+ case cmd === "build" : {
91+ this . validatePackages ( packages ) ;
92+ await runBuild ( this . options ) ;
93+ break ;
9394 }
94-
95- await startDeleteFlow ( user as string , force ) ;
96- break ;
95+ case cmd === "delete" : {
96+ if ( ! user || typeof user !== "string" ) {
97+ this . exitHelpfully ( "You must supply a user" ) ;
98+ }
99+ await startDeleteFlow ( user as string , force ) ;
100+ break ;
101+ }
102+ default :
103+ this . exitHelpfully ( "Unsupported cmd" ) ;
97104 }
98- default :
99- exitHelpfully ( program , "Unsupported cmd" ) ;
100105 }
101- } ;
102106
103- runScript ( ) . catch ( ( err ) => {
107+ private exitHelpfully ( msg ?: string ) {
108+ msg && log . error ( msg ) ;
109+ console . log ( this . program . helpInformation ( ) ) ;
110+ process . exit ( 1 ) ;
111+ }
112+ }
113+
114+ const cli = new CompassCli ( process . argv ) ;
115+ cli . run ( ) . catch ( ( err ) => {
104116 console . log ( err ) ;
105117 process . exit ( 1 ) ;
106118} ) ;
0 commit comments