1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | const truncate = require("truncate");
|
9 | const configs = require("./configs");
|
10 | const exec = require("./exec");
|
11 | const logger = require("./logger");
|
12 | const cmd_1 = require("./cmd");
|
13 | const fp_1 = require("./fp");
|
14 | const testing = process.env.NODE_ENV === 'testing';
|
15 | function createContext(scope) {
|
16 | return {
|
17 | options: scope.options,
|
18 | signature: scope.options.config.signature,
|
19 | };
|
20 | }
|
21 | exports.createContext = createContext;
|
22 | function getHooksFromPath(path, config) {
|
23 | const keys = path.split('.');
|
24 | let key = keys.shift();
|
25 | let hooks;
|
26 | hooks = config.hooks || {};
|
27 | while (hooks[key]) {
|
28 | hooks = hooks[key];
|
29 | key = keys.shift();
|
30 | }
|
31 | return Array.isArray(hooks) ? hooks : [];
|
32 | }
|
33 | exports.getHooksFromPath = getHooksFromPath;
|
34 | async function afterHooks(path, scope) {
|
35 | const after = getHooksFromPath(`${path}.after`, scope.options.config);
|
36 | const options = scope.options;
|
37 | if (options.hooks === false || process.env.NODEGH_HOOK_IS_LOCKED) {
|
38 | return;
|
39 | }
|
40 | let context = createContext(scope);
|
41 | if (!testing) {
|
42 | const pluginContext = await setupPlugins_(context, 'setupAfterHooks');
|
43 | if (pluginContext) {
|
44 | context = Object.assign({}, context, pluginContext);
|
45 | }
|
46 | }
|
47 | after.forEach(cmd => {
|
48 | wrapCommand_(cmd, context, 'after');
|
49 | });
|
50 | process.env.NODEGH_HOOK_IS_LOCKED = 'true';
|
51 | }
|
52 | exports.afterHooks = afterHooks;
|
53 | async function beforeHooks(path, scope) {
|
54 | const before = getHooksFromPath(`${path}.before`, scope.options.config);
|
55 | const options = scope.options;
|
56 | if (options.hooks === false || process.env.NODEGH_HOOK_IS_LOCKED) {
|
57 | return;
|
58 | }
|
59 | let context = createContext(scope);
|
60 | if (!testing) {
|
61 | const pluginContext = await setupPlugins_(context, 'setupBeforeHooks');
|
62 | if (pluginContext) {
|
63 | context = Object.assign({}, context, pluginContext);
|
64 | }
|
65 | }
|
66 | before.forEach(cmd => {
|
67 | wrapCommand_(cmd, context, 'before');
|
68 | });
|
69 | }
|
70 | exports.beforeHooks = beforeHooks;
|
71 | async function setupPlugins_(context, setupFn) {
|
72 | const plugins = configs.getPlugins();
|
73 | return Promise.all(plugins.map(async (pluginName) => {
|
74 |
|
75 | const name = pluginName.slice(3);
|
76 | try {
|
77 | var pluginFile = await cmd_1.tryResolvingByPlugin(name)
|
78 | .chain(fp_1.safeImport)
|
79 | .promise();
|
80 | }
|
81 | catch (e) {
|
82 | logger.warn(`Can't get ${name} plugin.`);
|
83 | }
|
84 | if (pluginFile && configs.pluginHasConfig(pluginName) && pluginFile[setupFn]) {
|
85 |
|
86 |
|
87 |
|
88 | pluginFile[setupFn](context);
|
89 | }
|
90 | }));
|
91 | }
|
92 | function wrapCommand_(cmd, context, when) {
|
93 | const raw = logger.compileTemplate(cmd, context);
|
94 | if (!raw) {
|
95 | return;
|
96 | }
|
97 | logger.log(logger.colors.cyan('[hook]'), truncate(raw.trim(), 120));
|
98 | if (testing)
|
99 | return;
|
100 | try {
|
101 | exec.execSyncInteractiveStream(raw, { cwd: process.cwd() });
|
102 | }
|
103 | catch (e) {
|
104 | logger.debug(`[${when} hook failure]`);
|
105 | }
|
106 | finally {
|
107 | logger.debug(logger.colors.cyan(`[end of ${when} hook]`));
|
108 | }
|
109 | }
|
110 | exports.wrapCommand_ = wrapCommand_;
|
111 |
|
\ | No newline at end of file |