|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import co from 'co'; |
| 4 | +import colors from 'colors'; |
| 5 | +import add from './actions/add'; |
| 6 | +import addMany from './actions/addMany'; |
| 7 | +import modify from './actions/modify'; |
| 8 | + |
| 9 | +export default function (plopfileApi) { |
| 10 | + var abort; |
| 11 | + |
| 12 | + // triggers inquirer with the correct prompts for this generator |
| 13 | + // returns a promise that resolves with the user's answers |
| 14 | + const runGeneratorPrompts = co.wrap(function* (genObject) { |
| 15 | + if (genObject.prompts == null) { |
| 16 | + throw Error(`${genObject.name} has no prompts`); |
| 17 | + } |
| 18 | + return yield plopfileApi.inquirer.prompt(genObject.prompts); |
| 19 | + }); |
| 20 | + |
| 21 | + // Run the actions for this generator |
| 22 | + const runGeneratorActions = co.wrap(function* (genObject, data) { |
| 23 | + var changes = []; // array of changed made by the actions |
| 24 | + var failures = []; // array of actions that failed |
| 25 | + var {actions} = genObject; // the list of actions to execute |
| 26 | + const customActionTypes = getCustomActionTypes(); |
| 27 | + const buildInActions = { add, addMany, modify }; |
| 28 | + const actionTypes = Object.assign({}, customActionTypes, buildInActions); |
| 29 | + |
| 30 | + abort = false; |
| 31 | + |
| 32 | + // if action is a function, run it to get our array of actions |
| 33 | + if (typeof actions === 'function') { actions = actions(data); } |
| 34 | + |
| 35 | + // if actions are not defined... we cannot proceed. |
| 36 | + if (actions == null) { |
| 37 | + throw Error(`${genObject.name} has no actions`); |
| 38 | + } |
| 39 | + |
| 40 | + // if actions are not an array, invalid! |
| 41 | + if (!(actions instanceof Array)) { |
| 42 | + throw Error(`${genObject.name} has invalid actions`); |
| 43 | + } |
| 44 | + |
| 45 | + for (let [actionIdx, action] of actions.entries()) { |
| 46 | + // bail out if a previous action aborted |
| 47 | + if (abort) { |
| 48 | + failures.push({ |
| 49 | + type: action.type || '', |
| 50 | + path: action.path || '', |
| 51 | + error: 'Aborted due to previous action failure' |
| 52 | + }); |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + const actionIsFunction = typeof action === 'function'; |
| 57 | + const actionCfg = (actionIsFunction ? {} : action); |
| 58 | + const actionLogic = (actionIsFunction ? action : actionTypes[actionCfg.type]); |
| 59 | + |
| 60 | + if (typeof actionLogic !== 'function') { |
| 61 | + if (actionCfg.abortOnFail !== false) { abort = true; } |
| 62 | + failures.push({ |
| 63 | + type: action.type || '', |
| 64 | + path: action.path || '', |
| 65 | + error: `Invalid action (#${actionIdx + 1})` |
| 66 | + }); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + const actionResult = yield executeActionLogic(actionLogic, actionCfg, data); |
| 72 | + changes.push(actionResult); |
| 73 | + } catch(failure) { |
| 74 | + failures.push(failure); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return { changes, failures }; |
| 79 | + }); |
| 80 | + |
| 81 | + // handle action logic |
| 82 | + const executeActionLogic = co.wrap(function* (action, cfg, data) { |
| 83 | + const failure = makeErrorLogger(cfg.type || 'function', '', cfg.abortOnFail); |
| 84 | + |
| 85 | + // convert any returned data into a promise to |
| 86 | + // return and wait on |
| 87 | + return yield Promise.resolve(action(data, cfg, plopfileApi)).then( |
| 88 | + // show the resolved value in the console |
| 89 | + result => ({ |
| 90 | + type: cfg.type || 'function', |
| 91 | + path: colors.blue(result.toString()) |
| 92 | + }), |
| 93 | + // a rejected promise is treated as a failure |
| 94 | + function (err) { |
| 95 | + throw failure(err.message || err.toString()); |
| 96 | + } |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + // request the list of custom actions from the plopfile |
| 101 | + function getCustomActionTypes() { |
| 102 | + return plopfileApi.getActionTypeList() |
| 103 | + .reduce(function (types, name) { |
| 104 | + types[name] = plopfileApi.getActionType(name); |
| 105 | + return types; |
| 106 | + }, {}); |
| 107 | + } |
| 108 | + |
| 109 | + // provide a function to handle action errors in a uniform way |
| 110 | + function makeErrorLogger(type, path, abortOnFail) { |
| 111 | + return function (error) { |
| 112 | + if (abortOnFail !== false) { abort = true; } |
| 113 | + return { type, path, error }; |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + runGeneratorActions, |
| 119 | + runGeneratorPrompts |
| 120 | + }; |
| 121 | +} |
0 commit comments