Skip to content

Commit 3ca427b

Browse files
author
月满
committed
feat: 校验 tnpm 包发布权限
1 parent 26a4459 commit 3ca427b

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/built-in-plugins/command-publish/plugin/run-publish.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { isWorkingTreeClean, getCurrentBranchName } from '../../../utils/git-ope
1616
import { logFatal, logInfo, spinner, logText } from '../../../utils/log';
1717
import { getMonoAndNpmDepsOnce, DepMap } from '../../../utils/packages';
1818
import { ProjectConfig } from '../../../utils/define';
19+
import { isOwner } from '../../../utils/npm';
1920

2021
export const publish = async (options: PublishOption) => {
2122
const currentBranchName = options.branch ? options.branch : await getCurrentBranchName();
@@ -61,6 +62,7 @@ export const publish = async (options: PublishOption) => {
6162
!options.commitOnly && (await buildDeclaration());
6263

6364
if (installAllPrompt.installAll) {
65+
await authPublish([pri.projectPackageJson.name, ...depMonoPackages.map(v => v.packageJson.name)])
6466
for (const eachPackage of depMonoPackages) {
6567
await publishByPackageName(eachPackage.name, options, depMap, isDevelopBranch, currentBranchName);
6668
}
@@ -69,7 +71,7 @@ export const publish = async (options: PublishOption) => {
6971
// eslint-disable-next-line no-unused-expressions
7072
!options.commitOnly && (await buildDeclaration());
7173
}
72-
74+
await authPublish([pri.projectPackageJson.name])
7375
await publishByPackageName(currentSelectedSourceType, options, depMap, isDevelopBranch, currentBranchName);
7476

7577
await fs.remove(path.join(pri.projectRootPath, tempPath.dir, declarationPath.dir));
@@ -84,6 +86,33 @@ export const publish = async (options: PublishOption) => {
8486
}
8587
};
8688

89+
async function authPublish(packageNames: string[]) {
90+
let name: string;
91+
try {
92+
const nameRet = execSync('tnpm whoami');
93+
name = nameRet.toString().trim();
94+
} catch (error) {
95+
logFatal(error);
96+
}
97+
const failedPkgSet = new Set<string>();
98+
const checkOwner = (uName: string, pName: string) =>
99+
new Promise((res, rej) => {
100+
isOwner(uName, pName)
101+
.then(v => {
102+
if (!v) {
103+
failedPkgSet.add(pName);
104+
}
105+
res(v);
106+
})
107+
.catch(e => rej(e));
108+
});
109+
const pkgsP = packageNames.map(p => checkOwner(name, p));
110+
await Promise.all(pkgsP);
111+
if (failedPkgSet.size > 0) {
112+
logFatal(`以下 npm 包无权限发布 \n ${Array.from(failedPkgSet).join('\n')}`);
113+
}
114+
}
115+
87116
async function publishByPackageName(
88117
sourceType: string,
89118
options: PublishOption,

0 commit comments

Comments
 (0)