refactor(cli): inline source launch script

This commit is contained in:
Turtle
2026-08-11 11:58:47 +08:00
parent 6ad289809b
commit 67ef355800
9 changed files with 16 additions and 139 deletions

View File

@@ -2,5 +2,5 @@
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write apps/cli/reference/README.md
README.md: 48c8e8fd902db4de5ceb5532a67d1559453392c4
README.zh.md: d51b4256b421ecfb71d294c5b3c25bfcf6ed2c8a
README.md: cef687dc392f97886ea18b162e8f668a44ce2284
README.zh.md: f6721ec256d404e2b602fb2ed921b41c03633a82

View File

@@ -81,4 +81,4 @@ Install external plugin bundles through `dsh plugin --profile <name> add <packag
## Source execution
From the repository root, use `pnpm dsh <args...>`. The script runs the complete repository build, launches `apps/cli/src/bin.ts` with `node --import tsx/esm`, and forwards every argument. Successful build output stays out of CLI stdout, while build failures report diagnostics on stderr. The CLI process receives `NODE_USE_ENV_PROXY=1` so Node versions with environment-proxy fetch support honor `HTTP_PROXY` and `HTTPS_PROXY`. The installed form launches the built `apps/cli/lib/bin.js` without rebuilding the repository.
From the repository root, use `pnpm dsh <args...>`. The `package.json` script runs the complete repository build, launches `apps/cli/src/bin.ts` with `node --import tsx/esm`, and forwards every argument. Build output appears before CLI output. The process inherits the launch environment; set `NODE_USE_ENV_PROXY=1` when a supporting Node version must honor `HTTP_PROXY` and `HTTPS_PROXY`. The installed form launches the built `apps/cli/lib/bin.js` without rebuilding the repository.

View File

@@ -81,4 +81,4 @@ dsh web --help
## 源码执行
请从仓库根目录使用 `pnpm dsh <args...>`脚本会完成整个仓库的构建,通过 `node --import tsx/esm` 启动 `apps/cli/src/bin.ts`,并转发所有参数。构建成功时,其输出不会进入 CLI stdout构建失败时诊断信息会写入 stderr。CLI 进程会接收 `NODE_USE_ENV_PROXY=1`,从而让支持环境代理的 Node 版本中的 fetch 遵循 `HTTP_PROXY``HTTPS_PROXY`。安装形式会直接启动构建后的 `apps/cli/lib/bin.js`,不会重新构建仓库。
请从仓库根目录使用 `pnpm dsh <args...>``package.json` 中的脚本会完成整个仓库的构建,通过 `node --import tsx/esm` 启动 `apps/cli/src/bin.ts`,并转发所有参数。构建输出会显示在 CLI 输出之前。该进程会继承启动环境;当支持环境代理的 Node 版本必须遵循 `HTTP_PROXY``HTTPS_PROXY` 时,请设置 `NODE_USE_ENV_PROXY=1`。安装形式会直接启动构建后的 `apps/cli/lib/bin.js`,不会重新构建仓库。

View File

@@ -1,7 +1,4 @@
import { existsSync } from 'node:fs'
import { chmod, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { delimiter, join } from 'node:path'
import { tmpdir } from 'node:os'
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { execa } from 'execa'
import { describe, expect, it } from 'vitest'
@@ -19,86 +16,14 @@ import { describe, expect, it } from 'vitest'
const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
const dshSourceBin = 'apps/cli/src/bin.ts'
function resolvePnpmExecutable(): string {
const pathKey = Object.keys(process.env).find(key => key.toUpperCase() === 'PATH') ?? 'PATH'
const executableNames = process.platform === 'win32' ? ['pnpm.cmd', 'pnpm.exe', 'pnpm'] : ['pnpm']
for (const directory of (process.env[pathKey] ?? '').split(delimiter)) {
for (const executableName of executableNames) {
const candidate = join(directory, executableName)
if (existsSync(candidate)) return candidate
}
}
throw new Error('pnpm executable is absent from PATH')
}
async function createPnpmStub(emitBuildOutput = false): Promise<{
readonly directory: string
readonly env: NodeJS.ProcessEnv
readonly invocationLog: string
}> {
const directory = await mkdtemp(join(tmpdir(), 'dsh-source-launch-'))
const invocationLog = join(directory, 'pnpm-args.json')
const simulatedOutput = emitBuildOutput
? "process.stdout.write('tsdown simulated build\\n')\nprocess.stderr.write('build:lib simulated build\\n')\n"
: ''
const stubBody = `const { appendFileSync } = require('node:fs')\nappendFileSync(process.env.DSH_TEST_PNPM_LOG, JSON.stringify(process.argv.slice(2)))\n${simulatedOutput}`
await writeFile(join(directory, 'pnpm'), `#!/usr/bin/env node\n${stubBody}`)
await chmod(join(directory, 'pnpm'), 0o755)
await writeFile(join(directory, 'pnpm-stub.cjs'), stubBody)
await writeFile(join(directory, 'pnpm.cmd'), '@echo off\r\nnode "%~dp0pnpm-stub.cjs" %*\r\n')
const pathKey = Object.keys(process.env).find(key => key.toUpperCase() === 'PATH') ?? 'PATH'
return {
directory,
invocationLog,
env: {
...process.env,
[pathKey]: `${directory}${delimiter}${process.env[pathKey] ?? ''}`,
DSH_TEST_PNPM_LOG: invocationLog,
},
}
}
describe('dsh SOURCE launcher (node --import tsx/esm)', () => {
it('runs the repository build before launching the source CLI', async () => {
const stub = await createPnpmStub()
try {
const result = await execa(process.execPath, ['--import', 'tsx/esm', 'scripts/run-source-dsh.ts', '--help'], {
cwd: repoRoot,
env: stub.env,
reject: false,
})
expect(result.exitCode).toBe(0)
expect(result.stdout).toMatch(/^Usage: dsh /)
await expect(readFile(stub.invocationLog, 'utf8')).resolves.toBe('["run","build"]')
} finally {
await rm(stub.directory, { recursive: true, force: true })
it('builds before launching the source CLI', async () => {
const rootPackage = JSON.parse(await readFile(new URL('../../../package.json', import.meta.url), 'utf8')) as {
readonly scripts?: Record<string, string>
}
expect(rootPackage.scripts?.dsh).toBe('pnpm run build && node --import tsx/esm apps/cli/src/bin.ts')
})
it('builds without mixing build logs into CLI stdout', async () => {
const pnpmExecutable = resolvePnpmExecutable()
const stub = await createPnpmStub(true)
try {
const result = await execa(pnpmExecutable, ['dsh', '--help'], {
cwd: repoRoot,
env: stub.env,
timeout: 25_000,
killSignal: 'SIGKILL',
reject: false,
})
if (result.timedOut) {
throw new Error(`pnpm dsh --help did not exit within 25s. stdout:\n${result.stdout}\nstderr:\n${result.stderr}`)
}
expect(result.exitCode).toBe(0)
expect(result.stdout).toMatch(/^Usage: dsh /)
expect(result.stdout).not.toContain('tsdown')
expect(result.stdout).not.toContain('build:lib')
await expect(readFile(stub.invocationLog, 'utf8')).resolves.toBe('["run","build"]')
} finally {
await rm(stub.directory, { recursive: true, force: true })
}
}, 30_000)
it('boots the source entry and requires a profile', async () => {
const result = await execa(process.execPath, ['--import', 'tsx/esm', dshSourceBin], {
cwd: repoRoot,