UNPKG

3.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const format_1 = require("@ionic/cli-framework/utils/format");
4const utils_array_1 = require("@ionic/utils-array");
5const Debug = require("debug");
6const lodash = require("lodash");
7const path = require("path");
8const color_1 = require("./color");
9const errors_1 = require("./errors");
10const debug = Debug('ionic:lib:hooks');
11class Hook {
12 constructor(e) {
13 this.e = e;
14 }
15 get script() {
16 return `ionic:${this.name}`;
17 }
18 async run(input) {
19 const { pkgManagerArgs } = await Promise.resolve().then(() => require('./utils/npm'));
20 const type = this.e.project.type;
21 if (!type || !this.e.project.directory) {
22 return; // TODO: will we need hooks outside a project?
23 }
24 const [pkg] = await this.e.project.getPackageJson(undefined, { logErrors: false });
25 if (!pkg) {
26 return;
27 }
28 debug(`Looking for ${color_1.ancillary(this.script)} npm script.`);
29 if (pkg.scripts && pkg.scripts[this.script]) {
30 debug(`Invoking ${color_1.ancillary(this.script)} npm script.`);
31 const [pkgManager, ...pkgArgs] = await pkgManagerArgs(this.e.config.get('npmClient'), { command: 'run', script: this.script });
32 await this.e.shell.run(pkgManager, pkgArgs, {});
33 }
34 const projectHooks = this.e.project.config.get('hooks');
35 const hooks = projectHooks ? utils_array_1.conform(projectHooks[this.name]) : [];
36 for (const h of hooks) {
37 const p = path.resolve(this.e.project.directory, h);
38 try {
39 if (path.extname(p) !== '.js') {
40 throw new Error(`Hooks must be .js files with a function for its default export.`);
41 }
42 const hook = await this.loadHookFn(p);
43 if (!hook) {
44 throw new Error(`Module must have a function for its default export.`);
45 }
46 await hook(lodash.assign({}, input, {
47 project: {
48 type,
49 dir: this.e.project.directory,
50 srcDir: await this.e.project.getSourceDir(),
51 },
52 argv: process.argv,
53 env: process.env,
54 }));
55 }
56 catch (e) {
57 throw new errors_1.HookException(`An error occurred while running an Ionic CLI hook defined in ${color_1.strong(format_1.prettyPath(this.e.project.filePath))}.\n` +
58 `Hook: ${color_1.strong(this.name)}\n` +
59 `File: ${color_1.strong(p)}\n\n` +
60 `${color_1.failure(e.stack ? e.stack : e)}`);
61 }
62 }
63 }
64 async loadHookFn(p) {
65 const module = require(p);
66 if (typeof module === 'function') {
67 return module;
68 }
69 else if (typeof module.default === 'function') {
70 return module.default;
71 }
72 debug(`Could not load hook function ${color_1.strong(p)}: %o not a function`, module);
73 }
74}
75exports.Hook = Hook;
76function addHook(baseDir, hooks, hook) {
77 const hookPaths = utils_array_1.conform(hooks);
78 const resolvedHookPaths = hookPaths.map(p => path.resolve(baseDir, p));
79 if (!resolvedHookPaths.includes(path.resolve(baseDir, hook))) {
80 hookPaths.push(hook);
81 }
82 return hookPaths;
83}
84exports.addHook = addHook;
85function removeHook(baseDir, hooks, hook) {
86 const hookPaths = utils_array_1.conform(hooks);
87 const i = locateHook(baseDir, hookPaths, hook);
88 if (i >= 0) {
89 hookPaths.splice(i, 1);
90 }
91 return hookPaths;
92}
93exports.removeHook = removeHook;
94function locateHook(baseDir, hooks, hook) {
95 return utils_array_1.conform(hooks).map(p => path.resolve(baseDir, p)).indexOf(path.resolve(baseDir, hook));
96}
97exports.locateHook = locateHook;