forked from denoland/wasmbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.ts
More file actions
111 lines (98 loc) · 2.54 KB
/
args.ts
File metadata and controls
111 lines (98 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
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
// Copyright 2018-2025 the Deno authors. MIT license.
import { parseArgs as parseFlags } from "@std/cli/parse-args";
import { Path } from "@david/path";
export type Command = NewCommand | BuildCommand | CheckCommand | HelpCommand;
export interface NewCommand {
kind: "new";
}
export interface HelpCommand {
kind: "help";
}
export interface CommonBuild {
outDir: Path;
bindingJsFileExt: "js" | "mjs";
profile: "debug" | "release";
project: string | undefined;
isOpt: boolean;
inline: boolean;
cargoFlags: string[];
}
export interface BuildCommand extends CommonBuild {
kind: "build";
}
export interface CheckCommand extends CommonBuild {
kind: "check";
}
export function parseArgs(rawArgs: string[]): Command {
const flags = parseFlags(rawArgs, {
"--": true,
});
if (flags.help || flags.h) return { kind: "help" };
switch (flags._[0]) {
case "new":
return {
kind: "new",
};
case "build":
case undefined:
case null:
if (flags.check) {
return {
kind: "check",
...getCommonBuild(),
};
} else {
return {
kind: "build",
...getCommonBuild(),
};
}
default:
throw new Error(`Unrecognized sub command: ${flags._[0]}`);
}
function getCommonBuild(): CommonBuild {
if (flags.sync) {
throw new Error(
"The --sync flag has been renamed to --inline.",
);
}
if (flags["no-cache"]) {
throw new Error(
"The --no-cache flag is no longer necessary now that Wasmbuild supports Wasm imports.",
);
}
return {
profile: flags.debug ? "debug" : "release",
project: flags.p ?? flags.project,
inline: flags.inline,
isOpt: !(flags["skip-opt"] ?? flags.debug == "debug"),
outDir: new Path(flags.out ?? "./lib"),
bindingJsFileExt: getBindingJsFileExt(),
cargoFlags: getCargoFlags(),
};
}
function getBindingJsFileExt() {
const ext: string = flags["js-ext"] ?? `js`;
if (ext !== "js" && ext !== "mjs") {
throw new Error("js-ext must be 'js' or 'mjs'");
}
return ext;
}
function getCargoFlags() {
const cargoFlags = [];
if (flags["no-default-features"]) {
cargoFlags.push("--no-default-features");
}
if (flags["features"]) {
cargoFlags.push(`--features`);
cargoFlags.push(flags["features"]);
}
if (flags["all-features"]) {
cargoFlags.push("--all-features");
}
if (flags["--"]) {
cargoFlags.push(...flags["--"]);
}
return cargoFlags;
}
}