-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathGruntfile.js
205 lines (171 loc) · 5.33 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
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
module.exports = function(grunt) {
var HTTPD_PORT = 28080 + Math.floor(Math.random() * 10);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
data: 'data/data.txt'
},
shell: {
'data': {
command: './build/pull-mcbopomofo-data.sh',
options: {
stdout: true,
stderr: true,
failOnError: true
}
}
},
connect: {
test: {
options: {
port: HTTPD_PORT
}
}
},
mochaTest: {
test: {
options: {
reporter: 'spec',
ui: 'qunit'
},
src: ['test/build/**/*.js', 'test/interaction/test_node.js']
}
},
jshint: {
options: {
jshintrc: true
},
all: ['Gruntfile.js',
'service-worker.js',
'lib/**/*.js',
'test/**/*.js',
'build/**/*.js',
'assets/*.js']
},
karma: {
options: {
configFile: 'karma.conf.js',
singleRun: true
},
test: { },
benchmark: {
options: {
browserNoActivityTimeout: 0,
browsers: [ 'Firefox', 'Chrome' ],
concurrency: 1,
files: [ /* To set elsewhere */ ]
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-karma');
// Run tests
grunt.registerTask('test', ['mochaTest', 'jshint', 'karma:test']);
// Run benchmark
grunt.registerTask('benchmark',
['check-database', 'benchmark:generate-steps',
'benchmark:set-config', 'karma:benchmark']);
grunt.registerTask('check-database', function() {
const fs = require('fs');
if (!fs.existsSync('./data/database.data')) {
throw new Error('Datafile does not exist. ' +
'Run npm run grunt data to create the database.');
}
});
grunt.registerTask('benchmark:generate-steps', function() {
const fs = require('fs');
const { JSZhuyin } = require('.');
var jszhuyin = new JSZhuyin();
jszhuyin.load();
var text = fs.readFileSync(
'./test/benchmark/corpus.txt', { encoding: 'utf-8' });
const { TestStepsGenerator } =
require('./test/benchmark/test_steps_generator.js');
grunt.log.write('May take a while...');
var generator = new TestStepsGenerator(text);
var flags = [
0,
generator.FLAG_CONFIRM_EVERY_PHRASE
];
var steps = flags
.map(function(flag) {
return generator.generateSteps(flag);
});
fs.writeFileSync('./test/benchmark/steps.json',
JSON.stringify(steps, null, 2));
});
grunt.registerTask('benchmark:set-config', function() {
// See https://github.com/karma-runner/grunt-karma/issues/21
grunt.config('karma.benchmark.options.files', [
'lib/client.js',
'test/benchmark/test.js',
{ pattern: 'test/**', included: false },
{ pattern: 'data/**', included: false },
{ pattern: 'lib/**', included: false }
]);
});
// Pull data from McBopomofo repo and convert them to our binary data.
grunt.registerTask('data', ['shell:data', 'convert-data', 'clean:data']);
// Quick shell command to rsync the code to my site
grunt.registerTask('convert-data', function gruntDataFuncitonTask() {
var McBopomofoDataConverter =
require('./build/mcbopomofo_data_converter.js');
var converter = new McBopomofoDataConverter();
converter.onprogress = function(stage, loadedEntries, totalEntries) {
switch (stage) {
case this.STAGE_READING_FILE:
grunt.log.write('Reading file...');
break;
case this.STAGE_CATEGORIZING_ENTRIES:
if (!loadedEntries) {
grunt.log.ok();
grunt.log.write('Categorizing ' + totalEntries + ' entries...');
grunt.verbose.writeln('');
} else if ((loadedEntries % 1000) === 0) {
grunt.verbose.or.write('.');
}
grunt.verbose.writeln(loadedEntries + '/' + totalEntries);
break;
case this.STAGE_SORTING_ENTRIES:
if (!loadedEntries) {
grunt.log.ok();
grunt.log.write('Sorting and packing into binary entries...');
grunt.verbose.writeln('');
} else if ((loadedEntries % 1000) === 0) {
grunt.verbose.or.write('.');
}
grunt.verbose.writeln(loadedEntries);
break;
case this.STAGE_CREATING_BLOB:
if (!loadedEntries) {
grunt.log.ok();
grunt.log.write('Creating blob from binary entries...');
grunt.verbose.writeln('');
} else if ((loadedEntries % 1000) === 0) {
grunt.verbose.or.write('.');
}
grunt.verbose.writeln(loadedEntries);
break;
case this.STAGE_WRITING_FILE:
grunt.log.ok();
grunt.log.write('Writing blob into disk...');
break;
case this.STAGE_IDLE:
grunt.log.ok();
break;
default:
throw new Error('Unknown stage: ' + stage);
}
};
converter.onwarning = function(subject, message) {
console.warn(subject + ': ' + message);
};
converter.convert('./data/data.txt', './data/database.data');
});
};