-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #790 from tszhong0411/dev
feat(mdx): improve dev server
- Loading branch information
Showing
11 changed files
with
101 additions
and
50 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
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 |
---|---|---|
|
@@ -15,4 +15,4 @@ jobs: | |
uses: ./.github/actions/check-setup | ||
|
||
- name: Run unit tests | ||
run: pnpm run test:unit | ||
run: pnpm test:unit |
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 |
---|---|---|
@@ -1,39 +1,18 @@ | ||
import * as chokidar from 'chokidar' | ||
import { type ChildProcess, spawn } from 'node:child_process' | ||
import path from 'node:path' | ||
|
||
import { LOG_PREFIX } from '../constants' | ||
import { getConfig } from '../get-config' | ||
import { build } from './build' | ||
let serverProcess: ChildProcess | undefined | ||
|
||
export const dev = async () => { | ||
const { contentDirPath } = await getConfig(process.cwd()) | ||
export const dev = () => { | ||
const startServerPath = path.resolve(import.meta.dirname, 'start-server') | ||
|
||
// Initial build | ||
await build() | ||
|
||
const watcher = chokidar.watch(`${contentDirPath}/**/*.mdx`, { | ||
persistent: true | ||
}) | ||
|
||
watcher.on('change', (path) => { | ||
console.log(`${LOG_PREFIX}${path} has changed. Rebuilding...`) | ||
void build() | ||
serverProcess = spawn('node', [startServerPath], { | ||
stdio: 'inherit' | ||
}) | ||
|
||
console.log(`${LOG_PREFIX}Watching for file changes...`) | ||
|
||
const handleTermination = async () => { | ||
console.log(`${LOG_PREFIX}Terminating watcher...`) | ||
|
||
await watcher.close() | ||
|
||
console.log(`${LOG_PREFIX}Watcher closed.`) | ||
console.log(`${LOG_PREFIX}Exiting...`) | ||
} | ||
|
||
process.on('SIGINT', () => { | ||
void handleTermination() | ||
}) | ||
process.on('SIGTERM', () => { | ||
void handleTermination() | ||
serverProcess.on('exit', (code) => { | ||
if (code === 99) { | ||
dev() | ||
} | ||
}) | ||
} |
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,52 @@ | ||
import * as chokidar from 'chokidar' | ||
import debounce from 'debounce' | ||
import { exit } from 'node:process' | ||
|
||
import { build } from './commands' | ||
import { LOG_PREFIX } from './constants' | ||
import { getConfig } from './get-config' | ||
|
||
const startServer = async () => { | ||
const { | ||
config: { contentDirPath }, | ||
filepath | ||
} = await getConfig(process.cwd()) | ||
|
||
// Initial build | ||
await build() | ||
|
||
const watcher = chokidar.watch(`${contentDirPath}/**/*.mdx`) | ||
const configWatcher = chokidar.watch(filepath) | ||
|
||
const debounceBuild = debounce((path) => { | ||
console.log(`${LOG_PREFIX}${path} has changed. Rebuilding...`) | ||
void build() | ||
}, 500) | ||
|
||
watcher.on('change', debounceBuild) | ||
configWatcher.on('change', () => { | ||
console.log(`${LOG_PREFIX}Config file changed. Restarting...`) | ||
exit(99) | ||
}) | ||
|
||
console.log(`${LOG_PREFIX}Watching for file changes...`) | ||
|
||
const handleTermination = async () => { | ||
console.log(`${LOG_PREFIX}Terminating watcher...`) | ||
|
||
await watcher.close() | ||
await configWatcher.close() | ||
|
||
console.log(`${LOG_PREFIX}Watcher closed.`) | ||
console.log(`${LOG_PREFIX}Exiting...`) | ||
} | ||
|
||
process.on('SIGINT', () => { | ||
void handleTermination() | ||
}) | ||
process.on('SIGTERM', () => { | ||
void handleTermination() | ||
}) | ||
} | ||
|
||
startServer() |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.