Skip to content

Commit eacff97

Browse files
committed
Initial
1 parent c88eab5 commit eacff97

File tree

6 files changed

+109
-182
lines changed

6 files changed

+109
-182
lines changed

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"build-versions": "deno run --allow-read --allow-write --allow-env --unstable tools/release.ts && deno fmt",
1818
"build-webinterface": "cd plugins/web-interface && rm static/bundle.json; deno run --allow-read --allow-write https://deno.land/x/bundlee/bundlee.ts --bundle static static/bundle.json && deno fmt",
1919
"build": "deno task check && deno task build-schema && deno task build-webinterface && deno task build-versions"
20-
}
20+
},
21+
"imports": { "@cross/utils": "jsr:@cross/utils@^0.8.0" }
2122
}

deps.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
* - Always use the same version of all imports from deno.land/std
1414
*/
1515
// cli
16-
export { parse } from "https://deno.land/[email protected]/flags/mod.ts"
17-
export type { Args } from "https://deno.land/[email protected]/flags/mod.ts"
1816
export * as path from "https://deno.land/[email protected]/path/mod.ts"
1917
export * as uuid from "https://deno.land/[email protected]/uuid/mod.ts"
2018
// logger

lib/cli/args.ts

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,32 @@
55
* @license MIT
66
*/
77

8-
import { Args, parse } from "../../deps.ts"
8+
import { ArgsParser } from "@cross/utils/args"
99

1010
/**
1111
* Parses command line arguments and returns a parsed object.
1212
*
1313
* @param args - An array of command line arguments.
1414
* @returns - A parsed object containing the command line arguments.
1515
*/
16-
function parseArguments(args: string[]): Args {
17-
// All boolean arguments
18-
const booleanArgs = [
19-
"setup",
20-
"upgrade",
21-
"help",
22-
"autostart",
23-
24-
"dry-run",
25-
]
26-
27-
// All string arguments
28-
const stringArgs = [
29-
"version",
30-
"config",
31-
"watch",
32-
"cmd",
33-
"worker",
34-
"cwd",
35-
"id",
36-
37-
"cron",
38-
"terminate",
39-
40-
"instances",
41-
"start-port",
42-
"common-port",
43-
"strategy",
44-
"stdout",
45-
"stderr",
46-
47-
"unsafely-ignore-certificate-errors",
48-
]
49-
50-
// All collection arguments
51-
const collectArgs = [
52-
"env",
53-
]
54-
55-
// And a list of aliases
56-
const alias = {
57-
"version": "v",
58-
"help": "h",
59-
"id": "I",
60-
"autostart": "A",
61-
"config": "c",
62-
"cmd": "C",
63-
"worker": "W",
64-
"watch": "w",
65-
"cron": "O",
66-
"terminate": "T",
67-
"cwd": "d",
68-
"update": "upgrade",
69-
"env": "e",
16+
function parseArguments(args: string[]): ArgsParser {
17+
const aliases = {
18+
"v": "version",
19+
"h": "help",
20+
"I": "id",
21+
"A": "autostart",
22+
"c": "config",
23+
"C": "cmd",
24+
"W": "worker",
25+
"w": "watch",
26+
"O": "cron",
27+
"T": "terminate",
28+
"d": "cwd",
29+
"upgrade": "update",
30+
"e": "env",
7031
}
7132

72-
return parse(args, { alias, boolean: booleanArgs, string: stringArgs, collect: collectArgs, stopEarly: false, "--": true })
33+
return new ArgsParser(args, { aliases })
7334
}
7435

7536
/**
@@ -78,7 +39,7 @@ function parseArguments(args: string[]): Args {
7839
* @returns - The parsed and checked arguments.
7940
* @throws - An error if any of the arguments are invalid.
8041
*/
81-
function checkArguments(args: Args): Args {
42+
function checkArguments(args: ArgsParser): ArgsParser {
8243
const validBaseArguments = [
8344
"init",
8445
"append",
@@ -126,28 +87,28 @@ function checkArguments(args: Args): Args {
12687
]
12788

12889
// Check that the base argument is either undefined or valid
129-
const baseArgument = args._[0]
90+
const baseArgument = args.getRest()
13091
if (baseArgument !== undefined && (typeof baseArgument !== "string" || !validBaseArguments.includes(baseArgument))) {
13192
throw new Error(`Invalid base argument: ${baseArgument}`)
13293
}
13394

134-
const hasDoubleDashCmd = args["--"] && args["--"].length > 0
135-
const hasCmd = hasDoubleDashCmd || args.cmd || args.worker
95+
const hasDoubleDashCmd = args.hasRest()
96+
const hasCmd = hasDoubleDashCmd || args.get("cmd") || args.get("worker")
13697
const expectConfigOptions = baseArgument === "init" || baseArgument === "append" || (baseArgument === "run" && hasCmd)
13798
const expectInstallerOptions = baseArgument === "setup" || baseArgument === "upgrade" || baseArgument === "update"
13899

139100
// Only one type of command can be present at the same time
140-
if ((hasDoubleDashCmd && args.cmd) || (args.cmd && args.worker) || (hasDoubleDashCmd && args.worker)) {
101+
if ((hasDoubleDashCmd && args.get("cmd")) || (args.get("cmd") && args.get("worker")) || (hasDoubleDashCmd && args.get("worker"))) {
141102
throw new Error("'--cmd', '--worker' and '--' cannot be used at the same time.")
142103
}
143104

144105
// Certain base arguments require --id
145-
if (!args.id && (baseArgument === "init" || baseArgument === "append" || baseArgument === "remove")) {
106+
if (!args.get("id") && (baseArgument === "init" || baseArgument === "append" || baseArgument === "remove")) {
146107
throw new Error("Arguments 'init', 'append', and 'remove' require '--id'")
147108
}
148109

149110
// Init and append require a command
150-
if ((args.init || args.append) && !hasCmd) {
111+
if ((args.get("init") || args.get("append")) && !hasCmd) {
151112
throw new Error("Arguments 'init' and 'append' requires '--cmd' or '--worker'")
152113
}
153114

@@ -158,27 +119,27 @@ function checkArguments(args: Args): Args {
158119

159120
// All arguments in processOptions require that init, append, cmd och worker is used
160121
for (const opt of processOptions) {
161-
if (args[opt] && !expectConfigOptions) {
122+
if (args.get(opt) && !expectConfigOptions) {
162123
throw new Error(`Argument '--${opt}' requires 'init', 'append', '--cmd' or '--worker'`)
163124
}
164125
}
165126

166127
// All arguments in installerOptions require that setup or upgrade (or update) is used
167128
for (const opt of installerOptions) {
168-
if (args[opt] && !expectInstallerOptions) {
129+
if (args.get(opt) && !expectInstallerOptions) {
169130
throw new Error(`Argument '--${opt}' requires 'setup' or 'upgrade'`)
170131
}
171132
}
172133

173134
// All arguments in numericArguments must be numeric
174135
for (const opt of numericArguments) {
175-
if (args[opt] && isNaN(Number(args[opt]))) {
136+
if (args.get(opt) && isNaN(Number(args.get(opt)))) {
176137
throw new Error(`Argument '--${opt}' must be a numeric value`)
177138
}
178139
}
179140

180141
// --env flag can only be used with 'service install' base argument
181-
if (args.env && (baseArgument !== "install")) {
142+
if (args.get("env") && (baseArgument !== "install")) {
182143
throw new Error("Argument '--env' can only be used with 'service install' base argument")
183144
}
184145

0 commit comments

Comments
 (0)