hooks.js 4.9 KB

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