forked from microsoft/tslint-microsoft-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
193 lines (165 loc) · 6.42 KB
/
Gruntfile.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
"use strict";
var _ = require('underscore');
module.exports = function(grunt) {
function getAllRuleNames() {
var ruleFiles = grunt.file.expand('src/*Rule.ts');
return _(ruleFiles).map(function(filename) {
filename = filename.substring(4, filename.length - 7);
return _(filename).reduce(function(memo, element) {
if (element.toLowerCase() === element) {
memo = memo + element;
} else {
memo = memo + '-' + element.toLowerCase();
}
return memo;
}, '');
});
}
function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
return group1.toUpperCase();
});
}
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
src: ['dist'],
options: {
force: true
}
},
copy: {
options: {
encoding: 'UTF-8'
},
package: {
files: [
{
expand: true,
cwd: 'dist/src',
src: ['**/*.js', '!references.js'],
dest: 'dist/build'
},
{
expand: true,
cwd: '.',
src: ['README.md'],
dest: 'dist/build'
}
]
}
},
mochaTest: {
test: {
src: ['dist/tests/**/*.js']
}
},
ts: {
default: {
src: [
'./src/**/*.ts',
'./tests/**/*.ts'
],
outDir: 'dist',
options: {
module: 'commonjs',
target: 'es5'
}
},
},
tslint: {
options: {
configuration: grunt.file.readJSON("tslint.json"),
rulesDirectory: 'dist/src'
},
files: {
src: [
'src/**/*.ts',
'tests/**/*.ts',
'!src/references.ts'
]
}
},
watch: {
scripts: {
files: [
'./src/**/*.ts',
'./tests/**/*.ts'
],
tasks: [
'ts',
'mochaTest',
'tslint'
]
}
}
});
require('load-grunt-tasks')(grunt); // loads all grunt-* npm tasks
require('time-grunt')(grunt);
grunt.registerTask('create-package-json-for-npm', 'A task that creates a package.json file for the npm module', function () {
var basePackageJson = grunt.file.readJSON('package.json');
delete basePackageJson.devDependencies;
grunt.file.write('dist/build/package.json', JSON.stringify(basePackageJson, null, 2), { encoding: 'UTF-8' });
});
grunt.registerTask('validate-documentation', 'A task that validates that all rules defined in src are documented in README.md\n' +
'and validates that the package.json version is the same version defined in README.md', function () {
var readmeText = grunt.file.read('README.md', { encoding: 'UTF-8' });
var packageJson = grunt.file.readJSON('package.json', { encoding: 'UTF-8' });
getAllRuleNames().forEach(function(ruleName) {
if (readmeText.indexOf(ruleName) === -1) {
grunt.fail.warn('A rule was found that is not documented in README.md: ' + ruleName);
}
});
if (readmeText.indexOf('\nVersion ' + packageJson.version + '\n') === -1) {
grunt.fail.warn('Version not documented in README.md correctly.\n' +
'package.json declares: ' + packageJson.version + '\n' +
'README.md declares something different.');
}
});
grunt.registerTask('validate-config', 'A task that makes sure all the rules in the project are defined in to run' +
' during the build.', function () {
var tslintConfig = grunt.file.readJSON('tslint.json', { encoding: 'UTF-8' });
var rulesToSkip = { 'no-unexternalized-strings': true };
getAllRuleNames().forEach(function(ruleName) {
if (rulesToSkip[ruleName]) {
return;
}
if (tslintConfig.rules[ruleName] !== true) {
if (tslintConfig.rules[ruleName] == null || tslintConfig.rules[ruleName][0] !== true) {
grunt.fail.warn('A rule was found that is not enabled on the project: ' + ruleName);
}
}
});
});
grunt.registerTask('create-rule', 'A task that creates a new rule from the rule templates. --rule-name parameter required', function () {
function applyTemplates(source) {
return source.replace(/%RULE_NAME%/gm, ruleName)
.replace(/%RULE_FILE_NAME%/gm, ruleFile)
.replace(/%WALKER_NAME%/gm, walkerName);
}
var ruleName = grunt.option('rule-name');
if (!ruleName) {
grunt.fail.warn('--rule-name parameter is required');
} else {
var ruleFile = camelCase(ruleName) + 'Rule';
var sourceFileName = './src/' + ruleFile + '.ts';
var testFileName = './tests/' + ruleFile.charAt(0).toUpperCase() + ruleFile.substr(1) + 'Tests.ts';
var walkerName = ruleFile.charAt(0).toUpperCase() + ruleFile.substr(1) + 'Walker';
var ruleTemplateText = grunt.file.read('./templates/rule.snippet', {encoding: 'UTF-8'});
var testTemplateText = grunt.file.read('./templates/rule-tests.snippet', {encoding: 'UTF-8'});
grunt.file.write(sourceFileName, applyTemplates(ruleTemplateText), {encoding: 'UTF-8'});
grunt.file.write(testFileName, applyTemplates(testTemplateText), {encoding: 'UTF-8'});
}
});
grunt.registerTask('all', 'Performs a cleanup and a full build with all tasks', [
'clean',
'ts',
'mochaTest',
'tslint',
'validate-documentation',
'validate-config',
'copy:package',
'create-package-json-for-npm'
]);
grunt.registerTask('default', ['all']);
};