Skip to content

Commit 8712f28

Browse files
author
雅影
committed
feat: support remote version
1 parent 3d960ce commit 8712f28

1 file changed

Lines changed: 46 additions & 13 deletions

File tree

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

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,56 @@ async function publishByPackageName(
221221

222222
// Publish beta version if branch is not master or develop
223223
if (!options.publishOnly) {
224+
// Generate beta version if branch is not master or develop
224225
if (options.tag === 'beta' || !isDevelopBranch) {
225-
targetPackageJson.version = (semver.inc as any)(
226-
targetPackageJson.version,
227-
'prerelease',
228-
currentBranchName
229-
.replace(/\//g, '')
230-
.replace(/\./g, '')
231-
.replace(/_/g, ''),
232-
);
226+
// fixed basicVersion to 1.0.0
227+
const basicVersion = '1.0.0';
233228

234-
await fs.outputFile(path.join(targetRoot, 'package.json'), `${JSON.stringify(targetPackageJson, null, 2)}\n`);
229+
const branchNameInVersion = currentBranchName.replace(/\//g, '').replace(/\./g, '');
235230

236-
if (!(await isWorkingTreeClean())) {
237-
await exec(`git add -A; git commit -m "upgrade ${sourceType} version to ${targetPackageJson.version}" -n`, {
238-
cwd: pri.projectRootPath,
239-
});
231+
let publishedVersions: string[] = [];
232+
try {
233+
// all of package versions
234+
const tempVersions = execSync(`${targetConfig.npmClient} view ${targetPackageJson.name} versions`);
235+
if (tempVersions) {
236+
publishedVersions = tempVersions
237+
.toString()
238+
.trim()
239+
.replace(/\n|'| |\[|\]/g, '')
240+
.split(',');
241+
} else {
242+
publishedVersions = [];
243+
}
244+
} catch (e) {
245+
publishedVersions = [];
246+
}
247+
248+
let maxBetaVersionNum = 0;
249+
250+
// 1.0.0-branchName.version
251+
const betaVersionReg = new RegExp(`\\d+\\.\\d+\\.\\d+-${branchNameInVersion}\\.\\d+`);
252+
253+
// get max beta version
254+
publishedVersions.forEach((v: string) => {
255+
if (betaVersionReg.test(v)) {
256+
const tempBetaVersion = Number(v.split(`${branchNameInVersion}.`)[1]);
257+
258+
if (maxBetaVersionNum < tempBetaVersion) {
259+
maxBetaVersionNum = tempBetaVersion;
260+
}
240261
}
262+
});
263+
264+
// basic version without branchName -> use basic version + branch name + beta version
265+
targetPackageJson.version = `${basicVersion}-${branchNameInVersion}.${maxBetaVersionNum + 1}`;
266+
267+
await fs.outputFile(path.join(targetRoot, 'package.json'), `${JSON.stringify(targetPackageJson, null, 2)}\n`);
268+
269+
if (!(await isWorkingTreeClean())) {
270+
await exec(`git add -A; git commit -m "upgrade ${sourceType} version to ${targetPackageJson.version}" -n`, {
271+
cwd: pri.projectRootPath,
272+
});
273+
}
241274
} else if (versionResult) {
242275
if (!options.semver) {
243276
const versionPrompt = await inquirer.prompt([

0 commit comments

Comments
 (0)