Skip to content

Commit

Permalink
fix: 正确输出安装错误信息 (Tencent#397)
Browse files Browse the repository at this point in the history
* fix: 正确输出安装错误信息

* ci: 自动识别 dist-tag

Co-authored-by: alexqxxu <[email protected]>
  • Loading branch information
xqxian and alexqxxu committed Apr 10, 2022
1 parent d7820b4 commit 524112e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
CI: true
- name: Build
run: npm run build
- name: Check dist tag
# 不能使用 npm script 包裹,会污染 stdout
run: node ./scripts/check-dist-tag.js >> $GITHUB_ENV
- name: Publish
run: npm run release
env:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"bootstrap": "lerna bootstrap --no-ci",
"build": "lerna run build --ignore @feflow/cli && lerna run build --scope @feflow/cli",
"test": "lerna run test",
"release": "lerna publish from-git --yes",
"release": "lerna publish from-git dist-tag $DIST_TAG --yes",
"prepare-husky": "husky install",
"check-npm-version": "node ./scripts/check-npm-version.js",
"prepare": "npm run prepare-husky",
Expand Down
52 changes: 23 additions & 29 deletions packages/feflow-cli/src/core/native/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { getRegistryUrl, install } from '../../shared/npm';
import { getURL } from '../../shared/url';
import { copyDir } from '../../shared/fs';
import { Logger } from '../logger';

export default (ctx: Feflow) => {
ctx.commander.register('install', 'Install a devkit or plugin', async () => {
Expand All @@ -41,7 +42,7 @@ export default (ctx: Feflow) => {
try {
await installPlugin(ctx, installPluginStr, true);
} catch (e) {
ctx.logger.error(`install error: ${JSON.stringify(e)}`);
ctx.logger.error(`install error: `, e);
process.exit(2);
}
});
Expand Down Expand Up @@ -184,7 +185,7 @@ async function installNpmPlugin(ctx: Feflow, ...dependencies: string[]) {
const data = fs.readFileSync(ctx.rootPkg, 'utf-8');
json = JSON.parse(data);
} catch (e) {
ctx.logger.error(`getCurversion error: ${JSON.stringify(e)}`);
ctx.logger.error(`getCurversion error: `, e);
}

if (!json.dependencies) {
Expand All @@ -211,7 +212,7 @@ async function installNpmPlugin(ctx: Feflow, ...dependencies: string[]) {
return '';
});
} catch (err) {
ctx.logger.error(`get pkg info error ${JSON.stringify(err)}`);
ctx.logger.error(`get pkg info error: `, err);
}
if (!needInstall.length) {
return Promise.resolve();
Expand Down Expand Up @@ -315,13 +316,12 @@ async function startInstall(ctx: Feflow, pkgInfo: PkgInfo, repoPath: string, upd
if (lastVersion) {
const oldRepoPath = getRepoPath(universalModules, pkgInfo.repoName, lastVersion);
lastRepoName = toSimpleCommand(pkgInfo.repoName);
try {
const oldPlugin = resolvePlugin(ctx, oldRepoPath);
if (oldPlugin.name) {
lastRepoName = oldPlugin.name;
}
} catch (e) {}
const oldPlugin = resolvePlugin(ctx, oldRepoPath);
if (oldPlugin.name) {
lastRepoName = oldPlugin.name;
}
}

if (pkgInfo.fromType !== PkgInfo.dir) {
logger.info(`switch to version: ${pkgInfo.checkoutTag}`);
await checkoutVersion(repoPath, pkgInfo.checkoutTag, pkgInfo.lastCheckoutTag);
Expand Down Expand Up @@ -438,7 +438,7 @@ export async function installPlugin(ctx: Feflow, installPluginStr: string, isGlo
universalPkg,
universalModules,
}: {
logger: any;
logger: Logger;
universalPkg: UniversalPkg;
universalModules: string;
} = ctx;
Expand Down Expand Up @@ -466,30 +466,24 @@ export async function installPlugin(ctx: Feflow, installPluginStr: string, isGlo
const repoPath = getRepoPath(universalModules, pkgInfo.repoName, pkgInfo.installVersion);
if (pkgInfo.installVersion === LATEST_VERSION) {
if (universalPkg.isInstalled(pkgInfo.repoName, LATEST_VERSION)) {
try {
const currentVersion = await getCurrentTag(repoPath);
if (currentVersion && pkgInfo.checkoutTag === currentVersion) {
if (global) {
logger.info(
`[${pkgInfo.repoName}] the plugin version currently installed is the latest version: ${currentVersion}`,
);
}
return;
}
updateFlag = true;
if (currentVersion) {
pkgInfo.lastCheckoutTag = currentVersion;
const currentVersion = await getCurrentTag(repoPath);
if (currentVersion && pkgInfo.checkoutTag === currentVersion) {
if (global) {
logger.info(
`[${pkgInfo.repoName}] the plugin version currently installed is the latest version: ${currentVersion}`,
);
}
} catch (e) {
logger.error(JSON.stringify(e));
return;
}
updateFlag = true;
if (currentVersion) {
pkgInfo.lastCheckoutTag = currentVersion;
}
}
}
if (updateFlag) {
logger.info(`[${pkgInfo.showName()}] update the plugin to version ${pkgInfo.checkoutTag}`);
try {
resolvePlugin(ctx, repoPath).preUpgrade.runLess();
} catch (e) {}
resolvePlugin(ctx, repoPath).preUpgrade.runLess();
} else {
logger.info(`[${pkgInfo.showName()}] installing plugin`);
}
Expand Down Expand Up @@ -732,6 +726,6 @@ async function updatePlugin(ctx: Feflow, pkg: string, version: string) {
try {
await installPlugin(ctx, `${pkg}@${version}`, isGlobal);
} catch (e) {
ctx.logger.error(`[${pkg}] update failure, ${e}`);
ctx.logger.error(`[${pkg}] update failure: `, e);
}
}
11 changes: 11 additions & 0 deletions scripts/check-dist-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const semverRegExp =
/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/g;

const { GITHUB_REF_NAME: tagName } = process.env;
const matchResult = semverRegExp.exec(tagName);
let distTag = 'latest';
if (matchResult) {
[, , , , , distTag = 'latest'] = matchResult;
}
// 注意:此脚本只能输出 dist tag,不能输出其他 log,否则会污染 stdout 导致无法正常发布!
console.log(`DIST_TAG=${distTag}`);

0 comments on commit 524112e

Please sign in to comment.