-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·129 lines (106 loc) · 3.57 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
/**
* The main module
* @module H5Recorder
*/
const { exec } = require('child_process');
const express = require('express');
const http = require('http')
const path = require('path');
const fs = require('fs');
const Ajv = require('ajv');
const schema = require('./schema.json')
const {
Recorder,
OverlayHandler,
MergeHandler,
AMergeHandler,
LocalPageServer } = require('./Handlers')
const {
isServerNeeded,
getAdjustedModelURL } = LocalPageServer
var app = express();
let data = []
let ajv = new Ajv()
let jsonSchemaValidator = ajv.compile(schema)
let port = process.env ? (process.env.PORT||0) : 0;
// If port is omitted or is 0, the operating system will assign an arbitrary unused port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.
// see https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
// https://github.com/wekan/wekan/wiki/REST-API-Swimlanes
/**
* Executes the process: data ->(Recorder-> OverlayHandler-> MergeHandler-> MergeHandler)-> callback
* @param {Object} data - data
* @param {boolean} runInCmd - runInCmd
* @param {boolean} callback - callback
// * @exports process -
*/
function process(data, runInCmd, callback) {
let outputs = []
let toClean = {}
function errorCallback(err) {
Object.keys(toClean).forEach(fs.unlinkSync)
console.log(err)
callback(err)
}
Recorder(data, runInCmd).then((screenRecords) => {
// console.log('recorder/screenRecords', screenRecords)
OverlayHandler(data, screenRecords).then((overlayOutputs) => {
console.log('OverlayHandler Outputs:', overlayOutputs)
screenRecords.forEach((v) => { toClean[v] = true;})
AMergeHandler(data, overlayOutputs).then((aMergeOutputs) => {
console.log('AMergeHandler Outputs:', aMergeOutputs)
overlayOutputs.forEach((v) => { toClean[v] = true;})
MergeHandler(aMergeOutputs).then((overlayMergeOutput) => {
console.log('MergeHandler Outputs:', overlayMergeOutput)
if (aMergeOutputs && aMergeOutputs.length > 0) {
aMergeOutputs.forEach((v) => { toClean[v] = true;})
}
Object.keys(toClean).forEach(fs.unlinkSync)
console.log('final output', overlayMergeOutput)
callback(null, overlayMergeOutput)
}, (err)=> {
fs.unlinkSync(overlayMergeOutput)
errorCallback(err)
})
}, errorCallback)
}, errorCallback)
}, errorCallback)
}
/**
* Loop over data entries, starts local server if needed then trigger the process function
* @param {Object} data - data
* @param {boolean} runInCmd - runInCmd
* @param {boolean} callback - callback
// * @exports process -
*/
function main(data, runInCmd = false, callback) {
var valid = jsonSchemaValidator(data);
if (!valid) {
jsonSchemaValidator.errors.push(data)
return callback(jsonSchemaValidator.errors, null)
}
if (isServerNeeded) {
// http://expressjs.com/en/4x/api.html#app.listen
let server = http.createServer(app)
server.listen(port, () => {
let serverPort = server.address().port
data = getAdjustedModelURL(data, app, serverPort)
// server.close()
console.log('LocalPageServer: ', server.address().port) //process.argv[2]
process(data, runInCmd, (err, result) => {
server.close();
callback(err, result);
});
})
} else {
process(data, runInCmd, callback);
}
}
module.exports = function (infile, runInCmd = false, cb) {
if (cb) return main(infile, runInCmd, cb)
return new Promise(function (resolve, reject) {
main(infile, runInCmd, function (err, data) {
if (err) return reject(err)
resolve(data)
})
})
}