feat: add install params for install denpendency
This commit is contained in:
parent
1ae123e63a
commit
e7e3e2b140
@ -88,7 +88,7 @@ app
|
|||||||
description: 'Deploy micro app in server',
|
description: 'Deploy micro app in server',
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const { id, key, force } = ctx.query?.data;
|
const { id, key, force, install } = ctx.query?.data;
|
||||||
// const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c';
|
// const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c';
|
||||||
// const key = 'envision-cli';
|
// const key = 'envision-cli';
|
||||||
// const id = '7c54a6de-9171-4093-926d-67a035042c6c';
|
// const id = '7c54a6de-9171-4093-926d-67a035042c6c';
|
||||||
@ -115,7 +115,7 @@ app
|
|||||||
await manager.removeApp(key);
|
await manager.removeApp(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const installAppData = await installApp({ path, key });
|
const installAppData = await installApp({ path, key, needInstallDeps: !!install });
|
||||||
await manager.add(installAppData.showAppInfo);
|
await manager.add(installAppData.showAppInfo);
|
||||||
ctx.body = installAppData;
|
ctx.body = installAppData;
|
||||||
if (appType === 'system-app') {
|
if (appType === 'system-app') {
|
||||||
|
@ -10,6 +10,7 @@ import { installAppFromKey } from './manager.ts';
|
|||||||
export type InstallAppOpts = {
|
export type InstallAppOpts = {
|
||||||
path?: string;
|
path?: string;
|
||||||
key?: string;
|
key?: string;
|
||||||
|
needInstallDeps?: boolean;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 检测路径是否存在
|
* 检测路径是否存在
|
||||||
@ -25,7 +26,7 @@ export const appPathCheck = async (opts: InstallAppOpts) => {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
export const installApp = async (opts: InstallAppOpts) => {
|
export const installApp = async (opts: InstallAppOpts) => {
|
||||||
const { key } = opts;
|
const { key, needInstallDeps } = opts;
|
||||||
const fileStream = await minioClient.getObject(bucketName, opts.path);
|
const fileStream = await minioClient.getObject(bucketName, opts.path);
|
||||||
const pathName = opts.path.split('/').pop();
|
const pathName = opts.path.split('/').pop();
|
||||||
const directory = path.join(appsPath, key);
|
const directory = path.join(appsPath, key);
|
||||||
@ -38,7 +39,7 @@ export const installApp = async (opts: InstallAppOpts) => {
|
|||||||
fileStream.pipe(writeStream);
|
fileStream.pipe(writeStream);
|
||||||
|
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
writeStream.on('finish', resolve);
|
writeStream.on('finish', () => resolve(true));
|
||||||
writeStream.on('error', reject);
|
writeStream.on('error', reject);
|
||||||
});
|
});
|
||||||
// 解压 tgz文件
|
// 解压 tgz文件
|
||||||
@ -47,5 +48,43 @@ export const installApp = async (opts: InstallAppOpts) => {
|
|||||||
file: filePath,
|
file: filePath,
|
||||||
cwd: extractPath,
|
cwd: extractPath,
|
||||||
});
|
});
|
||||||
|
if (needInstallDeps) {
|
||||||
|
try {
|
||||||
|
installDeps({ appPath: directory, isProduction: true, sync: true });
|
||||||
|
} catch (e) {
|
||||||
|
console.log('installDeps error, [need manual install deps]', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
return installAppFromKey(key);
|
return installAppFromKey(key);
|
||||||
};
|
};
|
||||||
|
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 params = ['i'];
|
||||||
|
if (isProduction) {
|
||||||
|
params.push('--production');
|
||||||
|
}
|
||||||
|
console.log('installDeps', appPath, params);
|
||||||
|
const syncSpawn = opts.sync ? spawnSync : spawn;
|
||||||
|
if (checkPnpm()) {
|
||||||
|
syncSpawn('pnpm', params, { cwd: appPath, stdio: 'inherit', env: process.env });
|
||||||
|
} else {
|
||||||
|
syncSpawn('npm', params, { cwd: appPath, stdio: 'inherit', env: process.env });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user