UNPKG

3.68 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const truncate = require("truncate");
9const configs = require("./configs");
10const exec = require("./exec");
11const logger = require("./logger");
12const cmd_1 = require("./cmd");
13const fp_1 = require("./fp");
14const testing = process.env.NODE_ENV === 'testing';
15function createContext(scope) {
16 return {
17 options: scope.options,
18 signature: scope.options.config.signature,
19 };
20}
21exports.createContext = createContext;
22function 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}
33exports.getHooksFromPath = getHooksFromPath;
34async 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}
52exports.afterHooks = afterHooks;
53async 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}
70exports.beforeHooks = beforeHooks;
71async function setupPlugins_(context, setupFn) {
72 const plugins = configs.getPlugins();
73 return Promise.all(plugins.map(async (pluginName) => {
74 // Slice off extra 'gh-' so it isn't 'gh-gh-'
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 // TODO: find a better state sharing mechanism than mutation
86 // Currently our approach is to give each plugin a chance to
87 // update the main options object for the before & after hooks
88 pluginFile[setupFn](context);
89 }
90 }));
91}
92function 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}
110exports.wrapCommand_ = wrapCommand_;
111//# sourceMappingURL=hooks.js.map
\No newline at end of file