UNPKG

3.04 kBJavaScriptView Raw
1'use strict';
2
3const Fotno = require('fotno');
4
5Fotno.FotnoCommand.addFileNameToSkipForSetControllerCallsites('/fontoxml-development-tools/src/FdtCommand.js');
6
7/**
8 * A FDT custom version of Fotno#FotnoCommand that has extra options for checking Fonto specific commands.
9 *
10 * @augments Fotno.FotnoCommand
11 */
12class FdtCommand extends Fotno.FotnoCommand {
13 /**
14 * @constructor
15 * @param {string} name
16 * @param {function(AskNicelyRequest, SpeakSoftly)} [controller]
17 */
18 constructor (name, controller) {
19 super(name, controller);
20
21 this.setNewChildClass(FdtCommand);
22
23 this.requiredProductLicenses = [];
24 this.requiresEditorRepository = false;
25 this.requiresToValidateLicenseFile = false;
26
27 this.addPreController(this._preController.bind(this));
28 }
29
30 /**
31 * Pre-controller which performs optional checks before executing the actual controller.
32 *
33 * @param {Object} req
34 * @param {Object} res
35 *
36 * @return {Promise}
37 */
38 _preController (req, res) {
39 let preControllerPromise = Promise.resolve();
40
41 if (this.requiresEditorRepository) {
42 preControllerPromise = preControllerPromise.then(() => {
43 req.fdt.editorRepository.throwIfNotInsideEditorRepository();
44 });
45 }
46
47 if (this.requiresToValidateLicenseFile) {
48 preControllerPromise = preControllerPromise.then(() => {
49 const destroySpinner = res.spinner('Validating license...');
50
51 return req.fdt.license.validateAndUpdateLicenseFile()
52 .then(result => {
53 destroySpinner();
54 return result;
55 })
56 .catch(error => {
57 destroySpinner();
58 throw error;
59 });
60 });
61 }
62
63 if (this.requiredProductLicenses.length > 0) {
64 preControllerPromise = preControllerPromise.then(() => {
65 const destroySpinner = res.spinner('Checking required product licenses...');
66
67 try {
68 req.fdt.license.ensureProductLicenses(this.requiredProductLicenses);
69 destroySpinner();
70 }
71 catch (error) {
72 destroySpinner();
73 throw error;
74 }
75
76 return true;
77 });
78 }
79
80 return preControllerPromise;
81 }
82
83 /**
84 * Add a list of required product licenses for performing this command. If the user does not have
85 * access to a product, the command will fail.
86 *
87 * @param {Array<string>} productIds
88 *
89 * @return {FdtCommand}
90 */
91 addRequiredProductLicenses (productIds) {
92 this.requiredProductLicenses = this.requiredProductLicenses.concat(productIds);
93
94 return this;
95 }
96
97 /**
98 * If set, check the license for validity online before executing the command, fail
99 * otherwise.
100 *
101 * @param {boolean} [validateLicense=true]
102 *
103 * @return {FdtCommand}
104 */
105 setRequiresLicenseValidation (validateLicense = true) {
106 this.requiresToValidateLicenseFile = !!validateLicense;
107
108 return this;
109 }
110
111 /**
112 * If set, check if running from an editor repository before executing the command, fail
113 * otherwise.
114 *
115 * @param {boolean} [value=true]
116 *
117 * @return {FdtCommand}
118 */
119 setRequiresEditorRepository (value = true) {
120 this.requiresEditorRepository = !!value;
121
122 return this;
123 }
124}
125
126module.exports = FdtCommand;