-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyNow.js
248 lines (228 loc) · 9.02 KB
/
syNow.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
var readline = require('readline');
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var fs = require('fs-extra');
var chokidar = require('chokidar');
console.log("Running sy(nc)Now, a program that syncs ServiceNow scripts with local files. Type 'close' and press enter to stop this program.")
const args = process.argv;
const confFile = (args.length > 2 && args[2].endsWith(".conf.json")) ? args[2] : "sgn.conf.json";
const conf = JSON.parse(fs.readFileSync(confFile, "utf8"));
const stdin = process.openStdin();
var interface;
if (hasFlag("--setup")) {
enableSetup();
interface.question('What is your <instance> name of <instance>.service-now.com?: ', (answer) => {
conf.instance = answer;
interface.question('What is your user name?: ', (answer) => {
var name = answer;
interface.question('What is your password?: ', (answer) => {
conf.authorization = "Basic " + Buffer.from(name + ":" + answer).toString('base64');
interface.question('What is your application name? Leave blank to skip: ', (appName) => {
if (appName != undefined && appName != "") {
conf.app = appName;
} else {
conf.app = null;
console.log("Skipping pulling application data");
}
fs.outputFile(confFile, JSON.stringify(conf));
getAppFilesInfo(conf);
runAppSync(conf);
});
})
});
});
} else if (hasFlag("--cli")) {
enableCli();
} else if (hasFlag("--update-set")) {
var command = args[args.indexOf("--update-set") + 1];
if (command == "list") {
var xhttp = new XMLHttpRequest();
var link = "";
if (hasFlag("--app")) {
link = "https://" + conf.instance + ".service-now.com/api/x_149971_test/synow_us?app=" + args[args.indexOf("--app") + 1];
} else {
link = "https://" + conf.instance + ".service-now.com/api/x_149971_test/synow_us";
}
xhttp.open("GET", link, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", conf.authorization);
xhttp.send();
if (xhttp.responseText && xhttp.responseText.length < 1000) {
var response = JSON.parse(xhttp.responseText);
console.log(response.result);
}
process.exit(0);
} else if (command == "download") {
var app = args[args.indexOf("--app") + 1];
var name = args[args.indexOf("--name") + 1];
var xhttp = new XMLHttpRequest();
const link = "https://" + conf.instance + ".service-now.com/api/x_149971_test/synow_us?app=" + app + "&name=" + name;
xhttp.open("PUT", link, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", conf.authorization);
xhttp.send();
var downloadUrl = "https://" + conf.instance + ".service-now.com/" + JSON.parse(xhttp.responseText).result;
var xdownload = new XMLHttpRequest();
xdownload.open("GET", downloadUrl, false);
xdownload.setRequestHeader("Accept", "application/json");
xdownload.setRequestHeader("Content-Type", "application/json");
xdownload.setRequestHeader("Authorization", conf.authorization);
xdownload.send();
fs.outputFile("./" + app + "-" + name + ".xml", xdownload.responseText.toString(), "utf8", () => process.exit(0));
} else if (command == "set") {
var app = args[args.indexOf("--app") + 1];
var name = args[args.indexOf("--name") + 1];
var status = args[args.indexOf("--status") + 1];
var xhttp = new XMLHttpRequest();
const link = "https://" + conf.instance + ".service-now.com/api/x_149971_test/synow_us?app=" + app + "&name=" + name + "&status=" + status;
xhttp.open("POST", link, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", conf.authorization);
xhttp.send();
process.exit(0);
}
} else if (hasFlag("--exec")) {
var number = args.indexOf("--exec") + 1;
if (args.length > number) {
var filename = args[number];
fs.readFile(filename, "utf8", (err, data) => {
sendCommand(data);
process.exit(0);
});
} else {
console.log("You must provide filename of script you want to execute");
}
} else {
enableSetup();
if (!conf.authorization || !conf.instance) {
console.log("You have not configured the instance, please run this command with --setup flag");
} else {
getAppFilesInfo(conf);
runAppSync(conf);
}
}
function getAppFilesInfo(conf) {
if (conf.app != null) {
console.log("You are syncing application: " + conf.app);
const app = "https://" + conf.instance + ".service-now.com/api/now/table/sys_app";
var response = syncWidgetCode(app, '', "GET");
if (response.length > 0) {
var filtered = response.filter(e => e.name == conf.app).reduce(e => e);
const appFiles = "https://" + conf.instance + ".service-now.com/api/now/table/sys_metadata?sysparm_query=sys_scope=" + filtered.sys_id;
var appFilesData = syncWidgetCode(appFiles, '', "GET");
appFilesData.forEach(e => {
var element = conf.tables.filter(table => table.name == e.sys_class_name);
if (element.length > 0) {
element[0].sys_ids.push(e.sys_id);
} else {
//console.log(e.sys_class_name + " is not supported in this version");
}
});
}
}
}
function runAppSync(conf) {
conf.tables.forEach((table) => {
table.sys_ids.forEach((sys_id) => {
const link = "https://" + conf.instance + ".service-now.com/api/now/table/" + table.name + "/" + sys_id;
const response = syncWidgetCode(link, '');
table.files.forEach((e) => {
var foldername = response[table.foldername].split(" ").join("_").split("/").join("_")
fs.ensureDirSync("./" + table.name + "/" + foldername);
fs.outputFile("./" + table.name + "/" + foldername + "/" + e.filename, response[e.bodyname])
.then(chokidar.watch("./" + table.name + "/" + foldername + "/" + e.filename, {
persistent: true,
interval: conf.watcherFrequency
})
.on('change', (event, path) => {
console.log("Syncing " + event);
var data = fs.readFile("./" + table.name + "/" + foldername + "/" + e.filename, "utf8", (err, data) => {
if (err || !data) {} else {
var body = {};
body[e.bodyname] = data;
syncWidgetCode(link, JSON.stringify(body));
}
});
})
)
});
})
});
}
function syncWidgetCode(link, body, type = "PUT") {
var xhttp = new XMLHttpRequest();
xhttp.open(type, link, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", conf.authorization);
xhttp.send(body);
var response = JSON.parse(xhttp.responseText);
console.log(response);
return response.result;
}
var commands = [];
function executeCommand(command) {
commands.push(command);
var xhttp = new XMLHttpRequest();
const link = "https://" + conf.instance + ".service-now.com/api/x_149971_test/synow";
xhttp.open("PUT", link, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", conf.authorization);
xhttp.send(commands.join("\n"));
if (xhttp.responseText && xhttp.responseText.length < 1000) {
var response = JSON.parse(xhttp.responseText);
console.log(response.result);
}
}
function sendCommand(command) {
var xhttp = new XMLHttpRequest();
const link = "https://" + conf.instance + ".service-now.com/api/x_149971_test/synow";
xhttp.open("PUT", link, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("Authorization", conf.authorization);
xhttp.send(command);
if (xhttp.responseText && xhttp.responseText.length < 1000) {
var response = JSON.parse(xhttp.responseText);
console.log(response.result);
}
}
function enableCli() {
interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "snow> "
});
interface.on("line", (line) => {
switch (line.trim()) {
case "close":
console.log("Exiting the application!");
process.exit(0);
default:
executeCommand(line.trim());
break;
}
interface.prompt();
}).on("close", () => {
console.log("Exiting the application!");
process.exit(0);
})
interface.prompt();
}
function enableSetup() {
interface = readline.createInterface(process.stdin, process.stdout);
interface.setPrompt('');
interface.on('line', function (line) {
if (line === "exit") interface.close();
interface.prompt();
}).on('close', function () {
process.exit(0);
});
interface.prompt();
}
function hasFlag(arg) {
return args.filter(e => e == arg).length
}