dependency-installer.ts
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { spawnSync } from 'child_process';
import { createRequire } from 'module';
import fs from 'fs';
import path from 'path';
import { logger } from './logger';
const require = createRequire(import.meta.url);
export function detectPackageManager(cwd: string = process.cwd()): 'npm' | 'yarn' | 'pnpm' {
if (fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))) return 'pnpm';
if (fs.existsSync(path.join(cwd, 'yarn.lock'))) return 'yarn';
return 'npm';
}
export function isPackageInstalled(packageName: string): boolean {
try {
require.resolve(packageName);
return true;
} catch {
return false;
}
}
function formatValidationDependencyHelp(missing: string[]): string {
const isWin = process.platform === 'win32';
const globalInstall = [
'npm install -g playwright sharp',
isWin ? 'npx playwright install chromium' : 'npx playwright install chromium',
].join('\n');
const localInstall = [
'npm install -D playwright sharp',
isWin ? 'npx playwright install chromium' : 'npx playwright install chromium',
].join('\n');
const pnpmGlobal = ['pnpm add -g playwright sharp', 'pnpm exec playwright install chromium'].join('\n');
const pnpmLocal = ['pnpm add -D playwright sharp', 'pnpm exec playwright install chromium'].join('\n');
return [
`Missing optional validation dependencies: ${missing.join(', ')}`,
'',
'Validation requires Playwright (browsers) and Sharp (image processing).',
'These dependencies are NOT bundled with coderio by default to keep installation lightweight.',
'',
'Recommended (global install):',
globalInstall,
'',
'Or install in your current project:',
localInstall,
'',
'If you use pnpm:',
'',
'Global:',
pnpmGlobal,
'',
'Local (project):',
pnpmLocal,
].join('\n');
}
export function assertValidationDependenciesInstalled(): void {
const missing: string[] = [];
if (!isPackageInstalled('sharp')) missing.push('sharp');
if (!isPackageInstalled('playwright')) missing.push('playwright');
if (missing.length === 0) {
return;
}
throw new Error(formatValidationDependencyHelp(missing));
}
export function ensurePackageInstalled(packageName: string, packageNameInRegistry?: string) {
const pkgName = packageNameInRegistry || packageName;
if (isPackageInstalled(packageName)) {
return;
}
logger.printInfoLog(`校验需要依赖包 '${packageName}',但尚未安装。`);
const pm = detectPackageManager();
// For dependencies that should be devDependencies in the user's project if they are using coderio as a tool
// But since this runs at runtime, we just install them.
const installArgs = pm === 'npm' ? 'install' : 'add';
const pmCmd = process.platform === 'win32' ? `${pm}.cmd` : pm;
const installCmdDisplay = `${pmCmd} ${installArgs} ${pkgName}`;
logger.printInfoLog(`正在使用 ${pm} 安装 ${pkgName}…`);
logger.printInfoLog(`执行命令:${installCmdDisplay}`);
try {
const result = spawnSync(pmCmd, [installArgs, pkgName], {
stdio: 'inherit',
cwd: process.cwd(),
shell: process.platform === 'win32',
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
throw new Error(`Command exited with code ${result.status}`);
}
logger.printSuccessLog(`已成功安装 ${pkgName}。`);
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to install ${pkgName}: ${errorMsg}. Please install it manually.`);
}
}
export function ensureValidationDependencies(): void {
// Default behavior: only assert presence (do not modify user environment).
// Auto-installing heavy native deps at runtime can fail and harms UX.
// If you still want auto-install, call ensurePackageInstalled(...) explicitly from your own wrapper.
assertValidationDependenciesInstalled();
// NOTE: We intentionally do not preinstall Playwright browsers here.
// The Playwright launcher has an auto-install fallback for missing Chromium binaries.
}