Skip to content

Commit

Permalink
fix: too old version check (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jul 31, 2024
1 parent 5aed372 commit eff8f7c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
57 changes: 44 additions & 13 deletions lib/version/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const { satisfies } = require('compare-versions');
const { updatePackageInWorker } = require('./updater');
const { debug, output } = require('../util');

class VersionNeedUpgradeError extends Error {
constructor(message) {
super(message);
this.name = 'VersionNeedUpgradeError';
}
}

/**
* 获取实际安装的版本
* @param {*} pkgName
Expand Down Expand Up @@ -67,6 +74,7 @@ function checkVersion(cwd, midwayVersionDir) {
);

if (!versionFile) {
// 版本文件不存在,不进行检查,要么就是当前业务用的框架版本太旧了,找不到了,要么就是框架版本太新了,版本文件还没来得及更新,这两种情况概率不高,暂时都忽略检查
return;
}

Expand All @@ -92,12 +100,21 @@ function checkVersion(cwd, midwayVersionDir) {
// eslint-disable-next-line no-empty
if (versions[pkgName].some(v => satisfies(version, v))) {
} else {
// fail
result.push({
name: pkgName,
current: version,
allow: versions[pkgName],
});
/**
* 如果组件当前版本满足 ~coreVersion 的话,说明是 patch 版本,需要中断检查去更新 midway-version
*/
if (satisfies(version, `~${coreVersion}`)) {
throw new VersionNeedUpgradeError(
`Package ${pkgName} version ${version} is too old, need to upgrade`
);
} else {
// 否则说明超出了 patch 的范畴,真的需要提示
result.push({
name: pkgName,
current: version,
allow: versions[pkgName],
});
}
}
}
}
Expand Down Expand Up @@ -188,16 +205,30 @@ function check(callback) {
debug('Downloading @midwayjs/version and skip');
callback();
} else {
// 检查版本
const result = checkVersion(cwd, join(midwayVersionPkgDir, 'package'));
if (result && result.length) {
debug(`Package version check failed, result=${result}`);
createCLIMessage(result, callback);
} else {
debug('Package version check success');
try {
// 检查版本
const result = checkVersion(cwd, join(midwayVersionPkgDir, 'package'));
if (result && result.length) {
debug(`Package version check failed, result=${result}`);
createCLIMessage(result, callback);
} else {
debug('Package version check success');
callback();
}
} catch (err) {
if (err instanceof VersionNeedUpgradeError) {
// 异步下载,不影响主进程
updatePackageInWorker('@midwayjs/version', midwayVersionPkgDir);
debug(
'Find package version need upgrade, downloading @midwayjs/version and skip check'
);
} else {
debug(`Package version check failed, error=${err}`);
}
callback();
}
}
}

exports.check = check;
exports.checkVersion = checkVersion;
27 changes: 26 additions & 1 deletion test/updater.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { updatePackageInWorker } = require('../lib/version/updater');
const { checkVersion } = require('../lib/version/check');
const { join } = require('path');
const { access } = require('fs').promises;
const { access, exists, rmdir, mkdir, writeFile, constants } = require('fs').promises;
const { existsSync } = require('fs');

describe('updater.test.js', () => {
it('should successfully download and unpack package', async () => {
Expand All @@ -18,5 +20,28 @@ describe('updater.test.js', () => {
// 检查 pkg 版本
const pkg = require(join(downloadDir, 'package', 'package.json'));
expect(pkg.version).toBe('3.6.0');

// 创建测试目录
const testDir = join(__dirname, 'ttt_test');
if (existsSync(testDir)) {
await rmdir(testDir, { recursive: true });
}
await mkdir(join(testDir, 'node_modules/@midwayjs/core'), { recursive: true });
await mkdir(join(testDir, 'node_modules/@midwayjs/decorator'), { recursive: true });
await mkdir(join(testDir, 'node_modules/@midwayjs/axios'), { recursive: true });
// add package.json
await writeFile(join(testDir, 'node_modules/@midwayjs/core/package.json'), JSON.stringify({ version: '3.6.0' }));
await writeFile(join(testDir, 'node_modules/@midwayjs/decorator/package.json'), JSON.stringify({ version: '3.6.0' }));
await writeFile(join(testDir, 'node_modules/@midwayjs/axios/package.json'), JSON.stringify({ version: '3.6.1' }));

// 测试指定版本
let err;
try {
checkVersion(testDir, join(downloadDir, 'package'));
} catch (e) {
err = e;
}

expect(err.name).toBe('VersionNeedUpgradeError');
});
});

0 comments on commit eff8f7c

Please sign in to comment.