This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.ts
78 lines (60 loc) · 2.18 KB
/
gulpfile.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
import * as path from 'path';
import * as fs from 'fs';
import * as gulp from 'gulp';
import { spawn, SpawnOptions } from 'child_process';
type GulpTaskDoneFn = (error?: any) => void;
const BASE_DIR = './microservices';
const TASK_PREFIX = 'microservice';
const MICROSERVICES = fs.readdirSync(BASE_DIR).filter((value) => {
const dirPath = path.resolve(path.join(BASE_DIR, value));
const nodePkgPath = path.join(dirPath, 'package.json');
return fs.statSync(dirPath).isDirectory()
&& fs.existsSync(nodePkgPath)
&& fs.statSync(nodePkgPath).isFile()
&& require(nodePkgPath)!.scripts!.start !== undefined;
}).map((value) => ({ name: value, path: path.resolve(path.join(BASE_DIR, value)) }));
function getTaskName(value: string): string {
return `${TASK_PREFIX ? TASK_PREFIX + '-' : ''}${value}`;
}
function start(args?: ReadonlyArray<string>, options?: SpawnOptions, done?: GulpTaskDoneFn) {
let hasExited: boolean = false;
const child = spawn(
/^win/.test(process.platform) ? 'yarn.cmd' : 'yarn',
args,
{ stdio: [0, 1, 2], ...options },
);
child.on('error', (err: Error) => {
if (hasExited)
return;
hasExited = true;
if (done)
done(err);
});
child.on('exit', (code: number) => {
if (hasExited)
return;
hasExited = true;
if (done && code !== 0)
done(new Error(`Exit code: ${code}`));
});
return child;
}
function install(done: GulpTaskDoneFn) {
start(['install', '--silent'], { cwd: path.resolve('./shared') }, done);
MICROSERVICES.map((obj) => start(['install', '--silent'], { cwd: obj.path }, done));
done();
}
MICROSERVICES.forEach((obj) => gulp.add(getTaskName(obj.name), (done: GulpTaskDoneFn) => {
start(['start', '--bail', '--display', 'errors-only'], { cwd: obj.path }, done);
}));
gulp.task('default', MICROSERVICES.map((obj) => getTaskName(obj.name)));
gulp.task('postinstall', install);
gulp.task('lint', (done: GulpTaskDoneFn) => {
start(['lint'], { cwd: path.resolve('./shared') });
MICROSERVICES.map((obj) => start(['lint'], { cwd: obj.path }, done));
done();
});
gulp.task('build', (done: GulpTaskDoneFn) => {
MICROSERVICES.map((obj) => start(['build'], { cwd: obj.path }, done));
done();
});