forked from bigcommerce/stencil-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencil-init.js
162 lines (142 loc) · 5.04 KB
/
stencil-init.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
162
require('colors');
const inquirerModule = require('inquirer');
const serverConfigModule = require('../server/config');
const StencilConfigManager = require('./StencilConfigManager');
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG, API_HOST } = require('../constants');
class StencilInit {
/**
* @param inquirer
* @param stencilConfigManager
* @param serverConfig
* @param logger
*/
constructor({
inquirer = inquirerModule,
stencilConfigManager = new StencilConfigManager(),
serverConfig = serverConfigModule,
logger = console,
} = {}) {
this._inquirer = inquirer;
this._stencilConfigManager = stencilConfigManager;
this._serverConfig = serverConfig;
this._logger = logger;
}
/**
* @param {object} cliOptions
* @param {string} cliOptions.normalStoreUrl
* @param {string} cliOptions.accessToken
* @param {number} cliOptions.port
* @returns {Promise<void>}
*/
async run(cliOptions = {}) {
const oldStencilConfig = await this.readStencilConfig();
const defaultAnswers = this.getDefaultAnswers(oldStencilConfig);
const questions = this.getQuestions(defaultAnswers, cliOptions);
const answers = await this.askQuestions(questions);
const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers, cliOptions);
await this._stencilConfigManager.save(updatedStencilConfig);
this._logger.log(
'You are now ready to go! To start developing, run $ ' + 'stencil start'.cyan,
);
}
/**
* @returns {object}
*/
async readStencilConfig() {
let parsedConfig;
try {
parsedConfig = await this._stencilConfigManager.read(true, true);
} catch (err) {
this._logger.error(
'Detected a broken stencil-cli config:\n',
err,
'\nThe file will be rewritten with your answers',
);
}
return parsedConfig || {};
}
/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string), apiHost: (string)}} stencilConfig
* @returns {{port: (number), normalStoreUrl: (string), accessToken: (string), apiHost: (string)}}
*/
getDefaultAnswers(stencilConfig) {
return {
normalStoreUrl: stencilConfig.normalStoreUrl,
accessToken: stencilConfig.accessToken,
port: stencilConfig.port || this._serverConfig.get('/server/port'),
apiHost: API_HOST,
};
}
/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliOptions
* @returns {{object[]}}
*/
getQuestions(defaultAnswers, cliOptions) {
const prompts = [];
if (!cliOptions.normalStoreUrl) {
prompts.push({
type: 'input',
name: 'normalStoreUrl',
message: "What is the URL of your store's home page?",
validate: (val) => /^https?:\/\//.test(val) || 'You must enter a URL',
default: defaultAnswers.normalStoreUrl,
});
}
if (!cliOptions.accessToken) {
prompts.push({
type: 'input',
name: 'accessToken',
message: 'What is your Stencil OAuth Access Token?',
default: defaultAnswers.accessToken,
filter: (val) => val.trim(),
});
}
if (!cliOptions.port) {
prompts.push({
type: 'input',
name: 'port',
message: 'What port would you like to run the server on?',
default: defaultAnswers.port,
validate: (val) => {
if (Number.isNaN(val)) {
return 'You must enter an integer';
}
if (val < 1024 || val > 65535) {
return 'The port number must be between 1025 and 65535';
}
return true;
},
});
}
return prompts;
}
/**
* @param {{object[]}} questions
* @returns {Promise<object>}
*/
async askQuestions(questions) {
return questions.length ? this._inquirer.prompt(questions) : {};
}
updateApiHost(stencilConfig, cliOptions) {
const host = cliOptions.apiHost || API_HOST;
console.log('Set API host to: ' + host);
return { ...stencilConfig, apiHost: host };
}
/**
* @param {object} stencilConfig
* @param {object} answers
* @param {object} cliOptions
* @returns {object}
*/
applyAnswers(stencilConfig, answers, cliOptions) {
const config = this.updateApiHost(stencilConfig, cliOptions);
return {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...config,
...cliOptions,
...answers,
};
}
}
module.exports = StencilInit;