Vim/gulpfile.js

274 lines
6.7 KiB
JavaScript
Raw Normal View History

var gulp = require('gulp'),
bump = require('gulp-bump'),
git = require('gulp-git'),
2018-02-04 12:21:44 +03:00
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_stream = require('webpack-stream'),
webpack_config = require('./webpack.config.js');
webpack_dev_config = require('./webpack.dev.js');
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const releaseOptions = {
semver: '',
2018-07-18 01:26:41 +03:00
gitHubToken: '',
};
// prettier
function runPrettier(command, done) {
exec(command, function (err, stdout) {
if (err) {
return done(new PluginError('runPrettier', { message: err }));
}
if (!stdout) {
return done();
}
var files = stdout
.split(/\r?\n/)
.filter((f) => {
return f.endsWith('.ts') || f.endsWith('.js') || f.endsWith('.json') || f.endsWith('.md');
})
.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();
});
});
}
2018-07-17 22:57:44 +03:00
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',
})
);
}
2018-07-18 01:26:41 +03:00
const gitHubToken = options.gitHubToken || process.env.CHANGELOG_GITHUB_TOKEN;
if (!gitHubToken) {
return done(
new PluginError('createChangelog', {
2018-07-17 22:57:44 +03:00
message:
2018-07-18 01:26:41 +03:00
'Missing GitHub API Token. Supply token using `--gitHubToken` option or `CHANGELOG_GITHUB_TOKEN` environment variable.',
})
);
}
2018-07-17 22:57:44 +03:00
done();
}
function createChangelog(done) {
const imageName = 'jpoon/github-changelog-generator';
2018-07-17 22:57:44 +03:00
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',
2018-07-17 22:57:44 +03:00
gitHubToken,
'--future-release',
2018-07-15 22:12:27 +03:00
'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', './package-lock.json', 'CHANGELOG.md'])
.pipe(git.commit('bump version'));
}
function updateVersion(done) {
var options = minimist(process.argv.slice(2), releaseOptions);
return gulp
.src(['./package.json', './package-lock.json'])
.pipe(bump({ type: options.semver }))
.pipe(gulp.dest('./'))
.on('end', () => {
done();
});
}
function copyPackageJson() {
return gulp.src('./package.json').pipe(gulp.dest('out'));
}
gulp.task('tsc', function () {
var isError = false;
2018-05-08 08:38:18 +03:00
var tsProject = ts.createProject('tsconfig.json', { noEmitOnError: true });
2018-05-08 08:38:18 +03:00
var tsResult = tsProject
.src()
2018-02-04 12:21:44 +03:00
.pipe(sourcemaps.init())
.pipe(tsProject())
.on('error', () => {
2018-05-08 08:38:18 +03:00
isError = true;
})
.on('finish', () => {
2018-05-08 08:38:18 +03:00
isError && process.exit(1);
});
return tsResult.js
.pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '' }))
2018-02-04 12:21:44 +03:00
.pipe(gulp.dest('out'));
});
gulp.task('webpack', function () {
return gulp.src('./extension.ts').pipe(webpack_stream(webpack_config)).pipe(gulp.dest('out'));
});
gulp.task('webpack-dev', function () {
return gulp.src('./extension.ts').pipe(webpack_stream(webpack_dev_config)).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 --name-only HEAD', done);
});
gulp.task('forceprettier', function (done) {
2017-11-12 08:05:43 +03:00
// files managed by git
runPrettier('git ls-files', done);
});
gulp.task('commit-hash', function (done) {
git.revParse({ args: 'HEAD', quiet: true }, function (err, hash) {
2019-01-31 13:50:54 +03:00
require('fs').writeFileSync('out/version', 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.',
})
);
}
2018-07-27 23:47:15 +03:00
const dockerRunArgs = [
'run',
'-it',
'--rm',
2018-07-27 23:47:15 +03:00
'--env',
`MOCHA_GREP=${options.grep}`,
'-v',
process.cwd() + ':/app',
dockerTag,
];
console.log('Running tests inside container...');
2018-07-27 23:47:15 +03:00
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'));
2018-07-18 01:26:41 +03:00
gulp.task('changelog', gulp.series(validateArgs, createChangelog));
2018-07-17 22:57:44 +03:00
gulp.task(
'release',
2018-08-27 21:41:26 +03:00
gulp.series(
validateArgs,
updateVersion,
createChangelog,
'prettier',
createGitCommit,
createGitTag
)
2018-07-17 22:57:44 +03:00
);
2018-07-15 22:12:27 +03:00
gulp.task('default', gulp.series('build', 'test'));