@bashlife/boxbox-beta 69.0.0
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/bin/boxbox.js +3 -0
- package/package.json +20 -0
- package/src/commands/JavaScriptRun/Loader.js +47 -0
- package/src/commands/JavaScriptRun/ModuleLoader.js +11 -0
- package/src/commands/JavaScriptRun/index.js +61 -0
- package/src/commands/JavaScriptRun/init.js +34 -0
- package/src/commands/JavaScriptRun/jsRun.js +111 -0
- package/src/commands/awk.js +58 -0
- package/src/commands/basename.js +10 -0
- package/src/commands/cal.js +33 -0
- package/src/commands/call.js +115 -0
- package/src/commands/cat.js +16 -0
- package/src/commands/cd.sh +12 -0
- package/src/commands/chmod.js +27 -0
- package/src/commands/clear.js +3 -0
- package/src/commands/cp.js +17 -0
- package/src/commands/curl.js +80 -0
- package/src/commands/cut.js +42 -0
- package/src/commands/date.js +5 -0
- package/src/commands/df.js +35 -0
- package/src/commands/diff.js +33 -0
- package/src/commands/dirname.js +10 -0
- package/src/commands/du.js +31 -0
- package/src/commands/echo.js +32 -0
- package/src/commands/env.js +38 -0
- package/src/commands/expr.js +97 -0
- package/src/commands/file.js +35 -0
- package/src/commands/find.js +25 -0
- package/src/commands/fmt.js +36 -0
- package/src/commands/fold.js +40 -0
- package/src/commands/grep.js +25 -0
- package/src/commands/groups.js +6 -0
- package/src/commands/head.js +23 -0
- package/src/commands/help.js +41 -0
- package/src/commands/hexdump.js +40 -0
- package/src/commands/hostname.js +5 -0
- package/src/commands/id.js +6 -0
- package/src/commands/info.js +47 -0
- package/src/commands/join.js +40 -0
- package/src/commands/kill.js +17 -0
- package/src/commands/ln.js +33 -0
- package/src/commands/ls.js +8 -0
- package/src/commands/mkdir.js +17 -0
- package/src/commands/mv.js +18 -0
- package/src/commands/od.js +32 -0
- package/src/commands/paste.js +28 -0
- package/src/commands/preview.js +38 -0
- package/src/commands/printenv.js +15 -0
- package/src/commands/printf.js +47 -0
- package/src/commands/ps.js +16 -0
- package/src/commands/pwd.js +3 -0
- package/src/commands/readlink.js +17 -0
- package/src/commands/rm.js +60 -0
- package/src/commands/rmdir.js +28 -0
- package/src/commands/sed.js +103 -0
- package/src/commands/seq.js +17 -0
- package/src/commands/sleep.js +17 -0
- package/src/commands/sort.js +24 -0
- package/src/commands/split.js +30 -0
- package/src/commands/strings.js +26 -0
- package/src/commands/tail.js +23 -0
- package/src/commands/tee.js +22 -0
- package/src/commands/touch.js +12 -0
- package/src/commands/tr.js +33 -0
- package/src/commands/tree.js +47 -0
- package/src/commands/tty.js +7 -0
- package/src/commands/uname.js +33 -0
- package/src/commands/uniq.js +29 -0
- package/src/commands/uptime.js +18 -0
- package/src/commands/version.js +20 -0
- package/src/commands/wc.js +22 -0
- package/src/commands/which.js +24 -0
- package/src/commands/whoami.js +5 -0
- package/src/commands/xargs.js +34 -0
- package/src/commands/yes.js +7 -0
- package/src/index.js +194 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function cut(args) {
|
|
4
|
+
let delimiter = "\t";
|
|
5
|
+
let field = null;
|
|
6
|
+
let file = null;
|
|
7
|
+
|
|
8
|
+
for (let i = 0; i < args.length; i++) {
|
|
9
|
+
const arg = args[i];
|
|
10
|
+
|
|
11
|
+
if (arg.startsWith("-d")) {
|
|
12
|
+
delimiter = arg.length > 2 ? arg.slice(2) : args[++i];
|
|
13
|
+
}
|
|
14
|
+
else if (arg.startsWith("-f")) {
|
|
15
|
+
field = Number(arg.length > 2 ? arg.slice(2) : args[++i]);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
file = arg;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!field || !file) {
|
|
23
|
+
console.log("Usage: cut -d delimiter -f field file");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!fs.existsSync(file)) {
|
|
28
|
+
console.log(`cut: ${file}: No such file`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const content = fs.readFileSync(file, "utf8");
|
|
33
|
+
|
|
34
|
+
const lines = content.split("\n");
|
|
35
|
+
|
|
36
|
+
for (const line of lines) {
|
|
37
|
+
if (line.length === 0) continue;
|
|
38
|
+
|
|
39
|
+
const parts = line.split(delimiter);
|
|
40
|
+
console.log(parts[field - 1] ?? "");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function(args) {
|
|
4
|
+
try {
|
|
5
|
+
const path = args[0] || ".";
|
|
6
|
+
|
|
7
|
+
const stats = fs.statfsSync(path);
|
|
8
|
+
|
|
9
|
+
const total = stats.blocks * stats.bsize;
|
|
10
|
+
const free = stats.bfree * stats.bsize;
|
|
11
|
+
const used = total - free;
|
|
12
|
+
|
|
13
|
+
console.log("Filesystem usage:");
|
|
14
|
+
console.log(`Total: ${format(total)}`);
|
|
15
|
+
console.log(`Used: ${format(used)}`);
|
|
16
|
+
console.log(`Free: ${format(free)}`);
|
|
17
|
+
|
|
18
|
+
} catch (err) {
|
|
19
|
+
console.log(`df: ${err.message}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function format(bytes) {
|
|
24
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
25
|
+
|
|
26
|
+
let size = bytes;
|
|
27
|
+
let unit = 0;
|
|
28
|
+
|
|
29
|
+
while (size >= 1024 && unit < units.length - 1) {
|
|
30
|
+
size /= 1024;
|
|
31
|
+
unit++;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return `${size.toFixed(2)} ${units[unit]}`;
|
|
35
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function diff(args) {
|
|
4
|
+
if (args.length < 2) {
|
|
5
|
+
console.log("diff: missing operand");
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const file1 = args[0];
|
|
10
|
+
const file2 = args[1];
|
|
11
|
+
|
|
12
|
+
if (!fs.existsSync(file1)) {
|
|
13
|
+
console.log(`diff: ${file1}: No such file`);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!fs.existsSync(file2)) {
|
|
18
|
+
console.log(`diff: ${file2}: No such file`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const a = fs.readFileSync(file1, "utf8").split("\n");
|
|
23
|
+
const b = fs.readFileSync(file2, "utf8").split("\n");
|
|
24
|
+
|
|
25
|
+
const max = Math.max(a.length, b.length);
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < max; i++) {
|
|
28
|
+
if (a[i] !== b[i]) {
|
|
29
|
+
if (a[i] !== undefined) console.log(`< ${a[i]}`);
|
|
30
|
+
if (b[i] !== undefined) console.log(`> ${b[i]}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export default function(args) {
|
|
5
|
+
const target = args[0] || ".";
|
|
6
|
+
|
|
7
|
+
function getSize(file) {
|
|
8
|
+
const stat = fs.statSync(file);
|
|
9
|
+
|
|
10
|
+
if (stat.isDirectory()) {
|
|
11
|
+
let total = 0;
|
|
12
|
+
|
|
13
|
+
for (const item of fs.readdirSync(file)) {
|
|
14
|
+
total += getSize(path.join(file, item));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return total;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return stat.size;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const size = getSize(target);
|
|
25
|
+
|
|
26
|
+
console.log(`${size}\t${target}`);
|
|
27
|
+
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.log(`du: ${err.message}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function(args) {
|
|
4
|
+
if (args.length === 0) {
|
|
5
|
+
console.log("");
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const outputIndex = args.indexOf("-o");
|
|
10
|
+
|
|
11
|
+
if (outputIndex !== -1) {
|
|
12
|
+
const file = args[outputIndex + 1];
|
|
13
|
+
const text = args
|
|
14
|
+
.slice(0, outputIndex)
|
|
15
|
+
.join(" ");
|
|
16
|
+
|
|
17
|
+
if (!file) {
|
|
18
|
+
console.log("Usage: echo <text> -o <file>");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
fs.writeFileSync(file, text + "\n");
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.log(`echo: ${err.message}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
console.log(args.join(" "));
|
|
32
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
|
|
3
|
+
export default function env(args) {
|
|
4
|
+
let customEnv = {};
|
|
5
|
+
let commandIndex = 0;
|
|
6
|
+
|
|
7
|
+
while (commandIndex < args.length && args[commandIndex].includes("=")) {
|
|
8
|
+
const [key, ...valueParts] = args[commandIndex].split("=");
|
|
9
|
+
customEnv[key] = valueParts.join("=");
|
|
10
|
+
commandIndex++;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// No command = print environment
|
|
14
|
+
if (commandIndex >= args.length) {
|
|
15
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
16
|
+
console.log(`${key}=${value}`);
|
|
17
|
+
}
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const command = args[commandIndex];
|
|
22
|
+
const commandArgs = args.slice(commandIndex + 1);
|
|
23
|
+
|
|
24
|
+
const newEnv = {
|
|
25
|
+
...process.env,
|
|
26
|
+
...customEnv
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const child = spawn(command, commandArgs, {
|
|
30
|
+
env: newEnv,
|
|
31
|
+
stdio: "inherit",
|
|
32
|
+
shell: true
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
child.on("error", (err) => {
|
|
36
|
+
console.error(`env: ${err.message}`);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export default function expr(args) {
|
|
2
|
+
if (args.length === 0) {
|
|
3
|
+
console.log("expr: missing expression");
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const vars = {};
|
|
8
|
+
|
|
9
|
+
// Collect variable assignments
|
|
10
|
+
while (args.length > 0 && /^[a-zA-Z_][a-zA-Z0-9_]*=/.test(args[0])) {
|
|
11
|
+
const [name, value] = args.shift().split("=");
|
|
12
|
+
|
|
13
|
+
const num = Number(value);
|
|
14
|
+
|
|
15
|
+
if (Number.isNaN(num)) {
|
|
16
|
+
console.log(`expr: invalid value for ${name}`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
vars[name] = num;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let expression = args.join(" ");
|
|
24
|
+
|
|
25
|
+
// Replace variables
|
|
26
|
+
for (const [name, value] of Object.entries(vars)) {
|
|
27
|
+
const regex = new RegExp(`\\b${name}\\b`, "g");
|
|
28
|
+
expression = expression.replace(regex, value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Comparisons
|
|
32
|
+
const comparison = expression.match(
|
|
33
|
+
/^(.+)\s*(=|!=|<=|>=|<|>)\s*(.+)$/
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
if (comparison) {
|
|
37
|
+
const left = calculate(comparison[1]);
|
|
38
|
+
const op = comparison[2];
|
|
39
|
+
const right = calculate(comparison[3]);
|
|
40
|
+
|
|
41
|
+
switch (op) {
|
|
42
|
+
case "=":
|
|
43
|
+
console.log(left === right ? 1 : 0);
|
|
44
|
+
return;
|
|
45
|
+
|
|
46
|
+
case "!=":
|
|
47
|
+
console.log(left !== right ? 1 : 0);
|
|
48
|
+
return;
|
|
49
|
+
|
|
50
|
+
case "<":
|
|
51
|
+
console.log(left < right ? 1 : 0);
|
|
52
|
+
return;
|
|
53
|
+
|
|
54
|
+
case ">":
|
|
55
|
+
console.log(left > right ? 1 : 0);
|
|
56
|
+
return;
|
|
57
|
+
|
|
58
|
+
case "<=":
|
|
59
|
+
console.log(left <= right ? 1 : 0);
|
|
60
|
+
return;
|
|
61
|
+
|
|
62
|
+
case ">=":
|
|
63
|
+
console.log(left >= right ? 1 : 0);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
console.log(calculate(expression));
|
|
70
|
+
} catch {
|
|
71
|
+
console.log("expr: invalid expression");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
function calculate(expression) {
|
|
77
|
+
expression = expression.trim();
|
|
78
|
+
expression = expression.replace(/\bx\b/g, "*");
|
|
79
|
+
|
|
80
|
+
const tokens = expression.match(
|
|
81
|
+
/-?\d+(?:\.\d+)?|[+\-*/%()]|\s+/g
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (!tokens) {
|
|
85
|
+
throw new Error();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const clean = tokens
|
|
89
|
+
.filter(t => !/\s/.test(t))
|
|
90
|
+
.join("");
|
|
91
|
+
|
|
92
|
+
if (!/^[0-9+\-*/%.()]+$/.test(clean)) {
|
|
93
|
+
throw new Error();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return Function(`"use strict"; return (${clean})`)();
|
|
97
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export default function(args) {
|
|
5
|
+
const target = args[0];
|
|
6
|
+
|
|
7
|
+
if (!target) {
|
|
8
|
+
console.log("Usage: file <filename>");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const stat = fs.statSync(target);
|
|
14
|
+
|
|
15
|
+
if (stat.isDirectory()) {
|
|
16
|
+
console.log(`${target}: directory`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const ext = path.extname(target).toLowerCase();
|
|
21
|
+
|
|
22
|
+
let type = "regular file";
|
|
23
|
+
|
|
24
|
+
if (ext === ".js") type = "JavaScript source";
|
|
25
|
+
else if (ext === ".json") type = "JSON data";
|
|
26
|
+
else if (ext === ".txt") type = "text file";
|
|
27
|
+
else if (ext === ".png") type = "PNG image";
|
|
28
|
+
else if (ext === ".jpg" || ext === ".jpeg") type = "JPEG image";
|
|
29
|
+
|
|
30
|
+
console.log(`${target}: ${type}`);
|
|
31
|
+
|
|
32
|
+
} catch (err) {
|
|
33
|
+
console.log(`file: ${err.message}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export default function(args) {
|
|
5
|
+
const start = args[0] || ".";
|
|
6
|
+
|
|
7
|
+
function walk(dir) {
|
|
8
|
+
const entries = fs.readdirSync(dir);
|
|
9
|
+
|
|
10
|
+
for (const entry of entries) {
|
|
11
|
+
const fullPath = path.join(dir, entry);
|
|
12
|
+
console.log(fullPath);
|
|
13
|
+
|
|
14
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
15
|
+
walk(fullPath);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
walk(start);
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.log(`find: ${err.message}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function fmt(args) {
|
|
4
|
+
if (!args[0]) {
|
|
5
|
+
console.log("Usage: fmt file");
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const file = args[0];
|
|
10
|
+
|
|
11
|
+
if (!fs.existsSync(file)) {
|
|
12
|
+
console.log(`fmt: ${file}: No such file`);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const width = 75;
|
|
17
|
+
const words = fs.readFileSync(file, "utf8")
|
|
18
|
+
.replace(/\s+/g, " ")
|
|
19
|
+
.trim()
|
|
20
|
+
.split(" ");
|
|
21
|
+
|
|
22
|
+
let line = "";
|
|
23
|
+
|
|
24
|
+
for (const word of words) {
|
|
25
|
+
if ((line + word).length > width) {
|
|
26
|
+
console.log(line.trim());
|
|
27
|
+
line = "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
line += word + " ";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (line.trim()) {
|
|
34
|
+
console.log(line.trim());
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function fold(args) {
|
|
4
|
+
let width = 80;
|
|
5
|
+
let file = null;
|
|
6
|
+
|
|
7
|
+
for (let i = 0; i < args.length; i++) {
|
|
8
|
+
if (args[i] === "-w" && args[i + 1]) {
|
|
9
|
+
width = Number(args[i + 1]);
|
|
10
|
+
i++;
|
|
11
|
+
} else {
|
|
12
|
+
file = args[i];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!file) {
|
|
17
|
+
console.log("Usage: fold -w width file");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!fs.existsSync(file)) {
|
|
22
|
+
console.log(`fold: ${file}: No such file`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const text = fs.readFileSync(file, "utf8");
|
|
27
|
+
|
|
28
|
+
for (const line of text.split("\n")) {
|
|
29
|
+
let current = line;
|
|
30
|
+
|
|
31
|
+
while (current.length > width) {
|
|
32
|
+
console.log(current.slice(0, width));
|
|
33
|
+
current = current.slice(width);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (current.length > 0) {
|
|
37
|
+
console.log(current);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function(args) {
|
|
4
|
+
const pattern = args[0];
|
|
5
|
+
const file = args[1];
|
|
6
|
+
|
|
7
|
+
if (!pattern || !file) {
|
|
8
|
+
console.log("Usage: grep <pattern> <file>");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const content = fs.readFileSync(file, "utf8");
|
|
14
|
+
const lines = content.split("\n");
|
|
15
|
+
|
|
16
|
+
for (const line of lines) {
|
|
17
|
+
if (line.includes(pattern)) {
|
|
18
|
+
console.log(line);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.log(`grep: ${err.message}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function(args) {
|
|
4
|
+
const file = args[0];
|
|
5
|
+
|
|
6
|
+
if (!file) {
|
|
7
|
+
console.log("Usage: head <file>");
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const content = fs.readFileSync(file, "utf8");
|
|
13
|
+
|
|
14
|
+
const lines = content.split("\n");
|
|
15
|
+
|
|
16
|
+
const amount = 10;
|
|
17
|
+
|
|
18
|
+
console.log(lines.slice(0, amount).join("\n"));
|
|
19
|
+
|
|
20
|
+
} catch (err) {
|
|
21
|
+
console.log(`head: ${err.message}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export function commands() {
|
|
2
|
+
console.log(`
|
|
3
|
+
BoxBox v0.1.0
|
|
4
|
+
Modern Unix toolbox
|
|
5
|
+
|
|
6
|
+
Commands:
|
|
7
|
+
ls List files
|
|
8
|
+
cat Show file contents
|
|
9
|
+
mkdir Create directory
|
|
10
|
+
ls
|
|
11
|
+
mkdir
|
|
12
|
+
mv
|
|
13
|
+
cd
|
|
14
|
+
touch
|
|
15
|
+
echo
|
|
16
|
+
cat
|
|
17
|
+
cp
|
|
18
|
+
rm
|
|
19
|
+
pwd
|
|
20
|
+
find
|
|
21
|
+
tree
|
|
22
|
+
wc
|
|
23
|
+
grep
|
|
24
|
+
head
|
|
25
|
+
tail
|
|
26
|
+
sort
|
|
27
|
+
uniq
|
|
28
|
+
which
|
|
29
|
+
uname
|
|
30
|
+
chmod
|
|
31
|
+
du
|
|
32
|
+
df
|
|
33
|
+
printenv
|
|
34
|
+
date
|
|
35
|
+
file
|
|
36
|
+
kill
|
|
37
|
+
call
|
|
38
|
+
curl
|
|
39
|
+
help Show help
|
|
40
|
+
`);
|
|
41
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
3
|
+
export default function hexdump(args) {
|
|
4
|
+
if (!args[0]) {
|
|
5
|
+
console.log("Usage: hexdump file");
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const file = args[0];
|
|
10
|
+
|
|
11
|
+
if (!fs.existsSync(file)) {
|
|
12
|
+
console.log(`hexdump: ${file}: No such file`);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const data = fs.readFileSync(file);
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < data.length; i += 16) {
|
|
19
|
+
const chunk = data.slice(i, i + 16);
|
|
20
|
+
|
|
21
|
+
const hex = Array.from(chunk)
|
|
22
|
+
.map(byte => byte.toString(16).padStart(2, "0"))
|
|
23
|
+
.join(" ");
|
|
24
|
+
|
|
25
|
+
const ascii = Array.from(chunk)
|
|
26
|
+
.map(byte => {
|
|
27
|
+
const c = String.fromCharCode(byte);
|
|
28
|
+
return byte >= 32 && byte <= 126 ? c : ".";
|
|
29
|
+
})
|
|
30
|
+
.join("");
|
|
31
|
+
|
|
32
|
+
console.log(
|
|
33
|
+
i.toString(16).padStart(8, "0") +
|
|
34
|
+
" " +
|
|
35
|
+
hex.padEnd(47, " ") +
|
|
36
|
+
" " +
|
|
37
|
+
ascii
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
export default function info() {
|
|
5
|
+
const root = process.cwd();
|
|
6
|
+
|
|
7
|
+
const packagePath = path.join(
|
|
8
|
+
root,
|
|
9
|
+
"package.json"
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const commandsDir = path.join(
|
|
13
|
+
root,
|
|
14
|
+
"src",
|
|
15
|
+
"commands"
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
let version = "unknown";
|
|
19
|
+
let commandCount = 0;
|
|
20
|
+
|
|
21
|
+
// Read version from package.json
|
|
22
|
+
if (fs.existsSync(packagePath)) {
|
|
23
|
+
try {
|
|
24
|
+
const pkg = JSON.parse(
|
|
25
|
+
fs.readFileSync(packagePath, "utf8")
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
version = pkg.version || "unknown";
|
|
29
|
+
} catch {
|
|
30
|
+
version = "unknown";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Count commands (files + folders)
|
|
35
|
+
if (fs.existsSync(commandsDir)) {
|
|
36
|
+
const entries = fs.readdirSync(commandsDir);
|
|
37
|
+
|
|
38
|
+
commandCount = entries.filter((entry) => {
|
|
39
|
+
return !entry.startsWith(".");
|
|
40
|
+
}).length;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
console.log(`BoxBox ${version}`);
|
|
44
|
+
console.log("Runtime: Node.js");
|
|
45
|
+
console.log(`Platform: ${process.platform}`);
|
|
46
|
+
console.log(`Commands: ${commandCount}`);
|
|
47
|
+
}
|