forked from versatica/JsSIP
/
Gruntfile.js
235 lines (207 loc) · 5.72 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* global module:false */
/*
* Usage:
*
* grunt devel: Build the JsSIP Grammar.
*
* grunt dist: Generate builds/jssip-X.Y.Z.js and a builds/jssip-last.js
* symlink pointing to it.
*
* grunt min: Generate builds/jssip-X.Y.Z.min.js.
*
* grunt watch: Watch for changes in the src/ directory and run 'grunt dist'.
*
* grunt test: Grunt Node test units.
*
* grunt: Alias for 'grunt dist'.
*/
module.exports = function(grunt) {
// Banner.
var banner = require('fs').readFileSync('src/banner.txt').toString();
var path = require('path');
// Generated builds.
var builds = {
dist: 'builds/<%= pkg.name %>-<%= pkg.version %>.js',
last: 'builds/<%= pkg.name %>-last.js',
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
// Default options.
options: {
// DOC: http://www.jshint.com/docs/options/
curly: true,
eqeqeq: true,
immed: true,
latedef: 'nofunc', // Allow functions to be used before defined (it is valid).
newcap: true,
noarg: true,
noempty: true,
nonbsp: true,
// nonew: true, // TODO: Enable when fixed.
plusplus: false,
undef: true,
unused: true,
boss: false,
eqnull: false,
funcscope: false,
sub: false,
supernew: false,
browser: true,
devel: true,
node: true,
nonstandard: true, // Allow 'unescape()' and 'escape()'.
globals: {
webkitRTCPeerConnection: false,
mozRTCPeerConnection: false,
RTCPeerConnection: false,
webkitRTCSessionDescription: false,
mozRTCSessionDescription: false,
RTCSessionDescription: false
}
},
// Lint JS files separately.
each_file: {
src: [ 'src/**/*.js' ],
options: {
ignores: [ 'src/Grammar.js' ]
}
}
},
browserify: {
dist: {
files: {
'builds/<%= pkg.name %>-<%= pkg.version %>.js': [ 'src/JsSIP.js' ]
},
options: {
browserifyOptions: {
standalone: 'JsSIP'
}
}
}
},
concat: {
dist: {
src: builds.dist,
dest: builds.dist,
options: {
banner: banner,
separator: '\n\n'
},
nonull: true
}
},
nodeunit: {
all: [ 'test/*.js' ],
options: {
reporter: 'default'
}
},
uglify: {
dist: {
files: {
'builds/<%= pkg.name %>-<%= pkg.version %>.min.js': [ builds.dist ]
}
},
options: {
banner: banner
}
},
watch: {
dist: {
files: [ 'src/**/*.js' ],
tasks: [ 'dist' ],
options: {
nospawn: true
}
}
},
symlink: {
options: {
overwrite: true
},
last: {
src: builds.dist,
dest: builds.last
}
},
jsdoc: {
basic: {
src: [ 'README.md', 'src/**/*.js' ],
options: {
destination: 'doc/basic/',
private: false
}
},
docstrap: {
src: [ 'README.md', 'src/**/*.js' ],
options: {
destination: 'doc/docstrap/',
private: false,
template: 'node_modules/grunt-jsdoc/node_modules/ink-docstrap/template',
configure: 'jsdoc.conf.json'
}
}
}
});
// Load Grunt plugins.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-symlink');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-jsdoc');
// Task for building src/Grammar.js.
// NOTE: This task is not included in 'grunt dist'.
grunt.registerTask('grammar', function() {
var done = this.async(); // This is an async task.
var sys = require('sys');
var exec = require('child_process').exec;
var child;
// First compile JsSIP grammar with PEGjs.
console.log('grammar task: compiling JsSIP PEGjs grammar into Grammar.js...');
var local_pegjs = path.resolve('./node_modules/.bin/pegjs');
var Grammar_pegjs = path.resolve('src/Grammar.pegjs');
var Grammar_js = path.resolve('src/Grammar.js');
child = exec('' +
'if [ -x "' + local_pegjs + '" ] ; then\n' +
'PEGJS_BIN="' + local_pegjs + '";\n' +
'else\n' +
'PEGJS_BIN="pegjs";\n' +
'fi &&\n' +
'$PEGJS_BIN ' + Grammar_pegjs + ' ' + Grammar_js,
function(error, stdout, stderr) {
if (error) {
sys.print('ERROR: ' + stderr);
done(false); // Tell grunt that async task has failed.
}
console.log('OK');
// Then modify the generated Grammar.js file with custom changes.
console.log('grammar task: applying custom changes to Grammar.js...');
var fs = require('fs');
var grammar = fs.readFileSync('src/Grammar.js').toString();
var modified_grammar = grammar.replace(/throw new this\.SyntaxError\(([\s\S]*?)\);([\s\S]*?)}([\s\S]*?)return result;/, 'new this.SyntaxError($1);\n return -1;$2}$3return data;');
fs.writeFileSync('src/Grammar.js', modified_grammar);
console.log('OK');
done(); // Tell grunt that async task has succeeded.
});
});
grunt.registerTask('devel', [ 'grammar' ]);
// Test task (nodeunit).
grunt.registerTask('test', [ 'nodeunit:all' ]);
// Taks for building builds/jssip-X.Y.Z.js and builds/jssip-last.js symlink.
// NOTE: This task assumes that 'grunt devel' has been already executed.
grunt.registerTask('dist', [ 'jshint:each_file', 'browserify:dist', 'concat:dist', 'symlink:last', 'test' ]);
// Taks for building builds/jssip-X.Y.Z.min.js (minified).
// NOTE: This task assumes that 'devel' and 'dist' tasks have been already executed.
grunt.registerTask('min', [ 'uglify:dist' ]);
// Build builds nice documentation using JsDoc3.
grunt.registerTask('doc', [ 'jsdoc:docstrap' ]);
// Task for Travis CI.
grunt.registerTask('travis', [ 'test' ]);
// Default task points to 'dist' task.
grunt.registerTask('default', [ 'dist' ]);
};