Skip to content

Commit 1a9ca65

Browse files
chore: use async/await Promises instead of .then/.catch Promises (#918)
* chore: use async/await Promises instead of .then/.catch Promises * Fixed existing incorrect test
1 parent 34ba20e commit 1a9ca65

File tree

8 files changed

+106
-83
lines changed

8 files changed

+106
-83
lines changed

lib/cli.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ async function init() {
139139
return;
140140
}
141141

142-
runYo(env).catch(error => onError(error));
142+
try {
143+
await runYo(env);
144+
} catch (error) {
145+
onError(error);
146+
}
147+
143148
return;
144149
}
145150

@@ -152,7 +157,11 @@ async function init() {
152157
console.log(chalk.red('Installed generators don\'t need the "generator-" prefix.'));
153158
console.log(`In the future, run ${generatorCommand} instead!\n`);
154159

155-
await env.run(generatorName, firstCmd.opts).catch(error => onError(error));
160+
try {
161+
await env.run(generatorName, firstCmd.opts);
162+
} catch (error) {
163+
onError(error);
164+
}
156165

157166
return;
158167
}
@@ -161,8 +170,12 @@ async function init() {
161170
// one that will be triggered by the below args. Maybe the nopt parsing
162171
// should be done internally, from the args.
163172
for (const generator of cli) {
164-
// eslint-disable-next-line no-await-in-loop
165-
await env.run(generator.args, generator.opts).catch(error => onError(error));
173+
try {
174+
// eslint-disable-next-line no-await-in-loop
175+
await env.run(generator.args, generator.opts);
176+
} catch (error) {
177+
onError(error);
178+
}
166179
}
167180
}
168181

lib/router.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ export default class Router {
3232
* Navigate to a route
3333
* @param {String} name Route name
3434
* @param {*} arg A single argument to pass to the route handler
35-
* @return {Promise} Promise this.
35+
* @return {Promise<this>}
3636
*/
37-
navigate(name, argument) {
37+
async navigate(name, argument) {
3838
if (typeof this.routes[name] === 'function') {
39-
return this.routes[name].call(null, this, argument).then(() => this);
39+
await this.routes[name].call(null, this, argument);
40+
return this;
4041
}
4142

4243
throw new Error(`No routes called: ${name}`);

lib/routes/clear-config.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ export const clearConfig = async app => {
4646
});
4747
}
4848

49-
return app.adapter.prompt([{
49+
const answer = await app.adapter.prompt([{
5050
name: 'whatNext',
5151
type: 'list',
5252
message: 'Which store would you like to clear?',
5353
choices: [
5454
generatorList,
5555
defaultChoices,
5656
].flat(),
57-
}]).then(answer => {
58-
if (answer.whatNext === 'home') {
59-
return app.navigate('home');
60-
}
57+
}]);
58+
59+
if (answer.whatNext === 'home') {
60+
return app.navigate('home');
61+
}
6162

62-
_clearGeneratorConfig(app, answer.whatNext);
63-
});
63+
_clearGeneratorConfig(app, answer.whatNext);
6464
};
6565

6666
/**

lib/routes/help.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@ import open from 'open';
44
* @param {import('../router.js').default} app
55
* @returns
66
*/
7-
export const help = async app => app.adapter.prompt([{
8-
name: 'whereTo',
9-
type: 'list',
10-
message: 'Here are a few helpful resources.\n\nI will open the link you select in your browser for you',
11-
choices: [{
12-
name: 'Take me to the documentation',
13-
value: 'http://yeoman.io/learning/',
14-
}, {
15-
name: 'View Frequently Asked Questions',
16-
value: 'http://yeoman.io/learning/faq.html',
17-
}, {
18-
name: 'File an issue on GitHub',
19-
value: 'http://yeoman.io/contributing/opening-issues.html',
20-
}, {
21-
name: 'Take me back home, Yo!',
22-
value: 'home',
23-
}],
24-
}]).then(async answer => {
7+
export const help = async app => {
8+
const answer = await app.adapter.prompt([{
9+
name: 'whereTo',
10+
type: 'list',
11+
message: 'Here are a few helpful resources.\n\nI will open the link you select in your browser for you',
12+
choices: [{
13+
name: 'Take me to the documentation',
14+
value: 'http://yeoman.io/learning/',
15+
}, {
16+
name: 'View Frequently Asked Questions',
17+
value: 'http://yeoman.io/learning/faq.html',
18+
}, {
19+
name: 'File an issue on GitHub',
20+
value: 'http://yeoman.io/contributing/opening-issues.html',
21+
}, {
22+
name: 'Take me back home, Yo!',
23+
value: 'home',
24+
}],
25+
}]);
26+
2527
if (answer.whereTo === 'home') {
2628
console.log('I get it, you like learning on your own. I respect that.');
2729
return app.navigate('home');
2830
}
2931

30-
open(answer.whereTo);
31-
});
32+
return open(answer.whereTo);
33+
};

lib/routes/home.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,29 @@ export const home = async app => {
5353
});
5454
}
5555

56-
return fullname().then(name => {
57-
const allo = (name && _.isString(name)) ? `'Allo ${name.split(' ')[0]}! ` : '\'Allo! ';
56+
const name = await fullname();
57+
const allo = (name && _.isString(name)) ? `'Allo ${name.split(' ')[0]}! ` : '\'Allo! ';
5858

59-
return app.adapter.prompt([{
60-
name: 'whatNext',
61-
type: 'list',
62-
message: `${allo}What would you like to do?`,
63-
choices: [
64-
app.adapter.separator?.('Run a generator'),
65-
generatorList,
66-
app.adapter.separator?.(),
67-
defaultChoices,
68-
app.adapter.separator?.(),
69-
].flat(),
70-
}]).then(answer => {
71-
if (answer.whatNext.method === 'run') {
72-
return app.navigate('run', answer.whatNext.generator);
73-
}
59+
const answer = await app.adapter.prompt([{
60+
name: 'whatNext',
61+
type: 'list',
62+
message: `${allo}What would you like to do?`,
63+
choices: [
64+
app.adapter.separator?.('Run a generator'),
65+
generatorList,
66+
app.adapter.separator?.(),
67+
defaultChoices,
68+
app.adapter.separator?.(),
69+
].flat(),
70+
}]);
7471

75-
if (answer.whatNext === 'exit') {
76-
return;
77-
}
72+
if (answer.whatNext.method === 'run') {
73+
return app.navigate('run', answer.whatNext.generator);
74+
}
7875

79-
return app.navigate(answer.whatNext);
80-
});
81-
});
76+
if (answer.whatNext === 'exit') {
77+
return;
78+
}
79+
80+
return app.navigate(answer.whatNext);
8281
};

lib/routes/install.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ const OFFICIAL_GENERATORS = new Set([
3232
* @param {import('../router.js').default} app
3333
* @returns
3434
*/
35-
export const install = app => app.adapter.prompt([{
36-
name: 'searchTerm',
37-
message: 'Search npm for generators:',
38-
}]).then(answers => searchNpm(app, answers.searchTerm));
35+
export const install = async app => {
36+
const answers = await app.adapter.prompt([{
37+
name: 'searchTerm',
38+
message: 'Search npm for generators:',
39+
}]);
40+
41+
return searchNpm(app, answers.searchTerm);
42+
};
3943

4044
const generatorMatchTerm = (generator, term) => `${generator.name} ${generator.description}`.includes(term);
4145
const getAllGenerators = _.memoize(() => npmKeyword('yeoman-generator'));
@@ -79,7 +83,7 @@ async function searchNpm(app, term) {
7983
* @param {import('../router.js').default} app
8084
* @returns
8185
*/
82-
function promptInstallOptions(app, choices) {
86+
async function promptInstallOptions(app, choices) {
8387
let introMessage = 'Sorry, no results matches your search term';
8488

8589
if (choices.length > 0) {
@@ -99,13 +103,13 @@ function promptInstallOptions(app, choices) {
99103
}],
100104
}];
101105

102-
return app.adapter.prompt(resultsPrompt).then(answer => {
103-
if (answer.toInstall === 'home' || answer.toInstall === 'install') {
104-
return app.navigate(answer.toInstall);
105-
}
106+
const answer = await app.adapter.prompt(resultsPrompt);
106107

107-
installGenerator(app, answer.toInstall);
108-
});
108+
if (answer.toInstall === 'home' || answer.toInstall === 'install') {
109+
return app.navigate(answer.toInstall);
110+
}
111+
112+
installGenerator(app, answer.toInstall);
109113
}
110114

111115
function installGenerator(app, packageName) {

lib/routes/update.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ function updateGenerators(app, pkgs) {
1818

1919
/**
2020
* @param {import('../router.js').default} app
21-
* @returns
2221
*/
23-
export const update = app => app.adapter.prompt([{
24-
name: 'generators',
25-
message: 'Generators to update',
26-
type: 'checkbox',
27-
validate(input) {
28-
return input.length > 0 ? true : 'Please select at least one generator to update.';
29-
},
30-
choices: Object.keys(app.generators || {}).map(key => ({
31-
name: app.generators[key].name,
32-
checked: true,
33-
})),
34-
}]).then(answer => {
22+
export const update = async app => {
23+
const answer = await app.adapter.prompt([{
24+
name: 'generators',
25+
message: 'Generators to update',
26+
type: 'checkbox',
27+
validate(input) {
28+
return input.length > 0 ? true : 'Please select at least one generator to update.';
29+
},
30+
choices: Object.keys(app.generators || {}).map(key => ({
31+
name: app.generators[key].name,
32+
checked: true,
33+
})),
34+
}]);
35+
3536
updateGenerators(app, answer.generators);
36-
});
37+
};

test/router.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ describe('Router', () => {
5656
});
5757

5858
it('throws on invalid route name', function () {
59-
assert.throws(this.router.navigate.bind(this.route, 'invalid route name'));
59+
assert.rejects(
60+
this.router.navigate.bind(this.router, 'invalid route name'),
61+
'No routes called: invalid route name',
62+
);
6063
});
6164
});
6265

0 commit comments

Comments
 (0)