Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 0d9579b

Browse files
committed
perf(plugin): plugin use meta to speed up
1 parent 0a0b3ab commit 0d9579b

8 files changed

Lines changed: 112 additions & 46 deletions

File tree

packages/framework-core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555
"figlet": "^1.4.0",
5656
"fs-extra": "^8.1.0",
5757
"gradient-string": "^1.2.0",
58+
"https-proxy-agent": "^5.0.0",
5859
"inquirer": "^7.1.0",
5960
"js-yaml": "^3.14.0",
6061
"lodash.merge": "^4.6.2",
62+
"package-json": "^6.5.0",
6163
"progress": "^2.0.3",
6264
"terminal-link": "^2.1.1",
6365
"winston": "^3.2.1"

packages/framework-core/src/config/resolve-config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,10 @@ async function resolveRcConfig(
224224
let projectName = config?.framework?.name;
225225
let originProjectInfo;
226226

227-
logger.debug('process.env', process.env);
228-
229227
// 如果是云端构建,addon 等信息环境变量中读取,配置优先读取本地,再读取环境变量中的信息
230228
if (process.env.CLOUDBASE_CIID) {
231229
logger.debug('云端构建场景');
230+
logger.debug('process.env', process.env);
232231
const cloudRcJSON = jsonParse(process.env.TCB_RC_JSON);
233232

234233
extraData = getCIProjectInfo();

packages/framework-core/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export class CloudBaseFrameworkCore {
223223
*/
224224
async run(module?: string, params?: CommandParams) {
225225
const logger = getLogger();
226-
logger.debug('run', module, params);
226+
logger.debug('run', module || '', params || '');
227227
await this.pluginManager.run(module, params?.runCommandKey);
228228
}
229229

@@ -235,7 +235,7 @@ export class CloudBaseFrameworkCore {
235235
*/
236236
async compile(module?: string, params?: any) {
237237
const logger = getLogger();
238-
logger.debug('compile', module, params);
238+
logger.debug('compile', module || '', params || '');
239239
await this.hooks.callHook('preDeploy');
240240
await this._compile(module);
241241
}
@@ -247,7 +247,7 @@ export class CloudBaseFrameworkCore {
247247
*/
248248
async deploy(module?: string, params?: any) {
249249
const logger = getLogger();
250-
logger.debug('deploy', module, params);
250+
logger.debug('deploy', module || '', params || '');
251251
await this.hooks.callHook('preDeploy');
252252
await this._compile(module);
253253
await this.samManager.install(this.ciId, async (extensionId) => {

packages/framework-core/src/plugin-manager/index.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import fs from 'fs';
1313

1414
const corePackageInfo = require('../../package');
1515

16-
import { install } from './pkg-install';
16+
import { npmInstallWithCheck } from './pkg-install';
1717
import { emoji } from '../utils/emoji';
1818
import { Config } from '../types';
1919
import Context from '../context';
@@ -68,11 +68,7 @@ export default class PluginManager {
6868
* @param id
6969
*/
7070
async init(id?: string) {
71-
try {
72-
await this.pluginInstallPromise;
73-
} catch (e) {
74-
this.context.logger.debug(e);
75-
}
71+
await this.pluginInstallPromise;
7672

7773
return this.callPluginHook('init', {
7874
id,
@@ -195,27 +191,13 @@ export default class PluginManager {
195191

196192
let PluginCode: Plugin | undefined;
197193

198-
try {
199-
await this.pluginInstallPromise;
200-
} catch (e) {
201-
this.context.logger.error(e);
202-
throw new Error(
203-
`CloudBase Framework: can't install plugin npm package '${pluginData.name}'`
204-
);
205-
}
194+
await this.pluginInstallPromise;
206195

207-
try {
208-
PluginCode = require(path.join(
209-
this.pluginRegistry,
210-
'node_modules',
211-
pluginData.name
212-
)).plugin;
213-
} catch (e) {
214-
this.context.logger.error(e);
215-
throw new Error(
216-
`CloudBase Framework: can't find plugin '${pluginData.name}'`
217-
);
218-
}
196+
PluginCode = require(path.join(
197+
this.pluginRegistry,
198+
'node_modules',
199+
pluginData.name
200+
)).plugin;
219201

220202
if (!PluginCode) {
221203
this.context.logger.error(
@@ -249,7 +231,7 @@ export default class PluginManager {
249231
*/
250232
private async installPackage(packageInfo: Record<string, string>) {
251233
this.context.logger.info(`${emoji('📦')} install plugins`);
252-
await install(
234+
await npmInstallWithCheck(
253235
{
254236
...packageInfo,
255237
},

packages/framework-core/src/plugin-manager/pkg-install.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,94 @@
55
*
66
* Please refer to license text included with this package for license details.
77
*/
8+
import path from 'path';
9+
import packageJson from 'package-json';
10+
import httpsProxyAgent from 'https-proxy-agent';
11+
import { getProxy } from '@cloudbase/toolbox';
12+
import getLogger from '../logger';
13+
814
import { spawnPromise } from '../utils/spawn';
915

16+
export async function npmInstallWithCheck(
17+
packageInfo: Record<string, string>,
18+
options?: Record<string, any>
19+
) {
20+
let needUpdatePackageInfo: Record<string, string> = {};
21+
const logger = getLogger();
22+
23+
await Promise.all(
24+
Object.entries(packageInfo).map(async ([name, version]) => {
25+
const startTime = new Date();
26+
const checkResult = await checkNeedUpdate(
27+
name,
28+
version,
29+
options?.cwd || process.cwd()
30+
);
31+
32+
if (checkResult.isNeedUpdate) {
33+
needUpdatePackageInfo[name] = checkResult.resolvedVersion;
34+
}
35+
logger.debug(
36+
`resolve plugin ${name}@${version}: use ${(
37+
(new Date().valueOf() - startTime.valueOf()) /
38+
1000
39+
).toFixed(2)} s, mismatch:${
40+
checkResult.isNeedUpdate
41+
}, resolved version:${checkResult.resolvedVersion}`
42+
);
43+
})
44+
);
45+
46+
const args = ['install'];
47+
const packageList = getPackageList(needUpdatePackageInfo);
48+
49+
if (packageList.length === 0) {
50+
return;
51+
}
52+
53+
const npmOptions = ['--no-audit', '--progress=false'];
54+
// 支持node8
55+
return spawnPromise('npm', [...args, ...packageList, ...npmOptions], {
56+
cwd: options?.cwd || process.cwd(),
57+
});
58+
}
59+
60+
async function checkNeedUpdate(name: string, version: string, cwd: string) {
61+
const isResolvedVersion = /^\d/.test(version);
62+
let resolvedVersion;
63+
64+
if (isResolvedVersion) {
65+
resolvedVersion = version;
66+
} else {
67+
const proxy = getProxy();
68+
const meta = await packageJson(name, {
69+
version,
70+
...(proxy ? { agent: httpsProxyAgent(proxy) } : {}),
71+
});
72+
resolvedVersion = meta.version as string;
73+
}
74+
const localVersion = checkLocalVersion(name, cwd);
75+
return {
76+
isNeedUpdate: resolvedVersion !== localVersion,
77+
resolvedVersion: resolvedVersion,
78+
};
79+
}
80+
81+
function checkLocalVersion(name: string, cwd: string) {
82+
let version;
83+
try {
84+
const localPackageJson = require(path.join(
85+
cwd,
86+
'node_modules',
87+
name,
88+
'package.json'
89+
));
90+
version = localPackageJson.version;
91+
} catch (e) {}
92+
93+
return version;
94+
}
95+
1096
export function install(
1197
packageInfo: Record<string, string>,
1298
options?: Record<string, any>
@@ -15,7 +101,7 @@ export function install(
15101

16102
const args = ['install'];
17103

18-
const npmOptions = ['--prefer-offline','--no-audit', '--progress=false'];
104+
const npmOptions = ['--prefer-offline', '--no-audit', '--progress=false'];
19105

20106
// 支持node8
21107
return spawnPromise('npm', [...args, ...packageList, ...npmOptions], {

packages/framework-plugin-container/src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ class ContainerPlugin extends Plugin {
305305

306306
let cloudInputs = latestVersionDetail
307307
? {
308-
cpu: latestVersionDetail.Cpu,
309-
mem: latestVersionDetail.Mem,
310-
maxNum: latestVersionDetail.MaxNum,
311-
minNum: latestVersionDetail.MinNum,
312-
}
308+
cpu: latestVersionDetail.Cpu,
309+
mem: latestVersionDetail.Mem,
310+
maxNum: latestVersionDetail.MaxNum,
311+
minNum: latestVersionDetail.MinNum,
312+
}
313313
: {};
314314

315315
let modeInputs = this.inputs.mode ? MODE_INPUTS[this.inputs.mode] : {};
@@ -452,7 +452,6 @@ class ContainerPlugin extends Plugin {
452452
*/
453453
async ensurePostPay() {
454454
const res = await this.api.cloudApi.tcbService.request('DescribeEnvs');
455-
this.api.logger.debug('环境信息', res);
456455

457456
let env = res?.EnvList?.[0];
458457

packages/framework-plugin-website/src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import path from 'path';
99
import fs from 'fs';
1010
import os from 'os';
11-
import { exec } from 'child_process';
12-
import { promisify } from 'util';
1311
import merge from 'lodash.merge';
1412

1513
import { Plugin, PluginServiceApi } from '@cloudbase/framework-core';
@@ -249,7 +247,7 @@ class WebsitePlugin extends Plugin {
249247
if (command) {
250248
this.api.logger.info('running', command);
251249
await this.api.spawnPromise(command, [], {
252-
env: Object.assign({}, process.env, envVariables)
250+
env: Object.assign({}, process.env, envVariables),
253251
});
254252
}
255253

@@ -292,7 +290,7 @@ class WebsitePlugin extends Plugin {
292290
this.api.logger.info('running', command);
293291

294292
return this.api.spawnPromise(command, [], {
295-
env: Object.assign({}, process.env, envVariables)
293+
env: Object.assign({}, process.env, envVariables),
296294
});
297295
}
298296

@@ -306,7 +304,7 @@ class WebsitePlugin extends Plugin {
306304
if (fs.statSync('package.json')) {
307305
this.api.logger.info('running', command);
308306
return this.api.spawnPromise(command, [], {
309-
env: Object.assign({}, process.env, envVariables)
307+
env: Object.assign({}, process.env, envVariables),
310308
});
311309
}
312310
} catch (e) {}
@@ -317,7 +315,6 @@ class WebsitePlugin extends Plugin {
317315
*/
318316
async ensurePostPay() {
319317
const res = await this.api.cloudApi.tcbService.request('DescribeEnvs');
320-
this.api.logger.debug('环境信息', res);
321318

322319
let env = res?.EnvList?.[0];
323320

scripts/spawn.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
const { spawn } = require('child_process');
1010

11-
async function spawnPromise(command, options) {
11+
async function spawnPromise(command, options, args) {
1212
return new Promise((resolve, reject) => {
1313
const cm = spawn(
1414
command,
15+
args,
1516
Object.assign(
1617
{
1718
shell: true,

0 commit comments

Comments
 (0)