Skip to content

Commit 59b54e4

Browse files
committed
chore(Scripts): speed up the process to get latest version when generate changelog
1 parent ac5f392 commit 59b54e4

File tree

1 file changed

+12
-55
lines changed

1 file changed

+12
-55
lines changed

scripts/release/changelog.js

+12-55
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const path = require('path');
22
const fs = require('fs-extra');
33
const semver = require('semver');
4-
const request = require('request');
54
const inquirer = require('inquirer');
65
const conventionalChangelog = require('conventional-changelog');
76
const config = require('conventional-changelog-alifd');
7+
const { execSync } = require('child_process');
88
const { logger } = require('../utils');
99

1010
const changelogPath = 'CHANGELOG.md';
@@ -16,15 +16,10 @@ module.exports = function* changelog() {
1616

1717
const packageInfo = require(packagePath);
1818

19-
const npmInfo = yield getRemotePkgInfo();
19+
// const npmInfo = yield getRemotePkgInfo();
2020

21-
const npmVersion = npmInfo['dist-tags'].latest;
22-
23-
logger.success(
24-
`[提示] [local:${
25-
packageInfo.version
26-
}] [npm:${npmVersion}] 请为本次提交指定新的版本号:`
27-
);
21+
const npmVersion = execSync(`npm show ${packageInfo.name} version`).toString();
22+
logger.success(`[提示] [local:${packageInfo.version}] [npm:${npmVersion}] 请为本次提交指定新的版本号:`);
2823

2924
const current = yield inquirer.prompt([
3025
{
@@ -34,9 +29,7 @@ module.exports = function* changelog() {
3429
message: '请输入待发布的版本号:',
3530
validate: function(value) {
3631
if (!semver.valid(value) || semver.lte(value, npmVersion)) {
37-
return logger.warn(
38-
'请输入正确的版本号,并且大于基线版本号!'
39-
);
32+
return logger.warn('请输入正确的版本号,并且大于基线版本号!');
4033
}
4134
return true;
4235
},
@@ -47,30 +40,23 @@ module.exports = function* changelog() {
4740

4841
yield fs.writeJson(packagePath, packageInfo, { spaces: 2 });
4942

50-
logger.success(
51-
`[提示] 回写版本号 ${packageInfo.version} 到 package.json success`
52-
);
43+
logger.success(`[提示] 回写版本号 ${packageInfo.version} 到 package.json success`);
5344

5445
logger.info(`正在生成 ${changelogPath} 文件,请稍等几秒钟...`);
5546

5647
conventionalChangelog({
5748
config,
5849
}).on('data', chunk => {
59-
const log = chunk
60-
.toString()
61-
.replace(/(\n## [.\d\w]+ )\(([\d-]+)\)\n/g, (all, s1, s2) => {
62-
return `${s1}/ ${s2}\n`;
63-
});
50+
const log = chunk.toString().replace(/(\n## [.\d\w]+ )\(([\d-]+)\)\n/g, (all, s1, s2) => {
51+
return `${s1}/ ${s2}\n`;
52+
});
6453

6554
let changelogContent = fs.readFileSync(changelogPath, 'utf8');
6655
changelogContent = changelogContent
6756
.split('\n')
6857
.slice(1)
6958
.join('\n');
70-
fs.writeFileSync(
71-
changelogPath,
72-
`# Change Log \n\n${log}${changelogContent}`
73-
);
59+
fs.writeFileSync(changelogPath, `# Change Log \n\n${log}${changelogContent}`);
7460

7561
const lines = log.split(/\n/g);
7662
let firstIndex = -1,
@@ -90,43 +76,14 @@ module.exports = function* changelog() {
9076

9177
if (firstIndex > -1) {
9278
secondIndex = secondIndex === -1 ? lines.length : secondIndex;
93-
const latestedLog = lines
94-
.slice(firstIndex, secondIndex - 1)
95-
.join('\n');
96-
fs.writeFileSync(
97-
latestedLogPath,
98-
`# Latest Log \n\n${latestedLog}`
99-
);
79+
const latestedLog = lines.slice(firstIndex, secondIndex - 1).join('\n');
80+
fs.writeFileSync(latestedLogPath, `# Latest Log \n\n${latestedLog}`);
10081
}
10182
});
10283

10384
logger.success(`成功将 ${changelogPath} 文件生成到 ${cwd} 目录下`);
10485
};
10586

106-
function getRemotePkgInfo() {
107-
return new Promise(function(resolve) {
108-
const requestUrl = 'https://registry.npmjs.org/@alifd/next';
109-
110-
try {
111-
request(
112-
{
113-
url: requestUrl,
114-
timeout: 5000,
115-
json: true,
116-
},
117-
function(error, response, body) {
118-
resolve(body);
119-
}
120-
);
121-
} catch (err) {
122-
logger.error(
123-
'Failed to reach: https://registry.npmjs.org/@alifd/next'
124-
);
125-
resolve();
126-
}
127-
});
128-
}
129-
13087
/**
13188
* 升级版本号,如果是非语义化版本号,则不做处理,直接返回原值
13289
*

0 commit comments

Comments
 (0)