Skip to content

Commit dba7200

Browse files
committed
添加同步sonar版本号配置的脚本
1 parent 089d72e commit dba7200

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modular-core",
3-
"version": "0.3.5",
3+
"version": "0.3.6",
44
"description": "JavaScript application modular support",
55
"main": "./dist/index.js",
66
"files": [
@@ -16,6 +16,7 @@
1616
"build": "babel src --out-dir dist",
1717
"lint": "eslint src/**",
1818
"test": "jest",
19+
"preinstall": "node script/sync-version.js",
1920
"prebuild": "rimraf dist/*",
2021
"prepublishOnly": "npm run build"
2122
},
@@ -27,6 +28,7 @@
2728
"@vue/eslint-config-standard": "^4.0.0",
2829
"babel-eslint": "^10.0.1",
2930
"babel-jest": "^24.1.0",
31+
"chalk": "^2.4.2",
3032
"eslint": "^5.14.1",
3133
"eslint-plugin-vue": "^5.2.2",
3234
"jest": "^24.1.0",

script/sync-version.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
// 版本号同步工具
3+
// 读取 package.json 中 version 的值,更新 sonar-project.properties 中的 sonar.projectVersion 的值
4+
const fs = require('fs')
5+
const path = require('path')
6+
const chalk = require('chalk')
7+
8+
const packagePath = path.resolve(process.cwd(), 'package.json')
9+
const sonarPath = path.resolve(process.cwd(), 'sonar-project.properties')
10+
11+
if (!fs.existsSync(packagePath)) {
12+
console.error(chalk.red(`No package.json file found in' ${process.cwd()} is this a JavaScript repo?`))
13+
process.exit(1)
14+
}
15+
16+
const packageJson = require(packagePath)
17+
18+
if (!packageJson.version) {
19+
console.error(chalk.red('package.json has no version field'))
20+
}
21+
22+
if (!fs.existsSync(sonarPath)) {
23+
console.log(chalk.yellow('skipped: sonar-project.properties'))
24+
return
25+
}
26+
const sonarFile = fs.readFileSync(sonarPath, 'utf8')
27+
28+
const sonarVersionRegex = /sonar\.projectVersion=(.*)/
29+
const match = sonarFile.match(sonarVersionRegex)
30+
if (!match) {
31+
console.error(chalk.red('sonar-project.properties file doesn\'t have a version number. Fix this and run again.'))
32+
process.exit(1)
33+
}
34+
35+
const oldSonarVersionString = match[0]
36+
const newSonarVersionString = `sonar.projectVersion=${packageJson.version}`
37+
if (oldSonarVersionString !== newSonarVersionString) {
38+
fs.writeFileSync(sonarPath, sonarFile.replace(oldSonarVersionString, newSonarVersionString), 'utf8')
39+
console.log(chalk.green(`replace '${oldSonarVersionString}' -> '${newSonarVersionString}'`))
40+
}

sonar-project.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sonar.projectKey=han-feng_modular
2-
sonar.projectName=Modular
3-
sonar.projectVersion=0.3.5
2+
sonar.projectName=modular-core
3+
sonar.projectVersion=0.3.6
44
sonar.language=js
55
sonar.sourceEncoding=UTF-8
66
sonar.sources=src

0 commit comments

Comments
 (0)