This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
bostr_cli.js
executable file
·103 lines (92 loc) · 3.36 KB
/
bostr_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
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
#!/usr/bin/env node
"use strict";
const { version } = require("./package.json");
const { nip19 } = require("nostr-tools")
const fs = require("fs");
const cluster = require("cluster");
const argv = process.argv.slice(2);
if (cluster.isPrimary) console.log(`Bostr ${version}\n`);
function showHelp() {
console.log(
"Usage: bostr [command] (argv)\n" +
"Available command:\n" +
" makeconf [conffile] - Make config file\n" +
" checkconf [conffile] - Check config file\n" +
" start [conffile] - Run bostr with specified config\n" +
" hexconverter [nip19] - Convert NIP-19 string to hex\n" +
" testworker [conffile] - Test worker with specified bostr config\n" +
" help - Show this help text\n\n" +
"Software is licensed under BSD-3-Clause\n" +
"https://codeberg.org/Yonle/bostr"
);
}
function readPath(p) {
return p.startsWith("/") ? p : process.cwd() + "/" + p;
}
switch (argv[0]) {
case "makeconf":
if (!argv[1]) return console.log("Usage: bostr makeconf [conffile]");
if (fs.existsSync(argv[1])) {
console.error("The specified config already exists.");
return process.exit(8);
}
fs.copyFileSync(__dirname + "/config.js.example", argv[1]);
console.log(`Succesfully copied example config file into ${argv[1]}`);
console.log(`Edit ${argv[1]} with your editor and start with the following command:`);
console.log(` $ bostr start ${argv[1]}\n`);
break;
case "checkconf": {
if (!argv[1]) return console.log("Usage: bostr checkconf [conffile]");
if (!fs.existsSync(argv[1])) {
console.error("Config not exists.");
return process.exit(254);
}
const masterConf = Object.keys(require("./config.js.example"));
const currentConf = Object.keys(require(readPath(argv[1])));
const unknown = currentConf.filter(i => !masterConf.includes(i))
const missing = masterConf.filter(i => !currentConf.includes(i))
if (unknown.length) console.log("Unknown Field:\n- " + unknown.join("\n- "));
if (missing.length) {
console.log("Missing Field:\n- " + missing.join("\n- "));
console.log("\nPlease check your config and try again.");
return process.exit(1);
} else {
console.log("\nNo config changes needed.");
}
break;
}
case "start":
if (!argv[1]) return console.log("Usage: bostr start [conffile]");
if (!fs.existsSync(argv[1])) {
console.error("Config not exists.");
return process.exit(254);
}
process.env.BOSTR_CONFIG_PATH = readPath(argv[1]);
require("./index.js");
break;
case "testworker":
if (!argv[1]) return console.log("Usage: bostr testworker [conffile]");
if (!fs.existsSync(argv[1])) {
console.error("Config not exists.");
return process.exit(254);
}
process.env.BOSTR_CONFIG_PATH = readPath(argv[1]);
console.log("Press CTRL + C to stop the test.");
console.log("Using config:", process.env.BOSTR_CONFIG_PATH);
require("./testworker.js");
break;
case "hexconverter":
if (!argv[1]) return console.log("Usage: bostr hexconverter [npub|nsec|....] ....");
for (const i of argv.slice(1)) {
console.log(nip19.decode(i).data);
}
break;
default:
if (argv[0] && (argv[0] !== "help")) {
console.error("Unrecognized command:", argv[0]);
return process.exit(100)
}
showHelp();
process.exit(1);
break;
}