-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
163 lines (150 loc) · 4.1 KB
/
build.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
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
import { build, emptyDir, PackageJson } from "https://deno.land/x/dnt@0.39.0/mod.ts";
import * as flagsModule from "https://deno.land/std@0.210.0/cli/parse_args.ts";
const textDecoder = new TextDecoder();
const args = flagsModule.parseArgs(Deno.args, {
boolean: [
"link",
"publish",
"autoformat",
],
string: [
"dist",
"package-manager",
"otp",
],
alias: {
"dist": ["d"],
"link": ["l", "ln"],
"package-manager": ["m", "pm"],
"publish": ["p"],
"autoformat": ["f", "format", "autofmt", "fmt"],
},
default: {
dist: "./dist",
},
});
const {
dist: distDir,
link: doLink,
publish: doPublish,
autoformat,
} = args;
const packageManager = args["package-manager"] || Deno.env.get("PACKAGE_MANAGER") ||
"npm";
class RunCommandError extends Error {
constructor(
readonly command: string,
readonly process: Deno.ChildProcess,
readonly status: Deno.CommandStatus,
) {
const { code, signal } = status;
super(`${command} failed. Code" ${code};${signal ? `Signal: ${status.signal}` : ""}`);
}
}
RunCommandError.prototype.name = RunCommandError.name;
async function runCommand(command: string, options?: Deno.CommandOptions) {
const _options = {
stderr: "piped",
stdout: "piped",
stdin: "null",
...options,
} satisfies Deno.CommandOptions;
const fullCommand = [command, ...options?.args || []].join(" ");
console.log(fullCommand);
const tryVariants = [command];
if (Deno.build.os === "windows") {
tryVariants.push(
command + ".bat",
command + ".cmd",
command + ".ps1",
);
}
let firstError: unknown;
let process: Deno.ChildProcess | undefined;
while (tryVariants.length) {
try {
process = new Deno.Command(tryVariants.pop()!, _options).spawn();
break;
} catch (e) {
if (firstError === undefined) {
firstError = e;
}
}
}
if (process === undefined) {
throw firstError;
}
process.stderr.pipeTo(Deno.stderr.writable, { preventClose: true });
process.stdout.pipeTo(Deno.stdout.writable, { preventClose: true });
const status = await process.status;
if (!status.success) {
throw new RunCommandError(fullCommand, process, status);
}
return process;
}
function runPmCommand(command: string, options?: Deno.CommandOptions) {
const args = [command, ...options?.args || []];
return runCommand(packageManager, { ...options, args });
}
await emptyDir(distDir);
const packageJson = JSON.parse(textDecoder.decode(await Deno.readFile("./package.json"))) as PackageJson;
delete packageJson.scripts;
delete packageJson.devDependencies;
delete packageJson.publishConfig;
if (autoformat) {
await format("./src");
}
await build({
entryPoints: ["./src/index.ts"],
outDir: distDir,
shims: {
deno: true,
},
package: packageJson,
packageManager: packageManager,
async postBuild() {
try {
if (doLink) {
await link();
}
if (doPublish) {
await publish();
}
//Deno.copyFileSync("CHANGELOG.md", "npm/CHANGELOG.md");
//Deno.copyFileSync("LICENSE", "npm/LICENSE");
//Deno.copyFileSync("README.md", "npm/README.md");
} catch (e) {
console.error(e);
}
},
});
async function publish() {
let otp: string | null | undefined = args.otp || Deno.env.get("OTP");
if (!(otp && /^(?:\d\s*){6}$/.test(otp))) {
otp = prompt("Enter code from Authenticator for your npm account to publish: ");
if (!(otp && /^(?:\d\s*){6}$/.test(otp))) {
console.log("Incorrect code format, publish failed");
return;
}
}
await runPmCommand("publish", { cwd: distDir, args: ["--no-git-checks", `--otp=${otp.replace(/s+/g, "")}`] });
}
async function link() {
let args: string[];
switch (packageManager) {
case "npm":
case "yarn":
args = [];
break;
case "pnpm":
args = ["--global"];
break;
default:
console.warn(`Unknown package manager ${packageManager}, can not link`);
return;
}
await runPmCommand("link", { args, cwd: distDir });
}
async function format(path: string, cwd?: string) {
await runCommand("deno", { cwd, args: ["fmt", path] });
}