UNPKG

8.96 kBJavaScriptView Raw
1'use strict';
2
3var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
6
7var _Object$defineProperty2 = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
8
9_Object$defineProperty2(exports, "__esModule", {
10 value: true
11});
12
13exports.default = _default;
14
15var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-property"));
16
17var _defineProperties = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-properties"));
18
19var _getOwnPropertyDescriptors = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors"));
20
21var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"));
22
23var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));
24
25var _reduce = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/reduce"));
26
27var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));
28
29var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
30
31var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
32
33var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
34
35var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
36
37var _defineProperty3 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
38
39var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/entries"));
40
41var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));
42
43var _promptBypass = _interopRequireDefault(require("./prompt-bypass"));
44
45var buildInActions = _interopRequireWildcard(require("./actions"));
46
47function ownKeys(object, enumerableOnly) { var keys = (0, _keys.default)(object); if (_getOwnPropertySymbols.default) { var symbols = (0, _getOwnPropertySymbols.default)(object); if (enumerableOnly) symbols = (0, _filter.default)(symbols).call(symbols, function (sym) { return (0, _getOwnPropertyDescriptor.default)(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
48
49function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context3; (0, _forEach.default)(_context3 = ownKeys(source, true)).call(_context3, function (key) { (0, _defineProperty3.default)(target, key, source[key]); }); } else if (_getOwnPropertyDescriptors.default) { (0, _defineProperties.default)(target, (0, _getOwnPropertyDescriptors.default)(source)); } else { var _context4; (0, _forEach.default)(_context4 = ownKeys(source)).call(_context4, function (key) { (0, _defineProperty2.default)(target, key, (0, _getOwnPropertyDescriptor.default)(source, key)); }); } } return target; }
50
51function _default(plopfileApi, flags) {
52 let abort; // triggers inquirer with the correct prompts for this generator
53 // returns a promise that resolves with the user's answers
54
55 const runGeneratorPrompts = async function (genObject, bypassArr = []) {
56 const {
57 prompts
58 } = genObject;
59
60 if (prompts == null) {
61 throw Error(`${genObject.name} has no prompts`);
62 }
63
64 if (typeof prompts === 'function') {
65 return await prompts(plopfileApi.inquirer);
66 } // handle bypass data when provided
67
68
69 const [promptsAfterBypass, bypassAnswers] = (0, _promptBypass.default)(prompts, bypassArr, plopfileApi);
70 return await plopfileApi.inquirer.prompt(promptsAfterBypass).then(answers => (0, _assign.default)(answers, bypassAnswers));
71 }; // Run the actions for this generator
72
73
74 const runGeneratorActions = async function (genObject, data = {}, hooks = {}) {
75 const noop = () => {};
76
77 const {
78 onSuccess = noop,
79 // runs after each successful action
80 onFailure = noop,
81 // runs after each failed action
82 onComment = noop // runs for each comment line in the actions array
83
84 } = hooks;
85 const changes = []; // array of changed made by the actions
86
87 const failures = []; // array of actions that failed
88
89 let {
90 actions
91 } = genObject; // the list of actions to execute
92
93 const customActionTypes = getCustomActionTypes();
94 const actionTypes = (0, _assign.default)({}, customActionTypes, buildInActions);
95 abort = false; // if action is a function, run it to get our array of actions
96
97 if (typeof actions === 'function') {
98 actions = actions(data);
99 } // if actions are not defined... we cannot proceed.
100
101
102 if (actions == null) {
103 throw Error(`${genObject.name} has no actions`);
104 } // if actions are not an array, invalid!
105
106
107 if (!(actions instanceof Array)) {
108 throw Error(`${genObject.name} has invalid actions`);
109 }
110
111 for (let [actionIdx, action] of (0, _entries.default)(actions).call(actions)) {
112 // including strings in the actions array is used for commenting
113 if (typeof action === 'string' && abort) {
114 continue;
115 }
116
117 if (typeof action === 'string') {
118 onComment(action);
119 continue;
120 }
121
122 const actionIsFunction = typeof action === 'function';
123 const actionCfg = actionIsFunction ? {
124 type: 'function'
125 } : action;
126 const actionLogic = actionIsFunction ? action : actionTypes[actionCfg.type]; // bail out if a previous action aborted
127
128 if (abort) {
129 const failure = {
130 type: actionCfg.type || '',
131 path: actionCfg.path || '',
132 error: 'Aborted due to previous action failure'
133 };
134 onFailure(failure);
135 failures.push(failure);
136 continue;
137 }
138
139 actionCfg.force = flags.force === true || actionCfg.force === true;
140
141 if (typeof actionLogic !== 'function') {
142 if (actionCfg.abortOnFail !== false) {
143 abort = true;
144 }
145
146 const failure = {
147 type: actionCfg.type || '',
148 path: actionCfg.path || '',
149 error: `Invalid action (#${actionIdx + 1})`
150 };
151 onFailure(failure);
152 failures.push(failure);
153 continue;
154 }
155
156 try {
157 const actionResult = await executeActionLogic(actionLogic, actionCfg, data);
158 onSuccess(actionResult);
159 changes.push(actionResult);
160 } catch (failure) {
161 if (actionCfg.abortOnFail !== false) {
162 abort = true;
163 }
164
165 onFailure(failure);
166 failures.push(failure);
167 }
168 }
169
170 return {
171 changes,
172 failures
173 };
174 }; // handle action logic
175
176
177 const executeActionLogic = async function (action, cfg, data) {
178 var _context;
179
180 const type = cfg.type || '';
181 let cfgData = cfg.data || {}; // data can also be a function that returns a data object
182
183 if (typeof cfgData === 'function') {
184 cfgData = await cfgData();
185 } // check if action should run
186
187
188 if (typeof cfg.skip === 'function') {
189 // Merge main data and config data in new object
190 const reasonToSkip = await cfg.skip(_objectSpread({}, data, {}, cfgData));
191
192 if (typeof reasonToSkip === 'string') {
193 // Return actionResult instead of string
194 return {
195 type: 'skip',
196 path: reasonToSkip
197 };
198 }
199 } // track keys that can be applied to the main data scope
200
201
202 const cfgDataKeys = (0, _filter.default)(_context = (0, _keys.default)(cfgData)).call(_context, k => typeof data[k] === 'undefined'); // copy config data into main data scope so it's available for templates
203
204 (0, _forEach.default)(cfgDataKeys).call(cfgDataKeys, k => {
205 data[k] = cfgData[k];
206 });
207 return await _promise.default.resolve(action(data, cfg, plopfileApi)).then( // show the resolved value in the console
208 result => ({
209 type,
210 path: typeof result === 'string' ? result : (0, _stringify.default)(result)
211 }), // a rejected promise is treated as a failure
212 err => {
213 throw {
214 type,
215 path: '',
216 error: err.message || err.toString()
217 };
218 }) // cleanup main data scope so config data doesn't leak
219 .finally(() => (0, _forEach.default)(cfgDataKeys).call(cfgDataKeys, k => {
220 delete data[k];
221 }));
222 }; // request the list of custom actions from the plopfile
223
224
225 function getCustomActionTypes() {
226 var _context2;
227
228 return (0, _reduce.default)(_context2 = plopfileApi.getActionTypeList()).call(_context2, function (types, name) {
229 types[name] = plopfileApi.getActionType(name);
230 return types;
231 }, {});
232 }
233
234 return {
235 runGeneratorActions,
236 runGeneratorPrompts
237 };
238}
\No newline at end of file