forked from buzinas/tslint-eslint-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
138 lines (121 loc) · 3.35 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
'use strict';
var argv = require('yargs').argv;
var path = require('path');
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');
var tslint = require('gulp-tslint');
var mocha = require('gulp-spawn-mocha');
var Promise = require('es6-promise').Promise;
var SINGLE_TEST = argv.single ? parseTestName(argv.single) : null;
process.env.SINGLE_TEST = JSON.stringify(SINGLE_TEST);
process.env.BENCHMARK = argv.benchmark;
var SRC_FOLDER = SINGLE_TEST ? singleRule(SINGLE_TEST.name) : 'src/**/*.ts';
var TEST_FOLDER = SINGLE_TEST ? singleTest(SINGLE_TEST.name) : 'dist/test/**/*.js';
var DEF_FOLDER = 'typings/**/*.ts'
var tsProject = ts.createProject('tsconfig.json');
gulp.task('readme', ['build'], function readme(gulpCallBack) {
var readme = require('./dist/readme');
readme.updateRuleFiles(function () {
readme.updateReadme(function () {
gulpCallBack();
});
});
});
gulp.task('fetch', ['build'], function fetch(gulpCallBack) {
var fetch = require('./dist/readme/fetch');
Promise.all([
fetch.compareToESLint(),
fetch.compareToTSLint()
]).then(function () {
gulpCallBack();
});
});
gulp.task('new-rule', ['build'], function newRule(done) {
var newRule = require('./dist/tools/newRule');
const ruleName = argv.rule;
if (!ruleName) {
done('missing `--rule` option');
} else if (!ruleName.startsWith('ter-')) {
done('rule name is missing the `ter-` prefix');
} else {
newRule.writeNewRule(ruleName);
newRule.writeNewRuleTests(ruleName);
done();
}
});
gulp.task('lint', function lint() {
return gulp
.src(SRC_FOLDER)
.pipe(tslint({
formatter: 'stylish',
}))
.pipe(tslint.report());
});
gulp.task('self-lint', function selfLint() {
return gulp
.src(SRC_FOLDER)
.pipe(tslint({
configuration: 'tslint_eslint_rules.json',
formatter: 'verbose'
}))
.pipe(tslint.report());
});
gulp.task('build', argv.lint === false ? [] : ['lint'], function build(done) {
var hasError = false;
gulp
.src([SRC_FOLDER, DEF_FOLDER])
.pipe(sourcemaps.init())
.pipe(tsProject())
.on('error', function onError() {
hasError = true;
})
.js
.pipe(sourcemaps.write({
includeContent: false,
sourceRoot: path.join(__dirname, '/src')
}))
.pipe(gulp.dest('dist'))
.on('end', function() {
if (hasError) {
done('TypeScript has reported errors');
} else {
done();
}
});
});
gulp.task('test', ['build'], function test() {
var opt = argv.benchmark ? { 'noTimeouts': true } : {};
return gulp
.src(TEST_FOLDER)
.pipe(mocha(opt));
});
gulp.task('watch', function watch() {
gulp.watch(SRC_FOLDER, ['build']);
});
gulp.task('default', ['watch']);
gulp.task('publish', function build() {
return tsProject
.src([SRC_FOLDER, DEF_FOLDER])
.pipe(tsProject());
});
// ---
function toCamelCase(str){
return str.replace(/-(.)/g, function (_, char) {
return char.toUpperCase();
});
}
function parseTestName(str) {
var parts = str.split(':');
return {
name: parts[0],
group: parts[1] || null,
num: parts[2] !== undefined ? parseInt(parts[2], 10) : null
};
}
function singleRule(name) {
return 'src/**/?(' + toCamelCase(name) + '*|helper).ts';
}
function singleTest(name) {
return 'dist/test/rules/' + toCamelCase(name) + 'RuleTests.js';
}