1 | 'use strict';
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
6 |
|
7 | var _Object$defineProperty2 = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
|
8 |
|
9 | _Object$defineProperty2(exports, "__esModule", {
|
10 | value: true
|
11 | });
|
12 |
|
13 | exports.default = _default;
|
14 |
|
15 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-property"));
|
16 |
|
17 | var _defineProperties = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-properties"));
|
18 |
|
19 | var _getOwnPropertyDescriptors = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors"));
|
20 |
|
21 | var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor"));
|
22 |
|
23 | var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));
|
24 |
|
25 | var _reduce = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/reduce"));
|
26 |
|
27 | var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));
|
28 |
|
29 | var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
|
30 |
|
31 | var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
|
32 |
|
33 | var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
|
34 |
|
35 | var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
|
36 |
|
37 | var _defineProperty3 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
|
38 |
|
39 | var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/entries"));
|
40 |
|
41 | var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));
|
42 |
|
43 | var _promptBypass = _interopRequireDefault(require("./prompt-bypass"));
|
44 |
|
45 | var buildInActions = _interopRequireWildcard(require("./actions"));
|
46 |
|
47 | function 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 |
|
49 | function _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 |
|
51 | function _default(plopfileApi, flags) {
|
52 | let abort;
|
53 |
|
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 | }
|
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 | };
|
72 |
|
73 |
|
74 | const runGeneratorActions = async function (genObject, data = {}, hooks = {}) {
|
75 | const noop = () => {};
|
76 |
|
77 | const {
|
78 | onSuccess = noop,
|
79 |
|
80 | onFailure = noop,
|
81 |
|
82 | onComment = noop
|
83 |
|
84 | } = hooks;
|
85 | const changes = [];
|
86 |
|
87 | const failures = [];
|
88 |
|
89 | let {
|
90 | actions
|
91 | } = genObject;
|
92 |
|
93 | const customActionTypes = getCustomActionTypes();
|
94 | const actionTypes = (0, _assign.default)({}, customActionTypes, buildInActions);
|
95 | abort = false;
|
96 |
|
97 | if (typeof actions === 'function') {
|
98 | actions = actions(data);
|
99 | }
|
100 |
|
101 |
|
102 | if (actions == null) {
|
103 | throw Error(`${genObject.name} has no actions`);
|
104 | }
|
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 |
|
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];
|
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 | };
|
175 |
|
176 |
|
177 | const executeActionLogic = async function (action, cfg, data) {
|
178 | var _context;
|
179 |
|
180 | const type = cfg.type || '';
|
181 | let cfgData = cfg.data || {};
|
182 |
|
183 | if (typeof cfgData === 'function') {
|
184 | cfgData = await cfgData();
|
185 | }
|
186 |
|
187 |
|
188 | if (typeof cfg.skip === 'function') {
|
189 |
|
190 | const reasonToSkip = await cfg.skip(_objectSpread({}, data, {}, cfgData));
|
191 |
|
192 | if (typeof reasonToSkip === 'string') {
|
193 |
|
194 | return {
|
195 | type: 'skip',
|
196 | path: reasonToSkip
|
197 | };
|
198 | }
|
199 | }
|
200 |
|
201 |
|
202 | const cfgDataKeys = (0, _filter.default)(_context = (0, _keys.default)(cfgData)).call(_context, k => typeof data[k] === 'undefined');
|
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(
|
208 | result => ({
|
209 | type,
|
210 | path: typeof result === 'string' ? result : (0, _stringify.default)(result)
|
211 | }),
|
212 | err => {
|
213 | throw {
|
214 | type,
|
215 | path: '',
|
216 | error: err.message || err.toString()
|
217 | };
|
218 | })
|
219 | .finally(() => (0, _forEach.default)(cfgDataKeys).call(cfgDataKeys, k => {
|
220 | delete data[k];
|
221 | }));
|
222 | };
|
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 |