-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33e7028
commit 0c9d7a7
Showing
14 changed files
with
482 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ docs/api | |
temp/*.api.json | ||
.vercel/project.json | ||
www/public | ||
www/template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
public | ||
/public | ||
/template |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { h } from 'preact'; | ||
import { render } from 'preact-render-to-string'; | ||
import { convertApiDocMapPathToUrlPathName } from '../../src/apiDocMapPathList'; | ||
import { ApiDocMapResponseContextProvider } from '../../src/ApiDocMapResponseContext'; | ||
import { ApiDocPage, IndexPage, NotFoundPage } from '../../src/App'; | ||
import { ResponseDoneType, ResponseState } from '../../src/loadApiDocMap'; | ||
import { PageNodeMapWithMetadata } from '../docs/apigen/types'; | ||
import { | ||
addFileToFolder, | ||
Folder, | ||
writeFolderToDirectoryPath, | ||
} from '../docs/apigen/util/Folder'; | ||
import { getRelativePath } from '../docs/apigen/util/getRelativePath'; | ||
|
||
const template = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8'); | ||
// eslint-disable-next-line max-len | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires | ||
const apiDocMap: PageNodeMapWithMetadata = require('../../temp/apiDocMap.json'); | ||
|
||
function shouldNotBeCalled(): never { | ||
throw new Error('This should not be called.'); | ||
} | ||
|
||
const outFolder = Folder(); | ||
|
||
function ensureRelative(path: string): string { | ||
if (path[0] !== '.') { | ||
return `./${path}`; | ||
} | ||
return path; | ||
} | ||
|
||
function addRenderedHtmlToFolder(html: string, filePath: string): void { | ||
const contents = template | ||
.replace('::ssr::', html) | ||
.replace( | ||
/::index\.tsx::/g, | ||
ensureRelative(getRelativePath(filePath, 'index.tsx')), | ||
) | ||
.replace( | ||
/::index\.css::/g, | ||
ensureRelative(getRelativePath(filePath, 'index.css')), | ||
) | ||
.replace( | ||
/::loadApiDocMap\.ts::/g, | ||
ensureRelative(getRelativePath(filePath, 'loadApiDocMap.ts')), | ||
); | ||
addFileToFolder(outFolder, filePath, contents); | ||
} | ||
|
||
addRenderedHtmlToFolder( | ||
render( | ||
<ApiDocMapResponseContextProvider | ||
value={{ | ||
getCurrentResponseState: shouldNotBeCalled, | ||
onResponseStateChange: shouldNotBeCalled, | ||
}} | ||
> | ||
<IndexPage /> | ||
</ApiDocMapResponseContextProvider>, | ||
), | ||
'index.html', | ||
); | ||
|
||
addRenderedHtmlToFolder( | ||
render( | ||
<ApiDocMapResponseContextProvider | ||
value={{ | ||
getCurrentResponseState: shouldNotBeCalled, | ||
onResponseStateChange: shouldNotBeCalled, | ||
}} | ||
> | ||
<NotFoundPage /> | ||
</ApiDocMapResponseContextProvider>, | ||
), | ||
'404.html', | ||
); | ||
|
||
const responseState: ResponseState = { | ||
type: ResponseDoneType, | ||
data: apiDocMap, | ||
}; | ||
|
||
for (const apiDocMapPath of Object.keys(apiDocMap.pageNodeMap)) { | ||
addRenderedHtmlToFolder( | ||
render( | ||
<ApiDocMapResponseContextProvider | ||
value={{ | ||
getCurrentResponseState: () => responseState, | ||
onResponseStateChange: shouldNotBeCalled, | ||
}} | ||
> | ||
<ApiDocPage pagePath={apiDocMapPath}></ApiDocPage> | ||
</ApiDocMapResponseContextProvider>, | ||
), | ||
`${convertApiDocMapPathToUrlPathName(apiDocMapPath).replace( | ||
/^\//, | ||
'', | ||
)}.html`, | ||
); | ||
} | ||
|
||
export const rootDir = path.join(__dirname, '..', '..', 'template'); | ||
|
||
writeFolderToDirectoryPath(outFolder, rootDir).catch((error) => { | ||
console.error('error writing pages to out directory...'); | ||
throw error; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createContext } from 'preact'; | ||
import { useState, useContext, useEffect } from 'preact/hooks'; | ||
import { ResponseState, NonLoadingResponseState } from './loadApiDocMap'; | ||
|
||
export interface ApiDocMapResponseContextValue { | ||
getCurrentResponseState: () => ResponseState; | ||
onResponseStateChange: ( | ||
setResponseState: (responseState: NonLoadingResponseState) => void, | ||
) => () => void; | ||
} | ||
|
||
const apiDocMapResponseContext = createContext<ApiDocMapResponseContextValue>( | ||
(null as unknown) as ApiDocMapResponseContextValue, | ||
); | ||
|
||
export const ApiDocMapResponseContextProvider = | ||
apiDocMapResponseContext.Provider; | ||
|
||
export function useApiDocMapResponseState(): ResponseState { | ||
const { getCurrentResponseState, onResponseStateChange } = useContext( | ||
apiDocMapResponseContext, | ||
); | ||
const { 0: responseState, 1: setResponseState } = useState( | ||
getCurrentResponseState(), | ||
); | ||
|
||
useEffect(() => { | ||
const currentResponseState = getCurrentResponseState(); | ||
if (currentResponseState !== responseState) { | ||
setResponseState(currentResponseState); | ||
} | ||
return onResponseStateChange(setResponseState); | ||
}); | ||
|
||
return responseState; | ||
} |
Oops, something went wrong.
0c9d7a7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: