-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManifestUpdate.js
executable file
·102 lines (95 loc) · 3.9 KB
/
ManifestUpdate.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
/****************************************************************************************************/
/* Update Extensions TREX File */
/* */
/* The XML schema for TREX files has changed. This script can be used to update an existing TREX */
/* to the new format. Pass in the path to a file or a directory. Requires node.js. */
/* */
/* Usage: */
/* */
/* node ManifestUpdate.js C:/myFolder */
/* */
/* node ManifestUpdate.js C:/myFolder/oldManifest.trex */
/****************************************************************************************************/
const fs = require("fs");
const readline = require("readline");
const path = require("path");
const eol = require('os').EOL;
const tagsToDelete = ["addin-type", "dashboard-addin"]
const contentToReplace = {
"http://wwww.tableau.com/xml/addin_manifest": "http://wwww.tableau.com/xml/extension_manifest",
"tableau-addin": "dashboard-extension",
"addin-version": "extension-version"
}
function updateManifestsInDir(dirPath) {
fs.readdir(dirPath, (err, files) => {
files.forEach((file) => {
if (path.extname(file) == ".trex") {
updateManifestFile(path.join(dirPath, file));
}
});
});
}
function processLine(lines, line) {
var fileUpdated = false;
for (i in tagsToDelete) {
if (line.includes(tagsToDelete[i])) {
fileUpdated = true;
return fileUpdated;
}
}
const toReplace = Object.keys(contentToReplace);
toReplace.forEach((tag) => {
if (line.includes(tag)) {
line = line.replace(tag, contentToReplace[tag]);
fileUpdated = true;
}
});
lines.push(line + eol);
return fileUpdated;
}
function updateManifestFile(filePath) {
const parser = path.parse(filePath);
const outputFile = parser.name + "_updated" + parser.ext;
const outputPath = path.join(parser.dir, outputFile);
if (fs.existsSync(outputPath)) {
console.log(`${outputFile} already exists. No changes Made`);
return;
}
const rs = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: rs,
});
var fileUpdated = false;
var newLines = [];
rl.on('line', (input) => {
if(processLine(newLines, input)) {
fileUpdated = true;
}
});
rl.on('close', () => {
rs.close();
if (fileUpdated) {
const ws = fs.createWriteStream(outputPath);
newLines.forEach((newLine) => {
ws.write(newLine);
});
console.log(`Updated File: ${outputFile}`);
} else {
console.log(`No Updates Made to: ${parser.name + parser.ext}`);
}
});
}
function main() {
const inputPath = process.argv.slice(2)[0];
fs.stat(inputPath, (err, stats) => {
if (err || !(stats.isDirectory() || stats.isFile())) {
console.error(inputPath + ": is not a valid directory or file");
}
if (stats.isDirectory()) {
updateManifestsInDir(inputPath);
} else if (stats.isFile()) {
updateManifestFile(inputPath);
}
});
}
main();