forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
167 lines (144 loc) · 4.81 KB
/
gulpfile.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const ts = require('gulp-typescript');
const sourcemaps = require('gulp-sourcemaps');
const clean = require('gulp-clean');
const deleteEmpty = require('delete-empty');
const childProcess = require('child_process');
const log = require('fancy-log');
const clc = require('cli-color');
const promiseSeries = require('promise.series');
const { promisify } = require('util');
const exec = promisify(childProcess.exec);
const SAMPLE = path.join(__dirname, 'sample');
const packages = {
common: ts.createProject('packages/common/tsconfig.json'),
core: ts.createProject('packages/core/tsconfig.json'),
microservices: ts.createProject('packages/microservices/tsconfig.json'),
websockets: ts.createProject('packages/websockets/tsconfig.json'),
testing: ts.createProject('packages/testing/tsconfig.json'),
'platform-express': ts.createProject(
'packages/platform-express/tsconfig.json',
),
'platform-fastify': ts.createProject(
'packages/platform-fastify/tsconfig.json',
),
'platform-socket.io': ts.createProject(
'packages/platform-socket.io/tsconfig.json',
),
'platform-ws': ts.createProject('packages/platform-ws/tsconfig.json'),
};
const modules = Object.keys(packages);
const source = 'packages';
const distId = process.argv.indexOf('--dist');
const dist = distId < 0 ? source : process.argv[distId + 1];
gulp.task('default', function() {
modules.forEach(module => {
gulp.watch(
[`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`],
[module],
);
});
});
gulp.task('copy-misc', function() {
return gulp
.src(['Readme.md', 'LICENSE', '.npmignore'])
.pipe(gulp.dest(`${source}/common`))
.pipe(gulp.dest(`${source}/core`))
.pipe(gulp.dest(`${source}/microservices`))
.pipe(gulp.dest(`${source}/websockets`))
.pipe(gulp.dest(`${source}/testing`))
.pipe(gulp.dest(`${source}/platform-fastify`))
.pipe(gulp.dest(`${source}/platform-express`))
.pipe(gulp.dest(`${source}/platform-ws`))
.pipe(gulp.dest(`${source}/platform-socket.io`));
});
gulp.task('clean:output', function() {
return gulp
.src(
[`${source}/**/*.js`, `${source}/**/*.d.ts`, `${source}/**/*.js.map`],
{
read: false,
},
)
.pipe(clean());
});
gulp.task('clean:dirs', function(done) {
deleteEmpty.sync(`${source}/`);
done();
});
gulp.task('clean:bundle', gulp.series('clean:output', 'clean:dirs'));
modules.forEach(module => {
gulp.task(module, () => {
return packages[module]
.src()
.pipe(packages[module]())
.pipe(gulp.dest(`${dist}/${module}`));
});
});
modules.forEach(module => {
gulp.task(module + ':dev', () => {
return packages[module]
.src()
.pipe(sourcemaps.init())
.pipe(packages[module]())
.pipe(
sourcemaps.mapSources(sourcePath => './' + sourcePath.split('/').pop()),
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dist}/${module}`));
});
});
gulp.task('common:dev', gulp.series(modules.map(module => module + ':dev')));
gulp.task('build', gulp.series(modules));
gulp.task('build:dev', gulp.series('common:dev'));
function getFolders(dir) {
return fs.readdirSync(dir).filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
const getDirs = base => getFolders(base).map(path => `${base}/${path}`);
gulp.task('install:samples', async () => {
const directories = getDirs(SAMPLE);
const promises = directories.map(async dir => {
const dirName = dir.replace(__dirname, '');
log.info(`Installing dependencies of ${clc.magenta(dirName)}`);
try {
await exec(`npm install --no-shrinkwrap --prefix ${dir}`);
log.info(`Finished installing ${clc.magenta(dirName)}`);
} catch (err) {
log.error(`Failed installing dependencies of ${dirName}`);
throw err;
}
});
return await promiseSeries(promises);
});
gulp.task('build:samples', async () => {
const directories = getDirs(SAMPLE);
const promises = directories.map(async dir => {
const dirName = dir.replace(__dirname, '');
log.info(`Building ${clc.magenta(dirName)}`);
try {
await exec(`npm run build --prefix ${dir}`);
log.info(`Finished building ${clc.magenta(dirName)}`);
} catch (err) {
log.error(`Failed building ${clc.magenta(dirName)}:`);
if (err.stdout) {
log.error(err.stdout);
}
throw err;
}
});
return await promiseSeries(promises);
});
gulp.task('move', function() {
const examplesDirs = getDirs('sample');
const integrationDirs = getDirs('integration');
const directories = examplesDirs.concat(integrationDirs);
let stream = gulp.src(['node_modules/@nestjs/**/*']);
directories.forEach(dir => {
stream = stream.pipe(gulp.dest(dir + '/node_modules/@nestjs'));
});
return stream;
});