-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathsnapshot.mjs
102 lines (85 loc) · 2.91 KB
/
snapshot.mjs
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
#!/usr/bin/env zx
import fs from 'node:fs'
import 'zx/globals'
$.verbose = false
if (!/pnpm/.test(process.env.npm_config_user_agent ?? ''))
throw new Error("Please use pnpm ('pnpm run snapshot') to generate snapshots!")
const featureFlags = [
'bare',
'typescript',
'jsx',
'router',
'pinia',
'vitest',
'cypress',
'playwright',
'nightwatch',
'eslint',
]
const featureFlagsDenylist = [
['cypress', 'playwright'],
['playwright', 'nightwatch'],
['cypress', 'nightwatch'],
['cypress', 'playwright', 'nightwatch'],
]
// The following code & comments are generated by GitHub CoPilot.
function fullCombination(arr) {
const combinations = []
// for an array of 5 elements, there are 2^5 - 1= 31 combinations
// (excluding the empty combination)
// equivalent to the following:
// [0, 0, 0, 0, 1] ... [1, 1, 1, 1, 1]
// We can represent the combinations as a binary number
// where each digit represents a flag
// and the number is the index of the flag
// e.g.
// [0, 0, 0, 0, 1] = 0b0001
// [1, 1, 1, 1, 1] = 0b1111
// Note we need to exclude the empty combination in our case
for (let i = 1; i < 1 << arr.length; i++) {
const combination = []
for (let j = 0; j < arr.length; j++) {
if (i & (1 << j)) {
combination.push(arr[j])
}
}
combinations.push(combination)
}
return combinations
}
let flagCombinations = fullCombination(featureFlags)
flagCombinations.push(['default'], ['bare', 'default'], ['eslint-with-prettier'])
// `--with-tests` are equivalent of `--vitest --cypress`
// Previously it means `--cypress` without `--vitest`.
// Here we generate the snapshots only for the sake of easier comparison with older templates.
// They may be removed in later releases.
const withTestsFlags = fullCombination(['typescript', 'jsx', 'router', 'pinia']).map((args) => [
...args,
'with-tests',
])
withTestsFlags.push(['with-tests'])
flagCombinations.push(...withTestsFlags)
const playgroundDir = path.resolve(__dirname, '../playground/')
cd(playgroundDir)
// remove all previous combinations
for (const flags of flagCombinations) {
const projectName = flags.join('-')
console.log(`Removing previously generated project ${projectName}`)
fs.rmSync(projectName, { recursive: true, force: true })
}
// Filter out combinations that are not allowed
flagCombinations = flagCombinations
.filter(
(combination) =>
!featureFlagsDenylist.some((denylist) =>
denylist.every((flag) => combination.includes(flag)),
),
)
// `--bare` is a supplementary flag and should not be used alone
.filter((combination) => !(combination.length === 1 && combination[0] === 'bare'))
const bin = path.posix.relative('../playground/', '../outfile.cjs')
for (const flags of flagCombinations) {
const projectName = flags.join('-')
console.log(`Creating project ${projectName}`)
await $`node ${[bin, projectName, ...flags.map((flag) => `--${flag}`), '--force']}`
}