|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { Observable, of } from 'rxjs'; |
| 9 | +import { JsonValue, experimental as core_experimental, schema } from '../../src'; |
| 10 | +import { ModuleNotFoundException, resolve } from '../resolve'; |
| 11 | + |
| 12 | +export class NodeModuleJobRegistry<MinimumArgumentValueT extends JsonValue = JsonValue, |
| 13 | + MinimumInputValueT extends JsonValue = JsonValue, |
| 14 | + MinimumOutputValueT extends JsonValue = JsonValue, |
| 15 | +> implements core_experimental.jobs.Registry<MinimumArgumentValueT, |
| 16 | + MinimumInputValueT, |
| 17 | + MinimumOutputValueT> { |
| 18 | + public constructor(private _resolveLocal = true, private _resolveGlobal = false) { |
| 19 | + } |
| 20 | + |
| 21 | + protected _resolve(name: string): string | null { |
| 22 | + try { |
| 23 | + return resolve(name, { |
| 24 | + checkLocal: this._resolveLocal, |
| 25 | + checkGlobal: this._resolveGlobal, |
| 26 | + basedir: __dirname, |
| 27 | + }); |
| 28 | + } catch (e) { |
| 29 | + if (e instanceof ModuleNotFoundException) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + throw e; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get a job description for a named job. |
| 38 | + * |
| 39 | + * @param name The name of the job. |
| 40 | + * @returns A description, or null if the job is not registered. |
| 41 | + */ |
| 42 | + get<A extends MinimumArgumentValueT, |
| 43 | + I extends MinimumInputValueT, |
| 44 | + O extends MinimumOutputValueT, |
| 45 | + >( |
| 46 | + name: core_experimental.jobs.JobName, |
| 47 | + ): Observable<core_experimental.jobs.JobHandler<A, I, O> | null> { |
| 48 | + const [moduleName, exportName] = name.split(/#/, 2); |
| 49 | + |
| 50 | + const resolvedPath = this._resolve(moduleName); |
| 51 | + if (!resolvedPath) { |
| 52 | + return of(null); |
| 53 | + } |
| 54 | + |
| 55 | + const pkg = require(resolvedPath); |
| 56 | + const handler = pkg[exportName || 'default']; |
| 57 | + if (!handler) { |
| 58 | + return of(null); |
| 59 | + } |
| 60 | + |
| 61 | + // TODO: this should be unknown |
| 62 | + // tslint:disable-next-line:no-any |
| 63 | + function _getValue(...fields: any[]) { |
| 64 | + return fields.find(x => schema.isJsonSchema(x)) || true; |
| 65 | + } |
| 66 | + |
| 67 | + const argument = _getValue(pkg.argument, handler.argument); |
| 68 | + const input = _getValue(pkg.input, handler.input); |
| 69 | + const output = _getValue(pkg.output, handler.output); |
| 70 | + const channels = _getValue(pkg.channels, handler.channels); |
| 71 | + |
| 72 | + return of(Object.assign(handler.bind(undefined), { |
| 73 | + jobDescription: { |
| 74 | + argument, |
| 75 | + input, |
| 76 | + output, |
| 77 | + channels, |
| 78 | + }, |
| 79 | + })); |
| 80 | + } |
| 81 | +} |
0 commit comments