Skip to content

Commit ee33715

Browse files
authored
feat: use adapter abstraction instead of inquirer (#890)
1 parent ce431db commit ee33715

17 files changed

+436
-443
lines changed

lib/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import fs from 'node:fs';
33
import path from 'node:path';
44
import process from 'node:process';
5+
import {TerminalAdapter} from '@yeoman/adapter';
56
import chalk from 'chalk';
67
import updateNotifier from 'update-notifier';
78
import yosay from 'yosay';
@@ -167,7 +168,8 @@ async function init() {
167168
}
168169

169170
function runYo(env) {
170-
const router = new Router(env);
171+
const adapter = new TerminalAdapter();
172+
const router = new Router({adapter, env});
171173
router.registerRoute('help', routes.help);
172174
router.registerRoute('update', routes.update);
173175
router.registerRoute('run', routes.run);

lib/router.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ import pkg from './utils/project-package.js';
1414
* @param {Configstore} [conf] An optional config store instance
1515
*/
1616
export default class Router {
17-
constructor(env, config) {
17+
/** @type {import('yeoman-environment').default} */
18+
env;
19+
/** @type {import('@yeoman/adapter').TerminalAdapter} */
20+
adapter;
21+
22+
constructor({adapter, env, config}) {
1823
this.routes = {};
1924
this.env = env;
25+
this.adapter = adapter ?? env.adapter;
2026
this.conf = config || new Configstore(pkg.name, {
2127
generatorRunCount: {},
2228
});

lib/routes/clear-config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import inquirer from 'inquirer';
44
import globalConfig from '../utils/global-config.js';
55
import {namespaceToName} from '../utils/namespace.js';
66

7+
/**
8+
* @param {import('../router.js').default} app
9+
* @returns
10+
*/
711
export const clearConfig = async app => {
812
const defaultChoices = [
913
{
@@ -43,7 +47,7 @@ export const clearConfig = async app => {
4347
});
4448
}
4549

46-
return inquirer.prompt([{
50+
return app.adapter.prompt([{
4751
name: 'whatNext',
4852
type: 'list',
4953
message: 'Which store would you like to clear?',

lib/routes/help.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import inquirer from 'inquirer';
21
import open from 'open';
32

4-
export const help = async app => inquirer.prompt([{
3+
/**
4+
* @param {import('../router.js').default} app
5+
* @returns
6+
*/
7+
export const help = async app => app.adapter.prompt([{
58
name: 'whereTo',
69
type: 'list',
710
message: 'Here are a few helpful resources.\n\nI will open the link you select in your browser for you',

lib/routes/home.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import inquirer from 'inquirer';
55
import {namespaceToName} from '../utils/namespace.js';
66
import globalConfig from '../utils/global-config.js';
77

8+
/**
9+
* @param {import('../router.js').default} app
10+
* @returns
11+
*/
812
export const home = async app => {
913
const defaultChoices = [{
1014
name: 'Install a generator',
@@ -53,7 +57,7 @@ export const home = async app => {
5357
return fullname().then(name => {
5458
const allo = (name && _.isString(name)) ? `'Allo ${name.split(' ')[0]}! ` : '\'Allo! ';
5559

56-
return inquirer.prompt([{
60+
return app.adapter.prompt([{
5761
name: 'whatNext',
5862
type: 'list',
5963
message: `${allo}What would you like to do?`,

lib/routes/install.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
import _ from 'lodash';
33
import chalk from 'chalk';
4-
import inquirer from 'inquirer';
54
import spawn from 'cross-spawn';
65
import sortOn from 'sort-on';
76
import figures from 'figures';
@@ -29,7 +28,11 @@ const OFFICIAL_GENERATORS = new Set([
2928
'generator-webapp',
3029
]);
3130

32-
export const install = app => inquirer.prompt([{
31+
/**
32+
* @param {import('../router.js').default} app
33+
* @returns
34+
*/
35+
export const install = app => app.adapter.prompt([{
3336
name: 'searchTerm',
3437
message: 'Search npm for generators:',
3538
}]).then(answers => searchNpm(app, answers.searchTerm));
@@ -72,6 +75,10 @@ async function searchNpm(app, term) {
7275
return promptInstallOptions(app, sortOn(packages, ['official', 'name']));
7376
}
7477

78+
/**
79+
* @param {import('../router.js').default} app
80+
* @returns
81+
*/
7582
function promptInstallOptions(app, choices) {
7683
let introMessage = 'Sorry, no results matches your search term';
7784

@@ -92,7 +99,7 @@ function promptInstallOptions(app, choices) {
9299
}],
93100
}];
94101

95-
return inquirer.prompt(resultsPrompt).then(answer => {
102+
return app.adapter.prompt(resultsPrompt).then(answer => {
96103
if (answer.toInstall === 'home' || answer.toInstall === 'install') {
97104
return app.navigate(answer.toInstall);
98105
}

lib/routes/update.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from 'chalk';
2-
import inquirer from 'inquirer';
32
import spawn from 'cross-spawn';
43

54
const successMessage = 'I\'ve just updated your generators. Remember, you can update\na specific generator with npm by running:\n'
@@ -17,7 +16,11 @@ function updateGenerators(app, pkgs) {
1716
.on('close', updateSuccess.bind(null, app));
1817
}
1918

20-
export const update = app => inquirer.prompt([{
19+
/**
20+
* @param {import('../router.js').default} app
21+
* @returns
22+
*/
23+
export const update = app => app.adapter.prompt([{
2124
name: 'generators',
2225
message: 'Generators to update',
2326
type: 'checkbox',

0 commit comments

Comments
 (0)