-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
cli.ts
executable file
·63 lines (50 loc) · 1.39 KB
/
cli.ts
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
import urlParse from "url-parse";
import path from "path";
import chalk from "chalk";
import { processUrl } from "./processUrl";
import { validateConfig } from "./validateConfig";
import { createAPI } from "./createAPI";
const [, , ...args] = process.argv;
const finicky = createAPI({
log(message) {
console.log(chalk`{dim [log]} ${message}`);
},
notify(title, subtitle) {
console.log(chalk`{dim [notification]} {bold ${title}} ${subtitle}`);
}
});
function errorMessage(message: string, exception: Error | string) {
console.log(
chalk`{red.bold Error:} ${message} {red ${exception.toString()}}`
);
}
// @ts-ignore
global.finicky = finicky;
const configPath = path.resolve(
process.cwd(),
args[1] || "./.finicky.example.js"
);
const url = args[0] || "https://example.com/test";
const protocol = urlParse(url).protocol.replace(":", "");
console.log(chalk`Opening configuration file {dim ${configPath}}`);
let config;
try {
config = require(configPath);
} catch (ex) {
errorMessage("Couldn't open configuration file. ", ex);
process.exit(1);
}
try {
validateConfig(config);
} catch (ex) {
errorMessage("Couldn't validate configuration. ", ex);
process.exit(1);
}
try {
const result = processUrl(config, url);
console.log(chalk`Result:
{green ${JSON.stringify(result, null, 2)}}`);
} catch (ex) {
errorMessage("Couldn't validate result. ", ex);
process.exit(1);
}