1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.lifecycleHooks = void 0;
|
4 | const child_process_1 = require("child_process");
|
5 | const path_1 = require("path");
|
6 | const shell_quote_1 = require("shell-quote");
|
7 | const debugging_js_1 = require("./utils/debugging.js");
|
8 | const DEFAULT_HOOKS = {
|
9 | afterStart: [],
|
10 | beforeDone: [],
|
11 | onWatchTriggered: [],
|
12 | onError: [],
|
13 | afterOneFileWrite: [],
|
14 | afterAllFileWrite: [],
|
15 | beforeOneFileWrite: [],
|
16 | beforeAllFileWrite: [],
|
17 | };
|
18 | function execShellCommand(cmd) {
|
19 | return new Promise((resolve, reject) => {
|
20 | (0, child_process_1.exec)(cmd, {
|
21 | env: {
|
22 | ...process.env,
|
23 | PATH: `${process.env.PATH}${path_1.delimiter}${process.cwd()}${path_1.sep}node_modules${path_1.sep}.bin`,
|
24 | },
|
25 | }, (error, stdout, stderr) => {
|
26 | if (error) {
|
27 | reject(error);
|
28 |
|
29 | console.error(error);
|
30 | }
|
31 | else {
|
32 | (0, debugging_js_1.debugLog)(stdout || stderr);
|
33 | resolve(stdout || stderr);
|
34 | }
|
35 | });
|
36 | });
|
37 | }
|
38 | async function executeHooks(hookName, _scripts = [], args = [], initialState) {
|
39 | (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" scripts...`);
|
40 | let state = initialState;
|
41 | const scripts = Array.isArray(_scripts) ? _scripts : [_scripts];
|
42 | const quotedArgs = (0, shell_quote_1.quote)(args);
|
43 | for (const script of scripts) {
|
44 | if (typeof script === 'string') {
|
45 | (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script} with args: ${quotedArgs}...`);
|
46 | await execShellCommand(`${script} ${quotedArgs}`);
|
47 | }
|
48 | else {
|
49 | (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script.name} with args: ${args.join(' ')}...`);
|
50 | const hookArgs = state === undefined ? args : [...args, state];
|
51 | const hookResult = await script(...hookArgs);
|
52 | if (typeof hookResult === 'string' && typeof state === 'string') {
|
53 | (0, debugging_js_1.debugLog)(`Received new content from lifecycle hook "${hookName}" script: ${script.name}`);
|
54 | state = hookResult;
|
55 | }
|
56 | }
|
57 | }
|
58 | return state;
|
59 | }
|
60 | const lifecycleHooks = (_hooks = {}) => {
|
61 | const hooks = {
|
62 | ...DEFAULT_HOOKS,
|
63 | ..._hooks,
|
64 | };
|
65 | return {
|
66 | afterStart: async () => {
|
67 | await executeHooks('afterStart', hooks.afterStart);
|
68 | },
|
69 | onWatchTriggered: async (event, path) => {
|
70 | await executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]);
|
71 | },
|
72 | onError: async (error) => {
|
73 | await executeHooks('onError', hooks.onError, [error]);
|
74 | },
|
75 | afterOneFileWrite: async (path) => {
|
76 | await executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]);
|
77 | },
|
78 | afterAllFileWrite: async (paths) => {
|
79 | await executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths);
|
80 | },
|
81 | beforeOneFileWrite: async (path, content) => {
|
82 | const result = await executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path], content);
|
83 | return typeof result === 'string' ? result : content;
|
84 | },
|
85 | beforeAllFileWrite: async (paths) => {
|
86 | await executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths);
|
87 | },
|
88 | beforeDone: async () => {
|
89 | await executeHooks('beforeDone', hooks.beforeDone);
|
90 | },
|
91 | };
|
92 | };
|
93 | exports.lifecycleHooks = lifecycleHooks;
|