1 | module.exports = function (grunt) {
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 | var testDir = 'test';
|
5 | var testHost = 'http://localhost:8000/';
|
6 | var pkg = grunt.file.readJSON('package.json');
|
7 |
|
8 | grunt.initConfig({
|
9 |
|
10 | qunit: {
|
11 | options: {
|
12 | '--web-security': 'no',
|
13 | coverage: {
|
14 | disposeCollector: true,
|
15 | src: ['dist/tablefilter/*.js'],
|
16 | instrumentedFiles: 'temp/',
|
17 | htmlReport: 'report/coverage',
|
18 | coberturaReport: 'report/',
|
19 | lcovReport: 'report/',
|
20 | jsonReport: 'report',
|
21 | linesThresholdPct: 80
|
22 | }
|
23 | },
|
24 | all: {
|
25 | options: {
|
26 | urls: getTestFiles(testDir, testHost)
|
27 | }
|
28 | },
|
29 | only: {
|
30 | options: {
|
31 | urls: []
|
32 | }
|
33 | }
|
34 | },
|
35 |
|
36 | connect: {
|
37 | server: {
|
38 | options: {
|
39 | port: 8000,
|
40 | base: '.'
|
41 | }
|
42 | }
|
43 | },
|
44 |
|
45 | copy: {
|
46 | dist: {
|
47 | src: [
|
48 | '**',
|
49 | '!**/*.styl',
|
50 | '!**/extensions/**',
|
51 | '!**/mixins/**'
|
52 | ],
|
53 | cwd: 'static/style',
|
54 | dest: 'dist/tablefilter/style',
|
55 | expand: true
|
56 | },
|
57 | templates: {
|
58 | src: ['**'],
|
59 | cwd: 'static/templates',
|
60 | dest: 'demos',
|
61 | expand: true
|
62 | },
|
63 | assets: {
|
64 | src: ['**'],
|
65 | cwd: 'static/demos-assets',
|
66 | dest: 'demos/assets',
|
67 | expand: true
|
68 | },
|
69 | starter: {
|
70 | src: ['demos/starter.html'],
|
71 | dest: 'dist/starter.html'
|
72 | }
|
73 | },
|
74 |
|
75 | 'string-replace': {
|
76 | demos: {
|
77 | files: { 'demos/': 'demos/*.html' },
|
78 | options: {
|
79 | replacements: [
|
80 | {
|
81 | pattern: /{NAME}/ig,
|
82 | replacement: pkg.name
|
83 | }, {
|
84 | pattern: /{VERSION}/ig,
|
85 | replacement: pkg.version
|
86 | }, {
|
87 | pattern: /{EZEDITTABLE_LINK}/ig,
|
88 | replacement: '<a href="http://edittable.free.fr/' +
|
89 | 'zip.php?f=ezEditTable.zip&p=1"' +
|
90 | 'target="_blank" title="ezEditTable is a ' +
|
91 | 'javascript code aimed at enhancing regular ' +
|
92 | 'HTML tables by adding features such as ' +
|
93 | 'inline editing components, advanced ' +
|
94 | 'selection and keyboard navigation ' +
|
95 | '- Developed by ' + pkg.author.name + '">' +
|
96 | 'ezEditTable</a>'
|
97 | }, {
|
98 | pattern: /<!-- @import (.*?) -->/ig,
|
99 | replacement: function (match, p1) {
|
100 | return grunt.file.read('static/' + p1);
|
101 | }
|
102 | }
|
103 | ]
|
104 | }
|
105 | }
|
106 | },
|
107 |
|
108 | clean: ['demos/starter.html'],
|
109 |
|
110 | watch: {
|
111 | app: {
|
112 | files: ['src/**/*', 'static/style/**/*'],
|
113 | tasks: ['dev'],
|
114 | options: {
|
115 | spawn: false
|
116 | }
|
117 | },
|
118 | templates: {
|
119 | files: ['static/templates/**/*', 'static/partials/**/*'],
|
120 | tasks: ['build-demos'],
|
121 | options: {
|
122 | spawn: false
|
123 | }
|
124 | }
|
125 | },
|
126 |
|
127 |
|
128 | shell: {
|
129 | eslint: {
|
130 | command: 'npm run lint'
|
131 | },
|
132 | esdoc: {
|
133 | command: 'npm run esdoc'
|
134 | },
|
135 | build: {
|
136 | command: 'npm run build'
|
137 | },
|
138 | dev: {
|
139 | command: 'npm run dev'
|
140 | },
|
141 | test: {
|
142 | command: 'npm run build:test'
|
143 | },
|
144 | 'build-css': {
|
145 | command: 'npm run build:css'
|
146 | }
|
147 | }
|
148 | });
|
149 |
|
150 | grunt.loadNpmTasks('grunt-contrib-copy');
|
151 | grunt.loadNpmTasks('grunt-contrib-clean');
|
152 | grunt.loadNpmTasks('grunt-contrib-watch');
|
153 | grunt.loadNpmTasks('grunt-string-replace');
|
154 | grunt.loadNpmTasks('grunt-contrib-connect');
|
155 | grunt.loadNpmTasks('grunt-shell');
|
156 | grunt.loadNpmTasks('grunt-qunit-istanbul');
|
157 |
|
158 | grunt.registerTask('eslint', ['shell:eslint']);
|
159 | grunt.registerTask('esdoc', ['shell:esdoc']);
|
160 |
|
161 | grunt.registerTask('default', ['test', 'build', 'build-demos']);
|
162 |
|
163 |
|
164 | grunt.registerTask('dev',
|
165 | ['eslint', 'shell:dev', 'copy:dist', 'shell:build-css', 'watch:app']);
|
166 |
|
167 |
|
168 | grunt.registerTask('build',
|
169 | ['eslint', 'shell:build', 'copy:dist', 'shell:build-css']);
|
170 |
|
171 |
|
172 | grunt.registerTask('dev-demos', ['build-demos', 'watch:templates']);
|
173 | grunt.registerTask('build-demos', ['copy:templates', 'copy:assets',
|
174 | 'string-replace:demos', 'copy:starter', 'clean']);
|
175 |
|
176 |
|
177 | grunt.registerTask('build-test',
|
178 | ['eslint', 'shell:test', 'copy:dist', 'shell:build-css']);
|
179 |
|
180 |
|
181 | grunt.registerTask('test', ['build-test', 'connect', 'qunit:all']);
|
182 |
|
183 |
|
184 | grunt.registerTask('build-all', 'Prepare for deployment', [
|
185 | 'build', 'build-demos', 'esdoc'
|
186 | ]);
|
187 |
|
188 |
|
189 |
|
190 | grunt.registerTask('test-only',
|
191 | 'A task that runs only specified tests.',
|
192 | function (tests) {
|
193 | if (!tests) {
|
194 | return;
|
195 | }
|
196 | tests = tests.split(',');
|
197 | var res = [];
|
198 |
|
199 | tests.forEach(function (itm) {
|
200 | var filePath = path.resolve(testDir, itm);
|
201 | var parts = filePath.split(path.sep);
|
202 | res.push(buildTestUrl(testHost, testDir, parts));
|
203 | });
|
204 |
|
205 | grunt.task.run('connect');
|
206 | grunt.config('qunit.only.options.urls', res);
|
207 | grunt.task.run('qunit:only');
|
208 | }
|
209 | );
|
210 |
|
211 | function isTestFile(pth) {
|
212 | var allowedExts = ['.html', '.htm'];
|
213 | for (var i = 0, len = allowedExts.length; i < len; i++) {
|
214 | var ext = allowedExts[i];
|
215 | if (pth.indexOf(ext) !== -1) {
|
216 | return true;
|
217 | }
|
218 | }
|
219 | return false;
|
220 | }
|
221 |
|
222 | function buildTestUrl(host, testDir, parts) {
|
223 | var idx = parts.indexOf(testDir);
|
224 | var fileIdx = parts.length;
|
225 | var relParts = parts.slice(idx, fileIdx);
|
226 | return host.concat(relParts.join('/'));
|
227 | }
|
228 |
|
229 |
|
230 | function getTestFiles(testDir, host) {
|
231 | var getFiles = function (dir, host) {
|
232 | var res = [];
|
233 | var items = fs.readdirSync(dir);
|
234 |
|
235 | items.forEach(function (itm) {
|
236 | var fileOrDir = path.resolve(dir, itm);
|
237 | if (isTestFile(fileOrDir)) {
|
238 | var parts = fileOrDir.split(path.sep);
|
239 | res.push(buildTestUrl(host, testDir, parts));
|
240 | } else {
|
241 | if (fs.lstatSync(fileOrDir).isDirectory()) {
|
242 | res = res.concat(getFiles(fileOrDir, host));
|
243 | }
|
244 | }
|
245 | });
|
246 |
|
247 | return res;
|
248 | };
|
249 |
|
250 | return getFiles(testDir, host);
|
251 | }
|
252 | };
|