Skip to content

Commit a3a657f

Browse files
hanslmgechev
authored andcommitted
feat(@angular-devkit/core): Jobs API added
See the README for more details about usage.
1 parent c89a042 commit a3a657f

25 files changed

+3447
-2
lines changed

etc/api/angular_devkit/core/node/_golden-api.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export declare class NodeJsSyncHost implements virtualFs.Host<fs.Stats> {
3939
write(path: Path, content: virtualFs.FileBuffer): Observable<void>;
4040
}
4141

42+
export declare class NodeModuleJobRegistry<MinimumArgumentValueT extends JsonValue = JsonValue, MinimumInputValueT extends JsonValue = JsonValue, MinimumOutputValueT extends JsonValue = JsonValue> implements core_experimental.jobs.Registry<MinimumArgumentValueT, MinimumInputValueT, MinimumOutputValueT> {
43+
constructor(_resolveLocal?: boolean, _resolveGlobal?: boolean);
44+
protected _resolve(name: string): string | null;
45+
get<A extends MinimumArgumentValueT, I extends MinimumInputValueT, O extends MinimumOutputValueT>(name: core_experimental.jobs.JobName): Observable<core_experimental.jobs.JobHandler<A, I, O> | null>;
46+
}
47+
4248
export interface ProcessOutput {
4349
write(buffer: string | Buffer): boolean;
4450
}

packages/angular_devkit/core/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ ts_library(
109109
deps = [
110110
":core",
111111
":node",
112+
"//tests/angular_devkit/core/node/jobs:jobs_test_lib",
112113
"@rxjs",
113114
"@rxjs//operators",
114115
"@npm//@types/node",

packages/angular_devkit/core/node/_golden-api.ts

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
9+
// Start experimental namespace
10+
// Start jobs namespace
11+
export * from './experimental/job-registry';
12+
// End jobs namespace
13+
// End experimental namespace
14+
815
export * from './fs';
916
export * from './cli-logger';
1017
export * from './host';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 * as jobs from './job-registry';
9+
10+
export {
11+
jobs,
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 * as path from 'path';
9+
import { experimental as core_experimental } from '../../src';
10+
import { NodeModuleJobRegistry } from './job-registry';
11+
12+
const root = path.join(
13+
path.dirname(require.resolve(__filename)),
14+
'../../../../../tests/angular_devkit/core/node/jobs',
15+
);
16+
17+
18+
describe('NodeModuleJobScheduler', () => {
19+
it('works', async () => {
20+
const registry = new NodeModuleJobRegistry();
21+
const scheduler = new core_experimental.jobs.SimpleScheduler(registry);
22+
23+
const job = scheduler.schedule(path.join(root, 'add'), [1, 2, 3]);
24+
expect(await job.output.toPromise()).toBe(6);
25+
});
26+
});

packages/angular_devkit/core/node/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import * as experimental from './experimental/job-registry';
89
import * as fs from './fs';
910
export * from './cli-logger';
1011
export * from './host';
1112
export { ModuleNotFoundException, ResolveOptions, resolve } from './resolve';
1213

1314
export {
15+
experimental,
1416
fs,
1517
};

packages/angular_devkit/core/src/experimental.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import * as jobs from './experimental/jobs/index';
89
import * as workspace from './workspace/index';
910

10-
export { workspace };
11+
export {
12+
jobs,
13+
workspace,
14+
};

0 commit comments

Comments
 (0)