feat: add easy-slides CLI
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { spawn } from 'node:child_process'
|
||||
import type { ChildProcess } from 'node:child_process'
|
||||
import { createRequire } from 'node:module'
|
||||
import { hashPresenterToken } from './token.ts'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const slidevCli = require.resolve('@slidev/cli/bin/slidev.mjs')
|
||||
|
||||
interface RunSlidevOptions {
|
||||
env?: NodeJS.ProcessEnv
|
||||
}
|
||||
|
||||
export function startSlidev(args: string[], options: RunSlidevOptions = {}) {
|
||||
const token = options.env?.PRESENTER_TOKEN ?? process.env.PRESENTER_TOKEN
|
||||
const presenterHash = token?.trim() ? hashPresenterToken(token) : ''
|
||||
return spawn(process.execPath, [slidevCli, ...args], {
|
||||
cwd: process.cwd(),
|
||||
env: {
|
||||
...process.env,
|
||||
...options.env,
|
||||
VITE_EASY_SLIDES_PRESENTER_HASH: presenterHash,
|
||||
},
|
||||
stdio: 'inherit',
|
||||
})
|
||||
}
|
||||
|
||||
export async function waitForSlidev(child: ChildProcess) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
child.once('error', reject)
|
||||
child.once('exit', (code, signal) => {
|
||||
if (code === 0)
|
||||
resolve()
|
||||
else
|
||||
reject(new Error(`Slidev 退出:${signal ?? `code ${code}`}`))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function runSlidev(args: string[], options: RunSlidevOptions = {}) {
|
||||
await waitForSlidev(startSlidev(args, options))
|
||||
}
|
||||
Reference in New Issue
Block a user