-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathchangeVersion.js
61 lines (53 loc) · 1.13 KB
/
changeVersion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 参考: https://github. com/long-hai-yang/change-version/blob/master/index.js
* create by d1y<[email protected]>
*/
const fs = require('fs')
const path = require('path')
const localFile = require('../package.json')
const versionWord = `versionCode`
/**
* 获取标签
*/
const getTag = ()=> {
const arr = process.argv
if (arr.length < 3) {
console.log('请传递参数..')
proces.exit(3)
}
return arr[2]
}
/**
* 修改版本号
*/
const changeVersion = (version)=> {
localFile[versionWord] = version
}
/**
* 将拿到的`json`转为字符串
*/
const version2string = (str)=> {
try {
const r = JSON.stringify(str, null, 2)
return r
} catch (error) {
console.error(error)
process.exit(2)
}
}
/**
* 写入文件
*/
const writePackageFile = (str)=> {
const dir = process.cwd()
const writeFile = path.join(dir, '../package.json')
console.log('write file: ', writeFile)
fs.writeFileSync(writeFile, str)
console.log('write file is success')
}
;(async ()=> {
const tag = getTag()
changeVersion(tag)
const filestr = version2string(localFile)
writePackageFile(filestr)
})()