MCPcopy Create free account
hub / github.com/MigoXLab/coderio / ensurePackageInstalled

Function ensurePackageInstalled

src/utils/dependency-installer.ts:73–109  ·  view source on GitHub ↗
(packageName: string, packageNameInRegistry?: string)

Source from the content-addressed store, hash-verified

71}
72
73export function ensurePackageInstalled(packageName: string, packageNameInRegistry?: string) {
74 const pkgName = packageNameInRegistry || packageName;
75
76 if (isPackageInstalled(packageName)) {
77 return;
78 }
79
80 logger.printInfoLog(`Package '${packageName}' is required for validation but not installed.`);
81 const pm = detectPackageManager();
82
83 // For dependencies that should be devDependencies in the user's project if they are using coderio as a tool
84 // But since this runs at runtime, we just install them.
85 const installArgs = pm === 'npm' ? 'install' : 'add';
86 const pmCmd = process.platform === 'win32' ? `${pm}.cmd` : pm;
87 const installCmdDisplay = `${pmCmd} ${installArgs} ${pkgName}`;
88
89 logger.printInfoLog(`Installing ${pkgName} using ${pm}...`);
90 logger.printInfoLog(`Command: ${installCmdDisplay}`);
91
92 try {
93 const result = spawnSync(pmCmd, [installArgs, pkgName], {
94 stdio: 'inherit',
95 cwd: process.cwd(),
96 shell: process.platform === 'win32',
97 });
98 if (result.error) {
99 throw result.error;
100 }
101 if (result.status !== 0) {
102 throw new Error(`Command exited with code ${result.status}`);
103 }
104 logger.printSuccessLog(`Successfully installed ${pkgName}.`);
105 } catch (error) {
106 const errorMsg = error instanceof Error ? error.message : String(error);
107 throw new Error(`Failed to install ${pkgName}: ${errorMsg}. Please install it manually.`);
108 }
109}
110
111export function ensureValidationDependencies(): void {
112 // Default behavior: only assert presence (do not modify user environment).

Callers

nothing calls this directly

Calls 2

isPackageInstalledFunction · 0.85
detectPackageManagerFunction · 0.85

Tested by

no test coverage detected