@@ -33,17 +33,15 @@ Options:
33
33
34
34
async function main ( ) {
35
35
const { _ : args , ...options } = flags . parse ( Deno . args )
36
- const hasCommand = args . length > 0 && args [ 0 ] in commands
37
- const command = ( hasCommand ? String ( args . shift ( ) ) : 'dev' ) as keyof typeof commands
38
36
39
37
// prints aleph.js version
40
- if ( options . v && command != 'upgrade' ) {
38
+ if ( options . v ) {
41
39
console . log ( `aleph.js v${ VERSION } ` )
42
40
Deno . exit ( 0 )
43
41
}
44
42
45
43
// prints aleph.js and deno version
46
- if ( options . version && command != 'upgrade' ) {
44
+ if ( options . version ) {
47
45
const { deno, v8, typescript } = Deno . version
48
46
console . log ( [
49
47
`aleph.js ${ VERSION } ` ,
@@ -54,45 +52,48 @@ async function main() {
54
52
Deno . exit ( 0 )
55
53
}
56
54
57
- // prints help message
55
+ // prints help message when the command not found
56
+ if ( ! ( args . length > 0 && args [ 0 ] in commands ) ) {
57
+ console . log ( helpMessage )
58
+ Deno . exit ( 0 )
59
+ }
60
+
61
+ const command = String ( args . shift ( ) ) as keyof typeof commands
62
+
63
+ // prints command help message
58
64
if ( options . h || options . help ) {
59
- if ( hasCommand ) {
60
- import ( `./cli/${ command } .ts` ) . then ( ( { helpMessage } ) => {
61
- console . log ( commands [ command ] + '\n' + helpMessage )
62
- Deno . exit ( 0 )
63
- } )
64
- return
65
- } else {
65
+ import ( `./cli/${ command } .ts` ) . then ( ( { helpMessage } ) => {
66
+ console . log ( commands [ command ] )
66
67
console . log ( helpMessage )
67
68
Deno . exit ( 0 )
68
- }
69
+ } )
70
+ return
69
71
}
70
72
71
- // sets log level
72
- const l = options . L || options [ 'log-level' ]
73
- if ( util . isNEString ( l ) ) {
74
- log . setLevel ( l . toLowerCase ( ) as LevelNames )
73
+ // import command module
74
+ const { default : cmd } = await import ( `./cli/${ command } .ts` )
75
+
76
+ // execute `init` command
77
+ if ( command === 'init' ) {
78
+ await cmd ( args [ 0 ] )
79
+ return
75
80
}
76
81
77
- if ( ! hasCommand && ! args [ 0 ] ) {
78
- const walkOptions = { includeDirs : false , exts : [ '.js' , '.jsx' , '.mjs' , '.ts' , '.tsx' ] , skip : [ / \. d \. t s $ / i] , dep : 1 }
79
- const pagesDir = path . join ( path . resolve ( '.' ) , 'pages' )
80
- let hasIndexPage = false
81
- if ( existsDirSync ( pagesDir ) ) {
82
- for await ( const { path : p } of walk ( pagesDir , walkOptions ) ) {
83
- if ( path . basename ( p ) . split ( '.' ) [ 0 ] === 'index' ) {
84
- hasIndexPage = true
85
- }
86
- }
87
- }
88
- if ( ! hasIndexPage ) {
89
- console . log ( helpMessage )
90
- Deno . exit ( 0 )
91
- }
82
+ // execute `upgrade` command
83
+ if ( command === 'upgrade' ) {
84
+ await cmd ( options . v || options . version || args [ 0 ] || 'latest' )
85
+ return
86
+ }
87
+
88
+ // check working Dir
89
+ const workingDir = path . resolve ( String ( args [ 0 ] || '.' ) )
90
+ if ( ! existsDirSync ( workingDir ) ) {
91
+ log . fatal ( 'No such directory:' , workingDir )
92
92
}
93
+ Deno . chdir ( workingDir )
93
94
94
95
// load .env
95
- for await ( const { path : p , } of walk ( Deno . cwd ( ) , { match : [ / ( ^ | \/ | \\ ) \. e n v ( \. | $ ) / i] , maxDepth : 1 } ) ) {
96
+ for await ( const { path : p , } of walk ( workingDir , { match : [ / ( ^ | \/ | \\ ) \. e n v ( \. | $ ) / i] , maxDepth : 1 } ) ) {
96
97
const text = await Deno . readTextFile ( p )
97
98
text . split ( '\n' ) . forEach ( line => {
98
99
let [ key , value ] = util . splitBy ( line , '=' )
@@ -110,23 +111,13 @@ async function main() {
110
111
localProxy ( parseInt ( v ) )
111
112
}
112
113
113
- const { default : cmd } = await import ( `./cli/${ command } .ts` )
114
- switch ( command ) {
115
- case 'init' :
116
- await cmd ( args [ 0 ] )
117
- break
118
- case 'upgrade' :
119
- await cmd ( options . v || options . version || args [ 0 ] || 'latest' )
120
- break
121
- default :
122
- const workingDir = path . resolve ( String ( args [ 0 ] || '.' ) )
123
- if ( ! existsDirSync ( workingDir ) ) {
124
- log . fatal ( 'No such directory:' , workingDir )
125
- }
126
- Deno . chdir ( workingDir )
127
- await cmd ( workingDir , options )
128
- break
114
+ // sets log level
115
+ const l = options . L || options [ 'log-level' ]
116
+ if ( util . isNEString ( l ) ) {
117
+ log . setLevel ( l . toLowerCase ( ) as LevelNames )
129
118
}
119
+
120
+ await cmd ( workingDir , options )
130
121
}
131
122
132
123
if ( import . meta. main ) {
0 commit comments