zudello-execute-local 1.0.0

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 (2) hide show
  1. package/index.js +271 -0
  2. package/package.json +25 -0
package/index.js ADDED
@@ -0,0 +1,271 @@
1
+ #!/usr/bin/env node
2
+
3
+ const yargs = require('yargs')
4
+ const dotenv = require('dotenv')
5
+ const fs = require('fs')
6
+ const path = require('path')
7
+ const { NodeVM, makeResolverFromLegacyOptions } = require('vm2')
8
+
9
+ const argv = yargs
10
+ .option('script', {
11
+ alias: 's',
12
+ description: 'Path to the script file',
13
+ type: 'string',
14
+ demandOption: true
15
+ })
16
+ .option('env', {
17
+ alias: 'e',
18
+ description: 'Path to the .env file',
19
+ type: 'string',
20
+ demandOption: true
21
+ })
22
+ .option('handlerName', {
23
+ alias: 'hn',
24
+ description: 'Handler function name',
25
+ type: 'string',
26
+ default: 'handle'
27
+ })
28
+ .option('data', {
29
+ alias: 'd',
30
+ description: 'Provided data that are passed to the script handler function (should be in JSON format)',
31
+ type: 'string',
32
+ default: '{}'
33
+ })
34
+ .help()
35
+ .alias('help', 'h')
36
+ .argv
37
+
38
+ dotenv.config({ path: argv.env })
39
+
40
+ const init = async () => {
41
+ let data = {}
42
+ let scriptContent = ''
43
+
44
+ console.log(argv)
45
+
46
+ try {
47
+ data = JSON.parse(argv.data)
48
+ } catch (err) {
49
+ console.error('Error: Invalid JSON provided in --data')
50
+ process.exit(1)
51
+ }
52
+
53
+ const scriptPath = path.resolve(argv.script)
54
+
55
+ try {
56
+ scriptContent = fs.readFileSync(scriptPath, 'utf8')
57
+ } catch (err) {
58
+ console.error(`Error: Unable to read script file at ${scriptPath}`)
59
+ console.error(err.message)
60
+ process.exit(1)
61
+ }
62
+
63
+ await runScript(argv.handlerName, scriptContent, data)
64
+ }
65
+
66
+ const initNodeVM = () => {
67
+ return new NodeVM({
68
+ console: 'inherit',
69
+ sandbox: {
70
+ process
71
+ },
72
+ require: makeResolverFromLegacyOptions({
73
+ external: {
74
+ modules: ['lodash', 'performance-now', 'moment', 'moment-timezone', 'he', 'zudello-integration-sdk']
75
+ },
76
+ builtin: ['https'],
77
+ root: path.resolve(__dirname),
78
+ import: ['lodash', 'performance-now', 'moment', 'moment-timezone', 'he', 'zudello-integration-sdk'],
79
+ mock: {
80
+ fs: {
81
+ readFileSync() {
82
+ return 'Nice try!'
83
+ }
84
+ }
85
+ }
86
+ })
87
+ })
88
+ }
89
+
90
+ const runScript = async (handler, source, object) => {
91
+ const nodeVM = initNodeVM()
92
+
93
+ const authData = {
94
+ team_uuid: process.env.AUTH_TEAM_UUID,
95
+ jwt_token: process.env.AUTH_JWT_TOKEN,
96
+ organization_uuid: process.env.AUTH_ORGANIZATION_UUID,
97
+ api_url: process.env.AUTH_API_URL,
98
+ api_version: process.env.AUTH_API_VERSION,
99
+ external_api_url: process.env.AUTH_EXTERNAL_API_URL
100
+ }
101
+
102
+ const externalIntegrationName = process.env.EXTERNAL_INTEGRATION_NAME
103
+ const externalConnectionUUID = process.env.EXTERNAL_CONNECTION_UUID
104
+
105
+ const triggerAuthenticate = `
106
+ const auth = new Auth({
107
+ teamUUID: '${authData.team_uuid}',
108
+ token: '${authData.jwt_token}'
109
+ })
110
+ await auth.authenticate()
111
+ `
112
+
113
+ const triggerZudello = `
114
+ const Zudello = new ZudelloSDK(auth, '${authData.organization_uuid}', '${authData.api_url}', '${authData.api_version}', logger)
115
+ `
116
+
117
+ let triggerExternal = ''
118
+
119
+ if (externalIntegrationName && externalConnectionUUID) {
120
+ switch (externalIntegrationName) {
121
+ case 'netsuite':
122
+ triggerExternal = `
123
+ const Netsuite = new NetsuiteSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
124
+ const NetsuiteSOAP = new NetsuiteSoapSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
125
+ `
126
+ break
127
+ case 'intacct':
128
+ triggerExternal = `
129
+ const Intacct = new IntacctSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
130
+ `
131
+ break
132
+ case 'bc':
133
+ triggerExternal = `
134
+ const BC = new BusinessCentralSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
135
+ `
136
+ break
137
+ case 'zenoti':
138
+ triggerExternal = `
139
+ const Zenoti = new ZenotiSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
140
+ `
141
+ break
142
+ case 'dear':
143
+ triggerExternal = `
144
+ const Dear = new DearSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
145
+ `
146
+ break
147
+ case 'nexvia':
148
+ triggerExternal = `
149
+ const Nexvia = new NexviaSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
150
+ `
151
+ break
152
+ case 'fo':
153
+ triggerExternal = `
154
+ const FO = new FoSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
155
+ `
156
+ break
157
+ case 'retailExpress':
158
+ triggerExternal = `
159
+ const RetailExpress = new RetailExpressSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
160
+ `
161
+ break
162
+ case 'myobAcumatica':
163
+ triggerExternal = `
164
+ const MYOBAcumatica = new MYOBAcumaticaSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
165
+ `
166
+ break
167
+ case 'sybiz':
168
+ triggerExternal = `
169
+ const Sybiz = new SybizSDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
170
+ `
171
+ break
172
+ case 'jobready':
173
+ triggerExternal = `
174
+ const JobReady = new JobReadySDK(auth, '${externalConnectionUUID}', '${authData.organization_uuid}', '${authData.external_api_url}', '${authData.api_version}', logger)
175
+ `
176
+ break
177
+ }
178
+ }
179
+
180
+ let scriptFunction = nodeVM.run(`module.exports = async function(callback) {
181
+ try {
182
+ const {
183
+ Auth,
184
+ ZudelloSDK,
185
+ NetsuiteSDK,
186
+ NetsuiteSoapSDK,
187
+ IntacctSDK,
188
+ BusinessCentralSDK,
189
+ ZenotiSDK,
190
+ DearSDK,
191
+ NexviaSDK,
192
+ FoSDK,
193
+ RetailExpressSDK,
194
+ MYOBAcumaticaSDK,
195
+ SybizSDK,
196
+ JobReadySDK,
197
+ Logger,
198
+ Metadata,
199
+ Tags,
200
+ Properties,
201
+ MiscHelper,
202
+ ModelHelper,
203
+ S3Client
204
+ } = require('zudello-integration-sdk')
205
+ const _ = require('lodash')
206
+ const now = require('performance-now')
207
+ const moment = require('moment-timezone')
208
+ const he = require('he')
209
+
210
+ const logger = new Logger()
211
+ const metadata = new Metadata()
212
+ const properties = new Properties()
213
+ const tags = new Tags()
214
+ const misc = new MiscHelper()
215
+
216
+ ${triggerAuthenticate}
217
+ ${triggerZudello}
218
+ ${triggerExternal}
219
+
220
+ let Model
221
+
222
+ if (typeof Zudello !== 'undefined') {
223
+ Model = new ModelHelper(Zudello, logger)
224
+ }
225
+
226
+ ${source}
227
+
228
+ const startTime = now()
229
+
230
+ let result = null
231
+ let status = 'success'
232
+ let error = ''
233
+
234
+ try {
235
+ result = await ${handler}(${JSON.stringify(object)})
236
+ } catch (e) {
237
+ logger.error('Script Failed', e)
238
+ status = 'failed'
239
+ error = e.message
240
+ }
241
+
242
+ logger.info('status', status)
243
+
244
+ if (error) {
245
+ logger.info('error', error)
246
+ }
247
+
248
+ const endTime = now()
249
+ const duration = endTime - startTime
250
+
251
+ logger.info('duration', duration)
252
+
253
+ callback(null, result)
254
+ } catch (error) {
255
+ callback(error)
256
+ }
257
+ }`, path.resolve(__dirname, 'index.js'))
258
+
259
+ return new Promise((resolve, reject) => {
260
+ scriptFunction((err, response) => {
261
+ if (err) {
262
+ console.error(err)
263
+ reject(new Error(`Error executing script: ${err.message}`))
264
+ } else {
265
+ resolve(response)
266
+ }
267
+ })
268
+ })
269
+ }
270
+
271
+ init()
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "zudello-execute-local",
3
+ "version": "1.0.0",
4
+ "description": "Zudello Execute tool for local runs",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "zudello-execute": "./index.js"
8
+ },
9
+ "scripts": {},
10
+ "author": "Zudello",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "dotenv": "^16.3.1",
14
+ "fs": "^0.0.1-security",
15
+ "he": "^1.2.0",
16
+ "lodash": "^4.17.21",
17
+ "moment": "^2.30.1",
18
+ "moment-timezone": "^0.5.46",
19
+ "path": "^0.12.7",
20
+ "performance-now": "^2.1.0",
21
+ "vm2": "^3.9.19",
22
+ "yargs": "^17.7.2",
23
+ "zudello-integration-sdk": "^1.0.0"
24
+ }
25
+ }