Skip to content

Commit af5d7b1

Browse files
committed
feat(publish): 重构发布流程并添加新功能
- 将 checkPackageExist 函数移至新文件 check-package.ts - 优化 publishPackage 函数,增加更多日志输出 - 在发布后添加同步到 npmmirror 的步骤 - 移除 publishPackages 中的重复同步逻辑
1 parent f826427 commit af5d7b1

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

src/check-package.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import core from '@actions/core';
2+
import type { InternalPublishPkg } from './types';
3+
import { runCommand } from './utils';
4+
5+
export function checkPackageExist(pkg: InternalPublishPkg) {
6+
try {
7+
const options = core.isDebug() ? '--verbose' : '';
8+
runCommand(`npm view ${pkg.name}@${pkg.version} ${options}`, pkg.cwd);
9+
return true;
10+
} catch (err) {
11+
return false;
12+
}
13+
}

src/publish-package.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@ import type { InternalPublishPkg, InternalPublishOptions, PublishTarget } from '
44
import core from '@actions/core';
55
import { registryRecord } from './const';
66
import { runCommand } from './utils';
7+
import { syncPackage } from './sync-package';
8+
import { checkPackageExist } from './check-package';
79

8-
export function checkPackageExist(pkg: InternalPublishPkg) {
9-
try {
10-
const options = core.isDebug() ? '--verbose' : '';
11-
runCommand(`npm view ${pkg.name}@${pkg.version} ${options}`, pkg.cwd);
12-
return true;
13-
} catch (err) {
14-
return false;
15-
}
16-
}
10+
export async function publishPackage(pkg: InternalPublishPkg, options: InternalPublishOptions) {
11+
core.info(`publish package cwd: ${pkg.cwd}`);
12+
core.info(`publish package name: ${pkg.name}`);
13+
core.info(`publish package version: ${pkg.version}`);
14+
core.info(`publish package tag: ${options.tag}`);
15+
core.info(`publish package target: ${options.target}`);
1716

18-
export function publishPackage(pkg: InternalPublishPkg, options: InternalPublishOptions) {
1917
// monorepo 下的所有 package 都参考根目录的 npmrc
2018
const npmrcFile = path.resolve('.npmrc');
2119
const backupFile = npmrcFile + '-' + Date.now();
@@ -47,11 +45,11 @@ export function publishPackage(pkg: InternalPublishPkg, options: InternalPublish
4745
};
4846

4947
const check = () => {
50-
core.info(`checking package is exist`);
48+
core.info(`checking package`);
5149
const isExist = checkPackageExist(pkg);
5250

5351
if (isExist) {
54-
core.info(`package is exists, skip publish`);
52+
core.info(`${pkg.name}@${pkg.version} is exists, skip publish`);
5553
cleanup();
5654
return true;
5755
}
@@ -81,5 +79,11 @@ export function publishPackage(pkg: InternalPublishPkg, options: InternalPublish
8179
};
8280

8381
if (check()) return;
82+
8483
publish();
84+
85+
if (options.target === 'npm' && options.syncNpmmirror) {
86+
core.info(`syncing package`);
87+
await syncPackage(pkg.name, options);
88+
}
8589
}

src/publish-packages.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export async function publishPackages(options: InternalPublishOptions) {
6363
}
6464

6565
try {
66-
core.info(`publish package: ${pkgPath} ${pkg.name}@${pkg.version} as ${options.tag} to ${options.target}`);
6766
publishPackage(
6867
{
6968
cwd: path.dirname(pkgFile),
@@ -72,11 +71,6 @@ export async function publishPackages(options: InternalPublishOptions) {
7271
},
7372
options,
7473
);
75-
76-
if (options.target === 'npm' && options.syncNpmmirror) {
77-
core.info(`sync package: ${pkgPath} ${pkg.name}@${pkg.version} to npmmirror.com`);
78-
await syncPackage(pkg.name, options);
79-
}
8074
} finally {
8175
if (options.target === 'github') {
8276
fs.writeFileSync(pkgPath, origin, 'utf-8');

0 commit comments

Comments
 (0)