@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.
Files changed (76) hide show
  1. package/bin/boxbox.js +3 -0
  2. package/package.json +20 -0
  3. package/src/commands/JavaScriptRun/Loader.js +47 -0
  4. package/src/commands/JavaScriptRun/ModuleLoader.js +11 -0
  5. package/src/commands/JavaScriptRun/index.js +61 -0
  6. package/src/commands/JavaScriptRun/init.js +34 -0
  7. package/src/commands/JavaScriptRun/jsRun.js +111 -0
  8. package/src/commands/awk.js +58 -0
  9. package/src/commands/basename.js +10 -0
  10. package/src/commands/cal.js +33 -0
  11. package/src/commands/call.js +115 -0
  12. package/src/commands/cat.js +16 -0
  13. package/src/commands/cd.sh +12 -0
  14. package/src/commands/chmod.js +27 -0
  15. package/src/commands/clear.js +3 -0
  16. package/src/commands/cp.js +17 -0
  17. package/src/commands/curl.js +80 -0
  18. package/src/commands/cut.js +42 -0
  19. package/src/commands/date.js +5 -0
  20. package/src/commands/df.js +35 -0
  21. package/src/commands/diff.js +33 -0
  22. package/src/commands/dirname.js +10 -0
  23. package/src/commands/du.js +31 -0
  24. package/src/commands/echo.js +32 -0
  25. package/src/commands/env.js +38 -0
  26. package/src/commands/expr.js +97 -0
  27. package/src/commands/file.js +35 -0
  28. package/src/commands/find.js +25 -0
  29. package/src/commands/fmt.js +36 -0
  30. package/src/commands/fold.js +40 -0
  31. package/src/commands/grep.js +25 -0
  32. package/src/commands/groups.js +6 -0
  33. package/src/commands/head.js +23 -0
  34. package/src/commands/help.js +41 -0
  35. package/src/commands/hexdump.js +40 -0
  36. package/src/commands/hostname.js +5 -0
  37. package/src/commands/id.js +6 -0
  38. package/src/commands/info.js +47 -0
  39. package/src/commands/join.js +40 -0
  40. package/src/commands/kill.js +17 -0
  41. package/src/commands/ln.js +33 -0
  42. package/src/commands/ls.js +8 -0
  43. package/src/commands/mkdir.js +17 -0
  44. package/src/commands/mv.js +18 -0
  45. package/src/commands/od.js +32 -0
  46. package/src/commands/paste.js +28 -0
  47. package/src/commands/preview.js +38 -0
  48. package/src/commands/printenv.js +15 -0
  49. package/src/commands/printf.js +47 -0
  50. package/src/commands/ps.js +16 -0
  51. package/src/commands/pwd.js +3 -0
  52. package/src/commands/readlink.js +17 -0
  53. package/src/commands/rm.js +60 -0
  54. package/src/commands/rmdir.js +28 -0
  55. package/src/commands/sed.js +103 -0
  56. package/src/commands/seq.js +17 -0
  57. package/src/commands/sleep.js +17 -0
  58. package/src/commands/sort.js +24 -0
  59. package/src/commands/split.js +30 -0
  60. package/src/commands/strings.js +26 -0
  61. package/src/commands/tail.js +23 -0
  62. package/src/commands/tee.js +22 -0
  63. package/src/commands/touch.js +12 -0
  64. package/src/commands/tr.js +33 -0
  65. package/src/commands/tree.js +47 -0
  66. package/src/commands/tty.js +7 -0
  67. package/src/commands/uname.js +33 -0
  68. package/src/commands/uniq.js +29 -0
  69. package/src/commands/uptime.js +18 -0
  70. package/src/commands/version.js +20 -0
  71. package/src/commands/wc.js +22 -0
  72. package/src/commands/which.js +24 -0
  73. package/src/commands/whoami.js +5 -0
  74. package/src/commands/xargs.js +34 -0
  75. package/src/commands/yes.js +7 -0
  76. package/src/index.js +194 -0
@@ -0,0 +1,40 @@
1
+ import fs from "fs";
2
+
3
+ export default function join(args) {
4
+ if (args.length < 2) {
5
+ console.log("Usage: join file1 file2");
6
+ return;
7
+ }
8
+
9
+ const file1 = args[0];
10
+ const file2 = args[1];
11
+
12
+ if (!fs.existsSync(file1) || !fs.existsSync(file2)) {
13
+ console.log("join: file not found");
14
+ return;
15
+ }
16
+
17
+ const lines1 = fs.readFileSync(file1, "utf8")
18
+ .trim()
19
+ .split("\n");
20
+
21
+ const lines2 = fs.readFileSync(file2, "utf8")
22
+ .trim()
23
+ .split("\n");
24
+
25
+ const map = new Map();
26
+
27
+ for (const line of lines2) {
28
+ const parts = line.split(/\s+/);
29
+ map.set(parts[0], parts.slice(1).join(" "));
30
+ }
31
+
32
+ for (const line of lines1) {
33
+ const parts = line.split(/\s+/);
34
+ const key = parts[0];
35
+
36
+ if (map.has(key)) {
37
+ console.log(`${line} ${map.get(key)}`);
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,17 @@
1
+ import process from "process";
2
+
3
+ export default function(args) {
4
+ const pid = args[0];
5
+
6
+ if (!pid) {
7
+ console.log("Usage: kill <pid>");
8
+ return;
9
+ }
10
+
11
+ try {
12
+ process.kill(Number(pid), "SIGTERM");
13
+ console.log(`Killed process ${pid}`);
14
+ } catch (err) {
15
+ console.log(`kill: ${err.message}`);
16
+ }
17
+ }
@@ -0,0 +1,33 @@
1
+ import fs from "fs";
2
+
3
+ export default function ln(args) {
4
+ if (args.length === 0) {
5
+ console.log("ln: missing operand");
6
+ return;
7
+ }
8
+
9
+ let symbolic = false;
10
+
11
+ if (args[0] === "-s") {
12
+ symbolic = true;
13
+ args.shift();
14
+ }
15
+
16
+ if (args.length < 2) {
17
+ console.log("ln: missing destination");
18
+ return;
19
+ }
20
+
21
+ const source = args[0];
22
+ const destination = args[1];
23
+
24
+ try {
25
+ if (symbolic) {
26
+ fs.symlinkSync(source, destination);
27
+ } else {
28
+ fs.linkSync(source, destination);
29
+ }
30
+ } catch (err) {
31
+ console.log(`ln: ${err.message}`);
32
+ }
33
+ }
@@ -0,0 +1,8 @@
1
+ import fs from "fs";
2
+
3
+ export default function () {
4
+ const files = fs.readdirSync(".");
5
+ for (const file of files) {
6
+ console.log(file);
7
+ }
8
+ }
@@ -0,0 +1,17 @@
1
+ import fs from "fs";
2
+
3
+ export default function(args) {
4
+ const dir = args[0];
5
+
6
+ if (!dir) {
7
+ console.log("Usage: boxbox mkdir <directory>");
8
+ return;
9
+ }
10
+
11
+ try {
12
+ fs.mkdirSync(dir, { recursive: true });
13
+ console.log(`Created ${dir}`);
14
+ } catch (err) {
15
+ console.log(`mkdir error: ${err.message}`);
16
+ }
17
+ }
@@ -0,0 +1,18 @@
1
+ import fs from "fs";
2
+
3
+ export default function(args) {
4
+ const source = args[0];
5
+ const destination = args[1];
6
+
7
+ if (!source || !destination) {
8
+ console.log("Usage: boxbox mv <source> <destination>");
9
+ return;
10
+ }
11
+
12
+ try {
13
+ fs.renameSync(source, destination);
14
+ console.log(`Moved ${source} -> ${destination}`);
15
+ } catch (err) {
16
+ console.log(`mv error: ${err.message}`);
17
+ }
18
+ }
@@ -0,0 +1,32 @@
1
+ import fs from "fs";
2
+
3
+ export default function od(args) {
4
+ if (!args[0]) {
5
+ console.log("Usage: od file");
6
+ return;
7
+ }
8
+
9
+ const file = args[0];
10
+
11
+ if (!fs.existsSync(file)) {
12
+ console.log(`od: ${file}: No such file`);
13
+ return;
14
+ }
15
+
16
+ const data = fs.readFileSync(file);
17
+
18
+ let output = "";
19
+
20
+ for (let i = 0; i < data.length; i++) {
21
+ if (i % 16 === 0) {
22
+ if (output) console.log(output);
23
+ output = i.toString(8).padStart(7, "0") + " ";
24
+ }
25
+
26
+ output += data[i].toString(16).padStart(2, "0") + " ";
27
+ }
28
+
29
+ if (output) {
30
+ console.log(output);
31
+ }
32
+ }
@@ -0,0 +1,28 @@
1
+ import fs from "fs";
2
+
3
+ export default function paste(args) {
4
+ if (args.length < 2) {
5
+ console.log("Usage: paste file1 file2");
6
+ return;
7
+ }
8
+
9
+ const files = args;
10
+
11
+ const contents = files.map(file => {
12
+ if (!fs.existsSync(file)) {
13
+ console.log(`paste: ${file}: No such file`);
14
+ process.exit(1);
15
+ }
16
+
17
+ return fs.readFileSync(file, "utf8")
18
+ .trim()
19
+ .split("\n");
20
+ });
21
+
22
+ const maxLines = Math.max(...contents.map(lines => lines.length));
23
+
24
+ for (let i = 0; i < maxLines; i++) {
25
+ const row = contents.map(lines => lines[i] ?? "");
26
+ console.log(row.join("\t"));
27
+ }
28
+ }
@@ -0,0 +1,38 @@
1
+ import { execFile } from "child_process";
2
+ import path from "path";
3
+
4
+ export default function preview(args) {
5
+ const input = args[0];
6
+
7
+ if (!input) {
8
+ console.log("Usage: preview <video>");
9
+ return;
10
+ }
11
+
12
+ const ext = path.extname(input);
13
+ const output = input.replace(ext, "-preview.png");
14
+
15
+ execFile(
16
+ "ffmpeg",
17
+ [
18
+ "-i",
19
+ input,
20
+ "-ss",
21
+ "00:00:01",
22
+ "-frames:v",
23
+ "1",
24
+ "-vf",
25
+ "scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720",
26
+ "-y",
27
+ output
28
+ ],
29
+ (error) => {
30
+ if (error) {
31
+ console.log(`preview error: ${error.message}`);
32
+ return;
33
+ }
34
+
35
+ console.log(`Created ${output}`);
36
+ }
37
+ );
38
+ }
@@ -0,0 +1,15 @@
1
+ export default function(args) {
2
+ if (args[0]) {
3
+ const value = process.env[args[0]];
4
+
5
+ if (value !== undefined) {
6
+ console.log(value);
7
+ }
8
+
9
+ return;
10
+ }
11
+
12
+ for (const key of Object.keys(process.env)) {
13
+ console.log(`${key}=${process.env[key]}`);
14
+ }
15
+ }
@@ -0,0 +1,47 @@
1
+ export default function printf(args) {
2
+ if (args.length === 0) {
3
+ return;
4
+ }
5
+
6
+ let color = null;
7
+ let bold = false;
8
+ let text = "";
9
+
10
+ for (const arg of args) {
11
+ if (arg.startsWith("color:")) {
12
+ color = arg.split(":")[1];
13
+ } else if (arg.startsWith("bold:")) {
14
+ bold = arg.split(":")[1] === "true";
15
+ } else if (arg.startsWith("text:")) {
16
+ text = arg.substring(5);
17
+ } else {
18
+ text += (text ? " " : "") + arg;
19
+ }
20
+ }
21
+
22
+ const colors = {
23
+ black: "\x1b[30m",
24
+ red: "\x1b[31m",
25
+ green: "\x1b[32m",
26
+ yellow: "\x1b[33m",
27
+ blue: "\x1b[34m",
28
+ magenta: "\x1b[35m",
29
+ cyan: "\x1b[36m",
30
+ white: "\x1b[37m"
31
+ };
32
+
33
+ let output = "";
34
+
35
+ if (bold) {
36
+ output += "\x1b[1m";
37
+ }
38
+
39
+ if (color && colors[color]) {
40
+ output += colors[color];
41
+ }
42
+
43
+ output += text;
44
+ output += "\x1b[0m";
45
+
46
+ console.log(output);
47
+ }
@@ -0,0 +1,16 @@
1
+ import { exec } from "child_process";
2
+
3
+ export default function ps() {
4
+ const command = process.platform === "win32"
5
+ ? "tasklist"
6
+ : "ps";
7
+
8
+ exec(command, (error, stdout) => {
9
+ if (error) {
10
+ console.log(`ps: ${error.message}`);
11
+ return;
12
+ }
13
+
14
+ console.log(stdout);
15
+ });
16
+ }
@@ -0,0 +1,3 @@
1
+ export default function() {
2
+ console.log(process.cwd());
3
+ }
@@ -0,0 +1,17 @@
1
+ import fs from "fs";
2
+
3
+ export default function readlink(args) {
4
+ if (args.length === 0) {
5
+ console.log("readlink: missing operand");
6
+ return;
7
+ }
8
+
9
+ const file = args[0];
10
+
11
+ try {
12
+ const target = fs.readlinkSync(file);
13
+ console.log(target);
14
+ } catch (err) {
15
+ console.log(`readlink: ${file}: ${err.message}`);
16
+ }
17
+ }
@@ -0,0 +1,60 @@
1
+ import fs from "fs";
2
+
3
+ export default function(args) {
4
+ if (args.length === 0) {
5
+ console.log("Usage: rm [-rf] <file|directory>");
6
+ return;
7
+ }
8
+
9
+ let recursive = false;
10
+ let force = false;
11
+
12
+ const targets = [];
13
+
14
+ for (const arg of args) {
15
+ if (arg.includes("r")) recursive = true;
16
+ if (arg.includes("f")) force = true;
17
+
18
+ if (!arg.startsWith("-")) {
19
+ targets.push(arg);
20
+ }
21
+ }
22
+
23
+ if (targets.length === 0) {
24
+ console.log("rm: missing operand");
25
+ return;
26
+ }
27
+
28
+ for (const target of targets) {
29
+ try {
30
+ if (!fs.existsSync(target)) {
31
+ if (!force) {
32
+ console.log(`rm: ${target}: No such file or directory`);
33
+ }
34
+ continue;
35
+ }
36
+
37
+ const stat = fs.statSync(target);
38
+
39
+ if (stat.isDirectory()) {
40
+ if (!recursive) {
41
+ console.log(`rm: ${target}: is a directory (use -r)`);
42
+ continue;
43
+ }
44
+
45
+ fs.rmSync(target, {
46
+ recursive: true,
47
+ force: true
48
+ });
49
+
50
+ } else {
51
+ fs.rmSync(target, {
52
+ force: true
53
+ });
54
+ }
55
+
56
+ } catch (err) {
57
+ console.log(`rm: ${err.message}`);
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,28 @@
1
+ import fs from "fs";
2
+
3
+ export default function rmdir(args) {
4
+ const dir = args[0];
5
+
6
+ if (!dir) {
7
+ console.error("rmdir: missing operand");
8
+ return;
9
+ }
10
+
11
+ try {
12
+ fs.rmdirSync(dir);
13
+ } catch (err) {
14
+ if (err.code === "ENOTEMPTY") {
15
+ console.error(
16
+ `rmdir: directory not empty: ${dir}`
17
+ );
18
+ } else if (err.code === "ENOENT") {
19
+ console.error(
20
+ `rmdir: no such directory: ${dir}`
21
+ );
22
+ } else {
23
+ console.error(
24
+ `rmdir: ${err.message}`
25
+ );
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,103 @@
1
+ import fs from "fs";
2
+
3
+ export default function sed(args) {
4
+ let quiet = false;
5
+ let expression = null;
6
+ let file = null;
7
+
8
+ for (let i = 0; i < args.length; i++) {
9
+ if (args[i] === "-n") {
10
+ quiet = true;
11
+ } else if (!expression) {
12
+ expression = args[i];
13
+ } else {
14
+ file = args[i];
15
+ }
16
+ }
17
+
18
+ let input = "";
19
+
20
+ if (file) {
21
+ if (!fs.existsSync(file)) {
22
+ console.log(`sed: ${file}: No such file`);
23
+ return;
24
+ }
25
+
26
+ input = fs.readFileSync(file, "utf8");
27
+ } else {
28
+ console.log("sed: stdin mode not supported yet");
29
+ return;
30
+ }
31
+
32
+ let lines = input.split("\n");
33
+
34
+ if (!expression) {
35
+ console.log("Usage: sed [-n] 'command' file");
36
+ return;
37
+ }
38
+
39
+ // Support multiple commands separated by ;
40
+ const commands = expression.split(";");
41
+
42
+ for (const cmd of commands) {
43
+
44
+ // s/old/new/
45
+ if (cmd.startsWith("s/")) {
46
+ const parts = cmd.split("/");
47
+
48
+ const oldText = parts[1];
49
+ const newText = parts[2];
50
+ const flags = parts[3] || "";
51
+
52
+ lines = lines.map(line => {
53
+ if (flags.includes("g")) {
54
+ return line.split(oldText).join(newText);
55
+ }
56
+
57
+ return line.replace(oldText, newText);
58
+ });
59
+
60
+ continue;
61
+ }
62
+
63
+ // /pattern/d
64
+ if (cmd.startsWith("/") && cmd.endsWith("/d")) {
65
+ const pattern = cmd.slice(1, -2);
66
+
67
+ lines = lines.filter(line => !line.includes(pattern));
68
+
69
+ continue;
70
+ }
71
+
72
+ // /pattern/p
73
+ if (cmd.startsWith("/") && cmd.endsWith("/p")) {
74
+ const pattern = cmd.slice(1, -2);
75
+
76
+ lines.forEach(line => {
77
+ if (line.includes(pattern)) {
78
+ console.log(line);
79
+ }
80
+ });
81
+
82
+ return;
83
+ }
84
+
85
+ // line number print: 2p
86
+ if (cmd.endsWith("p")) {
87
+ const num = Number(cmd.slice(0, -1));
88
+
89
+ if (!isNaN(num) && lines[num - 1]) {
90
+ console.log(lines[num - 1]);
91
+ }
92
+
93
+ return;
94
+ }
95
+
96
+ console.log(`sed: unsupported command '${cmd}'`);
97
+ return;
98
+ }
99
+
100
+ if (!quiet) {
101
+ console.log(lines.join("\n"));
102
+ }
103
+ }
@@ -0,0 +1,17 @@
1
+ export default function seq(args) {
2
+ if (!args[0]) {
3
+ console.log("seq: missing operand");
4
+ return;
5
+ }
6
+
7
+ const end = Number(args[0]);
8
+
9
+ if (!Number.isFinite(end)) {
10
+ console.log("seq: invalid number");
11
+ return;
12
+ }
13
+
14
+ for (let i = 1; i <= end; i++) {
15
+ console.log(i);
16
+ }
17
+ }
@@ -0,0 +1,17 @@
1
+ export default async function sleep(args) {
2
+ if (!args[0]) {
3
+ console.log("sleep: missing operand");
4
+ return;
5
+ }
6
+
7
+ const seconds = Number(args[0]);
8
+
9
+ if (!Number.isFinite(seconds) || seconds < 0) {
10
+ console.log("sleep: invalid time");
11
+ return;
12
+ }
13
+
14
+ await new Promise(resolve =>
15
+ setTimeout(resolve, seconds * 1000)
16
+ );
17
+ }
@@ -0,0 +1,24 @@
1
+ import fs from "fs";
2
+
3
+ export default function(args) {
4
+ const file = args[0];
5
+
6
+ if (!file) {
7
+ console.log("Usage: sort <file>");
8
+ return;
9
+ }
10
+
11
+ try {
12
+ const content = fs.readFileSync(file, "utf8");
13
+
14
+ const lines = content
15
+ .split("\n")
16
+ .filter(line => line.length > 0)
17
+ .sort();
18
+
19
+ console.log(lines.join("\n"));
20
+
21
+ } catch (err) {
22
+ console.log(`sort: ${err.message}`);
23
+ }
24
+ }
@@ -0,0 +1,30 @@
1
+ import fs from "fs";
2
+
3
+ export default function split(args) {
4
+ if (!args[0]) {
5
+ console.log("Usage: split file");
6
+ return;
7
+ }
8
+
9
+ const file = args[0];
10
+
11
+ if (!fs.existsSync(file)) {
12
+ console.log(`split: ${file}: No such file`);
13
+ return;
14
+ }
15
+
16
+ const lines = fs.readFileSync(file, "utf8").split("\n");
17
+
18
+ const chunkSize = 1000;
19
+ let index = 0;
20
+
21
+ for (let i = 0; i < lines.length; i += chunkSize) {
22
+ const chunk = lines.slice(i, i + chunkSize).join("\n");
23
+ const name = "x" + String.fromCharCode(97 + index);
24
+
25
+ fs.writeFileSync(name, chunk);
26
+ console.log(name);
27
+
28
+ index++;
29
+ }
30
+ }
@@ -0,0 +1,26 @@
1
+ import fs from "fs";
2
+
3
+ export default function strings(args) {
4
+ if (!args[0]) {
5
+ console.log("Usage: strings file");
6
+ return;
7
+ }
8
+
9
+ const file = args[0];
10
+
11
+ if (!fs.existsSync(file)) {
12
+ console.log(`strings: ${file}: No such file`);
13
+ return;
14
+ }
15
+
16
+ const data = fs.readFileSync(file);
17
+ const text = data.toString("utf8");
18
+
19
+ const matches = text.match(/[ -~]{4,}/g);
20
+
21
+ if (matches) {
22
+ for (const str of matches) {
23
+ console.log(str);
24
+ }
25
+ }
26
+ }
@@ -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: tail <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(-amount).join("\n"));
19
+
20
+ } catch (err) {
21
+ console.log(`tail: ${err.message}`);
22
+ }
23
+ }