@@ -2,7 +2,7 @@ import { listenAndServe, path, walk } from './deps.ts'
2
2
import { Request } from './server/api.ts'
3
3
import { getContentType } from './server/mime.ts'
4
4
import { createHtml } from './server/util.ts'
5
- import { existsDirSync , existsFileSync } from './shared/fs.ts'
5
+ import { existsDirSync } from './shared/fs.ts'
6
6
import type { LevelNames } from './shared/log.ts'
7
7
import log from './shared/log.ts'
8
8
import util from './shared/util.ts'
@@ -118,47 +118,57 @@ async function main() {
118
118
}
119
119
}
120
120
121
+ // load .env
122
+ for await ( const { path : p , } of walk ( Deno . cwd ( ) , { exts : [ 'env' ] , maxDepth : 1 } ) ) {
123
+ const text = await Deno . readTextFile ( p )
124
+ text . split ( '\n' ) . forEach ( line => {
125
+ let [ key , value ] = util . splitBy ( line , '=' )
126
+ key = key . trim ( )
127
+ if ( key ) {
128
+ Deno . env . set ( key , value . trim ( ) )
129
+ }
130
+ } )
131
+ log . debug ( 'load env from' , path . basename ( p ) )
132
+ }
133
+
121
134
// 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
135
+ const p = Deno . env . get ( 'ALEPH_DEV_PORT' )
136
+ if ( p && ! / ^ \d + $ / . test ( p ) ) {
137
+ log . fatal ( 'invalid ALEPH_DEV_PORT:' , p )
138
+ }
139
+ if ( p ) {
140
+ const cwd = Deno . cwd ( )
141
+ const port = parseInt ( p )
142
+ listenAndServe ( { port } , async ( req : ServerRequest ) => {
143
+ const url = new URL ( 'http://localhost' + req . url )
144
+ const resp = new Request ( req , util . cleanPath ( url . pathname ) , { } , url . searchParams )
145
+ const filepath = path . join ( cwd , url . pathname )
146
+ try {
147
+ const info = await Deno . lstat ( filepath )
148
+ if ( info . isDirectory ) {
149
+ const r = Deno . readDir ( filepath )
150
+ const items : string [ ] = [ ]
151
+ for await ( const item of r ) {
152
+ if ( ! item . name . startsWith ( '.' ) ) {
153
+ items . push ( `<li><a href='${ path . join ( url . pathname , encodeURI ( item . name ) ) } '>${ item . name } ${ item . isDirectory ? '/' : '' } <a></li>` )
154
154
}
155
- resp . status ( 500 ) . send ( err . message )
156
155
}
157
- } )
158
- Object . assign ( globalThis , { __ALEPH_DEV_PORT : port } )
159
- log . info ( `Proxy https://deno.land/x/aleph on http://localhost:${ port } ` )
156
+ resp . send ( createHtml ( {
157
+ head : [ `<title>aleph.js/</title>` ] ,
158
+ body : `<h1> aleph.js/</h1><ul>${ Array . from ( items ) . join ( '' ) } </ul>`
159
+ } ) , 'text/html' )
160
+ return
161
+ }
162
+ resp . send ( await Deno . readFile ( filepath ) , getContentType ( filepath ) )
163
+ } catch ( err ) {
164
+ if ( err instanceof Deno . errors . NotFound ) {
165
+ resp . status ( 404 ) . send ( 'file not found' )
166
+ return
167
+ }
168
+ resp . status ( 500 ) . send ( err . message )
160
169
}
161
- }
170
+ } )
171
+ log . info ( `Proxy https://deno.land/x/aleph on http://localhost:${ port } ` )
162
172
}
163
173
164
174
const { default : cmd } = await import ( `./cli/${ command } .ts` )
0 commit comments