generated from remal-github-actions/template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-node-version-in-files.js
86 lines (70 loc) · 2.88 KB
/
update-node-version-in-files.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import * as fs from 'fs'
const encoding = 'utf8'
const nodeVersionString = process.argv[2]
const nodeVersion = parseInt(nodeVersionString)
if (isNaN(nodeVersion)) {
throw new Error(`Node.js major version must be passed as a script parameter: ${nodeVersionString}`)
}
function readJsonFile(path) {
const content = fs.readFileSync(path, encoding)
return JSON.parse(content)
}
function writeJsonFile(path, json) {
let content = JSON.stringify(json, null, 2) + '\n'
content = content.replaceAll(/(\r\n)|(\n\r)|(\r)/g, '\n')
fs.writeFileSync(path, content, encoding)
}
;(function() {
const currentVer = parseInt(
fs.readFileSync('.nvmrc', encoding).trim().replace(/^v/, '')
)
if (currentVer !== nodeVersion) {
fs.writeFileSync('.nvmrc', `v${nodeVersion}`, encoding)
}
})()
;(function() {
const json = readJsonFile('package.json')
json.engines = json.engines || {}
json.engines.node = `>=${nodeVersion}`
if (json.devDependencies[`@tsconfig/node${nodeVersion}`] == null) {
json.devDependencies[`@tsconfig/node${nodeVersion}`] = '1.0.0'
}
if (json.devDependencies['@types/node'] == null) {
json.devDependencies['@types/node'] = `${nodeVersion}.0.0`
}
;[
'dependencies',
'devDependencies',
].forEach(dependenciesKey => {
const dependencies = json[dependenciesKey]
if (dependencies == null) return
Object.entries(dependencies).forEach(([dependency, version]) => {
if (dependency.startsWith('@tsconfig/node') && dependency !== `@tsconfig/node${nodeVersion}`) {
delete dependencies[dependency]
}
})
Object.entries(dependencies).forEach(([dependency, version]) => {
if (dependency === '@types/node' && !version.startsWith(`${nodeVersion}.`)) {
dependencies[dependency] = `${nodeVersion}.0.0`
}
})
})
writeJsonFile('package.json', json)
})()
;(function() {
const json = readJsonFile('tsconfig.json')
if ((json.extends || '').startsWith('@tsconfig/node')) {
json.extends = `@tsconfig/node${nodeVersion}/tsconfig.json`
}
writeJsonFile('tsconfig.json', json)
})()
;(function() {
const content = fs.readFileSync('.github/renovate.json5', encoding)
const modifiedContent = content.replace(
/([ ]*)\/\/\s*\$\$\$sync-with-template-modifiable:\s*(force: \{\s*)?constraints\s*\$\$\$[\s\S]*?\/\/\s*\$\$\$sync-with-template-modifiable-end\s*\$\$\$/,
`$1// $$$$$sync-with-template-modifiable: constraints $$$$$\n$1constraints: {\n$1$1node: "^${nodeVersion}.9999.9999",\n$1},\n$1force: {\n$1$1constraints: {\n$1$1$1node: "^${nodeVersion}.9999.9999",\n$1$1},\n$1},\n$1// $$$$$sync-with-template-modifiable-end$$$$$`
)
if (modifiedContent !== content) {
fs.writeFileSync('.github/renovate.json5', modifiedContent, encoding)
}
})()