@@ -2,10 +2,16 @@ import fs from 'fs'
22import path from 'path'
33import { execSync } from 'child_process'
44import { program } from 'commander'
5+ import type { NextFunction , Response } from 'express'
6+ import type { ExtendedRequest } from '@/types'
57import fpt from '#src/versions/lib/non-enterprise-default-version.js'
68import { allVersionKeys } from '#src/versions/lib/all-versions.js'
79import { liquid } from '#src/content-render/index.js'
8- import contextualize from '#src/frame/middleware/context/context'
10+ import contextualize from '#src/frame/middleware/context/context.js'
11+
12+ interface CommandOptions {
13+ openSections ?: string | string [ ]
14+ }
915
1016const layoutFilename = path . posix . join ( process . cwd ( ) , 'src/dev-toc/layout.html' )
1117const layout = fs . readFileSync ( layoutFilename , 'utf8' )
@@ -22,19 +28,30 @@ program
2228 )
2329 . parse ( process . argv )
2430
25- const options = program . opts ( )
31+ const options = program . opts < CommandOptions > ( )
2632
2733const openSections = options . openSections || ''
2834const defaultOpenSections = Array . isArray ( openSections ) ? openSections : [ openSections ]
2935
3036main ( )
3137
32- async function main ( ) {
33- const next = ( ) => { }
34- const res = { }
35- const req = { language : 'en' , cookies : { } }
36-
37- async function recurse ( tree ) {
38+ async function main ( ) : Promise < void > {
39+ const next = ( ( ) => { } ) as NextFunction
40+ const res = { } as Response
41+ const req = {
42+ language : 'en' ,
43+ cookies : { } ,
44+ headers : { } ,
45+ query : { } ,
46+ path : '' ,
47+ method : 'GET' ,
48+ get : ( ) => '' ,
49+ header : ( ) => '' ,
50+ accepts : ( ) => false ,
51+ context : { } as any ,
52+ } as unknown as ExtendedRequest
53+
54+ async function recurse ( tree : any ) : Promise < void > {
3855 const { page } = tree
3956 tree . renderedFullTitle = page . rawTitle . includes ( '{' )
4057 ? await liquid . parseAndRender ( page . rawTitle , req . context )
@@ -58,12 +75,18 @@ async function main() {
5875 await contextualize ( req , res , next )
5976
6077 // Add the tree to the req.context.
61- req . context . currentEnglishTree = req . context . siteTree . en [ req . context . currentVersion ]
78+ if ( req . context && req . context . siteTree && req . context . currentVersion ) {
79+ req . context . currentEnglishTree = req . context . siteTree . en [ req . context . currentVersion ]
80+ }
6281
63- await recurse ( req . context . currentEnglishTree )
82+ if ( req . context && req . context . currentEnglishTree ) {
83+ await recurse ( req . context . currentEnglishTree )
84+ }
6485
6586 // Add any defaultOpenSections to the context.
66- req . context . defaultOpenSections = defaultOpenSections
87+ if ( req . context ) {
88+ req . context . defaultOpenSections = defaultOpenSections
89+ }
6790
6891 // Parse the layout in src/dev-toc/layout.html with the context we created above.
6992 const outputHtml = await liquid . parseAndRender ( layout , Object . assign ( { } , req . context ) )
0 commit comments