-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
69 lines (51 loc) · 1.46 KB
/
cli.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
#!/usr/bin/env node
"use strict";
const path = require("path");
const resolve = require("path").resolve;
const fs = require("fs-extra");
const processor = require("./processor");
const pino = require("pino");
const logger = pino({
prettyPrint: true
});
function getConfig() {
function readConfig() {
const file = path.join(process.cwd(), "sol-flattener.json");
if(!fs.pathExistsSync(file)) {
return {};
}
const contents = fs.readFileSync(file);
const config = JSON.parse(contents.toString());
return config;
};
var config = readConfig();
const args = process.argv;
if(args.length > 6) {
logger.error(`Invalid command ${process.argv.join(" ")}`);
return;
}
if(args.length > 2) {
config.source = args[2];
config.destination = resolve(args[3]);
config.search = args[4];
config.removeRedundantEmptyLines = (args[5] || "").toLowerCase().startsWith("t");
}
if(config.source) {
config.source = resolve(config.source);
config.destination = resolve(config.destination);
config.search = (config.search || "").split(",").map(function(item) {
return item.trim();
});
}
return config;
}
const config = getConfig();
if(!config.source) {
logger.error("Invalid operation.");
return;
}
let contents = processor.process(config.source, config.search);
if(config.removeRedundantEmptyLines) {
contents = contents.replace(/\n\s*\n\s*\n/g, "\n\n");
}
fs.writeFileSync(config.destination, contents);