1 | import ask from 'ask-nicely';
|
2 | import path from 'path';
|
3 | import { fileURLToPath, pathToFileURL } from 'url';
|
4 |
|
5 | const __filename = fileURLToPath(import.meta.url);
|
6 | const __dirname = path.dirname(__filename);
|
7 |
|
8 | const FILENAMES_TO_SKIP_FOR_SET_CONTROLLER_CALLSITES = Symbol(
|
9 | 'FILENAMES_TO_SKIP_FOR_SET_CONTROLLER_CALLSITES'
|
10 | );
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export default class FdtCommand extends ask.Command {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | constructor(commandName, controller) {
|
24 | super(commandName, controller);
|
25 |
|
26 | this._moduleRegistration = null;
|
27 |
|
28 | this.examples = [];
|
29 | this.isHelpCommand = false;
|
30 | this.longDescription = null;
|
31 |
|
32 | this.hideIfMissingRequiredProductLicenses = false;
|
33 | this.requiredProductLicenses = [];
|
34 | this.requiresEditorRepository = false;
|
35 | this.requiresToValidateLicenseFile = false;
|
36 |
|
37 | this.rawOutput = false;
|
38 |
|
39 | this.setNewChildClass(FdtCommand);
|
40 |
|
41 | this.addPreController(this._preController.bind(this));
|
42 | }
|
43 |
|
44 | static addFileNameToSkipForSetControllerCallsites(fileNameToSkip) {
|
45 | FdtCommand[FILENAMES_TO_SKIP_FOR_SET_CONTROLLER_CALLSITES].push(
|
46 | path.join(fileNameToSkip)
|
47 | );
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | getModuleRegistration() {
|
56 | let command = this;
|
57 | while (command) {
|
58 | if (command._moduleRegistration) {
|
59 | return command._moduleRegistration;
|
60 | }
|
61 | command = command.parent;
|
62 | }
|
63 | return null;
|
64 | }
|
65 |
|
66 | _createLazyLoadController(controllerSource) {
|
67 | if (!path.isAbsolute(controllerSource)) {
|
68 | throw new Error(
|
69 | `Command controller "${controllerSource}" should be registered using an absolute path instead of a relative one.`
|
70 | );
|
71 | }
|
72 |
|
73 | return (...args) => {
|
74 | return import(pathToFileURL(controllerSource)).then((module) =>
|
75 | module.default(...args)
|
76 | );
|
77 | };
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | setController(controller) {
|
88 | if (typeof controller === 'string') {
|
89 | controller = this._createLazyLoadController(controller);
|
90 | }
|
91 |
|
92 | return super.setController(controller);
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | addExample(caption, content) {
|
105 | this.examples.push({
|
106 | caption,
|
107 | content,
|
108 | });
|
109 |
|
110 | return this;
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | addHiddenCommand(commandName, controller) {
|
122 | const addedCommand = super.addCommand(commandName, controller);
|
123 | addedCommand.hidden = true;
|
124 | return addedCommand;
|
125 | }
|
126 |
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | addHiddenOption(long, short, description, required) {
|
138 | super.addOption(long, short, description, required);
|
139 | const addedOption = this.options[this.options.length - 1];
|
140 | addedOption.hidden = true;
|
141 | return this;
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | setLongDescription(description) {
|
151 | this.longDescription = description;
|
152 |
|
153 | return this;
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | setAsHelpCommand(enabled) {
|
165 | this.isHelpCommand = enabled !== undefined ? !!enabled : true;
|
166 |
|
167 | return this;
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 | getLongName() {
|
176 | return (this.parent ? [this.parent.getLongName()] : [])
|
177 | .concat([this.name])
|
178 | .concat(this.parameters.map((parameter) => `<${parameter.name}>`))
|
179 | .join(' ');
|
180 | }
|
181 |
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | _preController(req, res) {
|
191 | let preControllerPromise = Promise.resolve();
|
192 |
|
193 | if (this.requiresEditorRepository) {
|
194 | preControllerPromise = preControllerPromise.then(() => {
|
195 | req.fdt.editorRepository.throwIfNotInsideEditorRepository();
|
196 | });
|
197 | }
|
198 |
|
199 | if (this.requiresToValidateLicenseFile) {
|
200 | preControllerPromise = preControllerPromise.then(() => {
|
201 | const destroySpinner = res.spinner('Validating license...');
|
202 |
|
203 | return req.fdt.license
|
204 | .validateAndUpdateLicenseFile()
|
205 | .then((result) => {
|
206 | destroySpinner();
|
207 | return result;
|
208 | })
|
209 | .catch((error) => {
|
210 | destroySpinner();
|
211 | throw error;
|
212 | });
|
213 | });
|
214 | }
|
215 |
|
216 | if (this.requiredProductLicenses.length > 0) {
|
217 | preControllerPromise = preControllerPromise.then(() => {
|
218 | const destroySpinner = res.spinner(
|
219 | 'Checking required product licenses...'
|
220 | );
|
221 |
|
222 | try {
|
223 | req.fdt.license.ensureProductLicenses(
|
224 | this.requiredProductLicenses
|
225 | );
|
226 | destroySpinner();
|
227 | } catch (error) {
|
228 | destroySpinner();
|
229 | throw error;
|
230 | }
|
231 |
|
232 | return true;
|
233 | });
|
234 | }
|
235 |
|
236 | return preControllerPromise;
|
237 | }
|
238 |
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | addRequiredProductLicenses(productIds) {
|
248 | this.requiredProductLicenses =
|
249 | this.requiredProductLicenses.concat(productIds);
|
250 |
|
251 | return this;
|
252 | }
|
253 |
|
254 | |
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | setHideIfMissingRequiredProductLicenses(hide = true) {
|
262 | this.hideIfMissingRequiredProductLicenses = !!hide;
|
263 |
|
264 | return this;
|
265 | }
|
266 |
|
267 | |
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | setRequiresLicenseValidation(validateLicense = true) {
|
276 | this.requiresToValidateLicenseFile = !!validateLicense;
|
277 |
|
278 | return this;
|
279 | }
|
280 |
|
281 | |
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 | setRequiresEditorRepository(value = true) {
|
290 | this.requiresEditorRepository = !!value;
|
291 |
|
292 | return this;
|
293 | }
|
294 |
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | setRawOutput(value = true) {
|
310 | this.rawOutput = value;
|
311 |
|
312 | return this;
|
313 | }
|
314 | }
|
315 |
|
316 | const fdtBasename = path.basename(path.resolve(__dirname, '..'));
|
317 | FdtCommand[FILENAMES_TO_SKIP_FOR_SET_CONTROLLER_CALLSITES] = [];
|
318 | FdtCommand.addFileNameToSkipForSetControllerCallsites(
|
319 | '/ask-nicely/dist/AskNicely.js'
|
320 | );
|
321 | FdtCommand.addFileNameToSkipForSetControllerCallsites(
|
322 | `/${fdtBasename}/src/FdtCommand.js`
|
323 | );
|
324 | FdtCommand.addFileNameToSkipForSetControllerCallsites(
|
325 | `/${fdtBasename}/src/ModuleRegistrationApi.js`
|
326 | );
|
327 | FdtCommand.addFileNameToSkipForSetControllerCallsites(
|
328 | `/${fdtBasename}/src/response/FdtResponse.js`
|
329 | );
|