-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathargs.ts
More file actions
77 lines (69 loc) · 2.54 KB
/
args.ts
File metadata and controls
77 lines (69 loc) · 2.54 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
import { mkdirSync, type Stats, statSync } from "node:fs";
import { resolve } from "node:path";
import { parseArgs } from "node:util";
import { isWranglerTarget, WranglerTarget } from "./utils/run-wrangler.js";
export type Arguments = (
| {
command: "build";
skipNextBuild: boolean;
skipWranglerConfigCheck: boolean;
minify: boolean;
}
| { command: "preview" | "deploy"; passthroughArgs: string[] }
| { command: "populateCache"; target: WranglerTarget }
) & { outputDir?: string };
export function getArgs(): Arguments {
const { positionals, values } = parseArgs({
options: {
skipBuild: { type: "boolean", short: "s", default: false },
output: { type: "string", short: "o" },
noMinify: { type: "boolean", default: false },
skipWranglerConfigCheck: { type: "boolean", default: false },
},
allowPositionals: true,
});
const outputDir = values.output ? resolve(values.output) : undefined;
if (outputDir) assertDirArg(outputDir, "output", true);
switch (positionals[0]) {
case "build":
return {
command: "build",
outputDir,
skipNextBuild:
values.skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
skipWranglerConfigCheck:
values.skipWranglerConfigCheck ||
["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
minify: !values.noMinify,
};
case "preview":
case "deploy":
return { command: positionals[0], outputDir, passthroughArgs: getPassthroughArgs() };
case "populateCache":
if (!isWranglerTarget(positionals[1])) {
throw new Error(`Error: invalid target for populating the cache, expected 'local' | 'remote'`);
}
return { command: "populateCache", outputDir, target: positionals[1] };
default:
throw new Error("Error: invalid command, expected 'build' | 'preview' | 'deploy' | 'populateCache'");
}
}
function getPassthroughArgs() {
const passthroughPos = process.argv.indexOf("--");
return passthroughPos === -1 ? [] : process.argv.slice(passthroughPos + 1);
}
function assertDirArg(path: string, argName?: string, make?: boolean) {
let dirStats: Stats;
try {
dirStats = statSync(path);
} catch {
if (!make) {
throw new Error(`Error: the provided${argName ? ` "${argName}"` : ""} input is not a valid path`);
}
mkdirSync(path);
return;
}
if (!dirStats.isDirectory()) {
throw new Error(`Error: the provided${argName ? ` "${argName}"` : ""} input is not a directory`);
}
}