@@ -14,6 +14,7 @@ type AppOptions = {
14
14
'public-dir' : string | undefined ,
15
15
'static-dir' : string | undefined ,
16
16
spa : string | null | undefined ,
17
+ 'not-found-page' : string | null | ( ( options : AppOptions ) => string | undefined ) | undefined ,
17
18
'auto-index' : string [ ] | null | undefined ,
18
19
'auto-ext' : string [ ] | null | undefined ,
19
20
name : string ,
@@ -99,6 +100,9 @@ const defaultOptions: AppOptions = {
99
100
'public-dir' : undefined ,
100
101
'static-dir' : undefined ,
101
102
spa : undefined ,
103
+ 'not-found-page' : ( options ) => {
104
+ return options [ 'public-dir' ] + '/404.html' ;
105
+ } ,
102
106
'auto-index' : [ 'index.html' , 'index.htm' ] ,
103
107
'auto-ext' : [ '.html' , '.htm' ] ,
104
108
@@ -154,10 +158,13 @@ export function initApp(commandLineValues: CommandLineOptions) {
154
158
options = {
155
159
...options ,
156
160
...pickKeys ( [ 'author' , 'name' , 'description' ] , packageJson ?? { } ) ,
157
- ...pickKeys ( [ 'public-dir' , 'static-dir' , 'spa' , 'auto-index' , 'auto-ext' , 'author' , 'name' , 'description' ] , commandLineValues )
161
+ ...pickKeys ( [ 'public-dir' , 'static-dir' , 'spa' , 'not-found-page' , ' auto-index', 'auto-ext' , 'author' , 'name' , 'description' ] , commandLineValues )
158
162
} ;
159
163
160
- console . log ( 'options' , options ) ;
164
+ if ( typeof options [ 'not-found-page' ] === 'function' ) {
165
+ // Apply function for this one
166
+ options [ 'not-found-page' ] = options [ 'not-found-page' ] ( options ) ;
167
+ }
161
168
162
169
if ( check != null ) {
163
170
if ( ! check ( packageJson , options ) ) {
@@ -180,6 +187,7 @@ export function initApp(commandLineValues: CommandLineOptions) {
180
187
const buildStaticDir = BUILD_STATIC_DIR != null ? path . resolve ( BUILD_STATIC_DIR ) : null ;
181
188
182
189
const spa = options [ 'spa' ] as string | null | undefined ;
190
+ const notFoundPage = options [ 'not-found-page' ] as string | null | undefined ;
183
191
184
192
const autoIndex = options [ 'auto-index' ] as string [ ] | null | undefined ;
185
193
const autoExt = options [ 'auto-ext' ] as string [ ] | null | undefined ;
@@ -204,6 +212,26 @@ export function initApp(commandLineValues: CommandLineOptions) {
204
212
}
205
213
}
206
214
215
+ let notFoundPageFilename = notFoundPage ;
216
+
217
+ // Specifically check for null instead of undefined
218
+ if ( notFoundPage === null ) {
219
+ notFoundPageFilename = path . resolve ( publicDir , './404.html' ) ;
220
+ let rel = path . relative ( path . resolve ( ) , notFoundPageFilename ) ;
221
+ if ( ! rel . startsWith ( '..' ) ) {
222
+ rel = './' + rel ;
223
+ }
224
+ console . log ( '--not-found-page provided with no value, assuming ' + rel ) ;
225
+ }
226
+
227
+ if ( notFoundPageFilename != null ) {
228
+ notFoundPageFilename = path . resolve ( notFoundPageFilename ) ;
229
+ if ( ! notFoundPageFilename . startsWith ( publicDir ) ) {
230
+ console . error ( `❌ --not-found-page file '${ notFoundPageFilename } ' not inside public directory!` ) ;
231
+ process . exit ( 1 ) ;
232
+ }
233
+ }
234
+
207
235
const exists = fs . existsSync ( computeJsDir ) ;
208
236
if ( exists ) {
209
237
console . error ( `❌ '${ COMPUTE_JS_DIR } ' directory already exists!` ) ;
@@ -219,10 +247,16 @@ export function initApp(commandLineValues: CommandLineOptions) {
219
247
spaRel = './' + spaRel ;
220
248
}
221
249
250
+ let notFoundRel : string | null = notFoundPageFilename != null ? path . relative ( path . resolve ( ) , notFoundPageFilename ) : null ;
251
+ if ( notFoundRel != null && ! notFoundRel . startsWith ( '..' ) ) {
252
+ notFoundRel = './' + notFoundRel ;
253
+ }
254
+
222
255
console . log ( '' ) ;
223
256
console . log ( 'Public Dir :' , PUBLIC_DIR ) ;
224
257
console . log ( 'Static Dir :' , BUILD_STATIC_DIR ?? '(None)' ) ;
225
258
console . log ( 'SPA :' , spaRel != null ? spaRel : '(None)' ) ;
259
+ console . log ( '404 Page :' , notFoundRel != null ? notFoundRel : '(None)' ) ;
226
260
console . log ( 'Auto-Index :' , autoIndex != null ? autoIndex : '(None)' )
227
261
console . log ( 'Auto-Ext :' , autoExt != null ? autoExt : '(None)' )
228
262
console . log ( 'name :' , name ) ;
@@ -320,13 +354,19 @@ service_id = ""
320
354
spaFileRel = '/' + path . relative ( publicDir , spaFilename ) ;
321
355
}
322
356
357
+ let notFoundFileRel : string | false = false ;
358
+ if ( notFoundPageFilename != null ) {
359
+ notFoundFileRel = '/' + path . relative ( publicDir , notFoundPageFilename ) ;
360
+ }
361
+
323
362
const staticPublishJsContent = `\
324
363
module.exports = {
325
364
publicDir: ${ JSON . stringify ( publicDirRel ) } ,
326
365
excludeDirs: [ './node_modules' ],
327
366
includeDirs: [ './.well-known' ],
328
367
staticDirs: ${ JSON . stringify ( staticDirsRel ) } ,
329
368
spa: ${ JSON . stringify ( spaFileRel ) } ,
369
+ notFoundPage: ${ JSON . stringify ( notFoundFileRel ) } ,
330
370
autoIndex: ${ JSON . stringify ( autoIndex ) } ,
331
371
autoExt: ${ JSON . stringify ( autoExt ) } ,
332
372
};` ;
0 commit comments