-
Notifications
You must be signed in to change notification settings - Fork 74
/
global-prompts.js
161 lines (141 loc) · 4.37 KB
/
global-prompts.js
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
const inquirer = require('inquirer');
const { pluginIdentifier, flattenPresets } = require('@gasket/resolve');
const action = require('../action-wrapper');
const { addPluginsToContext } = require('../utils');
/**
* What is your app description?
*
* @param {CreateContext} context - Create context
* @param {function} prompt - function to prompt user
* @returns {Promise} promise
*/
async function chooseAppDescription(context, prompt) {
if (!('appDescription' in context)) {
const { appDescription } = await prompt([
{
name: 'appDescription',
message: 'What is your app description?',
type: 'input',
default: 'A basic gasket app'
}
]);
Object.assign(context, { appDescription });
}
}
/**
* What package manager do you want to use?
*
* @param {CreateContext} context - Create context
* @param {function} prompt - function to prompt user
* @returns {Promise} promise
*/
async function choosePackageManager(context, prompt) {
const packageManager =
context.packageManager ||
(
await prompt([
{
name: 'packageManager',
message: 'Which packager would you like to use?',
type: 'list',
choices: [{ name: 'npm' }, { name: 'yarn' }]
}
])
).packageManager;
const installCmd = context.installCmd || `${packageManager} install`;
const runners = {
npm: 'npx',
yarn: 'yarn'
};
const localCmd = context.localCmd || `${runners[packageManager]} gasket local`;
Object.assign(context, {
packageManager,
installCmd,
localCmd
});
}
/**
* Choose your unit test suite
*
* @param {CreateContext} context - Create context
* @param {function} prompt - function to prompt user
* @returns {Promise} promise
*/
async function chooseTestPlugin(context, prompt) {
// Combine user-provided plugins with preset-provided plugins.
const { presetInfos = [], plugins = [] } = context;
// Flatten all plugins from presets and concat short names with cli plugins
const allPlugins = flattenPresets(presetInfos)
.map((presetInfo) => presetInfo.plugins || [])
.reduce((acc, arr) => acc.concat(arr), [])
.map((pluginInfo) => pluginIdentifier(pluginInfo.name).shortName)
.concat(plugins);
const knownTestPlugins = { mocha: '@gasket/mocha', jest: '@gasket/jest', cypress: '@gasket/cypress' };
if (!('testPlugin' in context)) {
let testPlugin = Object.values(knownTestPlugins).find((p) => allPlugins.includes(p));
if ('testSuite' in context) {
testPlugin = knownTestPlugins[context.testSuite];
}
if (!testPlugin) {
({ testPlugin } = await prompt([
{
name: 'testPlugin',
message: 'Choose your unit test suite',
type: 'list',
choices: [
{ name: 'none (not recommended)', value: 'none' },
{ name: 'mocha + nyc + sinon + chai', value: '@gasket/mocha' },
{ name: 'jest', value: '@gasket/jest' },
{ name: 'cypress', value: '@gasket/cypress' }
]
}
]));
}
if (testPlugin && testPlugin !== 'none') {
addPluginsToContext([testPlugin], context);
Object.assign(context, { testPlugin });
}
}
}
/**
* Given that gasket is creating in an already existing directory, it should
* confirm with the user that it's intentionally overwriting that directory
*
* @param {CreateContext} context - Create context
* @param {function} prompt - function to prompt user
* @returns {Promise} promise
*/
async function allowExtantOverwriting(context, prompt) {
const { dest, extant } = context;
if (extant && !('destOverride' in context)) {
const { destOverride } = await prompt([
{
name: 'destOverride',
type: 'confirm',
message: `Override contents of ${dest} ?`,
default: true
}
]);
Object.assign(context, { destOverride });
}
}
const questions = [
chooseAppDescription,
choosePackageManager,
chooseTestPlugin,
allowExtantOverwriting
];
/**
* Fire off prompts for user input
*
* @param {CreateContext} context - Create context
* @returns {Promise} promise
*/
async function globalPrompts(context) {
const prompt = context.prompts ? inquirer.prompt : () => ({});
for (var fn of questions) {
await fn(context, prompt);
}
}
module.exports = action('Global prompts', globalPrompts, { startSpinner: false });
module.exports.questions = questions;