11import fs from 'node:fs' ;
22import process from 'node:process' ;
3+ import { parseArgs } from 'node:util' ;
34import colors from 'kleur' ;
4- import sade from 'sade' ;
55import { load_config } from './core/config/index.js' ;
66import { coalesce_to_error } from './utils/error.js' ;
77
@@ -20,28 +20,76 @@ function handle_error(e) {
2020}
2121
2222const pkg = JSON . parse ( fs . readFileSync ( new URL ( '../package.json' , import . meta. url ) , 'utf-8' ) ) ;
23- const prog = sade ( 'svelte-kit' ) . version ( pkg . version ) ;
24-
25- prog
26- . command ( 'sync' )
27- . describe ( 'Synchronise generated type definitions' )
28- . option ( '--mode' , 'Specify a mode for loading environment variables' , 'development' )
29- . action ( async ( { mode } ) => {
30- const config_files = [ 'js' , 'ts' ]
31- . map ( ( ext ) => `svelte.config.${ ext } ` )
32- . filter ( ( f ) => fs . existsSync ( f ) ) ;
33- if ( config_files . length === 0 ) {
34- console . warn ( `Missing Svelte config file in ${ process . cwd ( ) } — skipping` ) ;
35- return ;
36- }
37-
38- try {
39- const config = await load_config ( ) ;
40- const sync = await import ( './core/sync/sync.js' ) ;
41- sync . all_types ( config , mode ) ;
42- } catch ( error ) {
43- handle_error ( error ) ;
44- }
23+
24+ const help = `
25+ Usage: svelte-kit <command> [options]
26+
27+ Commands:
28+ sync Synchronise generated type definitions
29+
30+ Options:
31+ --version, -v Show version number
32+ --help, -h Show this help message
33+
34+ Sync Options:
35+ --mode <mode> Specify a mode for loading environment variables (default: development)
36+ ` ;
37+
38+ let parsed ;
39+ try {
40+ parsed = parseArgs ( {
41+ options : {
42+ version : { type : 'boolean' , short : 'v' } ,
43+ help : { type : 'boolean' , short : 'h' } ,
44+ mode : { type : 'string' , default : 'development' }
45+ } ,
46+ allowPositionals : true ,
47+ strict : true
4548 } ) ;
49+ } catch ( err ) {
50+ const error = /** @type {Error } */ ( err ) ;
51+ console . error ( colors . bold ( ) . red ( `> ${ error . message } ` ) ) ;
52+ console . log ( help ) ;
53+ process . exit ( 1 ) ;
54+ }
55+
56+ const { values, positionals } = parsed ;
57+
58+ if ( values . version ) {
59+ console . log ( pkg . version ) ;
60+ process . exit ( 0 ) ;
61+ }
4662
47- prog . parse ( process . argv , { unknown : ( arg ) => `Unknown option: ${ arg } ` } ) ;
63+ if ( values . help ) {
64+ console . log ( help ) ;
65+ process . exit ( 0 ) ;
66+ }
67+
68+ const command = positionals [ 0 ] ;
69+
70+ if ( ! command ) {
71+ console . log ( help ) ;
72+ process . exit ( 0 ) ;
73+ }
74+
75+ if ( command === 'sync' ) {
76+ const config_files = [ 'js' , 'ts' ]
77+ . map ( ( ext ) => `svelte.config.${ ext } ` )
78+ . filter ( ( f ) => fs . existsSync ( f ) ) ;
79+ if ( config_files . length === 0 ) {
80+ console . warn ( `Missing Svelte config file in ${ process . cwd ( ) } — skipping` ) ;
81+ process . exit ( 0 ) ;
82+ }
83+
84+ try {
85+ const config = await load_config ( ) ;
86+ const sync = await import ( './core/sync/sync.js' ) ;
87+ sync . all_types ( config , values . mode ) ;
88+ } catch ( error ) {
89+ handle_error ( error ) ;
90+ }
91+ } else {
92+ console . error ( colors . bold ( ) . red ( `> Unknown command: ${ command } ` ) ) ;
93+ console . log ( help ) ;
94+ process . exit ( 1 ) ;
95+ }
0 commit comments