-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.mjs
More file actions
executable file
·163 lines (155 loc) · 4.34 KB
/
cli.mjs
File metadata and controls
executable file
·163 lines (155 loc) · 4.34 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
#!/usr/bin/env node
// @ts-check
import { createClient, parseSpecFile } from "@faizaanceg/open-sdk";
import { log } from "node:console";
import { existsSync } from "node:fs";
import { join, resolve } from "node:path";
import { argv, cwd, exit } from "node:process";
const SUPPORTED_LANGUAGES = ["java", "php", "typescript"];
let Manifest = {
name: "OpenSDK",
subcommands: {
generate: {
description: "Generate SDK",
options: {
"--output": {
shortFlag: "-o",
description: "Output directory for the code",
get defaultValue() {
throw new Error("Please provide output directory.");
},
takesValue: true,
validate(value) {
return existsSync(join(cwd(), value));
},
},
"--dry-run": {
shortFlag: null,
description: "Do not create files.",
get defaultValue() {
return false;
},
takesValue: false,
validate(value) {},
},
"--language": {
shortFlag: "-L",
description: `Specify language to use. Allowed options are (${SUPPORTED_LANGUAGES.join(
"|"
)})`,
get defaultValue() {
throw new Error("Please provide a language.");
},
takesValue: true,
validate(value) {
if (!["java", "php", "typescript"].includes(value?.toLowerCase())) {
throw new Error(
`Language "${value}" not supported. Allowed languages are ${SUPPORTED_LANGUAGES.join(
", "
)}`
);
}
},
},
"--spec": {
shortFlag: "-S",
description: "Path to the OpenAPI 3.1 JSON file",
get defaultValue() {
throw new Error("Please provide valid path to JSON spec file.");
},
takesValue: true,
validate(value) {
if (!existsSync(join(cwd(), value))) {
throw new Error("Spec file not found at " + value);
}
},
},
},
},
},
};
let [subcommand = "help", ...args] = argv.slice(2);
function parseOptions(subcommand, args) {
let options = Manifest.subcommands[subcommand].options;
let validFlags = Object.entries(options)
.flatMap(([option, details]) => [option, details.shortFlag])
.filter(Boolean);
let expandFlags = Object.fromEntries(
Object.entries(options).map(([option, details]) => [
details.shortFlag,
option,
])
);
let parsed = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (!validFlags.includes(arg)) {
throw new Error("UnknownOption: " + arg);
}
let flag = expandFlags[arg] ?? arg;
if (options[flag].takesValue) {
const value = args[i + 1];
options[flag].validate(value);
parsed[flag] = value;
i++;
} else {
parsed[flag] = true;
}
}
for (let [flag, details] of Object.entries(options)) {
if (parsed[flag] === undefined) {
parsed[flag] = details.defaultValue;
}
}
return parsed;
}
switch (subcommand) {
case "generate":
{
try {
let options = parseOptions(subcommand, args);
log(options);
let spec = parseSpecFile(
resolve(join(process.cwd(), options["--spec"]))
);
createClient(
spec,
resolve(join(process.cwd(), options["--output"])),
options["--language"].toLowerCase()
);
} catch (error) {
console.error(
error.message +
" Please use `opensdk help` to understand the correct usage",
error
);
exit(1);
}
}
break;
case "help": {
console.log(`${Manifest.name} ${Manifest.version}
usage: opensdk <command> [<flags>]
Subcommands available
${Object.entries(Manifest.subcommands).map(
([subcommand, info]) => `\t${subcommand}\t${info.description}`
)}
Subcommand Options
${Object.entries(Manifest.subcommands).map(
([subcommand, info]) =>
`${subcommand}\n${Object.entries(info.options)
.map(
([option, details]) =>
`\t${option}${details.shortFlag ? ", " + details.shortFlag : ""}\t${
details.description
}`
)
.join("\n")}`
)}
`);
break;
}
default:
console.error("Unknown command :" + subcommand);
exit(1);
}