-
Notifications
You must be signed in to change notification settings - Fork 1
/
client-example.ts
134 lines (116 loc) · 4.38 KB
/
client-example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* IntelliJ debug:
* Node parameters: --require ts-node/register --require tsconfig-paths/register
* Environment variables: TS_NODE_PROJECT=libs/client/tsconfig.lib.json
*
* Start API:
* nx serve api
* Command:
* DEBUG=tskmgr:* ts-node --project libs/client/tsconfig.lib.json -r tsconfig-paths/register "libs/client/src/lib/client-example.ts"
*/
import { execSync } from 'child_process';
import { CreateTaskDto, Run, Task, TaskPriority } from '@tskmgr/common';
import { ClientOptions } from './client-options';
import { ClientFactory } from './client-factory';
import { v4 as uuid } from 'uuid';
import Debug from 'debug';
import { readJsonFile } from '@nx/devkit';
import { unlinkSync } from 'fs';
const debug = Debug('tskmgr:client-example');
delete process.env.TS_NODE_PROJECT;
const options: ClientOptions = {
parallel: 1,
dataCallback,
errorCallback,
spawnOptions: { env: { ...process.env } },
};
const client = ClientFactory.createNew('http://localhost:3333', 'RUNNER_1', options);
let completed = false;
let run: Run;
(async () => {
try {
// 1. Create the new run
run = await client.createRun({
name: uuid(),
type: '123',
prioritization: [TaskPriority.Longest],
});
debug(run);
// 2. Leader should create some tasks to run
const election = await client.setLeader(run.id);
if (election.leader) {
const tasks = getNxTasks().map<CreateTaskDto>((nxTask) => {
const command = `npx nx run ${nxTask.target.project}:${nxTask.target.target} --configuration=production`;
return {
name: nxTask.target.project,
type: nxTask.target.target,
command: command.trim(),
options: { shell: true },
priority: TaskPriority.Longest,
};
});
const createdTasks = await client.createTasks(run.id, { tasks });
debug(createdTasks);
const closeRun = await client.closeRun(run.id);
debug(closeRun);
}
// 3. Execute tasks
const result = await client.runTasks(run.id);
if (result.completed) {
// if failFast set to false, runTasks will continue without throwing errors.
completed = true;
}
} catch (e) {
console.error(e);
} finally {
// 4. See results
console.log('--------------------------------------------------');
console.log(` tskmgr run: http://localhost:4200/runs/${run.id}`);
console.log('--------------------------------------------------');
console.log(`${completed ? 'COMPLETED!' : 'FAILED!'}`);
process.exit(completed ? 0 : 1);
}
})();
function getNxTasks(): NxTask[] {
const graphFileName = uuid() + '.json';
// In CI environment use npx nx affected --graph=${graphFileName} --target=lint command. run-many is just for demo purpose.
execSync(`npx nx run-many --graph=lint-${graphFileName} --target=lint`);
const lintJson = readJsonFile(`lint-${graphFileName}`);
unlinkSync(`lint-${graphFileName}`);
const lintTasks: NxTask[] = Object.values(lintJson.tasks.tasks);
execSync(`npx nx run-many --graph=test-${graphFileName} --target=test`);
const testJson = readJsonFile(`test-${graphFileName}`);
unlinkSync(`test-${graphFileName}`);
const testTasks: NxTask[] = Object.values(testJson.tasks.tasks);
execSync(`npx nx run-many --graph=build-${graphFileName} --target=build`);
const buildJson = readJsonFile(`build-${graphFileName}`);
unlinkSync(`build-${graphFileName}`);
const buildTasks: NxTask[] = Object.values(buildJson.tasks.tasks);
execSync(`npx nx run-many --graph=e2e-${graphFileName} --target=e2e`);
const e2eJson = readJsonFile(`e2e-${graphFileName}`);
unlinkSync(`e2e-${graphFileName}`);
const e2eTasks: NxTask[] = Object.values(e2eJson.tasks.tasks);
return [...lintTasks, ...testTasks, ...buildTasks, ...e2eTasks];
}
function dataCallback(task: Task, data: string, cached: () => void): void {
// > nx run frontend:lint [existing outputs match the cache, left as is]
// > nx run client:lint [local cache]
if (
(data.startsWith(`> nx run ${task.name}:${task.type}`) &&
data.endsWith('[existing outputs match the cache, left as is]')) ||
data.endsWith('[local cache]')
) {
cached();
}
console.log(`[stdout] ${data}`);
}
function errorCallback(task: Task, data: string): void {
console.log(`[stderr] ${data}`);
}
interface NxTask {
id: string;
overrides: any;
target: { project: string; target: string };
command: string;
outputs: string[];
}