-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·70 lines (58 loc) · 1.83 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
#!/usr/bin/env node
//dependency for file handling
var fs = require("fs");
//dependency to get the file extension
var path = require("path");
//dependency for taking inputs from CLI using flags
const args = require("yargs").argv;
//dependency to parse yaml to json
const yaml = require("js-yaml");
//dependency for replacing tokens in a file
const Jtr = require("json-token-replace");
const jtr = new Jtr();
//check for the inputfile present or not
if (args.inputfile !== undefined) {
var filePath = args.inputfile;
var fileContents = fs.readFileSync(filePath, "utf-8");
var fileExtension = path.extname(filePath);
if (fileExtension !== ".yaml" && fileExtension !== ".json") {
console.error("Please provide a valid json or yaml file");
return;
}
} else {
console.error("Please provide a valid file path");
return;
}
//check whether tokensfile present or not
if (args.tokensfile !== undefined) {
var tokensPath = args.tokensfile;
var tokens = fs.readFileSync(tokensPath, "utf-8");
var tokensExtension = path.extname(tokensPath);
if (tokensExtension !== ".yaml" && tokensExtension !== ".json") {
console.error("Please provide a valid json or yaml file");
return;
}
} else {
console.error("Please provide a valid file containing token values");
return;
}
//if yaml file then convert to json
if (fileExtension === ".yaml") {
fileContents = yaml.load(fileContents);
} else {
fileContents = JSON.parse(fileContents);
}
//if tokens file is a yaml file then convert to json
if (tokensExtension === ".yaml") {
tokens = yaml.load(tokens);
} else {
tokens = JSON.parse(tokens);
}
let output = jtr.replace(tokens, fileContents, "$(", ")");
if (fileExtension === ".yaml") {
output = yaml.safeDump(output);
} else {
output = JSON.stringify(output);
}
console.log(output);
fs.writeFileSync(filePath, output, "utf-8");