forked from VSCodeVim/Vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
312 lines (276 loc) · 7.72 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
var gulp = require('gulp'),
bump = require('gulp-bump'),
git = require('gulp-git'),
sourcemaps = require('gulp-sourcemaps'),
tag_version = require('gulp-tag-version'),
tslint = require('gulp-tslint'),
ts = require('gulp-typescript'),
PluginError = require('plugin-error'),
minimist = require('minimist'),
path = require('path'),
webpack = require('webpack'),
webpack_stream = require('webpack-stream'),
webpack_config = require('./webpack.config.js'),
es = require('event-stream');
webpack_dev_config = require('./webpack.dev.js');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const releaseOptions = {
semver: '',
gitHubToken: '',
};
// prettier
function runPrettier(command, done) {
exec(command, function (err, stdout) {
if (err) {
return done(new PluginError('runPrettier', { message: err }));
}
if (!stdout) {
return done();
}
const filetypes = ['.ts', '.js', '.json', '.md', '.yml'];
const files = stdout
.split(/\r?\n/)
.filter((f) => filetypes.some((filetype) => f.endsWith(filetype)))
.join(' ');
if (!files) {
return done();
}
const prettierPath = path.normalize('./node_modules/.bin/prettier');
exec(`${prettierPath} --write ${files}`, function (err) {
if (err) {
return done(new PluginError('runPrettier', { message: err }));
}
return done();
});
});
}
function validateArgs(done) {
const options = minimist(process.argv.slice(2), releaseOptions);
if (!options.semver) {
return done(
new PluginError('updateVersion', {
message: 'Missing `--semver` option. Possible values: patch, minor, major',
})
);
}
if (!['patch', 'minor', 'major'].includes(options.semver)) {
return done(
new PluginError('updateVersion', {
message: 'Invalid `--semver` option. Possible values: patch, minor, major',
})
);
}
const gitHubToken = options.gitHubToken || process.env.CHANGELOG_GITHUB_TOKEN;
if (!gitHubToken) {
return done(
new PluginError('createChangelog', {
message:
'Missing GitHub API Token. Supply token using `--gitHubToken` option or `CHANGELOG_GITHUB_TOKEN` environment variable.',
})
);
}
done();
}
function createChangelog(done) {
const imageName = 'jpoon/github-changelog-generator';
const version = require('./package.json').version;
const options = minimist(process.argv.slice(2), releaseOptions);
const gitHubToken = options.gitHubToken || process.env.CHANGELOG_GITHUB_TOKEN;
var dockerRunCmd = spawn(
'docker',
[
'run',
'-it',
'--rm',
'-v',
process.cwd() + ':/usr/local/src/your-app',
imageName,
'--token',
gitHubToken,
'--future-release',
'v' + version,
],
{
cwd: process.cwd(),
stdio: 'inherit',
}
);
dockerRunCmd.on('exit', function (exitCode) {
done(exitCode);
});
}
function createGitTag() {
return gulp.src(['./package.json']).pipe(tag_version());
}
function createGitCommit() {
return gulp
.src(['./package.json', './yarn.lock', 'CHANGELOG.md'])
.pipe(git.commit('bump version'));
}
function updateVersion(done) {
var options = minimist(process.argv.slice(2), releaseOptions);
return gulp
.src(['./package.json', './yarn.lock'])
.pipe(bump({ type: options.semver }))
.pipe(gulp.dest('./'))
.on('end', () => {
done();
});
}
function updatePath() {
const input = es.through();
const output = input.pipe(
es.mapSync((f) => {
const contents = f.contents.toString('utf8');
const filePath = f.path;
let platformRelativepath = path.relative(
path.dirname(filePath),
path.resolve(process.cwd(), 'out/src/platform/node')
);
platformRelativepath = platformRelativepath.replace(/\\/g, '/');
f.contents = Buffer.from(
contents.replace(
/\(\"platform\/([^"]*)\"\)/g,
'("' + (platformRelativepath === '' ? './' : platformRelativepath + '/') + '$1")'
),
'utf8'
);
return f;
})
);
return es.duplex(input, output);
}
function copyPackageJson() {
return gulp.src('./package.json').pipe(gulp.dest('out'));
}
gulp.task('tsc', function () {
var isError = false;
var tsProject = ts.createProject('tsconfig.json', { noEmitOnError: true });
var tsResult = tsProject
.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.on('error', () => {
isError = true;
})
.on('finish', () => {
isError && process.exit(1);
});
return tsResult.js
.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '' }))
.pipe(updatePath())
.pipe(gulp.dest('out'));
});
gulp.task('webpack', function () {
return webpack_stream(
{
config: webpack_config,
entry: ['./extension.ts', './extensionWeb.ts'],
},
webpack
).pipe(gulp.dest('out'));
});
gulp.task('webpack-dev', function () {
return webpack_stream(
{
config: webpack_dev_config,
entry: ['./extension.ts', './extensionWeb.ts'],
},
webpack
).pipe(gulp.dest('out'));
});
gulp.task('tslint', function () {
const program = require('tslint').Linter.createProgram('./tsconfig.json');
return gulp
.src(['**/*.ts', '!node_modules/**', '!typings/**'])
.pipe(
tslint({
formatter: 'prose',
program: program,
})
)
.pipe(tslint.report({ summarizeFailureOutput: true }));
});
gulp.task('prettier', function (done) {
// Files changed
runPrettier('git diff --diff-filter=d --name-only HEAD', done);
});
gulp.task('forceprettier', function (done) {
// Files managed by git
// TODO: if any file is deleted, but not yet staged, this will fail
runPrettier('git ls-files', done);
});
gulp.task('commit-hash', function (done) {
git.revParse({ args: 'HEAD', quiet: true }, function (err, hash) {
require('fs').writeFileSync('out/version.txt', hash);
done();
});
});
// test
gulp.task('run-test', function (done) {
// the flag --grep takes js regex as a string and filters by test and test suite names
var knownOptions = {
string: 'grep',
default: { grep: '' },
};
var options = minimist(process.argv.slice(2), knownOptions);
var spawn = require('child_process').spawn;
const dockerTag = 'vscodevim';
console.log('Building container...');
var dockerBuildCmd = spawn(
'docker',
['build', '-f', './build/Dockerfile', './build/', '-t', dockerTag],
{
cwd: process.cwd(),
stdio: 'inherit',
}
);
dockerBuildCmd.on('exit', function (exitCode) {
if (exitCode !== 0) {
return done(
new PluginError('test', {
message: 'Docker build failed.',
})
);
}
const dockerRunArgs = [
'run',
'-it',
'--rm',
'--env',
`MOCHA_GREP=${options.grep}`,
'-v',
process.cwd() + ':/app',
dockerTag,
];
console.log('Running tests inside container...');
var dockerRunCmd = spawn('docker', dockerRunArgs, {
cwd: process.cwd(),
stdio: 'inherit',
});
dockerRunCmd.on('exit', function (exitCode) {
done(exitCode);
});
});
});
gulp.task('build', gulp.series('prettier', gulp.parallel('webpack', 'tslint'), 'commit-hash'));
gulp.task(
'build-dev',
gulp.series('prettier', gulp.parallel('webpack-dev', 'tslint'), 'commit-hash')
);
gulp.task('prepare-test', gulp.parallel('tsc', copyPackageJson));
gulp.task('test', gulp.series('prepare-test', 'run-test'));
gulp.task('changelog', gulp.series(validateArgs, createChangelog));
gulp.task(
'release',
gulp.series(
validateArgs,
updateVersion,
createChangelog,
'prettier',
createGitCommit,
createGitTag
)
);
gulp.task('default', gulp.series('build', 'test'));