diff --git a/assistant/src/module/npm-install.ts b/assistant/src/module/npm-install.ts new file mode 100644 index 0000000..4371671 --- /dev/null +++ b/assistant/src/module/npm-install.ts @@ -0,0 +1,36 @@ +import { spawn, spawnSync } from 'child_process'; + +export const checkPnpm = () => { + try { + spawnSync('pnpm', ['--version']); + return true; + } catch (e) { + return false; + } +}; + + +type InstallDepsOptions = { + appPath: string; + isProduction?: boolean; + sync?: boolean; +}; + +export const installDeps = async (opts: InstallDepsOptions) => { + const { appPath } = opts; + const isProduction = opts.isProduction ?? true; + const isPnpm = checkPnpm(); + const params = ['i']; + if (isProduction && isPnpm) { + params.push('--production'); + } else { + params.push('--omit=dev'); + } + console.log('installDeps', appPath, params); + const syncSpawn = opts.sync ? spawnSync : spawn; + if (isPnpm) { + syncSpawn('pnpm', params, { cwd: appPath, stdio: 'inherit', env: process.env }); + } else { + syncSpawn('npm', params, { cwd: appPath, stdio: 'inherit', env: process.env }); + } +}; \ No newline at end of file diff --git a/assistant/src/services/init/index.ts b/assistant/src/services/init/index.ts index e3d9343..32c8646 100644 --- a/assistant/src/services/init/index.ts +++ b/assistant/src/services/init/index.ts @@ -4,7 +4,7 @@ import { checkFileExists, AssistantConfig, AssistantConfigData, parseHomeArg, pa import { chalk } from '@/module/chalk.ts'; import { HttpsPem } from '@/module/assistant/https/sign.ts'; import { Query } from '@kevisual/query/query'; - +import { installDeps } from '@/module/npm-install.ts' export { parseHomeArg, parseHelpArg }; export type AssistantInitOptions = { path?: string; @@ -114,8 +114,8 @@ export class AssistantInit extends AssistantConfig { "description": "assistant-app package pnpm, node pkgs projects", "type": "module", "scripts": { - "start": "pm2 start apps/code-center/dist/app.mjs --name code-center", - "proxy": "pm2 start apps/page-proxy/dist/app.mjs --name page-proxy" + "start": "pm2 start apps/root/code-center/app.mjs --name root/code-center", + "proxy": "pm2 start apps/root/page-proxy/app.mjs --name root/page-proxy" }, "keywords": [], "author": "", @@ -123,6 +123,7 @@ export class AssistantInit extends AssistantConfig { "dependencies": { "@kevisual/router": "latest", "@kevisual/use-config": "latest", + "@kevisual/query": "latest", "ioredis": "latest", "minio": "latest", "pg": "latest", @@ -143,7 +144,10 @@ export class AssistantInit extends AssistantConfig { } `, ); - console.log(chalk.green('助手 package.json 文件创建成功')); + console.log(chalk.green('助手 package.json 文件创建成功, 正在安装依赖...')); + installDeps({ appPath: path.dirname(packagePath), isProduction: true }).then(() => { + console.log(chalk.green('助手依赖安装完成')); + }); } return { create, diff --git a/assistant/src/services/init/package.json b/assistant/src/services/init/package.json index 6648109..cd3746f 100644 --- a/assistant/src/services/init/package.json +++ b/assistant/src/services/init/package.json @@ -13,6 +13,7 @@ "dependencies": { "@kevisual/router": "latest", "@kevisual/use-config": "latest", + "@kevisual/query": "latest", "ioredis": "latest", "minio": "latest", "pg": "latest",