@jsreport/jsreport-cli 3.0.0-beta.2 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +16 -12
  3. package/cli.js +93 -93
  4. package/example.config.json +43 -43
  5. package/example.server.js +14 -14
  6. package/index.js +20 -22
  7. package/jsreport.config.js +8 -8
  8. package/lib/cliExtension.js +59 -59
  9. package/lib/{createCommandParser.js → commander/createCommandParser.js} +44 -45
  10. package/lib/commander/createCustomCommandBuilder.js +150 -0
  11. package/lib/commander/createLogger.js +51 -0
  12. package/lib/commander/executeCommand.js +37 -0
  13. package/lib/commander/getCommandEventName.js +4 -0
  14. package/lib/commander/index.js +279 -0
  15. package/lib/commander/jsreportInstance.js +98 -0
  16. package/lib/commander/registerCommand.js +91 -0
  17. package/lib/commander/startCommand.js +234 -0
  18. package/lib/commander/startProcessing.js +208 -0
  19. package/lib/commands/_initializeApp.js +206 -208
  20. package/lib/commands/configure.js +393 -395
  21. package/lib/commands/help.js +453 -454
  22. package/lib/commands/init.js +76 -77
  23. package/lib/commands/kill.js +85 -86
  24. package/lib/commands/render.js +490 -488
  25. package/lib/commands/repair.js +55 -56
  26. package/lib/commands/start.js +72 -74
  27. package/lib/commands/win-install.js +124 -126
  28. package/lib/commands/win-uninstall.js +100 -102
  29. package/lib/daemonHandler.js +164 -164
  30. package/lib/daemonInstance.js +225 -225
  31. package/lib/{registerExtensionsCommands.js → detectAndRegisterExtensionsCommands.js} +74 -74
  32. package/lib/instanceHandler.js +316 -316
  33. package/lib/keepAliveProcess.js +205 -205
  34. package/lib/{errorUtils.js → utils/error.js} +149 -127
  35. package/lib/{getTempPaths.js → utils/getTempPaths.js} +39 -39
  36. package/lib/{normalizePathOptionOrArg.js → utils/normalizePathOptionOrArg.js} +41 -41
  37. package/lib/{normalizeSocketPath.js → utils/normalizeSocketPath.js} +9 -9
  38. package/lib/{startSocketServer.js → utils/startSocketServer.js} +53 -53
  39. package/package.json +105 -106
  40. package/test/testUtils.js +197 -198
  41. package/lib/commander.js +0 -1108
@@ -1,316 +1,316 @@
1
- 'use strict'
2
-
3
- const path = require('path')
4
- const fs = require('fs')
5
- const isPromise = require('is-promise')
6
- const once = require('once')
7
-
8
- exports.initialize = function (instance, verbose) {
9
- if (!instance._initialized) {
10
- // explicitly silent jsreport logging if verboseMode is not activated
11
- if (!verbose) {
12
- if (instance.options.logger) {
13
- instance.options.logger.silent = true
14
- } else {
15
- instance.options.logger = {
16
- silent: true
17
- }
18
- }
19
- }
20
-
21
- // initializing jsreport instance
22
- return instance.init().then(() => {
23
- return instance
24
- }).catch((err) => {
25
- let msg = 'An error has occurred when trying to initialize jsreport'
26
-
27
- if (err.code === 'EADDRINUSE') {
28
- msg += ', seems like there is already a server running in port: ' + err.port
29
- }
30
-
31
- const errorToReject = new Error(msg)
32
-
33
- if (err.code != null) {
34
- errorToReject.code = err.code
35
- }
36
-
37
- errorToReject.originalError = err
38
-
39
- throw errorToReject
40
- })
41
- }
42
-
43
- return Promise.resolve(instance)
44
- }
45
-
46
- exports.find = function find (cwd) {
47
- return new Promise((resolve, reject) => {
48
- // finding entry point before activating CLI
49
- let isAlreadyResolved = false
50
- let existsPackageJson
51
- let jsreportModuleInfo
52
- let userPkg
53
- let jsreportEntryPoint
54
- let pathToJsreportEntryPoint
55
-
56
- try {
57
- existsPackageJson = fs.existsSync(path.join(cwd, './package.json'))
58
- jsreportModuleInfo = getJsreportModuleInstalled(cwd, existsPackageJson)
59
-
60
- if (!jsreportModuleInfo) {
61
- return reject(new Error(
62
- 'Couldn\'t find a jsreport installation necessary to continue with the execution of the command, ' +
63
- 'make sure to install jsreport first.'
64
- ))
65
- }
66
-
67
- if (!existsPackageJson) {
68
- // creating a default instance
69
- return resolve({
70
- from: jsreportModuleInfo.name,
71
- isDefault: true,
72
- instance: createDefaultInstance(
73
- jsreportModuleInfo.module
74
- )
75
- })
76
- }
77
-
78
- userPkg = require(path.join(cwd, './package.json'))
79
- jsreportEntryPoint = (userPkg.jsreport || {}).entryPoint
80
-
81
- if (!jsreportEntryPoint) {
82
- return resolve({
83
- from: jsreportModuleInfo.name,
84
- isDefault: true,
85
- instance: createDefaultInstance(
86
- jsreportModuleInfo.module
87
- )
88
- })
89
- }
90
-
91
- const pathToJsreportEntryPoint = path.resolve(cwd, jsreportEntryPoint)
92
-
93
- if (!fs.existsSync(pathToJsreportEntryPoint)) {
94
- throw new Error(`Couldn't find a jsreport entry point at: ${pathToJsreportEntryPoint}`)
95
- }
96
-
97
- const jsreportEntryPointExport = require(pathToJsreportEntryPoint)
98
-
99
- if (typeof jsreportEntryPointExport === 'function') {
100
- // prevents resolving an instance more than once
101
- const resolveInstanceOnce = once(resolveInstance)
102
- const entryPointExportResult = jsreportEntryPointExport(resolveInstanceOnce)
103
-
104
- if (isAlreadyResolved) {
105
- return
106
- }
107
-
108
- // check if function returns a promise,
109
- // otherwise just wait until user calls `resolveInstanceOnce`
110
- if (isPromise(entryPointExportResult)) {
111
- if (resolveInstanceOnce.called) {
112
- isAlreadyResolved = true
113
- return reject(createDuplicateResolutionError(pathToJsreportEntryPoint))
114
- }
115
-
116
- handlePromiseExport(entryPointExportResult, {
117
- entryPoint: pathToJsreportEntryPoint,
118
- resolveCheck: resolveInstanceOnce,
119
- jsreportModule: jsreportModuleInfo.module
120
- }, (err, instance) => {
121
- if (isAlreadyResolved) {
122
- return
123
- }
124
-
125
- isAlreadyResolved = true
126
-
127
- if (err) {
128
- return reject(err)
129
- }
130
-
131
- return resolve({
132
- from: jsreportModuleInfo.name,
133
- isDefault: false,
134
- instance: instance,
135
- entryPoint: pathToJsreportEntryPoint
136
- })
137
- })
138
- }
139
- } else if (isJsreportInstance(jsreportEntryPointExport, jsreportModuleInfo.module)) {
140
- return resolve({
141
- from: jsreportModuleInfo.name,
142
- isDefault: false,
143
- instance: jsreportEntryPointExport,
144
- entryPoint: pathToJsreportEntryPoint
145
- })
146
- } else {
147
- return reject(new Error(
148
- 'Entry point must return a valid jsreport instance or a function resolving to a jsreport instance, check file in ' +
149
- pathToJsreportEntryPoint
150
- ))
151
- }
152
- } catch (e) {
153
- const errorToReject = new Error('An error has occurred when trying to find a jsreport instance')
154
- errorToReject.originalError = e
155
-
156
- return reject(errorToReject)
157
- }
158
-
159
- function resolveInstance (err, instance) {
160
- if (isAlreadyResolved) {
161
- return
162
- }
163
-
164
- isAlreadyResolved = true
165
-
166
- if (err) {
167
- const errorToReject = new Error('An error has occurred when trying to find a jsreport instance')
168
- errorToReject.originalError = err
169
- return reject(errorToReject)
170
- }
171
-
172
- if (!isJsreportInstance(instance, jsreportModuleInfo.module)) {
173
- return reject(new Error(
174
- 'Callback in entry point must return a valid jsreport instance, check file in ' +
175
- pathToJsreportEntryPoint
176
- ))
177
- }
178
-
179
- resolve({
180
- from: jsreportModuleInfo.name,
181
- isDefault: false,
182
- instance: instance,
183
- entryPoint: pathToJsreportEntryPoint
184
- })
185
- }
186
- })
187
- }
188
-
189
- exports.isJsreportInstance = isJsreportInstance
190
-
191
- function createDuplicateResolutionError (pathToJsreportEntryPoint) {
192
- return new Error(
193
- 'jsreport instance is already resolved, are you using promise and callback at the same time? ' +
194
- 'you should only use one way to resolve the instance from entry point, check file in ' +
195
- pathToJsreportEntryPoint
196
- )
197
- }
198
-
199
- function isJsreportInstance (instance, jsreportModule) {
200
- if (!instance) {
201
- return false
202
- }
203
-
204
- // only check if jsreportModule is not null or undefined
205
- if (jsreportModule != null) {
206
- return instance instanceof jsreportModule.Reporter
207
- }
208
-
209
- // if no jsreportModule is passed try to check if "instance" looks
210
- // like a jsreport instance
211
- return (
212
- typeof instance.init === 'function' &&
213
- typeof instance.render === 'function' &&
214
- typeof instance.afterConfigLoaded === 'function' &&
215
- instance.extensionsManager != null &&
216
- instance.beforeRenderListeners != null &&
217
- instance.afterRenderListeners != null
218
- )
219
- }
220
-
221
- function createDefaultInstance (jsreportModule) {
222
- return jsreportModule()
223
- }
224
-
225
- function handlePromiseExport (promiseToInstance, options, cb) {
226
- const entryPoint = options.entryPoint
227
- const jsreportModule = options.jsreportModule
228
- const resolveCheck = options.resolveCheck
229
-
230
- promiseToInstance.then((jsreportInstance) => {
231
- if (resolveCheck.called) {
232
- return cb(createDuplicateResolutionError(entryPoint))
233
- }
234
-
235
- if (!isJsreportInstance(jsreportInstance, jsreportModule)) {
236
- return cb(
237
- new Error(
238
- 'Promise in entry point must resolve to a jsreport instance, check file in ' +
239
- entryPoint
240
- )
241
- )
242
- }
243
-
244
- cb(null, jsreportInstance)
245
- }).catch((getJsreportInstanceError) => {
246
- if (resolveCheck.called) {
247
- return cb(createDuplicateResolutionError(entryPoint))
248
- }
249
-
250
- cb(getJsreportInstanceError)
251
- })
252
- }
253
-
254
- function getJsreportModuleInstalled (cwd, existsPackageJson) {
255
- let detectedJsreport
256
- let detectedModule
257
-
258
- if (existsPackageJson) {
259
- const userPkg = require(path.join(cwd, './package.json'))
260
- const userDependencies = userPkg.dependencies || {}
261
-
262
- if (userDependencies.jsreport) {
263
- detectedJsreport = 'jsreport'
264
- } else if (userDependencies['@jsreport/jsreport-core']) {
265
- detectedJsreport = '@jsreport/jsreport-core'
266
- } else if (userDependencies['jsreport-core']) {
267
- detectedJsreport = 'jsreport-core'
268
- }
269
- }
270
-
271
- if (!detectedJsreport) {
272
- if (fs.existsSync(path.join(cwd, 'node_modules/jsreport'))) {
273
- detectedJsreport = 'jsreport'
274
- } else if (fs.existsSync(path.join(cwd, 'node_modules/@jsreport/jsreport-core'))) {
275
- detectedJsreport = '@jsreport/jsreport-core'
276
- } else if (fs.existsSync(path.join(cwd, 'node_modules/jsreport-core'))) {
277
- detectedJsreport = 'jsreport-core'
278
- }
279
- }
280
-
281
- if (!detectedJsreport) {
282
- return null
283
- }
284
-
285
- try {
286
- // always require top-level package from cwd
287
- detectedModule = require(require.resolve(path.join(cwd, 'node_modules', detectedJsreport)))
288
-
289
- detectedModule = {
290
- name: detectedJsreport,
291
- module: detectedModule
292
- }
293
- } catch (err) {
294
- if (
295
- process.env.cli_instance_lookup_fallback === 'enabled' &&
296
- err.code === 'MODULE_NOT_FOUND'
297
- ) {
298
- // if not found from top-level package try to search using
299
- // default node resolution
300
- try {
301
- detectedModule = require(require.resolve(require.resolve(detectedJsreport)))
302
-
303
- detectedModule = {
304
- name: detectedJsreport,
305
- module: detectedModule
306
- }
307
- } catch (e) {
308
- detectedModule = null
309
- }
310
- } else {
311
- detectedModule = null
312
- }
313
- }
314
-
315
- return detectedModule
316
- }
1
+ 'use strict'
2
+
3
+ const path = require('path')
4
+ const fs = require('fs')
5
+ const isPromise = require('is-promise')
6
+ const once = require('once')
7
+
8
+ exports.initialize = function (instance, verbose) {
9
+ if (!instance._initialized) {
10
+ // explicitly silent jsreport logging if verboseMode is not activated
11
+ if (!verbose) {
12
+ if (instance.options.logger) {
13
+ instance.options.logger.silent = true
14
+ } else {
15
+ instance.options.logger = {
16
+ silent: true
17
+ }
18
+ }
19
+ }
20
+
21
+ // initializing jsreport instance
22
+ return instance.init().then(() => {
23
+ return instance
24
+ }).catch((err) => {
25
+ let msg = 'An error has occurred when trying to initialize jsreport'
26
+
27
+ if (err.code === 'EADDRINUSE') {
28
+ msg += ', seems like there is already a server running in port: ' + err.port
29
+ }
30
+
31
+ const errorToReject = new Error(msg)
32
+
33
+ if (err.code != null) {
34
+ errorToReject.code = err.code
35
+ }
36
+
37
+ errorToReject.originalError = err
38
+
39
+ throw errorToReject
40
+ })
41
+ }
42
+
43
+ return Promise.resolve(instance)
44
+ }
45
+
46
+ exports.find = function find (cwd) {
47
+ return new Promise((resolve, reject) => {
48
+ // finding entry point before activating CLI
49
+ let isAlreadyResolved = false
50
+ let existsPackageJson
51
+ let jsreportModuleInfo
52
+ let userPkg
53
+ let jsreportEntryPoint
54
+ let pathToJsreportEntryPoint
55
+
56
+ try {
57
+ existsPackageJson = fs.existsSync(path.join(cwd, './package.json'))
58
+ jsreportModuleInfo = getJsreportModuleInstalled(cwd, existsPackageJson)
59
+
60
+ if (!jsreportModuleInfo) {
61
+ return reject(new Error(
62
+ 'Couldn\'t find a jsreport installation necessary to continue with the execution of the command, ' +
63
+ 'make sure to install jsreport first.'
64
+ ))
65
+ }
66
+
67
+ if (!existsPackageJson) {
68
+ // creating a default instance
69
+ return resolve({
70
+ from: jsreportModuleInfo.name,
71
+ isDefault: true,
72
+ instance: createDefaultInstance(
73
+ jsreportModuleInfo.module
74
+ )
75
+ })
76
+ }
77
+
78
+ userPkg = require(path.join(cwd, './package.json'))
79
+ jsreportEntryPoint = (userPkg.jsreport || {}).entryPoint
80
+
81
+ if (!jsreportEntryPoint) {
82
+ return resolve({
83
+ from: jsreportModuleInfo.name,
84
+ isDefault: true,
85
+ instance: createDefaultInstance(
86
+ jsreportModuleInfo.module
87
+ )
88
+ })
89
+ }
90
+
91
+ const pathToJsreportEntryPoint = path.resolve(cwd, jsreportEntryPoint)
92
+
93
+ if (!fs.existsSync(pathToJsreportEntryPoint)) {
94
+ throw new Error(`Couldn't find a jsreport entry point at: ${pathToJsreportEntryPoint}`)
95
+ }
96
+
97
+ const jsreportEntryPointExport = require(pathToJsreportEntryPoint)
98
+
99
+ if (typeof jsreportEntryPointExport === 'function') {
100
+ // prevents resolving an instance more than once
101
+ const resolveInstanceOnce = once(resolveInstance)
102
+ const entryPointExportResult = jsreportEntryPointExport(resolveInstanceOnce)
103
+
104
+ if (isAlreadyResolved) {
105
+ return
106
+ }
107
+
108
+ // check if function returns a promise,
109
+ // otherwise just wait until user calls `resolveInstanceOnce`
110
+ if (isPromise(entryPointExportResult)) {
111
+ if (resolveInstanceOnce.called) {
112
+ isAlreadyResolved = true
113
+ return reject(createDuplicateResolutionError(pathToJsreportEntryPoint))
114
+ }
115
+
116
+ handlePromiseExport(entryPointExportResult, {
117
+ entryPoint: pathToJsreportEntryPoint,
118
+ resolveCheck: resolveInstanceOnce,
119
+ jsreportModule: jsreportModuleInfo.module
120
+ }, (err, instance) => {
121
+ if (isAlreadyResolved) {
122
+ return
123
+ }
124
+
125
+ isAlreadyResolved = true
126
+
127
+ if (err) {
128
+ return reject(err)
129
+ }
130
+
131
+ return resolve({
132
+ from: jsreportModuleInfo.name,
133
+ isDefault: false,
134
+ instance: instance,
135
+ entryPoint: pathToJsreportEntryPoint
136
+ })
137
+ })
138
+ }
139
+ } else if (isJsreportInstance(jsreportEntryPointExport, jsreportModuleInfo.module)) {
140
+ return resolve({
141
+ from: jsreportModuleInfo.name,
142
+ isDefault: false,
143
+ instance: jsreportEntryPointExport,
144
+ entryPoint: pathToJsreportEntryPoint
145
+ })
146
+ } else {
147
+ return reject(new Error(
148
+ 'Entry point must return a valid jsreport instance or a function resolving to a jsreport instance, check file in ' +
149
+ pathToJsreportEntryPoint
150
+ ))
151
+ }
152
+ } catch (e) {
153
+ const errorToReject = new Error('An error has occurred when trying to find a jsreport instance')
154
+ errorToReject.originalError = e
155
+
156
+ return reject(errorToReject)
157
+ }
158
+
159
+ function resolveInstance (err, instance) {
160
+ if (isAlreadyResolved) {
161
+ return
162
+ }
163
+
164
+ isAlreadyResolved = true
165
+
166
+ if (err) {
167
+ const errorToReject = new Error('An error has occurred when trying to find a jsreport instance')
168
+ errorToReject.originalError = err
169
+ return reject(errorToReject)
170
+ }
171
+
172
+ if (!isJsreportInstance(instance, jsreportModuleInfo.module)) {
173
+ return reject(new Error(
174
+ 'Callback in entry point must return a valid jsreport instance, check file in ' +
175
+ pathToJsreportEntryPoint
176
+ ))
177
+ }
178
+
179
+ resolve({
180
+ from: jsreportModuleInfo.name,
181
+ isDefault: false,
182
+ instance: instance,
183
+ entryPoint: pathToJsreportEntryPoint
184
+ })
185
+ }
186
+ })
187
+ }
188
+
189
+ exports.isJsreportInstance = isJsreportInstance
190
+
191
+ function createDuplicateResolutionError (pathToJsreportEntryPoint) {
192
+ return new Error(
193
+ 'jsreport instance is already resolved, are you using promise and callback at the same time? ' +
194
+ 'you should only use one way to resolve the instance from entry point, check file in ' +
195
+ pathToJsreportEntryPoint
196
+ )
197
+ }
198
+
199
+ function isJsreportInstance (instance, jsreportModule) {
200
+ if (!instance) {
201
+ return false
202
+ }
203
+
204
+ // only check if jsreportModule is not null or undefined
205
+ if (jsreportModule != null) {
206
+ return instance instanceof jsreportModule.Reporter
207
+ }
208
+
209
+ // if no jsreportModule is passed try to check if "instance" looks
210
+ // like a jsreport instance
211
+ return (
212
+ typeof instance.init === 'function' &&
213
+ typeof instance.render === 'function' &&
214
+ typeof instance.afterConfigLoaded === 'function' &&
215
+ instance.extensionsManager != null &&
216
+ instance.beforeRenderListeners != null &&
217
+ instance.afterRenderListeners != null
218
+ )
219
+ }
220
+
221
+ function createDefaultInstance (jsreportModule) {
222
+ return jsreportModule()
223
+ }
224
+
225
+ function handlePromiseExport (promiseToInstance, options, cb) {
226
+ const entryPoint = options.entryPoint
227
+ const jsreportModule = options.jsreportModule
228
+ const resolveCheck = options.resolveCheck
229
+
230
+ promiseToInstance.then((jsreportInstance) => {
231
+ if (resolveCheck.called) {
232
+ return cb(createDuplicateResolutionError(entryPoint))
233
+ }
234
+
235
+ if (!isJsreportInstance(jsreportInstance, jsreportModule)) {
236
+ return cb(
237
+ new Error(
238
+ 'Promise in entry point must resolve to a jsreport instance, check file in ' +
239
+ entryPoint
240
+ )
241
+ )
242
+ }
243
+
244
+ cb(null, jsreportInstance)
245
+ }).catch((getJsreportInstanceError) => {
246
+ if (resolveCheck.called) {
247
+ return cb(createDuplicateResolutionError(entryPoint))
248
+ }
249
+
250
+ cb(getJsreportInstanceError)
251
+ })
252
+ }
253
+
254
+ function getJsreportModuleInstalled (cwd, existsPackageJson) {
255
+ let detectedJsreport
256
+ let detectedModule
257
+
258
+ if (existsPackageJson) {
259
+ const userPkg = require(path.join(cwd, './package.json'))
260
+ const userDependencies = userPkg.dependencies || {}
261
+
262
+ if (userDependencies.jsreport) {
263
+ detectedJsreport = 'jsreport'
264
+ } else if (userDependencies['@jsreport/jsreport-core']) {
265
+ detectedJsreport = '@jsreport/jsreport-core'
266
+ } else if (userDependencies['jsreport-core']) {
267
+ detectedJsreport = 'jsreport-core'
268
+ }
269
+ }
270
+
271
+ if (!detectedJsreport) {
272
+ if (fs.existsSync(path.join(cwd, 'node_modules/jsreport'))) {
273
+ detectedJsreport = 'jsreport'
274
+ } else if (fs.existsSync(path.join(cwd, 'node_modules/@jsreport/jsreport-core'))) {
275
+ detectedJsreport = '@jsreport/jsreport-core'
276
+ } else if (fs.existsSync(path.join(cwd, 'node_modules/jsreport-core'))) {
277
+ detectedJsreport = 'jsreport-core'
278
+ }
279
+ }
280
+
281
+ if (!detectedJsreport) {
282
+ return null
283
+ }
284
+
285
+ try {
286
+ // always require top-level package from cwd
287
+ detectedModule = require(require.resolve(path.join(cwd, 'node_modules', detectedJsreport)))
288
+
289
+ detectedModule = {
290
+ name: detectedJsreport,
291
+ module: detectedModule
292
+ }
293
+ } catch (err) {
294
+ if (
295
+ process.env.cli_instance_lookup_fallback === 'enabled' &&
296
+ err.code === 'MODULE_NOT_FOUND'
297
+ ) {
298
+ // if not found from top-level package try to search using
299
+ // default node resolution
300
+ try {
301
+ detectedModule = require(require.resolve(require.resolve(detectedJsreport)))
302
+
303
+ detectedModule = {
304
+ name: detectedJsreport,
305
+ module: detectedModule
306
+ }
307
+ } catch (e) {
308
+ detectedModule = null
309
+ }
310
+ } else {
311
+ detectedModule = null
312
+ }
313
+ }
314
+
315
+ return detectedModule
316
+ }