-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli-actions.js
More file actions
226 lines (202 loc) · 7.23 KB
/
cli-actions.js
File metadata and controls
226 lines (202 loc) · 7.23 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const chalk = require('chalk')
const { inc } = require('./lib/util')
const semver = require('semver')
const {
buildGraph,
targetUpdate,
identifyOutOfSync,
commit,
summarize,
checkRootPackage,
} = require('./index')
const { confirm, checkbox, text, select, number, password } = require('./lib/inputs')
const { inverseDependencyGraph } = require('./lib/pkg')
function byKey([a], [b]) {
return a.localeCompare(b)
}
function sortByName(deps) {
return [...deps].sort((a, b) => a.name.localeCompare(b.name))
}
function printDeps(deps = [], color = chalk.blue) {
return sortByName(deps)
.map(({ name }) => color(name))
.join(' ')
}
function printChangedVersions(graph) {
Object.entries(graph)
.sort(byKey)
.forEach(
([
name,
{
version: { original, pending },
},
]) => {
if (original !== pending) {
console.log(` ${name} ${original} --> ${pending}`)
}
}
)
}
function printUpdatedVersions(targetPkg, updated) {
console.log(
`Moving ${targetPkg.name} from ${targetPkg.version.original} to ${targetPkg.version.pending} implies:`
)
printChangedVersions(updated)
}
module.exports = function getHandlers() {
async function test(action, type, rest) {
if (action === 'input') {
console.log(await confirm('This is a confirm prompt.', true))
console.log(await text('This is a question, answer it!'))
console.log(await select('Select a food', ['Taco', 'Burrito', 'Salad']))
console.log(await checkbox('Select any foods', ['Taco', 'Burrito', 'Salad']))
console.log(await number('type a number'))
console.log(await password('type a password'))
} else {
console.log('Current Args: ', chalk.blue(action), chalk.green(type), { rest })
}
}
async function graph(packages) {
const connections = buildGraph()
const dependents = inverseDependencyGraph(connections)
const list = Object.keys(connections)
.filter(packages?.length ? (name) => packages.includes(name) : () => true)
.sort()
list.forEach((packageName) => {
const deps = dependents.dependencies[packageName]
const peer = dependents.peerDependencies[packageName]
const devs = dependents.devDependencies[packageName]
console.log(
chalk.green(packageName),
'is depended on by:',
deps?.length ? '\n as a dependency: ' + printDeps(deps, chalk.cyan) : '',
peer?.length ? '\n as a peerDependency: ' + printDeps(peer, chalk.blue) : '',
devs?.length ? '\n as a devDependency: ' + printDeps(devs, chalk.grey) : '',
'\n'
)
})
}
async function sync() {
const connections = buildGraph()
const action = await select('What would you like to do?', [
'Prepare new release',
'View out of sync packages',
'Update out of sync packages',
])
if (action === 'View out of sync packages') {
const outOfSync = identifyOutOfSync(connections)
if (outOfSync.dependencies.length) {
console.log(chalk.bgBlue('dependencies'))
console.table(sortByName(outOfSync.dependencies))
}
if (outOfSync.peerDependencies.length) {
console.log(chalk.bgCyan('peerDependencies'))
console.table(sortByName(outOfSync.peerDependencies))
}
if (outOfSync.devDependencies.length) {
console.log(chalk.bgGray('devDependencies'))
console.table(sortByName(outOfSync.devDependencies))
}
if (
outOfSync.dependencies.length +
outOfSync.peerDependencies.length +
outOfSync.devDependencies.length ===
0
) {
console.log('Everything up to date.')
}
} else if (action === 'Prepare new release') {
let nextAction
let userLoop = 'release'
while (userLoop !== 'done') {
if (userLoop === 'release') {
const targetName = await select(
'Which package are you wanting to release?',
Object.keys(connections).sort()
)
const targetPkg = connections[targetName]
const promptDetail =
targetPkg.version.pending === targetPkg.version.original
? `current is ${targetPkg.version.pending}`
: `pending is ${targetPkg.version.pending}`
const targetVersion = await text(`What is the new version (${promptDetail})?`)
console.log(
chalk.green(
`Preparing update for ${targetPkg.name} to move to version ${targetVersion}`
)
)
const updated = targetUpdate(targetPkg, targetVersion, connections)
printUpdatedVersions(targetPkg, updated)
}
nextAction = await select('What would you like to do next?', [
'Accept all changes',
'Reject all changes',
'Prepare another update alongside the current pending state',
'View all versions',
])
if (nextAction === 'Prepare another update alongside the current pending state') {
userLoop = 'release'
}
if (nextAction === 'View all versions') {
console.table(summarize(connections))
userLoop = 'view-all'
}
if (nextAction === 'Accept all changes') {
const { outOfSync, updateRootPackage } = checkRootPackage(connections)
if (outOfSync) {
const updateRoot = await confirm(
'The monorepo root package.json looks like it might be out of date. Would you like to upgrade dependencies?',
true
)
if (updateRoot) {
updateRootPackage()
}
}
commit(connections)
userLoop = 'done'
}
if (nextAction === 'Reject all changes') {
console.log('Aborting...')
userLoop = 'done'
}
}
} else if (action === 'Update out of sync packages') {
const fixOutOfSync = (pkg) => {
const needsMajor = pkg.needsMajorUpdateIfConsumedFlat()
if (needsMajor) {
const { prerelease } = semver.parse(pkg.version.original)
let targetVersion = inc(pkg.version.original, 'major')
const pr = prerelease && prerelease.length ? prerelease[0] : null
if (pkg.version.original.startsWith('0')) {
targetVersion = inc(pkg.version.original, `major-pre`)
if (pr) {
targetVersion = inc(pkg.version.original, `major-pre${pr}`)
}
} else if (pr) {
targetVersion = inc(pkg.version.original, `major-${pr}`)
}
targetUpdate(pkg, targetVersion, connections)
}
}
Object.values(connections).forEach(fixOutOfSync)
Object.values(connections).forEach(fixOutOfSync)
Object.values(connections).forEach(fixOutOfSync)
console.log('The following packages will update:')
printChangedVersions(connections)
let nextAction = await select('What would you like to do next?', [
'Accept all changes',
'Reject all changes',
])
if (nextAction === 'Accept all changes') {
commit(connections)
}
if (nextAction === 'Reject all changes') {
console.log('Aborting...')
}
} else {
console.log('Not implemented yet, sorry.')
}
}
return { test, graph, sync }
}