UNPKG

@graphql-codegen/cli

Version:

<p align="center"> <img src="https://github.com/dotansimha/graphql-code-generator/blob/master/logo.png?raw=true" /> </p>

93 lines (92 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lifecycleHooks = void 0; const debugging_js_1 = require("./utils/debugging.js"); const child_process_1 = require("child_process"); const path_1 = require("path"); const shell_quote_1 = require("shell-quote"); const DEFAULT_HOOKS = { afterStart: [], beforeDone: [], onWatchTriggered: [], onError: [], afterOneFileWrite: [], afterAllFileWrite: [], beforeOneFileWrite: [], beforeAllFileWrite: [], }; function normalizeHooks(_hooks) { const keys = Object.keys({ ...DEFAULT_HOOKS, ..._hooks, }); return keys.reduce((prev, hookName) => { if (typeof _hooks[hookName] === 'string') { return { ...prev, [hookName]: [_hooks[hookName]], }; } if (typeof _hooks[hookName] === 'function') { return { ...prev, [hookName]: [_hooks[hookName]], }; } if (Array.isArray(_hooks[hookName])) { return { ...prev, [hookName]: _hooks[hookName], }; } return prev; }, {}); } function execShellCommand(cmd) { return new Promise((resolve, reject) => { (0, child_process_1.exec)(cmd, { env: { ...process.env, PATH: `${process.env.PATH}${path_1.delimiter}${process.cwd()}${path_1.sep}node_modules${path_1.sep}.bin`, }, }, (error, stdout, stderr) => { if (error) { reject(error); // eslint-disable-next-line no-console console.error(error); } else { (0, debugging_js_1.debugLog)(stdout || stderr); resolve(stdout || stderr); } }); }); } async function executeHooks(hookName, scripts = [], args = []) { (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" scripts...`); const quotedArgs = (0, shell_quote_1.quote)(args); for (const script of scripts) { if (typeof script === 'string') { (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script} with args: ${quotedArgs}...`); await execShellCommand(`${script} ${quotedArgs}`); } else { (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script.name} with args: ${args.join(' ')}...`); await script(...args); } } } const lifecycleHooks = (_hooks = {}) => { const hooks = normalizeHooks(_hooks); return { afterStart: async () => executeHooks('afterStart', hooks.afterStart), onWatchTriggered: async (event, path) => executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]), onError: async (error) => executeHooks('onError', hooks.onError, [error]), afterOneFileWrite: async (path) => executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]), afterAllFileWrite: async (paths) => executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths), beforeOneFileWrite: async (path) => executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path]), beforeAllFileWrite: async (paths) => executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths), beforeDone: async () => executeHooks('beforeDone', hooks.beforeDone), }; }; exports.lifecycleHooks = lifecycleHooks;