job-registry.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.NodeModuleJobRegistry = void 0;
  11. const core_1 = require("@angular-devkit/core");
  12. const rxjs_1 = require("rxjs");
  13. class NodeModuleJobRegistry {
  14. _resolve(name) {
  15. try {
  16. return require.resolve(name);
  17. }
  18. catch (e) {
  19. if (e.code === 'MODULE_NOT_FOUND') {
  20. return null;
  21. }
  22. throw e;
  23. }
  24. }
  25. /**
  26. * Get a job description for a named job.
  27. *
  28. * @param name The name of the job.
  29. * @returns A description, or null if the job is not registered.
  30. */
  31. get(name) {
  32. const [moduleName, exportName] = name.split(/#/, 2);
  33. const resolvedPath = this._resolve(moduleName);
  34. if (!resolvedPath) {
  35. return (0, rxjs_1.of)(null);
  36. }
  37. const pkg = require(resolvedPath);
  38. const handler = pkg[exportName || 'default'];
  39. if (!handler) {
  40. return (0, rxjs_1.of)(null);
  41. }
  42. function _getValue(...fields) {
  43. return fields.find((x) => core_1.schema.isJsonSchema(x)) || true;
  44. }
  45. const argument = _getValue(pkg.argument, handler.argument);
  46. const input = _getValue(pkg.input, handler.input);
  47. const output = _getValue(pkg.output, handler.output);
  48. const channels = _getValue(pkg.channels, handler.channels);
  49. return (0, rxjs_1.of)(Object.assign(handler.bind(undefined), {
  50. jobDescription: {
  51. argument,
  52. input,
  53. output,
  54. channels,
  55. },
  56. }));
  57. }
  58. }
  59. exports.NodeModuleJobRegistry = NodeModuleJobRegistry;