1//To build dist/thrift.js, dist/thrift.min.js and doc/*
2//run grunt at the command line in this directory.
3//Prerequisites:
4// Node Setup -   nodejs.org
5// Grunt Setup -  npm install  //reads the ./package.json and installs project dependencies
6// Run grunt -    npx grunt  // uses project-local installed version of grunt (from package.json)
7
8module.exports = function(grunt) {
9  'use strict';
10
11  grunt.initConfig({
12    pkg: grunt.file.readJSON('package.json'),
13    concat: {
14      options: {
15        separator: ';'
16      },
17      dist: {
18        src: ['src/**/*.js'],
19        dest: 'dist/<%= pkg.name %>.js'
20      }
21    },
22    jsdoc : {
23        dist : {
24            src: ['src/*.js', './README.md'],
25            options: {
26              destination: 'doc'
27            }
28        }
29    },
30    uglify: {
31      options: {
32        banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'
33      },
34      dist: {
35        files: {
36          'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
37        }
38      }
39    },
40    shell: {
41      InstallThriftJS: {
42        command: 'cp src/thrift.js test/build/js/thrift.js'
43      },
44      InstallThriftNodeJSDep: {
45        command: 'cd ../.. && npm install'
46      },
47      InstallTestLibs: {
48        command: 'cd test && ant download_jslibs'
49      },
50      ThriftGen: {
51        command: [
52          '"../../compiler/cpp/thrift" -gen js --out test/gen-js ../../test/ThriftTest.thrift',
53          '"../../compiler/cpp/thrift" -gen js --out test/gen-js ../../test/JsDeepConstructorTest.thrift',
54          '"../../compiler/cpp/thrift" -gen js:jquery --out test/gen-js-jquery ../../test/ThriftTest.thrift',
55          '"../../compiler/cpp/thrift" -gen js:node --out test/gen-nodejs ../../test/ThriftTest.thrift',
56          '"../../compiler/cpp/thrift" -gen js:es6 --out test/gen-js-es6 ../../test/ThriftTest.thrift',
57          '"../../compiler/cpp/thrift" -gen js:node,es6 --out ./test/gen-nodejs-es6 ../../test/ThriftTest.thrift',
58        ].join(' && ')
59      },
60      ThriftGenJQ: {
61        command: '../../compiler/cpp/thrift -gen js:jquery -gen js:node -o test ../../test/ThriftTest.thrift'
62      },
63      ThriftGenDeepConstructor: {
64        command: '../../compiler/cpp/thrift -gen js -o test ../../test/JsDeepConstructorTest.thrift'
65      },
66      ThriftBrowserifyNodeInt64: {
67        command: [
68          './node_modules/browserify/bin/cmd.js ./node_modules/node-int64/Int64.js -s Int64 -o test/build/js/lib/Int64.js',
69          './node_modules/browserify/bin/cmd.js ../nodejs/lib/thrift/int64_util.js -s Int64Util -o test/build/js/lib/Int64Util.js',
70          './node_modules/browserify/bin/cmd.js ./node_modules/json-int64/index.js -s JSONInt64 -o test/build/js/lib/JSONInt64.js'
71        ].join(' && ')
72      },
73      ThriftGenInt64: {
74        command: '../../compiler/cpp/thrift -gen js -o test ../../test/Int64Test.thrift'
75      },
76      ThriftGenDoubleConstants: {
77        command: '../../compiler/cpp/thrift -gen js -o test ../../test/DoubleConstantsTest.thrift'
78      },
79      ThriftTestServer: {
80        options: {
81          async: true,
82          execOptions: {
83            cwd: "./test",
84            env: {NODE_PATH: "../../nodejs/lib:../../../node_modules"}
85          }
86        },
87        command: "node server_http.js",
88      },
89      ThriftTestServerES6: {
90        options: {
91          async: true,
92          execOptions: {
93            cwd: "./test",
94            env: {NODE_PATH: "../../nodejs/lib:../../../node_modules"}
95          }
96        },
97        command: "node server_http.js --es6",
98      },
99      ThriftTestServer_TLS: {
100        options: {
101          async: true,
102          execOptions: {
103            cwd: "./test",
104            env: {NODE_PATH: "../../nodejs/lib:../../../node_modules"}
105          }
106        },
107        command: "node server_https.js",
108      },
109      ThriftTestServerES6_TLS: {
110        options: {
111          async: true,
112          execOptions: {
113            cwd: "./test",
114            env: {NODE_PATH: "../../nodejs/lib:../../../node_modules"}
115          }
116        },
117        command: "node server_https.js --es6",
118      },
119    },
120    qunit: {
121      ThriftJS: {
122        options: {
123          urls: [
124            'http://localhost:8089/test-nojq.html'
125          ],
126          puppeteer: {
127            headless: true,
128            args: ['--no-sandbox'],
129          },
130        }
131      },
132      ThriftJSJQ: {
133        options: {
134          urls: [
135            'http://localhost:8089/test.html'
136          ],
137          puppeteer: {
138            headless: true,
139            args: ['--no-sandbox'],
140          },
141        }
142      },
143      ThriftJS_DoubleRendering: {
144        options: {
145          urls: [
146            'http://localhost:8089/test-double-rendering.html'
147          ],
148          puppeteer: {
149            headless: true,
150            args: ['--no-sandbox'],
151            ignoreHTTPSErrors: true,
152          },
153        }
154      },
155      ThriftJS_Int64: {
156        options: {
157          urls: [
158            'http://localhost:8089/test-int64.html'
159          ],
160          puppeteer: {
161            headless: true,
162            args: ['--no-sandbox'],
163            ignoreHTTPSErrors: true,
164          },
165        }
166      },
167      ThriftWS: {
168        options: {
169          urls: [
170            'http://localhost:8089/testws.html'
171          ],
172          puppeteer: {
173            headless: true,
174            args: ['--no-sandbox'],
175          },
176        }
177      },
178      ThriftJS_TLS: {
179        options: {
180          urls: [
181            'https://localhost:8091/test-nojq.html'
182          ],
183          puppeteer: {
184            headless: true,
185            args: ['--no-sandbox'],
186            ignoreHTTPSErrors: true,
187          },
188        }
189      },
190      ThriftJSJQ_TLS: {
191        options: {
192          urls: [
193            'https://localhost:8091/test.html'
194          ],
195          puppeteer: {
196            headless: true,
197            args: ['--no-sandbox'],
198            ignoreHTTPSErrors: true,
199          },
200        }
201      },
202      ThriftWS_TLS: {
203        options: {
204          urls: [
205            'https://localhost:8091/testws.html'
206          ],
207          puppeteer: {
208            headless: true,
209            args: ['--no-sandbox'],
210            ignoreHTTPSErrors: true,
211          },
212        }
213      },
214      ThriftDeepConstructor: {
215        options: {
216          urls: [
217            'http://localhost:8089/test-deep-constructor.html'
218          ],
219          puppeteer: {
220            headless: true,
221            args: ['--no-sandbox'],
222          },
223        }
224      },
225      ThriftWSES6: {
226        options: {
227          urls: [
228            'http://localhost:8088/test-es6.html'
229          ],
230          puppeteer: {
231            headless: true,
232            args: ['--no-sandbox'],
233          },
234        }
235      }
236    },
237    jshint: {
238      // The main Thrift library file. not es6 yet :(
239      lib: {
240        src: ['src/**/*.js'],
241      },
242      // The test files use es6
243      test: {
244        src: ['Gruntfile.js', 'test/*.js'],
245        options: {
246          esversion: 6,
247        }
248      },
249      gen_js_code: {
250        src: ['test/gen-js/*.js', 'test/gen-js-jquery/*.js'],
251      },
252      gen_es6_code: {
253        src: ['test/gen-js-es6/*.js'],
254        options: {
255          esversion: 6,
256        }
257      },
258      gen_node_code: {
259        src: ['test/gen-nodejs/*.js'],
260        options: {
261          node: true,
262        }
263      },
264      gen_node_es6_code: {
265        src: ['test/gen-nodejs-es6/*.js'],
266        options: {
267          node: true,
268          esversion: 6,
269        }
270      }
271    },
272  });
273
274  grunt.loadNpmTasks('grunt-contrib-uglify');
275  grunt.loadNpmTasks('grunt-contrib-jshint');
276  grunt.loadNpmTasks('grunt-contrib-qunit');
277  grunt.loadNpmTasks('grunt-contrib-concat');
278  grunt.loadNpmTasks('grunt-jsdoc');
279  grunt.loadNpmTasks('grunt-shell-spawn');
280
281  grunt.registerTask('wait', 'Wait just one second for the server to start', function () {
282    var done = this.async();
283    setTimeout(function() {
284      done(true);
285    }, 1000);
286  });
287
288  grunt.registerTask('CreateDirectories', 'Creating required local directories', function () {
289    grunt.file.mkdir('test/build/js/lib');
290    grunt.file.mkdir('test/gen-js');
291    grunt.file.mkdir('test/gen-js-jquery');
292    grunt.file.mkdir('test/gen-nodejs');
293    grunt.file.mkdir('test/gen-js-es6');
294    grunt.file.mkdir('test/gen-nodejs-es6');
295});
296
297  grunt.registerTask('installAndGenerate', [
298    'CreateDirectories',
299    'shell:InstallThriftJS',
300    'shell:InstallThriftNodeJSDep',
301    'shell:ThriftGen',
302    'shell:ThriftGenDeepConstructor',
303    'shell:ThriftGenDoubleConstants',
304    'shell:InstallTestLibs',
305    'shell:ThriftBrowserifyNodeInt64',
306    'shell:ThriftGenInt64'
307  ]);
308
309  grunt.registerTask('test', [
310    'installAndGenerate',
311    'jshint',
312    'shell:ThriftTestServer',
313    'shell:ThriftTestServer_TLS',
314    'shell:ThriftTestServerES6',
315    'shell:ThriftTestServerES6_TLS',
316    'wait',
317    'qunit:ThriftDeepConstructor',
318    'qunit:ThriftJS',
319    'qunit:ThriftJS_TLS',
320    'qunit:ThriftJS_DoubleRendering',
321    'qunit:ThriftWS',
322    'qunit:ThriftJSJQ',
323    'qunit:ThriftJSJQ_TLS',
324    'qunit:ThriftWSES6',
325    'qunit:ThriftJS_Int64',
326    'shell:ThriftTestServer:kill',
327    'shell:ThriftTestServer_TLS:kill',
328    'shell:ThriftTestServerES6:kill',
329    'shell:ThriftTestServerES6_TLS:kill',
330  ]);
331  grunt.registerTask('default', ['test', 'concat', 'uglify', 'jsdoc']);
332};
333