forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_bulk.mjs
204 lines (155 loc) · 4.94 KB
/
module_bulk.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
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
196
197
198
199
200
201
202
203
204
#!/usr/bin/env zx
import fs from 'fs-extra'
const modulesDir = 'module-tmp'
const pkgJsonStr = await fs.readFile('./package.json')
const pkgJson = JSON.parse(pkgJsonStr.toString())
if (argv._[1] === 'clone') {
const answer = await question('Are you sure? This will delete a previous clone, and any local changes')
if (answer.toLowerCase() !== 'y' && answer.toLowerCase() !== 'yes') {
console.log('Cancelled')
process.exit(0)
}
console.log('Cloning all modules')
await fs.remove(modulesDir)
await fs.mkdir(modulesDir)
const ps = []
for (const [name, version] of Object.entries(pkgJson.dependencies)) {
if (name.startsWith('companion-module-')) {
const match = version.match(/^github:(.*)#(.*)$/)
if (!match) {
console.error(`Failed to match version for ${name}`)
} else {
const repoUrl = `https://github.com/${match[1]}.git`
const targetDir = `${modulesDir}/${name}`
ps.push($`git clone ${repoUrl} ${targetDir}`)
}
}
}
// Run all the clones in parallel
await Promise.all(ps)
} else if (argv._[1] === 'find-untagged-commits') {
let countWithChanges = 0
const output = []
const folders = await fs.readdir(modulesDir)
for (const folder of folders) {
const fullDir = `${modulesDir}/${folder}`
const entry = pkgJson.dependencies[folder]
if (!entry) {
output.push(`Missing module "${folder}" in package.json!`)
continue
}
const match = entry.match(/^github:(.*)#(.*)$/)
if (!match) {
output.push(`Failed to match version for ${folder}`)
continue
}
const taggedVersion = match[2]
try {
await $`git -C ${fullDir} describe --exact-match --tags HEAD`
} catch (e) {
output.push(`"${folder}" has untagged commits`)
const range = `${taggedVersion}...HEAD`
const changes = await $`git -C ${fullDir} log --pretty=oneline ${range}`
output.push(changes, '')
countWithChanges++
}
}
console.log(output.join('\n'))
console.log()
console.log(`${countWithChanges} modules have untagged commits`)
} else if (argv._[1] === 'find-changes') {
let countWithChanges = 0
const folders = await fs.readdir(modulesDir)
for (const folder of folders) {
const fullDir = `${modulesDir}/${folder}`
const changes = await $`git -C ${fullDir} status --porcelain`
if (changes.stdout.trim().length > 0) {
countWithChanges++
// console.log(changes.stdout)
// console.log()
}
}
console.log()
console.log(`${countWithChanges} modules have changes`)
} else if (argv._[1] === 'commit-all') {
const message = await question('Commit message:')
let countChanged = 0
const ps = []
const folders = await fs.readdir(modulesDir)
for (const folder of folders) {
const fullDir = `${modulesDir}/${folder}`
try {
const changes = await $`git -C ${fullDir} commit -a -m ${message}`
if (changes.stdout.trim().length > 0) {
countChanged++
console.log()
}
} catch (e) {
console.error(e)
}
}
await Promise.allSettled(ps)
console.log()
console.log(`${countChanged} modules were changed`)
} else if (argv._[1] === 'push-all') {
let countChanged = 0
const ps = []
const folders = await fs.readdir(modulesDir)
for (const folder of folders) {
const fullDir = `${modulesDir}/${folder}`
ps.push($`git -C ${fullDir} push`)
}
await Promise.allSettled(ps)
console.log()
console.log(`${countChanged} modules were changed`)
} else if (argv._[1] === 'pull-all') {
let countChanged = 0
const ps = []
const folders = await fs.readdir(modulesDir)
for (const folder of folders) {
const fullDir = `${modulesDir}/${folder}`
ps.push($`git -C ${fullDir} pull --rebase`)
}
await Promise.allSettled(ps)
console.log()
console.log(`${countChanged} modules were changed`)
} else if (argv._[1] === 'tag-all-changed') {
if (argv._[2] !== 'minor' && argv._[2] !== 'patch') {
console.error(`Must specify patch or minor`)
process.exit(1)
}
const versionArg = `--${argv._[2]}`
let countChanged = 0
// const ps = []
const output = []
const folders = await fs.readdir(modulesDir)
for (const folder of folders) {
const fullDir = `${modulesDir}/${folder}`
try {
await $`git -C ${fullDir} describe --exact-match --tags HEAD`
} catch (e) {
try {
await $`yarn --cwd ${fullDir} version ${versionArg}`
await $`git -C ${fullDir} push`
await $`git -C ${fullDir} push --tags`
const tagNameRaw = await $`git -C ${fullDir} describe --exact-match --tags HEAD`
const tagName = tagNameRaw.stdout.trim()
pkgJson.dependencies[folder] = `github:bitfocus/${folder}#${tagName}`
output.push(`Tagged "${folder}" as ${tagName}`)
countChanged++
} catch (e) {
output.push(`Failed to bump "${folder}": ${e}`)
}
}
}
// await Promise.allSettled(ps)
console.log(output.join('\n'))
console.log()
console.log(`${countChanged} modules were changed`)
console.log('Updating package.json')
await fs.writeFile('package.json', JSON.stringify(pkgJson, undefined, '\t') + '\n')
await $`yarn install`
} else {
console.error(`Unsupported operation ${argv._[1]}`)
process.exit(1)
}