Skip to content

Commit 1335ca6

Browse files
committed
feat: update optionalDependencies package name
1 parent 49f0868 commit 1335ca6

File tree

5 files changed

+65
-51
lines changed

5 files changed

+65
-51
lines changed

.releaserc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports = {
1313
'@semantic-release/exec',
1414
{
1515
// 这里可以运行你的自定义脚本
16-
prepareCmd: 'node ./scripts/build.js ${nextRelease.version} && node ./scripts/release.js',
16+
prepareCmd:
17+
'node ./scripts/build.js ${nextRelease.version} && node ./scripts/release.js ${nextRelease.version}',
1718
},
1819
],
1920
'@semantic-release/npm', // 用来更新 package.json 的,如果不需要发到 npm 可以设 npmPublish 为 false

install.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,18 @@ function installUsingNPM(pkg, subpath, binPath) {
4343
try {
4444
fs.writeFileSync(path.join(installDir, 'package.json'), '{}')
4545

46-
console.time('File download time')
4746
child_process.execSync(`npm install ${pkg}@${versionFromPackageJSON} --save`, {
4847
cwd: installDir,
4948
stdio: 'inherit',
5049
})
51-
console.timeEnd('File download time')
5250

53-
console.time('File moving time')
5451
const installedBinPath = path.join(installDir, 'node_modules', pkg, subpath)
5552
fs.renameSync(installedBinPath, binPath)
56-
console.timeEnd('File moving time')
5753
} catch (e) {
5854
throw e
55+
} finally {
56+
removeRecursive(installDir)
5957
}
60-
61-
removeRecursive(installDir)
6258
}
6359

6460
/**

package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@
3131
"bubbletea",
3232
"bubble"
3333
],
34-
"optionalDependencies": {
35-
"@grprogress/darwin-arm64": "1.0.0",
36-
"@grprogress/darwin-x64": "1.0.0",
37-
"@grprogress/linux-x64": "1.0.0",
38-
"@grprogress/win32-x64": "1.0.0"
39-
},
34+
"optionalDependencies": {},
4035
"devDependencies": {
4136
"@semantic-release/changelog": "^6.0.3",
4237
"@semantic-release/commit-analyzer": "^13.0.0",
@@ -47,4 +42,4 @@
4742
"@semantic-release/release-notes-generator": "^14.0.1",
4843
"semantic-release": "^24.1.1"
4944
}
50-
}
45+
}

scripts/release.js

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,69 @@
11
const fs = require('fs')
22
const path = require('path')
33
const { execSync } = require('child_process')
4+
const { getPackageInfoByCurrentPlatform } = require('../utils')
45

5-
try {
6-
// npm 文件夹路径
7-
const npmDir = path.join(__dirname, '../npm')
8-
// 获取子文件夹
9-
const folders = fs.readdirSync(npmDir)
6+
function updatePackageOptionalDependencies() {
7+
const packageJsonPath = path.join(__dirname, '../package.json')
8+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
9+
// 获取当前版本号
10+
const currentVersion = process.argv[2]
11+
if (!process.argv[2]) {
12+
return (packageJson.optionalDependencies = {})
13+
}
14+
// 更新 optionalDependencies 中的版本号
15+
const { packages } = getPackageInfoByCurrentPlatform()
16+
packageJson.optionalDependencies = Object.fromEntries(
17+
packages.map((name) => [name, currentVersion]),
18+
)
19+
}
1020

11-
// 从环境变量中获取 npm token
12-
const npmToken = process.env.NPM_TOKEN
21+
function publishChildPackage() {
22+
try {
23+
// npm 文件夹路径
24+
const npmDir = path.join(__dirname, '../npm')
25+
// 获取子文件夹
26+
const folders = fs.readdirSync(npmDir)
1327

14-
if (!npmToken) {
15-
console.error('请确保设置 NPM_TOKEN 环境变量。')
16-
process.exit(1)
17-
}
28+
// 从环境变量中获取 npm token
29+
const npmToken = process.env.NPM_TOKEN
30+
31+
if (!npmToken) {
32+
console.error('请确保设置 NPM_TOKEN 环境变量。')
33+
process.exit(1)
34+
}
1835

19-
// 遍历每个子文件夹
20-
folders.forEach((folder) => {
21-
const folderPath = path.join(npmDir, folder)
36+
// 遍历每个子文件夹
37+
folders.forEach((folder) => {
38+
const folderPath = path.join(npmDir, folder)
2239

23-
// 检查是否是文件夹
24-
const stats = fs.statSync(folderPath)
25-
if (stats.isDirectory()) {
26-
// 检查是否有 package.json
27-
const packageJsonPath = path.join(folderPath, 'package.json')
28-
if (fs.existsSync(packageJsonPath)) {
29-
console.log(`正在发布 ${folder}...`)
30-
try {
31-
// 执行 npm publish
32-
execSync(
33-
`npm publish --access public --scope=@grprogress --//registry.npmjs.org/:_authToken=${npmToken} --loglevel verbose`,
34-
{
35-
cwd: folderPath,
36-
stdio: 'inherit',
37-
},
38-
)
39-
console.log(`发布 ${folder} 成功!`)
40-
} catch (err) {
41-
console.error(`发布 ${folder} 失败:`, err.message)
40+
// 检查是否是文件夹
41+
const stats = fs.statSync(folderPath)
42+
if (stats.isDirectory()) {
43+
// 检查是否有 package.json
44+
const packageJsonPath = path.join(folderPath, 'package.json')
45+
if (fs.existsSync(packageJsonPath)) {
46+
console.log(`正在发布 ${folder}...`)
47+
try {
48+
// 执行 npm publish
49+
execSync(
50+
`npm publish --access public --scope=@grprogress --//registry.npmjs.org/:_authToken=${npmToken} --loglevel verbose`,
51+
{
52+
cwd: folderPath,
53+
stdio: 'inherit',
54+
},
55+
)
56+
console.log(`发布 ${folder} 成功!`)
57+
} catch (err) {
58+
console.error(`发布 ${folder} 失败:`, err.message)
59+
}
4260
}
4361
}
44-
}
45-
})
46-
} catch (err) {
47-
console.error('发布发生错误:', err.message)
62+
})
63+
} catch (err) {
64+
console.error('发布发生错误:', err.message)
65+
}
4866
}
67+
68+
updatePackageOptionalDependencies()
69+
publishChildPackage()

utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function getPackageInfoByCurrentPlatform() {
1717
platform: os.platform(),
1818
arch: os.arch(),
1919
dirName: dirName,
20+
packages: Object.values(packages),
2021
}
2122
}
2223

0 commit comments

Comments
 (0)