forked from microsoft/vscode-dapr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
151 lines (115 loc) · 4.48 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as cp from 'child_process';
import * as del from 'del';
import * as eslint from 'gulp-eslint';
import * as gulp from 'gulp';
import * as nls from 'vscode-nls-dev';
import * as sourcemaps from 'gulp-sourcemaps';
import * as ts from 'gulp-typescript';
import * as vsce from 'vsce';
import * as webpack from 'webpack';
import { config as webpackConfig } from './webpack.config';
const languages: nls.Language[] = [
{ folderName: 'jpn', id: 'ja' }
];
const tsProject = ts.createProject('./tsconfig.json');
function getDistDir(): string {
if (!webpackConfig.output?.path) {
throw new Error('path is not defined in webpack.config.ts');
}
return webpackConfig.output.path;
}
function getOutDir(): string {
if (!tsProject.options?.outDir) {
throw new Error('outDir is not defined in tsconfig.json.');
}
return tsProject.options.outDir;
}
function wrapThroughStream(stream: nls.ThroughStream): NodeJS.ReadWriteStream {
return (stream as unknown) as NodeJS.ReadWriteStream;
}
function cleanTask(): Promise<string[]> {
return del([getDistDir(), getOutDir(), 'package.nls.*.json', 'vscode-dapr-*.vsix']);
}
function lintTaskFactory(warningsAsErrors?: boolean) {
return function lintTask() {
let pipeline = gulp.src(['src/**/*.ts'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
if (warningsAsErrors) {
pipeline = pipeline
.pipe(eslint.results(
results => {
if (results.warningCount) {
throw new Error('ESLint generated warnings.');
}
}));
}
return pipeline;
}
}
function compileTask(): NodeJS.ReadWriteStream {
const outDir = getOutDir();
return tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject()).js
.pipe(wrapThroughStream(nls.rewriteLocalizeCalls()))
.pipe(wrapThroughStream(nls.createAdditionalLanguageFiles(languages, 'i18n', outDir)))
.pipe(wrapThroughStream(nls.bundleMetaDataFiles('vscode-dapr', outDir)))
.pipe(wrapThroughStream(nls.bundleLanguageFiles()))
.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: './' }))
.pipe(gulp.dest(outDir));
}
function compilePackedTaskFactory(mode: 'production' | 'development'): () => Promise<void> {
return function compilePackedTask() {
return new Promise(
(resolve, reject) => {
webpack(
{
...webpackConfig,
mode
},
(err, stats) => {
if (err) {
return reject(err);
}
const info = stats?.toJson();
if (stats?.hasErrors()) {
return reject(new Error(info!.errors!.join('\n')));
}
if (stats?.hasWarnings()) {
info!.warnings!.forEach((warning: unknown) => console.warn(warning));
}
return resolve();
});
});
}
}
function addI18nTask() {
return gulp.src(['package.nls.json'])
.pipe(wrapThroughStream(nls.createAdditionalLanguageFiles(languages, 'i18n')))
.pipe(gulp.dest('.'));
}
function testTask() {
return cp.spawn('node', ['./out/test/runAllTests.js'], { stdio: 'inherit' });
}
function unitTestTask() {
return cp.spawn('node', ['./out/test/runUnitTests.js'], { stdio: 'inherit' });
}
function vscePackageTask() {
return vsce.createVSIX();
}
const buildTask = gulp.series(cleanTask, compileTask, addI18nTask);
const ciBuildTask = gulp.series(cleanTask, compilePackedTaskFactory('production'), lintTaskFactory(/* warningsAsErrors: */ true));
gulp.task('clean', cleanTask);
gulp.task('lint', lintTaskFactory());
gulp.task('build', buildTask);
gulp.task('build-packed', gulp.series(cleanTask, compilePackedTaskFactory('development')));
gulp.task('unit-test', gulp.series(buildTask, unitTestTask));
gulp.task('test', gulp.series(buildTask, testTask));
gulp.task('package', gulp.series(buildTask, vscePackageTask));
gulp.task('ci-build', ciBuildTask);
gulp.task('ci-package', gulp.series(ciBuildTask, vscePackageTask));
gulp.task('default', buildTask);