forked from transloadit/uppy
/
after-version-bump.js
executable file
·106 lines (89 loc) · 3.06 KB
/
after-version-bump.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
// Called by the `version` npm script.
// This is run _after_ lerna updates the version numbers,
// but _before_ it commits, so we have time to update the
// version numbers throughout the repo and add it to the
// release commit.
// After updating version numbers, this runs a full
// IS_RELEASE_BUILD=1 build, so that the version numbers
// are properly embedded in the JS bundles.
// NOTE this _amends_ the previous commit, which should
// already be a "Release" commit generated by bin/release.
const { spawn } = require('child_process')
const { readFile, writeFile } = require('fs/promises')
const once = require('events.once')
const globby = require('globby')
async function replaceInFile (filename, replacements) {
let content = await readFile(filename, 'utf8')
for (const [rx, replacement] of replacements) {
content = content.replace(rx, replacement)
}
await writeFile(filename, content, 'utf8')
}
async function updateVersions (files, packageName) {
const { version } = require(`../packages/${packageName}/package.json`)
// uppy → uppy
// @uppy/robodog → uppy/robodog
const urlPart = packageName === 'uppy' ? packageName : packageName.slice(1)
const replacements = new Map([
[RegExp(`${urlPart}/v\\d+\\.\\d+\\.\\d+\\/`, 'g'), `${urlPart}/v${version}/`],
// maybe more later
])
console.log('replacing', replacements, 'in', files.length, 'files')
for (const f of files) {
await replaceInFile(f, replacements)
}
}
async function gitAdd (files) {
const git = spawn('git', ['add', ...files], { stdio: 'inherit' })
const [exitCode] = await once(git, 'exit')
if (exitCode !== 0) {
throw new Error(`git add failed with ${exitCode}`)
}
}
// Run the build as a release build (that inlines version numbers etc.)
async function npmRunBuild () {
const npmRun = spawn('yarn', ['run', 'build'], {
stdio: 'inherit',
env: {
...process.env,
FRESH: true, // force rebuild everything
IS_RELEASE_BUILD: true,
},
})
const [exitCode] = await once(npmRun, 'exit')
if (exitCode !== 0) {
throw new Error(`yarn run build failed with ${exitCode}`)
}
}
async function main () {
if (process.env.ENDTOEND === '1') {
console.log('Publishing for e2e tests, skipping version number sync.')
process.exit(0)
}
const files = await globby([
'README.md',
'BUNDLE-README.md',
'examples/**/*.html',
'packages/*/README.md',
'packages/@uppy/*/README.md',
'website/src/docs/**',
'website/src/examples/**',
'website/themes/uppy/layout/**',
'!**/node_modules/**',
])
await updateVersions(files, 'uppy')
await updateVersions(files, '@uppy/robodog')
await updateVersions(files, '@uppy/locales')
// gitignored files were updated for the npm package, but can't be updated
// on git.
const isIgnored = await globby.gitignore()
await gitAdd(files.filter((filename) => !isIgnored(filename)))
await npmRunBuild()
}
main().catch((err) => {
console.error(err.stack)
process.exit(1)
})