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