Skip to content

Commit

Permalink
feat: update optionalDependencies package name
Browse files Browse the repository at this point in the history
  • Loading branch information
shaobeichen committed Nov 2, 2024
1 parent 49f0868 commit 1335ca6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .releaserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
'@semantic-release/exec',
{
// 这里可以运行你的自定义脚本
prepareCmd: 'node ./scripts/build.js ${nextRelease.version} && node ./scripts/release.js',
prepareCmd:
'node ./scripts/build.js ${nextRelease.version} && node ./scripts/release.js ${nextRelease.version}',
},
],
'@semantic-release/npm', // 用来更新 package.json 的,如果不需要发到 npm 可以设 npmPublish 为 false
Expand Down
8 changes: 2 additions & 6 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,18 @@ function installUsingNPM(pkg, subpath, binPath) {
try {
fs.writeFileSync(path.join(installDir, 'package.json'), '{}')

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

console.time('File moving time')
const installedBinPath = path.join(installDir, 'node_modules', pkg, subpath)
fs.renameSync(installedBinPath, binPath)
console.timeEnd('File moving time')
} catch (e) {
throw e
} finally {
removeRecursive(installDir)
}

removeRecursive(installDir)
}

/**
Expand Down
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@
"bubbletea",
"bubble"
],
"optionalDependencies": {
"@grprogress/darwin-arm64": "1.0.0",
"@grprogress/darwin-x64": "1.0.0",
"@grprogress/linux-x64": "1.0.0",
"@grprogress/win32-x64": "1.0.0"
},
"optionalDependencies": {},
"devDependencies": {
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^13.0.0",
Expand All @@ -47,4 +42,4 @@
"@semantic-release/release-notes-generator": "^14.0.1",
"semantic-release": "^24.1.1"
}
}
}
95 changes: 58 additions & 37 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
const { getPackageInfoByCurrentPlatform } = require('../utils')

try {
// npm 文件夹路径
const npmDir = path.join(__dirname, '../npm')
// 获取子文件夹
const folders = fs.readdirSync(npmDir)
function updatePackageOptionalDependencies() {
const packageJsonPath = path.join(__dirname, '../package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
// 获取当前版本号
const currentVersion = process.argv[2]
if (!process.argv[2]) {
return (packageJson.optionalDependencies = {})
}
// 更新 optionalDependencies 中的版本号
const { packages } = getPackageInfoByCurrentPlatform()
packageJson.optionalDependencies = Object.fromEntries(
packages.map((name) => [name, currentVersion]),
)
}

// 从环境变量中获取 npm token
const npmToken = process.env.NPM_TOKEN
function publishChildPackage() {
try {
// npm 文件夹路径
const npmDir = path.join(__dirname, '../npm')
// 获取子文件夹
const folders = fs.readdirSync(npmDir)

if (!npmToken) {
console.error('请确保设置 NPM_TOKEN 环境变量。')
process.exit(1)
}
// 从环境变量中获取 npm token
const npmToken = process.env.NPM_TOKEN

if (!npmToken) {
console.error('请确保设置 NPM_TOKEN 环境变量。')
process.exit(1)
}

// 遍历每个子文件夹
folders.forEach((folder) => {
const folderPath = path.join(npmDir, folder)
// 遍历每个子文件夹
folders.forEach((folder) => {
const folderPath = path.join(npmDir, folder)

// 检查是否是文件夹
const stats = fs.statSync(folderPath)
if (stats.isDirectory()) {
// 检查是否有 package.json
const packageJsonPath = path.join(folderPath, 'package.json')
if (fs.existsSync(packageJsonPath)) {
console.log(`正在发布 ${folder}...`)
try {
// 执行 npm publish
execSync(
`npm publish --access public --scope=@grprogress --//registry.npmjs.org/:_authToken=${npmToken} --loglevel verbose`,
{
cwd: folderPath,
stdio: 'inherit',
},
)
console.log(`发布 ${folder} 成功!`)
} catch (err) {
console.error(`发布 ${folder} 失败:`, err.message)
// 检查是否是文件夹
const stats = fs.statSync(folderPath)
if (stats.isDirectory()) {
// 检查是否有 package.json
const packageJsonPath = path.join(folderPath, 'package.json')
if (fs.existsSync(packageJsonPath)) {
console.log(`正在发布 ${folder}...`)
try {
// 执行 npm publish
execSync(
`npm publish --access public --scope=@grprogress --//registry.npmjs.org/:_authToken=${npmToken} --loglevel verbose`,
{
cwd: folderPath,
stdio: 'inherit',
},
)
console.log(`发布 ${folder} 成功!`)
} catch (err) {
console.error(`发布 ${folder} 失败:`, err.message)
}
}
}
}
})
} catch (err) {
console.error('发布发生错误:', err.message)
})
} catch (err) {
console.error('发布发生错误:', err.message)
}
}

updatePackageOptionalDependencies()
publishChildPackage()
1 change: 1 addition & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function getPackageInfoByCurrentPlatform() {
platform: os.platform(),
arch: os.arch(),
dirName: dirName,
packages: Object.values(packages),
}
}

Expand Down

0 comments on commit 1335ca6

Please sign in to comment.