forked from kentcdodds/bookshelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-variants.js
94 lines (82 loc) · 2.92 KB
/
build-variants.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
const fs = require('fs')
const path = require('path')
const pkg = require('../package.json')
const {spawnSync, getExtraCreditTitles} = require('./utils')
const branch = spawnSync('git rev-parse --abbrev-ref HEAD')
if (branch === 'main') {
console.log('Cannot run swap on main as there are no exercises.')
} else {
go()
}
function go() {
const variants = [
'exercise',
...getExtraCreditTitles().map((x, i) => i + 1),
'final',
]
const originalHomepage = pkg.homepage
function updateHomepage(pathname = '') {
const url = new URL(originalHomepage)
// must end in "/"
url.pathname = pathname.endsWith('/') ? pathname : `${pathname}/`
const newHomepage = url.toString()
fs.writeFileSync(
'package.json',
JSON.stringify({...pkg, homepage: newHomepage}, null, 2) + '\n',
)
}
const buildPath = path.join('node_modules', '.cache', 'build')
if (!fs.existsSync(buildPath)) {
fs.mkdirSync(buildPath, {recursive: true})
}
function getRedirect(baseRoute) {
baseRoute = baseRoute.endsWith('/') ? baseRoute : `${baseRoute}/`
baseRoute = baseRoute.startsWith('/') ? baseRoute : `/${baseRoute}`
return `
${baseRoute} ${baseRoute}list 302!
${baseRoute}* ${baseRoute}index.html 200
`.trim()
}
let redirects = []
const getDirname = variant =>
typeof variant === 'number' ? `extra-${variant}` : variant
function buildVariant(variant, {dirname = getDirname(variant)} = {}) {
console.log(`▶️ Starting build for "${variant}" in "${dirname}"`)
try {
updateHomepage(dirname)
spawnSync(`node ./scripts/swap ${variant}`, {stdio: 'inherit'})
spawnSync(`npx react-scripts build --profile`, {stdio: 'inherit'})
if (variant !== 'exercise') {
spawnSync(`npm run test:coverage`, {stdio: 'inherit'})
}
if (dirname) {
const dirPath = path.join('node_modules', '.cache', 'build', dirname)
if (fs.existsSync(dirPath)) {
fs.rmdirSync(dirPath, {recursive: true})
}
fs.renameSync('build', dirPath)
}
console.log(`✅ finished build for "${variant}" in "${dirname}"`)
redirects.push(getRedirect(dirname))
} catch (error) {
console.log(`🚨 error building for "${variant}" in "${dirname}"`)
throw error
}
}
for (const variant of variants) {
buildVariant(variant)
}
// build the final as the main thing with the homepage set to the root
buildVariant('final', {dirname: ''})
console.log(
'✅ all variants have been built, moving them to build and creating redirects file',
)
for (const variant of variants) {
const dirname = getDirname(variant)
const oldPath = path.join('node_modules', '.cache', 'build', dirname)
const newPath = path.join('build', dirname)
fs.renameSync(oldPath, newPath)
}
fs.writeFileSync('build/_redirects', redirects.join('\n\n'))
console.log('✅ all done. Ready to deploy')
}