-
Notifications
You must be signed in to change notification settings - Fork 2
/
bin.ts
177 lines (149 loc) · 3.87 KB
/
bin.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { execa } from "execa";
import prettyBytes from "pretty-bytes";
import { createInterface } from "node:readline";
import { arch as _arch, platform as _platform } from "node:os";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
const _dirname = typeof __dirname !== "undefined"
? __dirname
: dirname(fileURLToPath(import.meta.url));
let defaultBinPath = "";
// Cache the inferred version to avoid recomputing it
let cachedInferredVersion = null;
export function inferVersion() {
if (cachedInferredVersion) {
return cachedInferredVersion;
}
const platform = _platform();
if (!/win32|linux|darwin/.test(platform)) {
throw new Error(`${platform} is not support`);
}
const arch = _arch();
if (!/amd64_v1|arm64|386|x64/.test(arch)) {
throw new Error(`${arch} is not support`);
}
cachedInferredVersion = `${platform === "win32" ? "windows" : platform}_${
arch === "x64" ? "amd64_v1" : arch
}`;
return cachedInferredVersion;
}
export function detectBinName(version = inferVersion()) {
return `go-get-folder-size${version.startsWith("windows") ? ".exe" : ""}`;
}
export function detectDefaultBinPath() {
if (defaultBinPath) {
return defaultBinPath;
}
const version = inferVersion();
const name = detectBinName(version);
defaultBinPath = resolve(
_dirname,
`../dist/go-get-folder-size_${version}/${name}`,
);
return defaultBinPath;
}
interface Options {
binPath?: string;
/**
* @default false
*/
loose?: boolean;
}
export async function getFolderSizeBin(
base: string,
pretty?: false,
options?: Options,
): Promise<number>;
export async function getFolderSizeBin(
base: string,
pretty?: true,
options?: Options,
): Promise<string>;
export async function getFolderSizeBin(
base: string,
pretty = false,
options: Options = {},
) {
const { binPath = detectDefaultBinPath(), loose = false } = options;
const { stdout, stderr } = await execa(binPath, {
cwd: base,
env: { loose: String(loose) },
});
if (stderr) {
throw new Error(stderr);
}
if (pretty) {
return prettyBytes(Number(stdout));
}
return Number(stdout);
}
export function createGetFolderSizeBinIpc(options: Options = {}) {
const { binPath = detectDefaultBinPath(), loose = false } = options;
let tasks = new Map<
string,
{ pretty: boolean; resolve: Function; reject: Function }
>();
const go = execa(binPath, {
env: {
ipc: String(true),
loose: String(loose),
},
});
const readline = createInterface({
input: go.stdout,
});
function close() {
if (!go.killed) {
readline.close();
go.cancel();
tasks.clear();
tasks = null;
}
}
let full = false;
function send(base: string) {
if (full) {
return go.stdin.once("drain", () => {
full = false;
go.stdin.write(`${base},`);
});
}
full = !go.stdin.write(`${base},`);
}
readline.on("line", (item: string) => {
const [base, size] = item.split(",");
const { pretty, resolve } = tasks.get(base);
resolve(pretty ? prettyBytes(Number(size)) : Number(size));
tasks.delete(base);
});
go.stderr.on("data", (item: Buffer) => {
const [base, ...error] = String(item).split(",");
const { reject } = tasks.get(base);
reject(new Error(error.toString()));
tasks.delete(base);
});
process.once("exit", () => {
close();
});
async function getFolderSizeWithIpc(
base: string,
pretty?: false,
): Promise<number>;
async function getFolderSizeWithIpc(
base: string,
pretty?: true,
): Promise<string>;
async function getFolderSizeWithIpc(
base: string,
pretty = false,
): Promise<number | string> {
return new Promise((resolve, reject) => {
tasks.set(base, { pretty, resolve, reject });
send(base);
});
}
return {
close,
getFolderSizeWithIpc,
};
}