-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathchoose-blueprint-updates.js
More file actions
177 lines (156 loc) · 4.88 KB
/
choose-blueprint-updates.js
File metadata and controls
177 lines (156 loc) · 4.88 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
'use strict';
const checkForBlueprintUpdates = require('./check-for-blueprint-updates');
const inquirer = require('inquirer');
const loadSafeBlueprint = require('./load-safe-blueprint');
const { defaultTo } = require('./constants');
/**
* Format the string that is displayed when user is prompted for a blueprint
*
* @param {object} blueprint - Expected to contain `name` and `version` attributes
* @param {string} latestVersion - Latest version for the blueprint
* @returns {string}
*/
function formatBlueprintLine({
blueprint,
latestVersion
}) {
return `${blueprint.name}, current: ${blueprint.version}, latest: ${latestVersion}`;
}
async function chooseBlueprint({
choicesByName,
message
}) {
let answer = await inquirer.prompt([{
type: 'list',
message,
name: 'blueprint',
choices: Object.values(choicesByName).map(({ choice }) => choice)
}]);
return choicesByName[answer.blueprint];
}
/**
* Facilitate prompting the user for which blueprint they want to update
*
* @param {string} cwd - Used in `checkForBlueprintUpdates` in order to generate url or path to it if on local disk
* @param {object} emberCliUpdateJson - Use the `blueprints` property from this
* @param {boolean} reset - Optional
* @param {boolean} compare - Optional
* @param {boolean} codemods - Optional
* @param {string} to - Optional (could be undefined).
* @returns {Promise<{blueprint: (*|{}), areAllUpToDate, to: string}>}
*/
async function chooseBlueprintUpdates({
cwd,
emberCliUpdateJson,
reset,
compare,
codemods,
to
}) {
let existingBlueprint;
let areAllUpToDate;
let { blueprints } = emberCliUpdateJson;
if (reset || compare || codemods) {
let choicesByName = blueprints.reduce((choices, blueprint) => {
let name = blueprint.packageName;
choices[name] = {
blueprint,
choice: {
name
}
};
return choices;
}, {});
let message;
if (reset) {
message = 'Which blueprint would you like to reset?';
} else if (compare) {
message = 'Which blueprint would you like to compare?';
} else {
message = 'Which blueprint would you like to run codemods for?';
}
existingBlueprint = (await chooseBlueprint({
choicesByName,
message
})).blueprint;
} else {
let blueprintUpdates = await checkForBlueprintUpdates({
cwd,
blueprints
});
areAllUpToDate = blueprintUpdates.every(blueprintUpdate => blueprintUpdate.isUpToDate);
if (areAllUpToDate) {
// eslint-disable-next-line no-console
console.log(`${blueprintUpdates.map(formatBlueprintLine).join(`
`)}
All blueprints are up-to-date!`);
} else {
let choicesByName = blueprintUpdates.reduce((choices, blueprintUpdate) => {
let name = formatBlueprintLine(blueprintUpdate);
choices[name] = {
blueprintUpdate,
choice: {
name,
disabled: blueprintUpdate.isUpToDate
}
};
return choices;
}, {});
if (typeof to === "string" && to == "latest") {
let keys = Object.keys(choicesByName);
if (
keys.length > 0 &&
choicesByName[keys[0]]?.blueprintUpdate?.latestVersion &&
choicesByName[keys[0]]?.blueprintUpdate?.blueprint
) {
to = choicesByName[keys[0]].blueprintUpdate.latestVersion;
existingBlueprint = choicesByName[keys[0]].blueprintUpdate.blueprint;
}
} else if (typeof to === "string" && to !== "latest") {
let keys = Object.keys(choicesByName);
if (
keys.length > 0 &&
choicesByName[keys[0]]?.blueprintUpdate?.blueprint
) {
existingBlueprint = choicesByName[keys[0]].blueprintUpdate.blueprint;
}
} else {
let { blueprintUpdate } = await chooseBlueprint({
choicesByName,
message: 'Blueprint updates have been found. Which one would you like to update?'
});
existingBlueprint = blueprintUpdate.blueprint;
if (typeof to !== 'string') {
let latestVersion = `${blueprintUpdate.latestVersion} (latest)`;
let answer = await inquirer.prompt([{
type: 'list',
message: 'Do you want the latest version?',
name: 'choice',
choices: [
latestVersion,
'SemVer string'
]
}]);
if (answer.choice === latestVersion) {
to = defaultTo;
} else {
answer = await inquirer.prompt([{
type: 'input',
message: 'What version?',
name: 'semver'
}]);
to = answer.semver;
}
}
}
}
}
let blueprint = loadSafeBlueprint(existingBlueprint);
return {
areAllUpToDate,
to,
blueprint
};
}
module.exports = chooseBlueprintUpdates;
module.exports.formatBlueprintLine = formatBlueprintLine;