-
-
Notifications
You must be signed in to change notification settings - Fork 78.7k
Expand file tree
/
Copy pathchange-version.mjs
More file actions
195 lines (159 loc) · 4.64 KB
/
Copy pathchange-version.mjs
File metadata and controls
195 lines (159 loc) · 4.64 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
/*!
* Script to update version number references in the project.
* Copyright 2017-2026 The Bootstrap Authors
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/
import { execFile } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import { pathToFileURL } from 'node:url'
const VERBOSE = process.argv.includes('--verbose')
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
// These are the files we only care about replacing the version
const FILES = [
'README.md',
'config.yml',
'js/src/base-component.js',
'package.js',
'scss/mixins/_banner.scss',
'site/data/docs-versions.yml'
]
const DOCS_LINK_FILES = [
'README.md'
]
const DOCS_LINK_DIRECTORIES = [
'site/src'
]
const DOCS_LINK_EXTENSIONS = new Set([
'.astro',
'.html',
'.md',
'.mdx'
])
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
function regExpQuote(string) {
return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&')
}
function regExpQuoteReplacement(string) {
return string.replace(/\$/g, '$$')
}
export function shortVersion(version) {
return version.replace(/^v/, '').split('.').slice(0, 2).join('.')
}
export function replaceDocsShortVersionLinks(string, oldVersion, newVersion) {
const oldShortVersion = shortVersion(oldVersion)
const newShortVersion = shortVersion(newVersion)
if (oldShortVersion === newShortVersion) {
return string
}
return string.replace(
new RegExp(`/docs/${regExpQuote(oldShortVersion)}(?=/)`, 'g'),
`/docs/${regExpQuoteReplacement(newShortVersion)}`
)
}
async function replaceInFile(file, transform) {
const originalString = await fs.readFile(file, 'utf8')
const newString = transform(originalString)
// No need to move any further if the strings are identical
if (originalString === newString) {
return
}
if (VERBOSE) {
console.log(`Updating ${file}`)
}
if (DRY_RUN) {
return
}
await fs.writeFile(file, newString, 'utf8')
}
async function replaceRecursively(file, oldVersion, newVersion) {
await replaceInFile(file, originalString => {
return originalString
.replace(
new RegExp(regExpQuote(oldVersion), 'g'),
regExpQuoteReplacement(newVersion)
)
// Also replace the version used by the rubygem,
// which is using periods (`.`) instead of hyphens (`-`)
.replace(
new RegExp(regExpQuote(oldVersion.replace(/-/g, '.')), 'g'),
regExpQuoteReplacement(newVersion.replace(/-/g, '.'))
)
})
}
async function findDocsLinkFiles(directory) {
const entries = await fs.readdir(directory, { withFileTypes: true })
const files = []
for (const entry of entries) {
const entryPath = path.join(directory, entry.name)
if (entry.isDirectory()) {
const nestedFiles = await findDocsLinkFiles(entryPath)
files.push(...nestedFiles)
continue
}
if (entry.isFile() && DOCS_LINK_EXTENSIONS.has(path.extname(entry.name))) {
files.push(entryPath)
}
}
return files
}
async function replaceDocsLinks(oldVersion, newVersion) {
const files = [
...DOCS_LINK_FILES,
...(
await Promise.all(
DOCS_LINK_DIRECTORIES.map(directory => findDocsLinkFiles(directory))
)
).flat()
]
await Promise.all(
files.map(file => replaceInFile(file, originalString => {
return replaceDocsShortVersionLinks(originalString, oldVersion, newVersion)
}))
)
}
function bumpNpmVersion(newVersion) {
if (DRY_RUN) {
return
}
execFile('npm', ['version', newVersion, '--no-git-tag'], { shell: true }, error => {
if (error) {
console.error(error)
process.exit(1)
}
})
}
function showUsage(args) {
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
console.error('Got arguments:', args)
process.exit(1)
}
async function main(args) {
let [oldVersion, newVersion] = args
if (!oldVersion || !newVersion) {
showUsage(args)
}
// Strip any leading `v` from arguments because
// otherwise we will end up with duplicate `v`s
[oldVersion, newVersion] = [oldVersion, newVersion].map(arg => {
return arg.startsWith('v') ? arg.slice(1) : arg
})
if (oldVersion === newVersion) {
showUsage(args)
}
bumpNpmVersion(newVersion)
try {
await Promise.all(
FILES.map(file => replaceRecursively(file, oldVersion, newVersion))
)
await replaceDocsLinks(oldVersion, newVersion)
} catch (error) {
console.error(error)
process.exit(1)
}
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
main(process.argv.slice(2))
}