@lifo-sh/core 0.5.0 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/args-BhoL0JcG.js +58 -0
- package/dist/awk-DahiO6Jl.js +170 -0
- package/dist/base64-Dj_AnQAH.js +92 -0
- package/dist/basename-D8sjWfzX.js +14 -0
- package/dist/bc-CCuJi0IY.js +230 -0
- package/dist/browser-B0dadKCA.js +43 -0
- package/dist/cal-DQsLvArw.js +85 -0
- package/dist/cat-KYGveROo.js +32 -0
- package/dist/chmod-gYZdg3Kv.js +81 -0
- package/dist/chown-DoPi7NLO.js +32 -0
- package/dist/commands/net/curl.d.ts +2 -2
- package/dist/commands/net/curl.d.ts.map +1 -1
- package/dist/commands/net/forward.d.ts +14 -0
- package/dist/commands/net/forward.d.ts.map +1 -0
- package/dist/commands/net/host.d.ts +14 -0
- package/dist/commands/net/host.d.ts.map +1 -0
- package/dist/commands/net/ifconfig.d.ts +8 -0
- package/dist/commands/net/ifconfig.d.ts.map +1 -0
- package/dist/commands/net/ip.d.ts +14 -0
- package/dist/commands/net/ip.d.ts.map +1 -0
- package/dist/commands/net/netstat.d.ts +8 -0
- package/dist/commands/net/netstat.d.ts.map +1 -0
- package/dist/commands/net/ports.d.ts +8 -0
- package/dist/commands/net/ports.d.ts.map +1 -0
- package/dist/commands/net/route.d.ts +8 -0
- package/dist/commands/net/route.d.ts.map +1 -0
- package/dist/commands/net/test-registry.d.ts +8 -0
- package/dist/commands/net/test-registry.d.ts.map +1 -0
- package/dist/commands/net/tunnel-v2.d.ts +4 -0
- package/dist/commands/net/tunnel-v2.d.ts.map +1 -0
- package/dist/commands/net/tunnel.d.ts +6 -0
- package/dist/commands/net/tunnel.d.ts.map +1 -0
- package/dist/commands/registry.d.ts +1 -0
- package/dist/commands/registry.d.ts.map +1 -1
- package/dist/commands/system/kill.d.ts +2 -2
- package/dist/commands/system/kill.d.ts.map +1 -1
- package/dist/commands/system/lifo.d.ts +19 -2
- package/dist/commands/system/lifo.d.ts.map +1 -1
- package/dist/commands/system/newtab.d.ts +8 -0
- package/dist/commands/system/newtab.d.ts.map +1 -0
- package/dist/commands/system/node.d.ts +3 -2
- package/dist/commands/system/node.d.ts.map +1 -1
- package/dist/commands/system/npm.d.ts +20 -7
- package/dist/commands/system/npm.d.ts.map +1 -1
- package/dist/commands/system/ps.d.ts +3 -2
- package/dist/commands/system/ps.d.ts.map +1 -1
- package/dist/commands/system/systemctl.d.ts +4 -0
- package/dist/commands/system/systemctl.d.ts.map +1 -0
- package/dist/commands/system/top.d.ts +2 -2
- package/dist/commands/system/top.d.ts.map +1 -1
- package/dist/commands/system/watch.d.ts.map +1 -1
- package/dist/commands/system/whoami.d.ts.map +1 -1
- package/dist/commands/types.d.ts +3 -2
- package/dist/commands/types.d.ts.map +1 -1
- package/dist/cp-BP7-nkZi.js +29 -0
- package/dist/cut-Dg3Sxlw_.js +84 -0
- package/dist/date-DqL4oCg8.js +91 -0
- package/dist/df-CEHqyq91.js +47 -0
- package/dist/diff-aYLFp43G.js +250 -0
- package/dist/dig-0_V5UoWW.js +91 -0
- package/dist/dirname-ByojkmnI.js +14 -0
- package/dist/du-feoEC53Q.js +89 -0
- package/dist/env-CIcDulqw.js +10 -0
- package/dist/fastfetch-CRfsfb4t.js +378 -0
- package/dist/file-BHHCgjt9.js +79 -0
- package/dist/find-C-pGJyTZ.js +63 -0
- package/dist/free-DCkGta9v.js +33 -0
- package/dist/grep-Bv7fu1qw.js +179 -0
- package/dist/gunzip-BZk2000a.js +57 -0
- package/dist/gzip-D55LwDu8.js +70 -0
- package/dist/head-CQQ8pAha.js +66 -0
- package/dist/hostname-Ba0tnCzG.js +8 -0
- package/dist/index-GLjTMF7C.js +22717 -0
- package/dist/index.d.ts +44 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +99 -72
- package/dist/less-C-8XcQ_A.js +294 -0
- package/dist/ln-CpXREm6e.js +15 -0
- package/dist/ls-Ce1mcASB.js +132 -0
- package/dist/{man-Dg26tZ-a.js → man-DjxHeM01.js} +46 -27
- package/dist/mime-B5knsVEw.js +101 -0
- package/dist/mkdir-CtDdaiaM.js +31 -0
- package/dist/mktemp-DwAY8Lz2.js +44 -0
- package/dist/mv-vS020rcy.js +29 -0
- package/dist/nano-DKfqXthq.js +445 -0
- package/dist/nl-DHh-MmKX.js +66 -0
- package/dist/ping-BQO3Z9CT.js +72 -0
- package/dist/pkg/PackageManager.d.ts +2 -2
- package/dist/pkg/PackageManager.d.ts.map +1 -1
- package/dist/pkg/lifo-dev.d.ts +6 -6
- package/dist/pkg/lifo-dev.d.ts.map +1 -1
- package/dist/pkg/lifo-runtime.d.ts +3 -3
- package/dist/pkg/lifo-runtime.d.ts.map +1 -1
- package/dist/pkg/loader.d.ts +2 -2
- package/dist/pkg/loader.d.ts.map +1 -1
- package/dist/printf-CdBVx2dT.js +81 -0
- package/dist/realpath-CZ_ja3FL.js +27 -0
- package/dist/rev-KZ9O5vNS.js +42 -0
- package/dist/rm-dW7YB25j.js +48 -0
- package/dist/rmdir-Db2wctQ6.js +42 -0
- package/dist/runtime/ContextSerialization.d.ts +7 -0
- package/dist/runtime/ContextSerialization.d.ts.map +1 -0
- package/dist/runtime/ProcessExecutor.d.ts +14 -0
- package/dist/runtime/ProcessExecutor.d.ts.map +1 -0
- package/dist/runtime/command-worker.d.ts +9 -0
- package/dist/runtime/command-worker.d.ts.map +1 -0
- package/dist/sandbox/Sandbox.d.ts +1 -2
- package/dist/sandbox/Sandbox.d.ts.map +1 -1
- package/dist/sandbox/SandboxFs.d.ts +2 -2
- package/dist/sandbox/SandboxFs.d.ts.map +1 -1
- package/dist/sandbox/types.d.ts +1 -2
- package/dist/sandbox/types.d.ts.map +1 -1
- package/dist/sed-BK_pjM2M.js +142 -0
- package/dist/seq-B0yurkx9.js +64 -0
- package/dist/sha256sum-BP0l9D35.js +85 -0
- package/dist/shell/ProcessRegistry.d.ts +152 -0
- package/dist/shell/ProcessRegistry.d.ts.map +1 -0
- package/dist/shell/ProcessRegistry.example.d.ts +8 -0
- package/dist/shell/ProcessRegistry.example.d.ts.map +1 -0
- package/dist/shell/ProcessRegistry.test.d.ts +2 -0
- package/dist/shell/ProcessRegistry.test.d.ts.map +1 -0
- package/dist/shell/Shell.d.ts +13 -4
- package/dist/shell/Shell.d.ts.map +1 -1
- package/dist/shell/completer.d.ts +2 -2
- package/dist/shell/completer.d.ts.map +1 -1
- package/dist/shell/expander.d.ts +2 -2
- package/dist/shell/expander.d.ts.map +1 -1
- package/dist/shell/history.d.ts +2 -2
- package/dist/shell/history.d.ts.map +1 -1
- package/dist/shell/interpreter.d.ts +8 -2
- package/dist/shell/interpreter.d.ts.map +1 -1
- package/dist/shell/test-builtin.d.ts +2 -2
- package/dist/shell/test-builtin.d.ts.map +1 -1
- package/dist/sl-BmMNz7ir.js +200 -0
- package/dist/sleep-C_3mjcFi.js +25 -0
- package/dist/sort-DYZuiMaO.js +87 -0
- package/dist/stat-CXSSmsrA.js +38 -0
- package/dist/strings-BwqQy-zL.js +63 -0
- package/dist/tac-CuZSbu_c.js +32 -0
- package/dist/tail-DH7xR0rW.js +62 -0
- package/dist/tar-AnR03GXP.js +155 -0
- package/dist/tee-ByHsMExU.js +29 -0
- package/dist/touch-CcmnMpg5.js +26 -0
- package/dist/tr-D_cg13q8.js +92 -0
- package/dist/tree-Ck1ymlSv.js +64 -0
- package/dist/uname-07bQ7DTD.js +50 -0
- package/dist/uniq-BHkwpex3.js +76 -0
- package/dist/unzip-DVjk3GA4.js +91 -0
- package/dist/uptime-ujk4Nb77.js +17 -0
- package/dist/utils/archive.d.ts +2 -2
- package/dist/utils/archive.d.ts.map +1 -1
- package/dist/utils/glob.d.ts +2 -2
- package/dist/utils/glob.d.ts.map +1 -1
- package/dist/wc-CK9Flybx.js +98 -0
- package/dist/wget-Cww1uNE2.js +84 -0
- package/dist/{which-D3u0cwyh.js → which-CJwCjtVK.js} +25 -14
- package/dist/whoami-EHyrQ1q-.js +25 -0
- package/dist/xargs-I1ZWqhOp.js +40 -0
- package/dist/yes-XolnW6bp.js +11 -0
- package/dist/zip-BTmRwUYQ.js +40 -0
- package/package.json +16 -10
- package/dist/args-CG6b9cZt.js +0 -43
- package/dist/awk-CC_Qo0hQ.js +0 -123
- package/dist/base64-BBckAmkE.js +0 -72
- package/dist/basename-BIICW06y.js +0 -12
- package/dist/bc-BYiKVTNg.js +0 -200
- package/dist/cal-DPULSAhj.js +0 -56
- package/dist/cat-Jv35D4BV.js +0 -29
- package/dist/chmod-Bh-u8XOr.js +0 -56
- package/dist/chown-Dv7xfl2a.js +0 -28
- package/dist/cp-CB-sGTRH.js +0 -19
- package/dist/cut-B79kgCm4.js +0 -65
- package/dist/date-C8NB8v8G.js +0 -86
- package/dist/df-CeZb4WOr.js +0 -34
- package/dist/diff-D5Ej-JEe.js +0 -150
- package/dist/dig-BEJ8Ky96.js +0 -69
- package/dist/dirname-w2Y6csox.js +0 -13
- package/dist/du-UupzdKc_.js +0 -77
- package/dist/env-DXzvDKsN.js +0 -9
- package/dist/fastfetch-aPM9Z3ym.js +0 -310
- package/dist/file-DOl4Wjtc.js +0 -70
- package/dist/find-C0T-P7sl.js +0 -36
- package/dist/free-DAxE4b2U.js +0 -23
- package/dist/grep-Blwrv9k5.js +0 -147
- package/dist/gunzip-R7EtgP15.js +0 -47
- package/dist/gzip-BxF7M1np.js +0 -57
- package/dist/head-CJwcsX18.js +0 -53
- package/dist/hostname-BxU2S3nT.js +0 -8
- package/dist/index-BjKSFBDg.js +0 -8640
- package/dist/kernel/index.d.ts +0 -26
- package/dist/kernel/index.d.ts.map +0 -1
- package/dist/kernel/persistence/PersistenceManager.d.ts +0 -12
- package/dist/kernel/persistence/PersistenceManager.d.ts.map +0 -1
- package/dist/kernel/persistence/backends.d.ts +0 -22
- package/dist/kernel/persistence/backends.d.ts.map +0 -1
- package/dist/kernel/persistence/index.d.ts +0 -6
- package/dist/kernel/persistence/index.d.ts.map +0 -1
- package/dist/kernel/persistence/serializer.d.ts +0 -21
- package/dist/kernel/persistence/serializer.d.ts.map +0 -1
- package/dist/kernel/samples.d.ts +0 -3
- package/dist/kernel/samples.d.ts.map +0 -1
- package/dist/kernel/storage/BlobStore.d.ts +0 -31
- package/dist/kernel/storage/BlobStore.d.ts.map +0 -1
- package/dist/kernel/storage/ContentStore.d.ts +0 -52
- package/dist/kernel/storage/ContentStore.d.ts.map +0 -1
- package/dist/kernel/storage/index.d.ts +0 -7
- package/dist/kernel/storage/index.d.ts.map +0 -1
- package/dist/kernel/vfs/VFS.d.ts +0 -64
- package/dist/kernel/vfs/VFS.d.ts.map +0 -1
- package/dist/kernel/vfs/index.d.ts +0 -7
- package/dist/kernel/vfs/index.d.ts.map +0 -1
- package/dist/kernel/vfs/providers/DevProvider.d.ts +0 -11
- package/dist/kernel/vfs/providers/DevProvider.d.ts.map +0 -1
- package/dist/kernel/vfs/providers/NativeFsProvider.d.ts +0 -63
- package/dist/kernel/vfs/providers/NativeFsProvider.d.ts.map +0 -1
- package/dist/kernel/vfs/providers/ProcProvider.d.ts +0 -14
- package/dist/kernel/vfs/providers/ProcProvider.d.ts.map +0 -1
- package/dist/kernel/vfs/types.d.ts +0 -70
- package/dist/kernel/vfs/types.d.ts.map +0 -1
- package/dist/less-B0gFLJ_7.js +0 -230
- package/dist/ln-Bf3f3jVA.js +0 -11
- package/dist/ls-YnhMRefh.js +0 -106
- package/dist/mkdir-DY1P3Jxj.js +0 -27
- package/dist/mktemp-BRzo2c8y.js +0 -31
- package/dist/mv-bH08L-F_.js +0 -19
- package/dist/nano-CueO2oS0.js +0 -304
- package/dist/nl-D26SSOD4.js +0 -51
- package/dist/node-compat/buffer.d.ts +0 -44
- package/dist/node-compat/buffer.d.ts.map +0 -1
- package/dist/node-compat/child_process.d.ts +0 -10
- package/dist/node-compat/child_process.d.ts.map +0 -1
- package/dist/node-compat/console.d.ts +0 -21
- package/dist/node-compat/console.d.ts.map +0 -1
- package/dist/node-compat/crypto.d.ts +0 -17
- package/dist/node-compat/crypto.d.ts.map +0 -1
- package/dist/node-compat/events.d.ts +0 -19
- package/dist/node-compat/events.d.ts.map +0 -1
- package/dist/node-compat/fs.d.ts +0 -175
- package/dist/node-compat/fs.d.ts.map +0 -1
- package/dist/node-compat/http.d.ts +0 -87
- package/dist/node-compat/http.d.ts.map +0 -1
- package/dist/node-compat/index.d.ts +0 -19
- package/dist/node-compat/index.d.ts.map +0 -1
- package/dist/node-compat/os.d.ts +0 -39
- package/dist/node-compat/os.d.ts.map +0 -1
- package/dist/node-compat/path.d.ts +0 -58
- package/dist/node-compat/path.d.ts.map +0 -1
- package/dist/node-compat/process.d.ts +0 -62
- package/dist/node-compat/process.d.ts.map +0 -1
- package/dist/node-compat/stream.d.ts +0 -36
- package/dist/node-compat/stream.d.ts.map +0 -1
- package/dist/node-compat/string_decoder.d.ts +0 -11
- package/dist/node-compat/string_decoder.d.ts.map +0 -1
- package/dist/node-compat/timers.d.ts +0 -16
- package/dist/node-compat/timers.d.ts.map +0 -1
- package/dist/node-compat/url.d.ts +0 -53
- package/dist/node-compat/url.d.ts.map +0 -1
- package/dist/node-compat/util.d.ts +0 -27
- package/dist/node-compat/util.d.ts.map +0 -1
- package/dist/node-compat/zlib.d.ts +0 -66
- package/dist/node-compat/zlib.d.ts.map +0 -1
- package/dist/ping-DtVEwwda.js +0 -50
- package/dist/printf-Cc2o7KZx.js +0 -70
- package/dist/realpath-BH29Lem_.js +0 -24
- package/dist/rev-ySwmRf_K.js +0 -39
- package/dist/rm-D7iWUUE2.js +0 -37
- package/dist/rmdir-BLtdNjt1.js +0 -35
- package/dist/sed-DwBPYYFb.js +0 -95
- package/dist/seq-B01YKIbO.js +0 -37
- package/dist/sha256sum-DED6ePiU.js +0 -64
- package/dist/sl-Dv4iPBN6.js +0 -138
- package/dist/sleep-B4C3fZiR.js +0 -19
- package/dist/sort-OXqZY83v.js +0 -64
- package/dist/stat-BvwNpE5B.js +0 -29
- package/dist/strings-BesRwUt_.js +0 -42
- package/dist/tac-D4BMSdj9.js +0 -31
- package/dist/tail-xVmLkzJK.js +0 -52
- package/dist/tar-BHRJ0-4j.js +0 -120
- package/dist/tee-DxygINEJ.js +0 -17
- package/dist/touch-DCb9Qn5q.js +0 -23
- package/dist/tr-DsCiwSIu.js +0 -60
- package/dist/tree-4QpgsnA0.js +0 -46
- package/dist/uname-BCn8HlJD.js +0 -37
- package/dist/uniq-xmgE0zRj.js +0 -54
- package/dist/unzip-CDKcoB0B.js +0 -81
- package/dist/uptime-CgBJegqU.js +0 -8
- package/dist/wc-B-jNRfLa.js +0 -69
- package/dist/wget-CO-45JoL.js +0 -53
- package/dist/whoami-Y18wqme3.js +0 -25
- package/dist/xargs-DfbCRFFd.js +0 -35
- package/dist/yes-DXEyHHD2.js +0 -10
- package/dist/zip-Cu1DzbAu.js +0 -33
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { r as resolve, V as VFSError } from "./index-GLjTMF7C.js";
|
|
2
|
+
import { i as isBinaryMime, g as getMimeType } from "./mime-B5knsVEw.js";
|
|
3
|
+
function parseFieldSpec(spec) {
|
|
4
|
+
const fields = [];
|
|
5
|
+
for (const part of spec.split(",")) {
|
|
6
|
+
const rangeParts = part.split("-");
|
|
7
|
+
if (rangeParts.length === 2) {
|
|
8
|
+
const start = parseInt(rangeParts[0], 10);
|
|
9
|
+
const end = parseInt(rangeParts[1], 10);
|
|
10
|
+
if (!isNaN(start) && !isNaN(end)) {
|
|
11
|
+
for (let i = start; i <= end; i++) fields.push(i);
|
|
12
|
+
}
|
|
13
|
+
} else {
|
|
14
|
+
const n = parseInt(part, 10);
|
|
15
|
+
if (!isNaN(n)) fields.push(n);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return fields;
|
|
19
|
+
}
|
|
20
|
+
const command = async (ctx) => {
|
|
21
|
+
let delimiter = " ";
|
|
22
|
+
let fieldSpec = "";
|
|
23
|
+
const files = [];
|
|
24
|
+
for (let i = 0; i < ctx.args.length; i++) {
|
|
25
|
+
const arg = ctx.args[i];
|
|
26
|
+
if (arg === "-d" && i + 1 < ctx.args.length) {
|
|
27
|
+
delimiter = ctx.args[++i];
|
|
28
|
+
} else if (arg === "-f" && i + 1 < ctx.args.length) {
|
|
29
|
+
fieldSpec = ctx.args[++i];
|
|
30
|
+
} else if (arg.startsWith("-d") && arg.length > 2) {
|
|
31
|
+
delimiter = arg.slice(2);
|
|
32
|
+
} else if (arg.startsWith("-f") && arg.length > 2) {
|
|
33
|
+
fieldSpec = arg.slice(2);
|
|
34
|
+
} else {
|
|
35
|
+
files.push(arg);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!fieldSpec) {
|
|
39
|
+
ctx.stderr.write("cut: you must specify a list of fields\n");
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
const fields = parseFieldSpec(fieldSpec);
|
|
43
|
+
function cutLine(line) {
|
|
44
|
+
const parts = line.split(delimiter);
|
|
45
|
+
return fields.map((f) => parts[f - 1] || "").join(delimiter);
|
|
46
|
+
}
|
|
47
|
+
let text = "";
|
|
48
|
+
if (files.length === 0) {
|
|
49
|
+
if (ctx.stdin) {
|
|
50
|
+
text = await ctx.stdin.readAll();
|
|
51
|
+
} else {
|
|
52
|
+
ctx.stderr.write("cut: missing file operand\n");
|
|
53
|
+
return 1;
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
for (const file of files) {
|
|
57
|
+
const path = resolve(ctx.cwd, file);
|
|
58
|
+
try {
|
|
59
|
+
ctx.vfs.stat(path);
|
|
60
|
+
if (isBinaryMime(getMimeType(path))) {
|
|
61
|
+
ctx.stderr.write(`cut: ${file}: binary file, skipping
|
|
62
|
+
`);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
text += ctx.vfs.readFileString(path);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if (e instanceof VFSError) {
|
|
68
|
+
ctx.stderr.write(`cut: ${file}: ${e.message}
|
|
69
|
+
`);
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const lines = text.replace(/\n$/, "").split("\n");
|
|
77
|
+
for (const line of lines) {
|
|
78
|
+
ctx.stdout.write(cutLine(line) + "\n");
|
|
79
|
+
}
|
|
80
|
+
return 0;
|
|
81
|
+
};
|
|
82
|
+
export {
|
|
83
|
+
command as default
|
|
84
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
|
2
|
+
const MONTHS = [
|
|
3
|
+
"January",
|
|
4
|
+
"February",
|
|
5
|
+
"March",
|
|
6
|
+
"April",
|
|
7
|
+
"May",
|
|
8
|
+
"June",
|
|
9
|
+
"July",
|
|
10
|
+
"August",
|
|
11
|
+
"September",
|
|
12
|
+
"October",
|
|
13
|
+
"November",
|
|
14
|
+
"December"
|
|
15
|
+
];
|
|
16
|
+
function pad(n, len = 2) {
|
|
17
|
+
return String(n).padStart(len, "0");
|
|
18
|
+
}
|
|
19
|
+
function formatDate(format, d) {
|
|
20
|
+
let result = "";
|
|
21
|
+
let i = 0;
|
|
22
|
+
while (i < format.length) {
|
|
23
|
+
if (format[i] === "%" && i + 1 < format.length) {
|
|
24
|
+
i++;
|
|
25
|
+
switch (format[i]) {
|
|
26
|
+
case "Y":
|
|
27
|
+
result += d.getFullYear();
|
|
28
|
+
break;
|
|
29
|
+
case "m":
|
|
30
|
+
result += pad(d.getMonth() + 1);
|
|
31
|
+
break;
|
|
32
|
+
case "d":
|
|
33
|
+
result += pad(d.getDate());
|
|
34
|
+
break;
|
|
35
|
+
case "H":
|
|
36
|
+
result += pad(d.getHours());
|
|
37
|
+
break;
|
|
38
|
+
case "M":
|
|
39
|
+
result += pad(d.getMinutes());
|
|
40
|
+
break;
|
|
41
|
+
case "S":
|
|
42
|
+
result += pad(d.getSeconds());
|
|
43
|
+
break;
|
|
44
|
+
case "A":
|
|
45
|
+
result += DAYS[d.getDay()];
|
|
46
|
+
break;
|
|
47
|
+
case "B":
|
|
48
|
+
result += MONTHS[d.getMonth()];
|
|
49
|
+
break;
|
|
50
|
+
case "Z":
|
|
51
|
+
result += "UTC";
|
|
52
|
+
break;
|
|
53
|
+
case "s":
|
|
54
|
+
result += Math.floor(d.getTime() / 1e3);
|
|
55
|
+
break;
|
|
56
|
+
case "p":
|
|
57
|
+
result += d.getHours() >= 12 ? "PM" : "AM";
|
|
58
|
+
break;
|
|
59
|
+
case "I": {
|
|
60
|
+
const h = d.getHours() % 12;
|
|
61
|
+
result += pad(h === 0 ? 12 : h);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
case "%":
|
|
65
|
+
result += "%";
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
result += "%" + format[i];
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
i++;
|
|
72
|
+
} else {
|
|
73
|
+
result += format[i];
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
const command = async (ctx) => {
|
|
80
|
+
const now = /* @__PURE__ */ new Date();
|
|
81
|
+
if (ctx.args.length > 0 && ctx.args[0].startsWith("+")) {
|
|
82
|
+
const format = ctx.args[0].slice(1);
|
|
83
|
+
ctx.stdout.write(formatDate(format, now) + "\n");
|
|
84
|
+
} else {
|
|
85
|
+
ctx.stdout.write(now.toString() + "\n");
|
|
86
|
+
}
|
|
87
|
+
return 0;
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
command as default
|
|
91
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
function humanSize(bytes) {
|
|
2
|
+
if (bytes < 1024) return bytes + "B";
|
|
3
|
+
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + "K";
|
|
4
|
+
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + "M";
|
|
5
|
+
return (bytes / (1024 * 1024 * 1024)).toFixed(1) + "G";
|
|
6
|
+
}
|
|
7
|
+
const command = async (ctx) => {
|
|
8
|
+
let human = false;
|
|
9
|
+
for (const arg of ctx.args) {
|
|
10
|
+
if (arg === "-h") human = true;
|
|
11
|
+
}
|
|
12
|
+
let totalFiles = 0;
|
|
13
|
+
let totalBytes = 0;
|
|
14
|
+
function walk(dirPath) {
|
|
15
|
+
try {
|
|
16
|
+
const entries = ctx.vfs.readdir(dirPath);
|
|
17
|
+
for (const entry of entries) {
|
|
18
|
+
const fullPath = dirPath === "/" ? "/" + entry.name : dirPath + "/" + entry.name;
|
|
19
|
+
if (entry.type === "file") {
|
|
20
|
+
totalFiles++;
|
|
21
|
+
const st = ctx.vfs.stat(fullPath);
|
|
22
|
+
totalBytes += st.size;
|
|
23
|
+
} else {
|
|
24
|
+
walk(fullPath);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
walk("/");
|
|
31
|
+
const totalSpace = 256 * 1024 * 1024;
|
|
32
|
+
const used = totalBytes;
|
|
33
|
+
const avail = totalSpace - used;
|
|
34
|
+
if (human) {
|
|
35
|
+
ctx.stdout.write("Filesystem Size Used Avail Use% Mounted on\n");
|
|
36
|
+
ctx.stdout.write(`vfs ${humanSize(totalSpace)} ${humanSize(used)} ${humanSize(avail)} ${Math.round(used / totalSpace * 100)}% /
|
|
37
|
+
`);
|
|
38
|
+
} else {
|
|
39
|
+
ctx.stdout.write("Filesystem 1K-blocks Used Available Use% Mounted on\n");
|
|
40
|
+
ctx.stdout.write(`vfs ${Math.round(totalSpace / 1024)} ${Math.round(used / 1024)} ${Math.round(avail / 1024)} ${Math.round(used / totalSpace * 100)}% /
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
return 0;
|
|
44
|
+
};
|
|
45
|
+
export {
|
|
46
|
+
command as default
|
|
47
|
+
};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { p as parseArgs } from "./args-BhoL0JcG.js";
|
|
2
|
+
import { r as resolve, V as VFSError } from "./index-GLjTMF7C.js";
|
|
3
|
+
import { i as isBinaryMime, g as getMimeType } from "./mime-B5knsVEw.js";
|
|
4
|
+
const spec = {
|
|
5
|
+
unified: { type: "boolean", short: "u" }
|
|
6
|
+
};
|
|
7
|
+
function computeLCS(a, b) {
|
|
8
|
+
const m = a.length;
|
|
9
|
+
const n = b.length;
|
|
10
|
+
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
|
|
11
|
+
for (let i2 = 1; i2 <= m; i2++) {
|
|
12
|
+
for (let j2 = 1; j2 <= n; j2++) {
|
|
13
|
+
if (a[i2 - 1] === b[j2 - 1]) {
|
|
14
|
+
dp[i2][j2] = dp[i2 - 1][j2 - 1] + 1;
|
|
15
|
+
} else {
|
|
16
|
+
dp[i2][j2] = Math.max(dp[i2 - 1][j2], dp[i2][j2 - 1]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const ops = [];
|
|
21
|
+
let i = m, j = n;
|
|
22
|
+
while (i > 0 || j > 0) {
|
|
23
|
+
if (i > 0 && j > 0 && a[i - 1] === b[j - 1]) {
|
|
24
|
+
ops.push({ type: "keep", oldLine: a[i - 1] });
|
|
25
|
+
i--;
|
|
26
|
+
j--;
|
|
27
|
+
} else if (j > 0 && (i === 0 || dp[i][j - 1] >= dp[i - 1][j])) {
|
|
28
|
+
ops.push({ type: "insert", newLine: b[j - 1] });
|
|
29
|
+
j--;
|
|
30
|
+
} else {
|
|
31
|
+
ops.push({ type: "delete", oldLine: a[i - 1] });
|
|
32
|
+
i--;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return ops.reverse();
|
|
36
|
+
}
|
|
37
|
+
function formatNormal(ops) {
|
|
38
|
+
const output = [];
|
|
39
|
+
let oldIdx = 0;
|
|
40
|
+
let newIdx = 0;
|
|
41
|
+
let i = 0;
|
|
42
|
+
while (i < ops.length) {
|
|
43
|
+
const op = ops[i];
|
|
44
|
+
if (op.type === "keep") {
|
|
45
|
+
oldIdx++;
|
|
46
|
+
newIdx++;
|
|
47
|
+
i++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const delStart = oldIdx;
|
|
51
|
+
const insStart = newIdx;
|
|
52
|
+
const delLines = [];
|
|
53
|
+
const insLines = [];
|
|
54
|
+
while (i < ops.length && ops[i].type !== "keep") {
|
|
55
|
+
if (ops[i].type === "delete") {
|
|
56
|
+
delLines.push(ops[i].oldLine);
|
|
57
|
+
oldIdx++;
|
|
58
|
+
} else {
|
|
59
|
+
insLines.push(ops[i].newLine);
|
|
60
|
+
newIdx++;
|
|
61
|
+
}
|
|
62
|
+
i++;
|
|
63
|
+
}
|
|
64
|
+
const delRange = delLines.length === 1 ? `${delStart + 1}` : delLines.length > 0 ? `${delStart + 1},${delStart + delLines.length}` : `${delStart}`;
|
|
65
|
+
const insRange = insLines.length === 1 ? `${insStart + 1}` : insLines.length > 0 ? `${insStart + 1},${insStart + insLines.length}` : `${insStart}`;
|
|
66
|
+
if (delLines.length > 0 && insLines.length > 0) {
|
|
67
|
+
output.push(`${delRange}c${insRange}`);
|
|
68
|
+
for (const l of delLines) output.push(`< ${l}`);
|
|
69
|
+
output.push("---");
|
|
70
|
+
for (const l of insLines) output.push(`> ${l}`);
|
|
71
|
+
} else if (delLines.length > 0) {
|
|
72
|
+
output.push(`${delRange}d${insRange}`);
|
|
73
|
+
for (const l of delLines) output.push(`< ${l}`);
|
|
74
|
+
} else {
|
|
75
|
+
output.push(`${delRange}a${insRange}`);
|
|
76
|
+
for (const l of insLines) output.push(`> ${l}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return output.length > 0 ? output.join("\n") + "\n" : "";
|
|
80
|
+
}
|
|
81
|
+
function formatUnified(ops, file1, file2) {
|
|
82
|
+
if (ops.every((o) => o.type === "keep")) return "";
|
|
83
|
+
const annotated = [];
|
|
84
|
+
let oldIdx = 0, newIdx = 0;
|
|
85
|
+
for (const op of ops) {
|
|
86
|
+
if (op.type === "keep") {
|
|
87
|
+
annotated.push({ type: "keep", line: op.oldLine, oldIdx, newIdx });
|
|
88
|
+
oldIdx++;
|
|
89
|
+
newIdx++;
|
|
90
|
+
} else if (op.type === "delete") {
|
|
91
|
+
annotated.push({ type: "delete", line: op.oldLine, oldIdx, newIdx });
|
|
92
|
+
oldIdx++;
|
|
93
|
+
} else {
|
|
94
|
+
annotated.push({ type: "insert", line: op.newLine, oldIdx, newIdx });
|
|
95
|
+
newIdx++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const context = 3;
|
|
99
|
+
const hunks = [];
|
|
100
|
+
let currentHunk = null;
|
|
101
|
+
let lastChangeIdx = -Infinity;
|
|
102
|
+
for (let i = 0; i < annotated.length; i++) {
|
|
103
|
+
const a = annotated[i];
|
|
104
|
+
if (a.type !== "keep") {
|
|
105
|
+
if (!currentHunk || i - lastChangeIdx > context * 2 + 1) {
|
|
106
|
+
if (currentHunk) {
|
|
107
|
+
for (let j = lastChangeIdx + 1; j < Math.min(lastChangeIdx + 1 + context, annotated.length); j++) {
|
|
108
|
+
if (annotated[j].type === "keep") {
|
|
109
|
+
currentHunk.lines.push(` ${annotated[j].line}`);
|
|
110
|
+
currentHunk.oldCount++;
|
|
111
|
+
currentHunk.newCount++;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
hunks.push(currentHunk);
|
|
115
|
+
}
|
|
116
|
+
const ctxStart = Math.max(0, i - context);
|
|
117
|
+
let oStart = 0, nStart = 0;
|
|
118
|
+
for (let j = 0; j < ctxStart; j++) {
|
|
119
|
+
if (annotated[j].type !== "insert") oStart++;
|
|
120
|
+
if (annotated[j].type !== "delete") nStart++;
|
|
121
|
+
}
|
|
122
|
+
currentHunk = {
|
|
123
|
+
oldStart: oStart + 1,
|
|
124
|
+
newStart: nStart + 1,
|
|
125
|
+
oldCount: 0,
|
|
126
|
+
newCount: 0,
|
|
127
|
+
lines: []
|
|
128
|
+
};
|
|
129
|
+
for (let j = ctxStart; j < i; j++) {
|
|
130
|
+
if (annotated[j].type === "keep") {
|
|
131
|
+
currentHunk.lines.push(` ${annotated[j].line}`);
|
|
132
|
+
currentHunk.oldCount++;
|
|
133
|
+
currentHunk.newCount++;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} else if (i - lastChangeIdx > 1) {
|
|
137
|
+
for (let j = lastChangeIdx + 1; j < i; j++) {
|
|
138
|
+
if (annotated[j].type === "keep") {
|
|
139
|
+
currentHunk.lines.push(` ${annotated[j].line}`);
|
|
140
|
+
currentHunk.oldCount++;
|
|
141
|
+
currentHunk.newCount++;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (a.type === "delete") {
|
|
146
|
+
currentHunk.lines.push(`-${a.line}`);
|
|
147
|
+
currentHunk.oldCount++;
|
|
148
|
+
} else {
|
|
149
|
+
currentHunk.lines.push(`+${a.line}`);
|
|
150
|
+
currentHunk.newCount++;
|
|
151
|
+
}
|
|
152
|
+
lastChangeIdx = i;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (currentHunk) {
|
|
156
|
+
for (let j = lastChangeIdx + 1; j < Math.min(lastChangeIdx + 1 + context, annotated.length); j++) {
|
|
157
|
+
if (annotated[j].type === "keep") {
|
|
158
|
+
currentHunk.lines.push(` ${annotated[j].line}`);
|
|
159
|
+
currentHunk.oldCount++;
|
|
160
|
+
currentHunk.newCount++;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
hunks.push(currentHunk);
|
|
164
|
+
}
|
|
165
|
+
const output = [];
|
|
166
|
+
output.push(`--- ${file1}`);
|
|
167
|
+
output.push(`+++ ${file2}`);
|
|
168
|
+
for (const hunk of hunks) {
|
|
169
|
+
output.push(`@@ -${hunk.oldStart},${hunk.oldCount} +${hunk.newStart},${hunk.newCount} @@`);
|
|
170
|
+
output.push(...hunk.lines);
|
|
171
|
+
}
|
|
172
|
+
return output.join("\n") + "\n";
|
|
173
|
+
}
|
|
174
|
+
const command = async (ctx) => {
|
|
175
|
+
const { flags, positional } = parseArgs(ctx.args, spec);
|
|
176
|
+
if (positional.length < 2) {
|
|
177
|
+
ctx.stderr.write("diff: missing operand\n");
|
|
178
|
+
return 2;
|
|
179
|
+
}
|
|
180
|
+
const file1 = positional[0];
|
|
181
|
+
const file2 = positional[1];
|
|
182
|
+
let content1;
|
|
183
|
+
let content2;
|
|
184
|
+
const path1 = resolve(ctx.cwd, file1);
|
|
185
|
+
const path2 = resolve(ctx.cwd, file2);
|
|
186
|
+
const binary1 = isBinaryMime(getMimeType(path1));
|
|
187
|
+
const binary2 = isBinaryMime(getMimeType(path2));
|
|
188
|
+
if (binary1 || binary2) {
|
|
189
|
+
try {
|
|
190
|
+
ctx.vfs.stat(path1);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
if (e instanceof VFSError) {
|
|
193
|
+
ctx.stderr.write(`diff: ${file1}: ${e.message}
|
|
194
|
+
`);
|
|
195
|
+
return 2;
|
|
196
|
+
}
|
|
197
|
+
throw e;
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
ctx.vfs.stat(path2);
|
|
201
|
+
} catch (e) {
|
|
202
|
+
if (e instanceof VFSError) {
|
|
203
|
+
ctx.stderr.write(`diff: ${file2}: ${e.message}
|
|
204
|
+
`);
|
|
205
|
+
return 2;
|
|
206
|
+
}
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
209
|
+
ctx.stdout.write(`Binary files ${file1} and ${file2} differ
|
|
210
|
+
`);
|
|
211
|
+
return 2;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
content1 = ctx.vfs.readFileString(resolve(ctx.cwd, file1));
|
|
215
|
+
} catch (e) {
|
|
216
|
+
if (e instanceof VFSError) {
|
|
217
|
+
ctx.stderr.write(`diff: ${file1}: ${e.message}
|
|
218
|
+
`);
|
|
219
|
+
return 2;
|
|
220
|
+
}
|
|
221
|
+
throw e;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
content2 = ctx.vfs.readFileString(resolve(ctx.cwd, file2));
|
|
225
|
+
} catch (e) {
|
|
226
|
+
if (e instanceof VFSError) {
|
|
227
|
+
ctx.stderr.write(`diff: ${file2}: ${e.message}
|
|
228
|
+
`);
|
|
229
|
+
return 2;
|
|
230
|
+
}
|
|
231
|
+
throw e;
|
|
232
|
+
}
|
|
233
|
+
if (content1 === content2) {
|
|
234
|
+
return 0;
|
|
235
|
+
}
|
|
236
|
+
const lines1 = content1.split("\n");
|
|
237
|
+
const lines2 = content2.split("\n");
|
|
238
|
+
if (lines1.length > 0 && lines1[lines1.length - 1] === "") lines1.pop();
|
|
239
|
+
if (lines2.length > 0 && lines2[lines2.length - 1] === "") lines2.pop();
|
|
240
|
+
const ops = computeLCS(lines1, lines2);
|
|
241
|
+
if (flags.unified) {
|
|
242
|
+
ctx.stdout.write(formatUnified(ops, file1, file2));
|
|
243
|
+
} else {
|
|
244
|
+
ctx.stdout.write(formatNormal(ops));
|
|
245
|
+
}
|
|
246
|
+
return 1;
|
|
247
|
+
};
|
|
248
|
+
export {
|
|
249
|
+
command as default
|
|
250
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const TYPE_MAP = {
|
|
2
|
+
1: "A",
|
|
3
|
+
2: "NS",
|
|
4
|
+
5: "CNAME",
|
|
5
|
+
6: "SOA",
|
|
6
|
+
15: "MX",
|
|
7
|
+
16: "TXT",
|
|
8
|
+
28: "AAAA"
|
|
9
|
+
};
|
|
10
|
+
const command = async (ctx) => {
|
|
11
|
+
let queryType = "A";
|
|
12
|
+
let domain;
|
|
13
|
+
for (const arg of ctx.args) {
|
|
14
|
+
if (arg.startsWith("-") || arg.startsWith("+")) continue;
|
|
15
|
+
const upper = arg.toUpperCase();
|
|
16
|
+
if (["A", "AAAA", "CNAME", "MX", "NS", "TXT", "SOA"].includes(upper)) {
|
|
17
|
+
queryType = upper;
|
|
18
|
+
} else {
|
|
19
|
+
domain = arg;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (!domain) {
|
|
23
|
+
ctx.stderr.write("dig: missing domain\n");
|
|
24
|
+
ctx.stderr.write("Usage: dig [type] domain\n");
|
|
25
|
+
return 1;
|
|
26
|
+
}
|
|
27
|
+
const url = `https://dns.google/resolve?name=${encodeURIComponent(domain)}&type=${queryType}`;
|
|
28
|
+
try {
|
|
29
|
+
const response = await fetch(url, {
|
|
30
|
+
headers: { Accept: "application/dns-json" },
|
|
31
|
+
signal: ctx.signal
|
|
32
|
+
});
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
ctx.stderr.write(`dig: DNS query failed (HTTP ${response.status})
|
|
35
|
+
`);
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
const data = await response.json();
|
|
39
|
+
ctx.stdout.write(`; <<>> Lifo dig <<>> ${queryType} ${domain}
|
|
40
|
+
`);
|
|
41
|
+
ctx.stdout.write(`;; Got answer:
|
|
42
|
+
`);
|
|
43
|
+
ctx.stdout.write(`;; ->>HEADER<<- status: ${data.Status === 0 ? "NOERROR" : "NXDOMAIN"}
|
|
44
|
+
`);
|
|
45
|
+
ctx.stdout.write(`
|
|
46
|
+
`);
|
|
47
|
+
if (data.Question && data.Question.length > 0) {
|
|
48
|
+
ctx.stdout.write(`;; QUESTION SECTION:
|
|
49
|
+
`);
|
|
50
|
+
for (const q of data.Question) {
|
|
51
|
+
ctx.stdout.write(`;${q.name}. IN ${TYPE_MAP[q.type] ?? q.type}
|
|
52
|
+
`);
|
|
53
|
+
}
|
|
54
|
+
ctx.stdout.write(`
|
|
55
|
+
`);
|
|
56
|
+
}
|
|
57
|
+
if (data.Answer && data.Answer.length > 0) {
|
|
58
|
+
ctx.stdout.write(`;; ANSWER SECTION:
|
|
59
|
+
`);
|
|
60
|
+
for (const ans of data.Answer) {
|
|
61
|
+
const typeName = TYPE_MAP[ans.type] ?? String(ans.type);
|
|
62
|
+
ctx.stdout.write(`${ans.name}. ${ans.TTL} IN ${typeName} ${ans.data}
|
|
63
|
+
`);
|
|
64
|
+
}
|
|
65
|
+
ctx.stdout.write(`
|
|
66
|
+
`);
|
|
67
|
+
} else {
|
|
68
|
+
ctx.stdout.write(`;; No answers found.
|
|
69
|
+
|
|
70
|
+
`);
|
|
71
|
+
}
|
|
72
|
+
ctx.stdout.write(`;; SERVER: dns.google
|
|
73
|
+
`);
|
|
74
|
+
return 0;
|
|
75
|
+
} catch (e) {
|
|
76
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
77
|
+
if (msg.includes("Failed to fetch") || msg.includes("NetworkError")) {
|
|
78
|
+
ctx.stderr.write(`dig: connection to DNS server failed
|
|
79
|
+
`);
|
|
80
|
+
ctx.stderr.write(`Note: This may be a CORS restriction or network issue.
|
|
81
|
+
`);
|
|
82
|
+
} else {
|
|
83
|
+
ctx.stderr.write(`dig: ${msg}
|
|
84
|
+
`);
|
|
85
|
+
}
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
command as default
|
|
91
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { i as dirname } from "./index-GLjTMF7C.js";
|
|
2
|
+
const command = async (ctx) => {
|
|
3
|
+
if (ctx.args.length === 0) {
|
|
4
|
+
ctx.stderr.write("dirname: missing operand\n");
|
|
5
|
+
return 1;
|
|
6
|
+
}
|
|
7
|
+
for (const arg of ctx.args) {
|
|
8
|
+
ctx.stdout.write(dirname(arg) + "\n");
|
|
9
|
+
}
|
|
10
|
+
return 0;
|
|
11
|
+
};
|
|
12
|
+
export {
|
|
13
|
+
command as default
|
|
14
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { r as resolve, V as VFSError } from "./index-GLjTMF7C.js";
|
|
2
|
+
function humanSize(bytes) {
|
|
3
|
+
if (bytes < 1024) return bytes + "B";
|
|
4
|
+
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + "K";
|
|
5
|
+
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + "M";
|
|
6
|
+
return (bytes / (1024 * 1024 * 1024)).toFixed(1) + "G";
|
|
7
|
+
}
|
|
8
|
+
const command = async (ctx) => {
|
|
9
|
+
let human = false;
|
|
10
|
+
let summaryOnly = false;
|
|
11
|
+
const paths = [];
|
|
12
|
+
for (const arg of ctx.args) {
|
|
13
|
+
if (arg === "-h") human = true;
|
|
14
|
+
else if (arg === "-s") summaryOnly = true;
|
|
15
|
+
else paths.push(arg);
|
|
16
|
+
}
|
|
17
|
+
if (paths.length === 0) paths.push(".");
|
|
18
|
+
function calcSize(dirPath) {
|
|
19
|
+
let total = 0;
|
|
20
|
+
try {
|
|
21
|
+
const entries = ctx.vfs.readdir(dirPath);
|
|
22
|
+
for (const entry of entries) {
|
|
23
|
+
const fullPath = dirPath === "/" ? "/" + entry.name : dirPath + "/" + entry.name;
|
|
24
|
+
if (entry.type === "file") {
|
|
25
|
+
const st = ctx.vfs.stat(fullPath);
|
|
26
|
+
total += st.size;
|
|
27
|
+
} else {
|
|
28
|
+
total += calcSize(fullPath);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} catch {
|
|
32
|
+
}
|
|
33
|
+
return total;
|
|
34
|
+
}
|
|
35
|
+
function walkAndPrint(dirPath, name) {
|
|
36
|
+
let total = 0;
|
|
37
|
+
try {
|
|
38
|
+
const entries = ctx.vfs.readdir(dirPath);
|
|
39
|
+
for (const entry of entries) {
|
|
40
|
+
const fullPath = dirPath === "/" ? "/" + entry.name : dirPath + "/" + entry.name;
|
|
41
|
+
const displayPath = name === "/" ? "/" + entry.name : name + "/" + entry.name;
|
|
42
|
+
if (entry.type === "file") {
|
|
43
|
+
const st = ctx.vfs.stat(fullPath);
|
|
44
|
+
total += st.size;
|
|
45
|
+
} else {
|
|
46
|
+
const subSize = walkAndPrint(fullPath, displayPath);
|
|
47
|
+
total += subSize;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
if (!summaryOnly) {
|
|
53
|
+
const display = human ? humanSize(total) : String(total);
|
|
54
|
+
ctx.stdout.write(display + " " + name + "\n");
|
|
55
|
+
}
|
|
56
|
+
return total;
|
|
57
|
+
}
|
|
58
|
+
let exitCode = 0;
|
|
59
|
+
for (const p of paths) {
|
|
60
|
+
const absPath = resolve(ctx.cwd, p);
|
|
61
|
+
try {
|
|
62
|
+
const st = ctx.vfs.stat(absPath);
|
|
63
|
+
if (st.type === "file") {
|
|
64
|
+
const display = human ? humanSize(st.size) : String(st.size);
|
|
65
|
+
ctx.stdout.write(display + " " + p + "\n");
|
|
66
|
+
} else {
|
|
67
|
+
if (summaryOnly) {
|
|
68
|
+
const total = calcSize(absPath);
|
|
69
|
+
const display = human ? humanSize(total) : String(total);
|
|
70
|
+
ctx.stdout.write(display + " " + p + "\n");
|
|
71
|
+
} else {
|
|
72
|
+
walkAndPrint(absPath, p);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch (e) {
|
|
76
|
+
if (e instanceof VFSError) {
|
|
77
|
+
ctx.stderr.write(`du: ${p}: ${e.message}
|
|
78
|
+
`);
|
|
79
|
+
exitCode = 1;
|
|
80
|
+
} else {
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return exitCode;
|
|
86
|
+
};
|
|
87
|
+
export {
|
|
88
|
+
command as default
|
|
89
|
+
};
|