1 | var APIClient = require( '@bipsync/apiclient' ),
|
2 | os = require( 'os' )
|
3 | fs = require( 'fs' ),
|
4 | exec = require( 'child_process' ).exec;
|
5 |
|
6 | APIClient.generateReportTool( {
|
7 |
|
8 | 'formats' : [ 'docx' ],
|
9 |
|
10 | schema : {},
|
11 |
|
12 | fetch : function() {
|
13 |
|
14 | APIClient( 'http://devenvone-api.bipsync' ).then( function( client ) {
|
15 |
|
16 | var params = {
|
17 | select : 'id,title',
|
18 | limit : 10,
|
19 | dateFormat : 'm/d/Y'
|
20 | };
|
21 |
|
22 |
|
23 | client.request( 'v1/research', { body : params } )
|
24 | .then( function( response ) {
|
25 | client.output( {
|
26 | notes : response.results
|
27 | } );
|
28 | } );
|
29 |
|
30 | } );
|
31 |
|
32 | },
|
33 |
|
34 | render: function( helpers, view ) {
|
35 |
|
36 | var outputString = helpers.renderMustacheTemplate( this.template, view );
|
37 |
|
38 | fs.mkdtemp( `${os.tmpdir()}/report-`, function( err, folder ) {
|
39 | if ( err ) {
|
40 | return console.log( err );
|
41 | }
|
42 |
|
43 | const htmlFilePath = `${folder}/report-output.html`;
|
44 | fs.writeFile( `${htmlFilePath}`, outputString, function( err ) {
|
45 | if ( err ) {
|
46 | return console.log( err );
|
47 | }
|
48 |
|
49 | var command = `HOME=/tmp timeout 10m libreoffice --invisible --nologo --headless --norestore --nolockcheck --eventtesting --convert-to doc:"MS Word 2007 XML" --outdir ${folder} ${htmlFilePath}`;
|
50 |
|
51 | exec( command, function( err, stdout, stderr ) {
|
52 | if ( err ) {
|
53 | return console.log( err );
|
54 | }
|
55 |
|
56 | var outputFilePath = `${folder}/report-output.doc`;
|
57 |
|
58 | var readStream = fs.createReadStream( outputFilePath );
|
59 |
|
60 | readStream.on( 'end', () => {
|
61 | fs.unlink( htmlFilePath );
|
62 | fs.unlink( outputFilePath );
|
63 | } );
|
64 | readStream.pipe( process.stdout );
|
65 |
|
66 | } );
|
67 |
|
68 | } );
|
69 | } );
|
70 |
|
71 | },
|
72 |
|
73 | template : `<h1>This is a test!</h1>
|
74 | <ul>
|
75 | {{#notes}}
|
76 | <li>{{title}}</li>
|
77 | {{/notes}}
|
78 | </ul>`
|
79 |
|
80 | } );
|