-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (147 loc) · 5.06 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// spotty - Extract scripts from a Spotfire DXP file
// Usage:
// node spotty.js source.dxp ./output
// Result:
// JS will be placed in ./output/js and Python in ./output/python
const unzip = require('unzip-stream')
const parser = require('fast-xml-parser')
const fs = require('fs')
const debug = true
const verbose = false
const xmlParsingOpts = {
ignoreAttributes: false,
parseAttributeValue: true
}
var inputFile = ''
var outputDir = ''
inputFile = process.argv[2]
outputDir = process.argv[3]
getScripts().then(writeScripts).catch((err) => {
console.log('Error: ' + err)
})
function getScripts () {
return unzipResources().then(parseResources).then(unzipScripts).then(parseScripts)
}
function unzipResources () {
return extractFileFromZip(inputFile, 'EmbeddedResources.xml')
}
function unzipScripts (resourcePath) {
return extractFileFromZip(inputFile, resourcePath)
}
function parseResources (xmlData) {
return new Promise((resolve, reject) => {
if (debug) console.log('Parsing EmbeddedResources.xml to find EmbeddedScripts.xml')
if (parser.validate(xmlData) === false) {
reject(new Error('Invalid or nonexistent XML in EmbeddedResources.xml'))
}
let xmlObj = parser.getTraversalObj(xmlData, xmlParsingOpts)
if (
xmlObj.child.EmbeddedResources.count === 0 ||
xmlObj.child.EmbeddedResources[0].child.count === 0
) reject(new Error('No embedded resources in DXP file.'))
let foundScripts = false
for (let resource of xmlObj.child.EmbeddedResources[0].child.EmbeddedResource) {
if (verbose) console.log(resource.attrsMap)
let resourceName = resource.attrsMap['@_Name']
let filePath = resource.attrsMap['@_ArchiveElementPath']
if (resourceName === 'EmbeddedScripts.xml') {
if (debug) console.log('Found EmbeddedScripts.xml in file ' + filePath)
foundScripts = true
resolve(filePath)
}
}
if (foundScripts === false) reject(new Error('No scripts found embedded in DXP file.'))
})
}
function parseScripts (xmlData) {
return new Promise((resolve, reject) => {
if (parser.validate(xmlData) === false) {
reject(new Error('Invalid or nonexistent XML in EmbeddedScripts.xml'))
}
let xmlObj = parser.getTraversalObj(xmlData, xmlParsingOpts)
if (
xmlObj.child.EmbeddedScripts.count === 0 ||
xmlObj.child.EmbeddedScripts[0].child.EmbeddedScript.count === 0
) reject(new Error('No embedded resources in DXP file.'))
let scripts = []
for (let script of xmlObj.child.EmbeddedScripts[0].child.EmbeddedScript) {
let scriptDefinition = script.child.ScriptDefinition[0]
let attrs = scriptDefinition.attrsMap
if (verbose) console.log(attrs)
let parsedCode = scriptDefinition.child.ScriptCode[0].val
.replace(/_x09/g, '\t')
.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '<')
scripts.push({
name: attrs['@_Name'],
language_name: attrs['@_LanguageName'],
language_version: attrs['@_LanguageVersion'],
wrap_script: attrs['@_WrapScript'],
code: parsedCode
})
}
if (scripts.length > 0) {
resolve(scripts)
} else {
reject(new Error('No scripts found embedded in DXP file.'))
}
})
}
function writeScripts (scripts) {
return new Promise((resolve, reject) => {
ensureDirectory(outputDir)
for (let script of scripts) {
let subdir; let extension = ''
if (script.language_name === 'JavaScript') {
subdir = '/js'
extension = 'js'
} else if (script.language_name === 'IronPython') {
subdir = '/python'
extension = 'py'
} else {
subdir = script.language_name
extension = script.language_name
}
ensureDirectory(outputDir + subdir)
let outputPath = outputDir + subdir + '/' + script.name + '.' + extension
fs.writeFile(outputPath, script.code, (err) => {
if (err) reject(err)
if (verbose) console.log('Wrote file: ' + outputPath)
})
}
console.log(`Wrote ${scripts.length} script files to ${outputDir}`)
})
}
function extractFileFromZip (zipFile, filePath) {
return new Promise((resolve, reject) => {
if (debug) console.log(`Searching ${zipFile} for ${filePath}`)
var fileFound = false
fs.createReadStream(zipFile)
.pipe(unzip.Parse())
.on('entry', (entry) => {
if ((entry.path === filePath) && !fileFound) {
if (debug) console.log('Found file: ' + entry.path)
fileFound = true
let data = ''
entry.on('data', (chunk) => { data = data + chunk })
entry.on('end', () => {
if (verbose) console.log(data)
resolve(data)
entry.emit('close')
})
} else {
entry.autodrain()
}
})
.on('close', () => {
if (!fileFound) { reject(new Error('Could not find file ' + filePath + ' in file ' + zipFile)) }
})
.on('error', (err) => {
reject(err)
})
})
}
function ensureDirectory (path) {
if (!fs.existsSync(path)) fs.mkdirSync(path)
}