-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathargs.ts
More file actions
87 lines (79 loc) · 2.4 KB
/
args.ts
File metadata and controls
87 lines (79 loc) · 2.4 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
import { mkdirSync, type Stats, statSync } from "node:fs";
import { resolve } from "node:path";
import { parseArgs } from "node:util";
import type { CacheBindingMode } from "./build/utils/index.js";
import { isCacheBindingMode } from "./build/utils/index.js";
export function getArgs(): {
skipNextBuild: boolean;
skipWranglerConfigCheck: boolean;
outputDir?: string;
minify: boolean;
populateCache?: { mode: CacheBindingMode; onlyPopulateWithoutBuilding: boolean };
} {
const { skipBuild, skipWranglerConfigCheck, output, noMinify, populateCache, onlyPopulateCache } =
parseArgs({
options: {
skipBuild: {
type: "boolean",
short: "s",
default: false,
},
output: {
type: "string",
short: "o",
},
noMinify: {
type: "boolean",
default: false,
},
skipWranglerConfigCheck: {
type: "boolean",
default: false,
},
populateCache: {
type: "string",
},
onlyPopulateCache: {
type: "boolean",
default: false,
},
},
allowPositionals: false,
}).values;
const outputDir = output ? resolve(output) : undefined;
if (outputDir) {
assertDirArg(outputDir, "output", true);
}
if (
(populateCache !== undefined || onlyPopulateCache) &&
(!populateCache?.length || !isCacheBindingMode(populateCache))
) {
throw new Error(`Error: missing mode for populate cache flag, expected 'local' | 'remote'`);
}
return {
outputDir,
skipNextBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
skipWranglerConfigCheck:
skipWranglerConfigCheck ||
["1", "true", "yes"].includes(String(process.env.SKIP_WRANGLER_CONFIG_CHECK)),
minify: !noMinify,
populateCache: populateCache
? { mode: populateCache, onlyPopulateWithoutBuilding: !!onlyPopulateCache }
: undefined,
};
}
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`);
}
}