-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
146 lines (121 loc) · 4.7 KB
/
index.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
'use strict'
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')
const JasperError = require('./JasperError')
class JasperNode {
constructor(resourcePath) {
this.executable = __dirname + '/JasperStarter/bin/jasperstarter'
this.resourcePath = resourcePath || null
}
compile(inputFile, outputFile = null) {
this.inputFile = inputFile
this.outputFile = outputFile
this.format = null
this.task = 'compile'
this.command = this.executable
this.command += ' compile ' + inputFile
if (outputFile) this.command += ' -o ' + outputFile
return this
}
process(inputFile, outputFile, parameters, connections = null, format = 'pdf') {
this.inputFile = inputFile
this.outputFile = outputFile
this.format = format
this.task = 'process'
if (!this.resourcePath) this.resourcePath = path.dirname(this.inputFile)
this.command = this.executable
this.command += ' process '
this.command += inputFile
this.command += ' -o ' + outputFile
this.command += ' -f ' + format
this.command += ' -r ' + this.resourcePath
if (parameters) {
this.command += ' -P'
for (var key in parameters) {
this.command += ` ${key}=${parameters[key]}`
}
}
if (connections) {
this.command += ' -t ' + connections.driver
this.command += ' -H ' + connections.host
this.command += ' -n ' + connections.database
this.command += ' -u ' + connections.username
// optional parameters
if (connections.hasOwnProperty('password')) this.command += ' -p ' + connections.password
if (connections.hasOwnProperty('port')) this.command += ' --db-port ' + connections.port
if (connections.hasOwnProperty('sid')) this.command += ' --db-sid ' + connections.sid
}
return this
}
listParameters(inputFile) {
this.inputFile = inputFile
this.outputFile = null
this.format = null
this.task = 'listParameters'
this.command = this.executable
this.command += ' list_parameters ' + inputFile
return this
}
output() {
return new Promise((resolve, reject) => {
resolve(this.command)
})
}
/**
* @returns path of file OR
* @returns list params OR
* @returns JasperError
*/
execute() {
return new Promise((resolve, reject) => {
exec(this.command, (error, stdout, stderr) => {
if (error) {
reject(new JasperError(stderr))
} else {
if (this.task == 'process' || this.task == 'listParameters') {
const ext = path.extname(this.inputFile)
let file = null
if (ext == '.jasper') {
file = path.basename(this.inputFile, '.jasper')
} else {
file = path.basename(this.inputFile, '.jrxml')
}
const path1 = `${this.outputFile}.${this.format}`
const path2 = `${this.outputFile}${path.sep}${file}.${this.format}`
if (this.outputFile) {
if (fs.existsSync(path1)) {
resolve(path1)
} else {
resolve(path2)
}
} else {
resolve(stdout)
}
} else { // this.task == compile
const filename = path.basename(this.inputFile, '.jrxml')
const path1 = `${path.dirname(this.inputFile)}${path.sep}${filename}.jasper`
if (fs.existsSync(path1)) {
resolve(path1)
} else {
const path2 = `${this.outputFile}.jasper`
const path3 = `${this.outputFile}${path.sep}${filename}.jasper`
if (fs.existsSync(path2)) {
resolve(path2)
} else {
resolve(path3)
}
}
}
}
})
})
}
static quotes(str) {
return `\"${str}\"`
}
quotes(str) {
return JasperNode.quotes(str)
}
}
module.exports = JasperNode