-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
124 lines (113 loc) · 3.46 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
const gulp = require('gulp');
const gulpTS = require('gulp-typescript');
const gulpSourceMaps = require('gulp-sourcemaps');
const del = require('del');
const mergeStream = require('merge-stream');
const gulpFile = require('gulp-file');
const gulpShell = require('gulp-shell');
const gulpMocha = require('gulp-mocha');
const gulpUglify = require('gulp-uglify');
const gulpDependencies = [
'del',
'gulp',
'gulp-rename',
'gulp-sourcemaps',
'gulp-typescript',
'gulp-uglify',
'merge-stream',
'gulp-file',
'gulp-shell',
'gulp-mocha'
];
const testDependencies = [
'@types/mocha',
'mocha',
'@types/chai',
'chai',
'@types/supertest',
'supertest',
'@types/koa-bodyparser',
'koa-bodyparser'
];
const baseProject = gulpTS.createProject('./tsconfig.json');
const tsReleaseProject = gulpTS.createProject('./tsconfig.release.json');
const tsTestProject = gulpTS.createProject('./tsconfig.test.json');
function compileProjectTask(project, options) {
options = options || {};
return () => {
let compileStream = project.src();
if (project.options.sourceMap === true) {
compileStream = compileStream.pipe(gulpSourceMaps.init());
}
compileStream = compileStream.pipe(project());
let jsStream = compileStream.js;
if (options.uglify === true) {
jsStream = jsStream.pipe(gulpUglify());
}
if (project.options.sourceMap === true) {
jsStream = jsStream.pipe(gulpSourceMaps.write());
}
let outDir = project.options.outDir;
if (options.outPath !== undefined) {
outDir += `/${options.outPath}`;
}
return mergeStream(
compileStream.dts,
jsStream
).pipe(gulp.dest(outDir))
}
}
function bundleFilesTask(project) {
return function bundleFiles() {
return gulp.src(['LICENSE', 'README.md'])
.pipe(gulp.dest(project.options.outDir));
};
}
function preparePackageJsonTask(project) {
return function preparePackageJson() {
const pack = require('./package.json');
for (let gulpDependency of gulpDependencies) {
delete pack.devDependencies[gulpDependency];
}
for (let testDependency of testDependencies) {
delete pack.devDependencies[testDependency];
}
pack.main = 'lib/index.js';
if (project.options.declaration === true) {
pack.types = 'lib/index.d.js';
}
return gulpFile('package.json', JSON.stringify(pack, null, 2), { src: true })
.pipe(gulp.dest(project.options.outDir));
};
}
function runMochaTask(project) {
return function mocha() {
return gulp.src(project.options.outDir + '/**/*.spec.js')
.pipe(gulpMocha());
};
}
gulp.task('clean', () => {
return del(baseProject.options.outDir);
});
gulp.task('compile', compileProjectTask(tsReleaseProject));
gulp.task('compileRelease', compileProjectTask(tsReleaseProject, {
outPath: 'lib',
uglify: true
}));
gulp.task('compileTesting', compileProjectTask(tsTestProject));
gulp.task('default', gulp.series(
'clean',
'compile'
));
gulp.task('test', gulp.series(
'clean',
'compileTesting',
runMochaTask(tsTestProject)
));
gulp.task('deploy', gulp.series(
'clean',
'compileRelease',
preparePackageJsonTask(tsReleaseProject),
bundleFilesTask(tsReleaseProject),
gulpShell.task(`npm publish ${tsReleaseProject.options.outDir}`)
));