@@ -10,11 +10,11 @@ import type { ServerRequest } from './types.ts'
10
10
import { VERSION } from './version.ts'
11
11
12
12
const commands = {
13
- 'init' : 'Create a new application' ,
14
- 'dev' : 'Start the app in development mode' ,
15
- 'start' : 'Start the app in production mode' ,
16
- 'build' : 'Build the app to a static site (SSG)' ,
17
- 'upgrade' : 'Upgrade Aleph.js command'
13
+ 'init' : 'Create a new application' ,
14
+ 'dev' : 'Start the app in development mode' ,
15
+ 'start' : 'Start the app in production mode' ,
16
+ 'build' : 'Build the app to a static site (SSG)' ,
17
+ 'upgrade' : 'Upgrade Aleph.js command'
18
18
}
19
19
20
20
const helpMessage = `Aleph.js v${ VERSION }
@@ -35,144 +35,144 @@ Options:
35
35
`
36
36
37
37
async function main ( ) {
38
- // parse deno args
39
- const args : Array < string > = [ ]
40
- const flags : Record < string , string | boolean > = { }
41
- for ( let i = 0 ; i < Deno . args . length ; i ++ ) {
42
- const arg = Deno . args [ i ]
43
- if ( arg . startsWith ( '-' ) ) {
44
- if ( arg . includes ( '=' ) ) {
45
- const [ key , value ] = arg . replace ( / ^ - + / , '' ) . split ( '=' , 2 )
46
- flags [ key ] = value
47
- } else {
48
- const key = arg . replace ( / ^ - + / , '' )
49
- const nextArg = Deno . args [ i + 1 ]
50
- if ( nextArg && ! nextArg . startsWith ( '-' ) ) {
51
- flags [ key ] = nextArg
52
- i ++
53
- } else {
54
- flags [ key ] = true
55
- }
56
- }
38
+ // parse deno args
39
+ const args : Array < string > = [ ]
40
+ const flags : Record < string , string | boolean > = { }
41
+ for ( let i = 0 ; i < Deno . args . length ; i ++ ) {
42
+ const arg = Deno . args [ i ]
43
+ if ( arg . startsWith ( '-' ) ) {
44
+ if ( arg . includes ( '=' ) ) {
45
+ const [ key , value ] = arg . replace ( / ^ - + / , '' ) . split ( '=' , 2 )
46
+ flags [ key ] = value
47
+ } else {
48
+ const key = arg . replace ( / ^ - + / , '' )
49
+ const nextArg = Deno . args [ i + 1 ]
50
+ if ( nextArg && ! nextArg . startsWith ( '-' ) ) {
51
+ flags [ key ] = nextArg
52
+ i ++
57
53
} else {
58
- args . push ( arg )
54
+ flags [ key ] = true
59
55
}
56
+ }
57
+ } else {
58
+ args . push ( arg )
60
59
}
61
-
62
- const hasCommand = args . length > 0 && args [ 0 ] in commands
63
- const command = ( hasCommand ? String ( args . shift ( ) ) : 'dev' ) as keyof typeof commands
64
-
65
- // prints aleph.js version
66
- if ( flags . v && command != 'upgrade' ) {
67
- console . log ( `aleph.js v${ VERSION } ` )
68
- Deno . exit ( 0 )
69
- }
70
-
71
- // prints aleph.js and deno version
72
- if ( flags . version && command != 'upgrade' ) {
73
- const { deno, v8, typescript } = Deno . version
74
- console . log ( `aleph.js ${ VERSION } ` )
75
- console . log ( `deno ${ deno } ` )
76
- console . log ( `v8 ${ v8 } ` )
77
- console . log ( `typescript ${ typescript } ` )
60
+ }
61
+
62
+ const hasCommand = args . length > 0 && args [ 0 ] in commands
63
+ const command = ( hasCommand ? String ( args . shift ( ) ) : 'dev' ) as keyof typeof commands
64
+
65
+ // prints aleph.js version
66
+ if ( flags . v && command != 'upgrade' ) {
67
+ console . log ( `aleph.js v${ VERSION } ` )
68
+ Deno . exit ( 0 )
69
+ }
70
+
71
+ // prints aleph.js and deno version
72
+ if ( flags . version && command != 'upgrade' ) {
73
+ const { deno, v8, typescript } = Deno . version
74
+ console . log ( `aleph.js ${ VERSION } ` )
75
+ console . log ( `deno ${ deno } ` )
76
+ console . log ( `v8 ${ v8 } ` )
77
+ console . log ( `typescript ${ typescript } ` )
78
+ Deno . exit ( 0 )
79
+ }
80
+
81
+ // prints help message
82
+ if ( flags . h || flags . help ) {
83
+ if ( hasCommand ) {
84
+ import ( `./cli/${ command } .ts` ) . then ( ( { helpMessage } ) => {
85
+ console . log ( commands [ command ] )
86
+ if ( util . isNEString ( helpMessage ) ) {
87
+ console . log ( helpMessage )
88
+ }
78
89
Deno . exit ( 0 )
90
+ } )
91
+ return
92
+ } else {
93
+ console . log ( helpMessage )
94
+ Deno . exit ( 0 )
79
95
}
80
-
81
- // prints help message
82
- if ( flags . h || flags . help ) {
83
- if ( hasCommand ) {
84
- import ( `./cli/${ command } .ts` ) . then ( ( { helpMessage } ) => {
85
- console . log ( commands [ command ] )
86
- if ( util . isNEString ( helpMessage ) ) {
87
- console . log ( helpMessage )
88
- }
89
- Deno . exit ( 0 )
90
- } )
91
- return
92
- } else {
93
- console . log ( helpMessage )
94
- Deno . exit ( 0 )
96
+ }
97
+
98
+ // sets log level
99
+ const l = flags . L || flags [ 'log-level' ]
100
+ if ( util . isNEString ( l ) ) {
101
+ log . setLevel ( l . toLowerCase ( ) as LevelNames )
102
+ }
103
+
104
+ if ( ! hasCommand && ! args [ 0 ] ) {
105
+ const walkOptions = { includeDirs : false , exts : [ '.js' , '.jsx' , '.mjs' , '.ts' , '.tsx' ] , skip : [ / \. d \. t s $ / i] , dep : 1 }
106
+ const pagesDir = path . join ( path . resolve ( '.' ) , 'pages' )
107
+ let hasIndexPage = false
108
+ if ( existsDirSync ( pagesDir ) ) {
109
+ for await ( const { path : p } of walk ( pagesDir , walkOptions ) ) {
110
+ if ( path . basename ( p ) . split ( '.' ) [ 0 ] === 'index' ) {
111
+ hasIndexPage = true
95
112
}
113
+ }
96
114
}
97
-
98
- // sets log level
99
- const l = flags . L || flags [ 'log-level' ]
100
- if ( util . isNEString ( l ) ) {
101
- log . setLevel ( l . toLowerCase ( ) as LevelNames )
115
+ if ( ! hasIndexPage ) {
116
+ console . log ( helpMessage )
117
+ Deno . exit ( 0 )
102
118
}
103
-
104
- if ( ! hasCommand && ! args [ 0 ] ) {
105
- const walkOptions = { includeDirs : false , exts : [ '.js' , '.jsx' , '.mjs' , '.ts' , '.tsx' ] , skip : [ / \. d \. t s $ / i] , dep : 1 }
106
- const pagesDir = path . join ( path . resolve ( '.' ) , 'pages' )
107
- let hasIndexPage = false
108
- if ( existsDirSync ( pagesDir ) ) {
109
- for await ( const { path : p } of walk ( pagesDir , walkOptions ) ) {
110
- if ( path . basename ( p ) . split ( '.' ) [ 0 ] === 'index' ) {
111
- hasIndexPage = true
119
+ }
120
+
121
+ // proxy https://deno.land/x/aleph for aleph.js dev
122
+ if ( [ 'dev' , 'start' , 'build' ] . includes ( command ) && existsFileSync ( './import_map.json' ) ) {
123
+ const { imports } = JSON . parse ( Deno . readTextFileSync ( './import_map.json' ) )
124
+ if ( imports [ 'https://deno.land/x/aleph/' ] ) {
125
+ const match = String ( imports [ 'https://deno.land/x/aleph/' ] ) . match ( / ^ h t t p : \/ \/ l o c a l h o s t : ( \d + ) \/ $ / )
126
+ if ( match ) {
127
+ const cwd = Deno . cwd ( )
128
+ const port = parseInt ( match [ 1 ] )
129
+ listenAndServe ( { port } , async ( req : ServerRequest ) => {
130
+ const url = new URL ( 'http://localhost' + req . url )
131
+ const resp = new Request ( req , util . cleanPath ( url . pathname ) , { } , url . searchParams )
132
+ const filepath = path . join ( cwd , url . pathname )
133
+ try {
134
+ const info = await Deno . lstat ( filepath )
135
+ if ( info . isDirectory ) {
136
+ const r = Deno . readDir ( filepath )
137
+ const items : string [ ] = [ ]
138
+ for await ( const item of r ) {
139
+ if ( ! item . name . startsWith ( '.' ) ) {
140
+ items . push ( `<li><a href='${ path . join ( url . pathname , encodeURI ( item . name ) ) } '>${ item . name } ${ item . isDirectory ? '/' : '' } <a></li>` )
112
141
}
142
+ }
143
+ resp . send ( createHtml ( {
144
+ head : [ `<title>aleph.js/</title>` ] ,
145
+ body : `<h1> aleph.js/</h1><ul>${ Array . from ( items ) . join ( '' ) } </ul>`
146
+ } ) , 'text/html' )
147
+ return
113
148
}
114
- }
115
- if ( ! hasIndexPage ) {
116
- console . log ( helpMessage )
117
- Deno . exit ( 0 )
118
- }
119
- }
120
-
121
- // proxy https://deno.land/x/aleph for aleph.js dev
122
- if ( [ 'dev' , 'start' , 'build' ] . includes ( command ) && existsFileSync ( './import_map.json' ) ) {
123
- const { imports } = JSON . parse ( Deno . readTextFileSync ( './import_map.json' ) )
124
- if ( imports [ 'https://deno.land/x/aleph/' ] ) {
125
- const match = String ( imports [ 'https://deno.land/x/aleph/' ] ) . match ( / ^ h t t p : \/ \/ l o c a l h o s t : ( \d + ) \/ $ / )
126
- if ( match ) {
127
- const cwd = Deno . cwd ( )
128
- const port = parseInt ( match [ 1 ] )
129
- listenAndServe ( { port } , async ( req : ServerRequest ) => {
130
- const url = new URL ( 'http://localhost' + req . url )
131
- const resp = new Request ( req , util . cleanPath ( url . pathname ) , { } , url . searchParams )
132
- const filepath = path . join ( cwd , url . pathname )
133
- try {
134
- const info = await Deno . lstat ( filepath )
135
- if ( info . isDirectory ) {
136
- const r = Deno . readDir ( filepath )
137
- const items : string [ ] = [ ]
138
- for await ( const item of r ) {
139
- if ( ! item . name . startsWith ( '.' ) ) {
140
- items . push ( `<li><a href='${ path . join ( url . pathname , encodeURI ( item . name ) ) } '>${ item . name } ${ item . isDirectory ? '/' : '' } <a></li>` )
141
- }
142
- }
143
- resp . send ( createHtml ( {
144
- head : [ `<title>aleph.js/</title>` ] ,
145
- body : `<h1> aleph.js/</h1><ul>${ Array . from ( items ) . join ( '' ) } </ul>`
146
- } ) , 'text/html' )
147
- return
148
- }
149
- resp . send ( await Deno . readFile ( filepath ) , getContentType ( filepath ) )
150
- } catch ( err ) {
151
- if ( err instanceof Deno . errors . NotFound ) {
152
- resp . status ( 404 ) . send ( 'file not found' )
153
- return
154
- }
155
- resp . status ( 500 ) . send ( err . message )
156
- }
157
- } )
158
- Object . assign ( globalThis , { __ALEPH_DEV_PORT : port } )
159
- log . info ( `Proxy https://deno.land/x/aleph on http://localhost:${ port } ` )
149
+ resp . send ( await Deno . readFile ( filepath ) , getContentType ( filepath ) )
150
+ } catch ( err ) {
151
+ if ( err instanceof Deno . errors . NotFound ) {
152
+ resp . status ( 404 ) . send ( 'file not found' )
153
+ return
160
154
}
161
- }
162
- }
163
-
164
- const { default : cmd } = await import ( `./cli/${ command } .ts` )
165
- if ( command === 'upgrade' ) {
166
- await cmd ( flags . v || flags . version || args [ 0 ] || 'latest' )
167
- return
168
- }
169
- const appDir = path . resolve ( args [ 0 ] || '.' )
170
- if ( command !== 'init' && ! existsDirSync ( appDir ) ) {
171
- log . fatal ( 'No such directory:' , appDir )
155
+ resp . status ( 500 ) . send ( err . message )
156
+ }
157
+ } )
158
+ Object . assign ( globalThis , { __ALEPH_DEV_PORT : port } )
159
+ log . info ( `Proxy https://deno.land/x/aleph on http://localhost:${ port } ` )
160
+ }
172
161
}
173
- await cmd ( appDir , flags )
162
+ }
163
+
164
+ const { default : cmd } = await import ( `./cli/${ command } .ts` )
165
+ if ( command === 'upgrade' ) {
166
+ await cmd ( flags . v || flags . version || args [ 0 ] || 'latest' )
167
+ return
168
+ }
169
+ const appDir = path . resolve ( args [ 0 ] || '.' )
170
+ if ( command !== 'init' && ! existsDirSync ( appDir ) ) {
171
+ log . fatal ( 'No such directory:' , appDir )
172
+ }
173
+ await cmd ( appDir , flags )
174
174
}
175
175
176
176
if ( import . meta. main ) {
177
- main ( )
177
+ main ( )
178
178
}
0 commit comments