-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathoptions.ts
More file actions
177 lines (164 loc) · 4.35 KB
/
options.ts
File metadata and controls
177 lines (164 loc) · 4.35 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
import { parseArgs } from "node:util";
import { Logger, printUsage, printVersion } from "./log.js";
import process from "node:process";
import { doesFileExist } from "./network.js";
import PromptSync from "prompt-sync";
import { normalizePath, resolvePath } from "@typespec/compiler";
export interface Options {
debug: boolean;
command: string;
tspConfig?: string;
outputDir: string;
noCleanup: boolean;
skipSyncAndGenerate: boolean;
commit?: string;
repo?: string;
isUrl: boolean;
localSpecRepo?: string;
emitterOptions?: string;
generateLockFile: boolean;
swaggerReadme?: string;
}
export async function getOptions(): Promise<Options> {
const { values, positionals } = parseArgs({
allowPositionals: true,
options: {
help: {
type: "boolean",
short: "h",
},
version: {
type: "boolean",
short: "v",
},
debug: {
type: "boolean",
short: "d",
},
["output-dir"]: {
type: "string",
short: "o",
},
["tsp-config"]: {
type: "string",
short: "c",
},
commit: {
type: "string",
},
repo: {
type: "string",
},
["emitter-options"]: {
type: "string",
},
["generate-lock-file"]: {
"type": "boolean",
},
["local-spec-repo"]: {
type: "string",
},
["save-inputs"]: {
type: "boolean",
},
["skip-sync-and-generate"]: {
type: "boolean",
},
["swagger-readme"]: {
type: "string",
}
},
});
if (values.help) {
printUsage();
process.exit(0);
}
if (values.version) {
await printVersion();
process.exit(0);
}
let isUrl = true;
const supportedCommands = ["sync", "generate", "update", "init", "convert"];
const command = positionals[0];
if (!values["generate-lock-file"]) {
if (positionals.length === 0) {
Logger.error("Command is required");
printUsage();
process.exit(1);
}
if (!command) {
Logger.error("Command is required");
printUsage();
process.exit(1);
}
if (!supportedCommands.includes(command)) {
Logger.error(`Unknown command ${command}`);
printUsage();
process.exit(1);
}
if (command === "init") {
if (!values["tsp-config"]) {
Logger.error("tspConfig is required");
printUsage();
process.exit(1);
}
if (await doesFileExist(values["tsp-config"])) {
isUrl = false;
}
if (!isUrl) {
if (!values.commit || !values.repo) {
Logger.error("The commit and repo options are required when tspConfig is a local directory");
printUsage();
process.exit(1);
}
}
}
if (command === "convert") {
if (!values["swagger-readme"]) {
Logger.error("Must specify a swagger readme with the `--swagger-readme` flag");
printUsage();
process.exit(1);
}
}
}
// By default, assume that the command is run from the output directory
let outputDir = ".";
if (values["output-dir"]) {
outputDir = values["output-dir"];
}
outputDir = resolvePath(process.cwd(), outputDir);
let useOutputDir;
if (process.stdin.isTTY) {
// Ask user is this is the correct output directory
const prompt = PromptSync();
useOutputDir = prompt("Use output directory '" + outputDir + "'? (y/n) ", "y");
} else {
// There is no user to ask, so assume yes
useOutputDir = 'y';
}
if (useOutputDir.toLowerCase() === "n") {
const newOutputDir = prompt("Enter output directory: ");
if (!newOutputDir) {
Logger.error("Output directory is required");
printUsage();
process.exit(1);
}
outputDir = resolvePath(normalizePath(newOutputDir));
}
Logger.info("Using output directory '" + outputDir + "'");
return {
debug: values.debug ?? false,
command: command ?? "",
tspConfig: values["tsp-config"],
noCleanup: values["save-inputs"] ?? false,
skipSyncAndGenerate: values["skip-sync-and-generate"] ?? false,
outputDir: outputDir,
commit: values.commit,
repo: values.repo,
isUrl: isUrl,
localSpecRepo: values["local-spec-repo"],
emitterOptions: values["emitter-options"],
generateLockFile: values["generate-lock-file"] ?? false,
swaggerReadme: values["swagger-readme"],
};
}