-
Notifications
You must be signed in to change notification settings - Fork 22
/
args.ts
82 lines (71 loc) · 1.81 KB
/
args.ts
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { parseFlags } from "./deps.ts";
export type Command = NewCommand | BuildCommand | CheckCommand;
export interface NewCommand {
kind: "new";
}
export interface CommonBuild {
outDir: string;
bindingJsFileExt: string;
profile: "debug" | "release";
project: string | undefined;
isSync: boolean;
isOpt: 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);
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 {
return {
profile: flags.debug ? "debug" : "release",
project: flags.p ?? flags.project,
isSync: flags.sync ?? false,
isOpt: !(flags["skip-opt"] ?? false),
outDir: flags.out ?? "./lib",
bindingJsFileExt: flags["js-ext"] ?? `js`,
cargoFlags: getCargoFlags(),
};
}
function getCargoFlags() {
const cargoFlags = [];
if (flags["default-features"] === false) {
cargoFlags.push("--no-default-features");
}
if (flags["features"]) {
cargoFlags.push(`--features`);
cargoFlags.push(flags["features"]);
}
if (flags["all-features"]) {
cargoFlags.push("--all-features");
}
return cargoFlags;
}
}