@ecmaos/coreutils 0.3.1 → 0.4.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +42 -0
- package/dist/commands/cron.d.ts.map +1 -1
- package/dist/commands/cron.js +116 -23
- package/dist/commands/cron.js.map +1 -1
- package/dist/commands/env.d.ts +4 -0
- package/dist/commands/env.d.ts.map +1 -0
- package/dist/commands/env.js +129 -0
- package/dist/commands/env.js.map +1 -0
- package/dist/commands/head.d.ts.map +1 -1
- package/dist/commands/head.js +184 -77
- package/dist/commands/head.js.map +1 -1
- package/dist/commands/less.d.ts.map +1 -1
- package/dist/commands/less.js +1 -0
- package/dist/commands/less.js.map +1 -1
- package/dist/commands/man.d.ts.map +1 -1
- package/dist/commands/man.js +3 -1
- package/dist/commands/man.js.map +1 -1
- package/dist/commands/mount.d.ts +4 -0
- package/dist/commands/mount.d.ts.map +1 -0
- package/dist/commands/mount.js +1041 -0
- package/dist/commands/mount.js.map +1 -0
- package/dist/commands/umount.d.ts +4 -0
- package/dist/commands/umount.d.ts.map +1 -0
- package/dist/commands/umount.js +104 -0
- package/dist/commands/umount.js.map +1 -0
- package/dist/commands/view.d.ts +1 -0
- package/dist/commands/view.d.ts.map +1 -1
- package/dist/commands/view.js +408 -66
- package/dist/commands/view.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/package.json +10 -2
- package/src/commands/cron.ts +115 -23
- package/src/commands/env.ts +143 -0
- package/src/commands/head.ts +176 -77
- package/src/commands/less.ts +1 -0
- package/src/commands/man.ts +4 -1
- package/src/commands/mount.ts +1193 -0
- package/src/commands/umount.ts +117 -0
- package/src/commands/view.ts +463 -73
- package/src/index.ts +9 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
import { mounts } from '@zenfs/core'
|
|
4
|
+
import type { Kernel, Process, Shell, Terminal } from '@ecmaos/types'
|
|
5
|
+
import { TerminalCommand } from '../shared/terminal-command.js'
|
|
6
|
+
import { writelnStdout, writelnStderr } from '../shared/helpers.js'
|
|
7
|
+
|
|
8
|
+
function printUsage(process: Process | undefined, terminal: Terminal): void {
|
|
9
|
+
const usage = `Usage: umount [OPTIONS] TARGET
|
|
10
|
+
umount [-a|--all]
|
|
11
|
+
|
|
12
|
+
Unmount a filesystem.
|
|
13
|
+
|
|
14
|
+
Options:
|
|
15
|
+
-a, --all unmount all filesystems (except root)
|
|
16
|
+
--help display this help and exit
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
umount /mnt/tmp unmount filesystem at /mnt/tmp
|
|
20
|
+
umount -a unmount all filesystems`
|
|
21
|
+
writelnStderr(process, terminal, usage)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createCommand(kernel: Kernel, shell: Shell, terminal: Terminal): TerminalCommand {
|
|
25
|
+
return new TerminalCommand({
|
|
26
|
+
command: 'umount',
|
|
27
|
+
description: 'Unmount a filesystem',
|
|
28
|
+
kernel,
|
|
29
|
+
shell,
|
|
30
|
+
terminal,
|
|
31
|
+
run: async (pid: number, argv: string[]) => {
|
|
32
|
+
const process = kernel.processes.get(pid) as Process | undefined
|
|
33
|
+
|
|
34
|
+
if (argv.length > 0 && (argv[0] === '--help' || argv[0] === '-h')) {
|
|
35
|
+
printUsage(process, terminal)
|
|
36
|
+
return 0
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let allMode = false
|
|
40
|
+
const positionalArgs: string[] = []
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < argv.length; i++) {
|
|
43
|
+
const arg = argv[i]
|
|
44
|
+
if (arg === '-a' || arg === '--all') {
|
|
45
|
+
allMode = true
|
|
46
|
+
} else if (arg && !arg.startsWith('-')) {
|
|
47
|
+
positionalArgs.push(arg)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
if (allMode) {
|
|
53
|
+
const mountList = Array.from(mounts.keys())
|
|
54
|
+
let unmountedCount = 0
|
|
55
|
+
let errorCount = 0
|
|
56
|
+
|
|
57
|
+
for (const target of mountList) {
|
|
58
|
+
if (target === '/') continue
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
kernel.filesystem.fsSync.umount(target)
|
|
62
|
+
unmountedCount++
|
|
63
|
+
await writelnStdout(process, terminal, chalk.green(`Unmounted ${target}`))
|
|
64
|
+
} catch (error) {
|
|
65
|
+
errorCount++
|
|
66
|
+
await writelnStderr(process, terminal, chalk.red(`umount: failed to unmount ${target}: ${error instanceof Error ? error.message : 'Unknown error'}`))
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (unmountedCount === 0 && errorCount === 0) {
|
|
71
|
+
await writelnStdout(process, terminal, 'No filesystems to unmount.')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return errorCount > 0 ? 1 : 0
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (positionalArgs.length === 0) {
|
|
78
|
+
await writelnStderr(process, terminal, chalk.red('umount: missing target argument'))
|
|
79
|
+
await writelnStderr(process, terminal, 'Try \'umount --help\' for more information.')
|
|
80
|
+
return 1
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (positionalArgs.length > 1) {
|
|
84
|
+
await writelnStderr(process, terminal, chalk.red('umount: too many arguments'))
|
|
85
|
+
await writelnStderr(process, terminal, 'Try \'umount --help\' for more information.')
|
|
86
|
+
return 1
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const targetArg = positionalArgs[0]
|
|
90
|
+
if (!targetArg) {
|
|
91
|
+
await writelnStderr(process, terminal, chalk.red('umount: missing target argument'))
|
|
92
|
+
return 1
|
|
93
|
+
}
|
|
94
|
+
const target = path.resolve(shell.cwd, targetArg)
|
|
95
|
+
|
|
96
|
+
if (target === '/') {
|
|
97
|
+
await writelnStderr(process, terminal, chalk.red('umount: cannot unmount root filesystem'))
|
|
98
|
+
return 1
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const mountList = Array.from(mounts.keys())
|
|
102
|
+
if (!mountList.includes(target)) {
|
|
103
|
+
await writelnStderr(process, terminal, chalk.red(`umount: ${target} is not mounted`))
|
|
104
|
+
return 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
kernel.filesystem.fsSync.umount(target)
|
|
109
|
+
await writelnStdout(process, terminal, chalk.green(`Unmounted ${target}`))
|
|
110
|
+
return 0
|
|
111
|
+
} catch (error) {
|
|
112
|
+
await writelnStderr(process, terminal, chalk.red(`umount: failed to unmount ${target}: ${error instanceof Error ? error.message : 'Unknown error'}`))
|
|
113
|
+
return 1
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
}
|