-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·290 lines (270 loc) · 10.6 KB
/
cli.js
File metadata and controls
executable file
·290 lines (270 loc) · 10.6 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Author: Daan van den Bergh
* Copyright: © 2022 - 2023 Daan van den Bergh.
*/
// ---------------------------------------------------------
// Imports.
const {vlib} = require("./vinc.js")
const {Client, Config} = require("./libris.js")
// ---------------------------------------------------------
// Global constants.
const DOCUMENTATION = "https://libris.com/docs"
const CONFIG_PATHS = [
"./libris",
"./libris.json",
"./libris.js",
"./docs/config",
"./docs/config.json",
"./docs/config.js",
];
const CONFIG_PATHS_STR = `"./libris", "./libris.json", "./libris.js", "./docs/config", "./docs/config.json" and "./docs/config.js"`;
// ---------------------------------------------------------
// CLI.
// Load or find the configuration file.
const load_config = (path = null, source = "") => {
if (path == null) {
const path = CONFIG_PATHS.iterate((subpath) => {
const path = new vlib.Path(source + subpath);
if (path.exists()) {
return Config.load(path);
}
})
if (path == null) {
throw Error(`Unable to find a configuration file, either specify a the path to the configuration file using argument "--config". Or create a configuration file at one of the following sub paths: ${CONFIG_PATHS_STR}.`);
}
}
return Config.load(path);
}
// Create the CLI.
/* @docs:
* @parse: false
* @lang: CLI
* @name: CLI
* @title: {{BRAND_NAME}} CLI
* @description: The {{BRAND_NAME}} CLI.
* @usage:
* @CLI:
* $ libris --generate --config /path/to/config.json
*/
const CLI = new vlib.CLI({name: "libris", version: "1.1.1", commands: [
// Generate.
/* @docs:
* @parse: false
* @lang: CLI
* @parent: CLI
* @title: Generate
* @name: --generate
* @description: Generate a documentation page based on your configuration.
* @param:
* @name: --source
* @type: string
* @desc: The source path to the package, the configuration file will be automatically loaded. It must reside at one of the following sub paths {{CONFIG_PATHS_STR}}.
* @con_required: true
* @param:
* @name: --sources
* @type: array
* @desc: The source paths of the packages, the configuration file will be automatically loaded. It must reside at one of the following sub paths {{CONFIG_PATHS_STR}}.
* @con_required: true
* @param:
* @name: --config
* @type: string
* @desc: The path to the configuration file.
* @con_required: true
* @param:
* @name: --configs
* @type: array
* @desc: The paths to the configuration files.
* @con_required: true
* @param:
* @name: --api-key
* @type: string
* @desc: The {{BRAND_NAME}} API key, when undefined the environment variable `LIBRIS_API_KEY` will be used.
* @required: false
* @usage:
* @CLI:
* $ libris --generate
* $ libris --generate --source path/to/my/package
* $ libris --generate --sources path/to/my/package1,path/to/my/package2
* $ libris --generate --config path/to/my/package/config.json
* $ libris --generate --configs path/to/my/package1/config.json,path/to/my/package2/config.json
* $ libris --generate --api-key ...
*/
{
id: "--generate",
description: "Generate a documentation page based on your configuration.",
examples: {
"Generate": "libris --generate",
"Generate": "libris --generate --source path/to/my/package",
"Generate": "libris --generate --sources path/to/my/package1,path/to/my/package2",
"Generate": "libris --generate --config path/to/my/package/config.json",
"Generate": "libris --generate --configs path/to/my/package1/config.json,path/to/my/package2/config.json",
"Generate": "libris --generate --api-key ...",
},
args: [
{id: "--source", type: "string", description: `The source path to the package, the configuration file will be automatically loaded. It must reside at one of the following sub paths ${CONFIG_PATHS_STR}.`},
{id: "--sources", type: "array", description: `The source paths of the packages, the configuration file will be automatically loaded. It must reside at one of the following sub paths ${CONFIG_PATHS_STR}.`},
{id: "--config", type: "string", description: `The path to the configuration file.`},
{id: "--configs", type: "array", description: `The paths to the configuration files.`},
{id: "--api-key", type: "string", description: `The Libris API key, when undefined the environment variable "LIBRIS_API_KEY" will be used.`},
],
callback: async ({
source = null,
sources = null,
config = null,
configs = null,
api_key = null,
_command = null,
}) => {
// Load all clients based on the arguments.
let clients = [];
// By single config.
if (config !== null) {
clients.push(new Client({
config: load_config(config),
api_key: api_key,
}));
}
// By configs.
else if (configs !== null) {
configs.iterate((config) => {
clients.push(new Client({
config: load_config(config),
api_key: api_key,
}));
})
}
// By single source.
else if (source !== null) {
clients.push(new Client({
config: load_config(null, source),
api_key: api_key,
}));
}
// By sources.
else if (sources !== null) {
sources.iterate((source) => {
clients.push(new Client({
config: load_config(null, source),
api_key: api_key,
}));
})
}
// Invalid arguments.
else {
cli.error(`One of the following arguments must be defined "--source", "--sources", "--config" or "--configs".`);
cli.docs(_command);
process.exit(1);
}
// Iterate all clients.
await clients.iterate_async_await(async (client) => {
if (client.config.output_path == null) {
cli.throw_error(`Configuration file "${client.config.config_path}" does not have a defined HTML output path for the generated documentation, define configuration attribute "output_path". More information about the configuration can be found at "${DOCUMENTATION}?id=Configuration:Config".`);
}
try {
await client.generate();
} catch (error) {
cli.throw_error(error);
}
if (Array.isArray(client.config.output_path)) {
vlib.print_marker(`Successfully generated the "${client.config.name}" documentation, saved the HTML to ${client.config.output_path.length} output paths.`);
} else {
vlib.print_marker(`Successfully generated the "${client.config.name}" documentation, saved the HTML to "${client.config.output_path}".`);
}
})
}
},
// List projects.
/* @docs:
* @parse: false
* @lang: CLI
* @parent: CLI
* @title: List Projects
* @name: --list-projects
* @description: List your account's active projects.
* @param:
* @name: --api-key
* @type: string
* @desc: The {{BRAND_NAME}} API key, when undefined the environment variable `LIBRIS_API_KEY` will be used.
* @required: false
* @usage:
* @CLI:
* $ libris --list-projects
*/
{
id: "--list-projects",
description: "List your account's active projects.",
examples: {
"List Projects": "libris --list-projects",
},
args: [
{id: "--api-key", type: "string", description: `The Libris API key, when undefined the environment variable "LIBRIS_API_KEY" will be used.`},
],
callback: async ({
api_key = null,
}) => {
if (api_key != null) {
Client.api_key = api_key;
}
let projects;
try {
projects = await Client.list_projects();
} catch (error) {
cli.throw_error(error);
}
vlib.print_marker("Projects:")
projects.iterate((project) => {
vlib.print(` *${project}`);
})
}
},
// Delete Project.
/* @docs:
* @parse: false
* @lang: CLI
* @parent: CLI
* @title: Delete Project
* @name: --delete-project
* @description: Delete one of your your account's active projects.
* @param:
* @name: --name
* @type: string
* @desc: The name of the project you want to delete.
* @required: true
* @param:
* @name: --api-key
* @type: string
* @desc: The {{BRAND_NAME}} API key, when undefined the environment variable `LIBRIS_API_KEY` will be used.
* @required: false
* @usage:
* @CLI:
* $ libris --delete-project myproject
*/
{
id: ["--delete-project", "--del-project"],
description: "Delete one of your your account's active projects.",
examples: {
"List Projects": "libris --delete-project myproject",
},
args: [
{id: "--name", type: "string", description: `The name of the project you want to delete.`, required: true},
{id: "--api-key", type: "string", description: `The Libris API key, when undefined the environment variable "LIBRIS_API_KEY" will be used.`},
],
callback: async ({
name = null,
api_key = null,
}) => {
if (api_key != null) {
Client.api_key = api_key;
}
let projects;
try {
projects = await Client.del_project(name);
} catch (error) {
cli.throw_error(error);
}
vlib.print_marker(`Successfully deleted project "${name}".`)
}
},
]});
// Start.
cli.start();