kiriya 0.1.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/CHANGELOG.md +58 -0
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/dist/src/config/clean-rules.js +39 -0
- package/dist/src/config/config-keys.js +24 -0
- package/dist/src/config/dependency-directories.js +16 -0
- package/dist/src/config/modules.js +38 -0
- package/dist/src/config/protected-paths.js +32 -0
- package/dist/src/config/rebuildable-directories.js +20 -0
- package/dist/src/core/application/command-registry.js +57 -0
- package/dist/src/core/application/config-values.js +33 -0
- package/dist/src/core/application/path-guard.js +69 -0
- package/dist/src/core/application/paths.js +77 -0
- package/dist/src/core/application/plugin-loader.js +100 -0
- package/dist/src/core/application/process-ending.js +28 -0
- package/dist/src/core/application/root-scope.js +123 -0
- package/dist/src/core/application/safety.js +26 -0
- package/dist/src/core/application/text-input.js +44 -0
- package/dist/src/core/application/walk.js +34 -0
- package/dist/src/core/domain/command.js +8 -0
- package/dist/src/core/domain/encodings.js +70 -0
- package/dist/src/core/domain/errors.js +49 -0
- package/dist/src/core/domain/glob.js +57 -0
- package/dist/src/core/domain/global-options.js +10 -0
- package/dist/src/core/domain/input-schema.js +54 -0
- package/dist/src/core/domain/message.js +7 -0
- package/dist/src/core/domain/module.js +2 -0
- package/dist/src/core/domain/names.js +22 -0
- package/dist/src/core/domain/ports/clipboard.js +2 -0
- package/dist/src/core/domain/ports/clock.js +2 -0
- package/dist/src/core/domain/ports/command-catalog.js +2 -0
- package/dist/src/core/domain/ports/compression.js +2 -0
- package/dist/src/core/domain/ports/config-store.js +2 -0
- package/dist/src/core/domain/ports/confirmation.js +2 -0
- package/dist/src/core/domain/ports/environment.js +2 -0
- package/dist/src/core/domain/ports/file-content.js +2 -0
- package/dist/src/core/domain/ports/file-system.js +2 -0
- package/dist/src/core/domain/ports/hasher.js +2 -0
- package/dist/src/core/domain/ports/network.js +2 -0
- package/dist/src/core/domain/ports/opener.js +2 -0
- package/dist/src/core/domain/ports/passthrough.js +2 -0
- package/dist/src/core/domain/ports/plugin-inventory.js +2 -0
- package/dist/src/core/domain/ports/plugin-source.js +2 -0
- package/dist/src/core/domain/ports/port-table.js +2 -0
- package/dist/src/core/domain/ports/process-runner.js +2 -0
- package/dist/src/core/domain/ports/process-table.js +2 -0
- package/dist/src/core/domain/ports/protected-paths.js +2 -0
- package/dist/src/core/domain/ports/random-source.js +2 -0
- package/dist/src/core/domain/ports/standard-input.js +2 -0
- package/dist/src/core/domain/ports/system-info.js +2 -0
- package/dist/src/core/domain/ports/trash.js +2 -0
- package/dist/src/core/domain/secrets.js +59 -0
- package/dist/src/core/domain/targets.js +41 -0
- package/dist/src/core/domain/text-case.js +39 -0
- package/dist/src/core/domain/values/bytes.js +13 -0
- package/dist/src/core/domain/view.js +2 -0
- package/dist/src/core/infrastructure/node/can-listen.js +10 -0
- package/dist/src/core/infrastructure/node/closed-standard-input.adapter.js +12 -0
- package/dist/src/core/infrastructure/node/config-location.js +27 -0
- package/dist/src/core/infrastructure/node/fs-errors.js +22 -0
- package/dist/src/core/infrastructure/node/json-config-store.adapter.js +69 -0
- package/dist/src/core/infrastructure/node/node-compression.adapter.js +82 -0
- package/dist/src/core/infrastructure/node/node-environment.adapter.js +21 -0
- package/dist/src/core/infrastructure/node/node-file-content.adapter.js +99 -0
- package/dist/src/core/infrastructure/node/node-file-system.adapter.js +169 -0
- package/dist/src/core/infrastructure/node/node-hasher.adapter.js +32 -0
- package/dist/src/core/infrastructure/node/node-network.adapter.js +168 -0
- package/dist/src/core/infrastructure/node/node-plugin-source.adapter.js +89 -0
- package/dist/src/core/infrastructure/node/node-process-runner.adapter.js +48 -0
- package/dist/src/core/infrastructure/node/node-random-source.adapter.js +7 -0
- package/dist/src/core/infrastructure/node/node-standard-input.adapter.js +24 -0
- package/dist/src/core/infrastructure/node/node-system-info.adapter.js +63 -0
- package/dist/src/core/infrastructure/node/process-signals.js +51 -0
- package/dist/src/core/infrastructure/node/run-program.js +65 -0
- package/dist/src/core/infrastructure/node/system-clock.adapter.js +22 -0
- package/dist/src/core/infrastructure/node/typed-programs.js +45 -0
- package/dist/src/core/infrastructure/platform/linux/freedesktop-trash.adapter.js +115 -0
- package/dist/src/core/infrastructure/platform/linux/linux-clipboard.adapter.js +70 -0
- package/dist/src/core/infrastructure/platform/linux/linux-opener.adapter.js +21 -0
- package/dist/src/core/infrastructure/platform/linux/linux-port-table.adapter.js +97 -0
- package/dist/src/core/infrastructure/platform/linux/linux-process-table.adapter.js +61 -0
- package/dist/src/core/infrastructure/platform/macos/macos-clipboard.adapter.js +29 -0
- package/dist/src/core/infrastructure/platform/macos/macos-opener.adapter.js +13 -0
- package/dist/src/core/infrastructure/platform/macos/macos-port-table.adapter.js +86 -0
- package/dist/src/core/infrastructure/platform/macos/macos-process-table.adapter.js +62 -0
- package/dist/src/core/infrastructure/platform/macos/macos-trash.adapter.js +64 -0
- package/dist/src/core/infrastructure/platform/windows/system32.js +6 -0
- package/dist/src/core/infrastructure/platform/windows/windows-clipboard.adapter.js +50 -0
- package/dist/src/core/infrastructure/platform/windows/windows-opener.adapter.js +11 -0
- package/dist/src/core/infrastructure/platform/windows/windows-port-table.adapter.js +38 -0
- package/dist/src/core/infrastructure/platform/windows/windows-process-table.adapter.js +134 -0
- package/dist/src/core/infrastructure/platform/windows/windows-trash.adapter.js +86 -0
- package/dist/src/core/presentation/cli/argv.js +83 -0
- package/dist/src/core/presentation/cli/cli-application.js +201 -0
- package/dist/src/core/presentation/cli/cli-view-format.js +46 -0
- package/dist/src/core/presentation/cli/help.js +202 -0
- package/dist/src/core/presentation/cli/json-output.js +28 -0
- package/dist/src/core/presentation/cli/style.js +21 -0
- package/dist/src/core/presentation/cli/suggest.js +25 -0
- package/dist/src/core/presentation/cli/terminal-confirmation.js +63 -0
- package/dist/src/core/presentation/ended-processes.js +18 -0
- package/dist/src/core/presentation/i18n/translator.js +23 -0
- package/dist/src/core/presentation/list-preview.js +10 -0
- package/dist/src/core/presentation/mcp/elicitation.js +55 -0
- package/dist/src/core/presentation/mcp/mcp-application.js +60 -0
- package/dist/src/core/presentation/mcp/mcp-confirmation.js +59 -0
- package/dist/src/core/presentation/mcp/mcp-server.js +355 -0
- package/dist/src/core/presentation/mcp/protocol.js +32 -0
- package/dist/src/core/presentation/mcp/request-state.js +66 -0
- package/dist/src/core/presentation/mcp/tool-definitions.js +149 -0
- package/dist/src/core/presentation/mcp/tool-results.js +56 -0
- package/dist/src/i18n/locales/en.js +827 -0
- package/dist/src/main.js +154 -0
- package/dist/src/modules/archive/application/archive-sources.js +55 -0
- package/dist/src/modules/archive/application/create-tar.use-case.js +123 -0
- package/dist/src/modules/archive/application/create-zip.use-case.js +106 -0
- package/dist/src/modules/archive/application/extract-tar.use-case.js +240 -0
- package/dist/src/modules/archive/application/extract-zip.use-case.js +181 -0
- package/dist/src/modules/archive/application/tar-reader.js +45 -0
- package/dist/src/modules/archive/application/zip-reader.js +57 -0
- package/dist/src/modules/archive/archive.module.js +27 -0
- package/dist/src/modules/archive/domain/crc32.js +18 -0
- package/dist/src/modules/archive/domain/tar-format.js +97 -0
- package/dist/src/modules/archive/domain/tar-stream.js +223 -0
- package/dist/src/modules/archive/domain/zip-format.js +158 -0
- package/dist/src/modules/archive/presentation/tar.view.js +17 -0
- package/dist/src/modules/archive/presentation/untar.view.js +30 -0
- package/dist/src/modules/archive/presentation/unzip.view.js +24 -0
- package/dist/src/modules/archive/presentation/zip.view.js +17 -0
- package/dist/src/modules/clip/application/copy-text.use-case.js +43 -0
- package/dist/src/modules/clip/application/paste-text.use-case.js +25 -0
- package/dist/src/modules/clip/clip.module.js +16 -0
- package/dist/src/modules/clip/presentation/clip.views.js +7 -0
- package/dist/src/modules/completion/application/print-script.use-case.js +38 -0
- package/dist/src/modules/completion/application/suggest-words.use-case.js +37 -0
- package/dist/src/modules/completion/completion.module.js +14 -0
- package/dist/src/modules/completion/domain/scripts.js +91 -0
- package/dist/src/modules/completion/domain/suggest.js +134 -0
- package/dist/src/modules/completion/presentation/completion.views.js +6 -0
- package/dist/src/modules/config/application/config-commands.js +14 -0
- package/dist/src/modules/config/application/get-setting.use-case.js +25 -0
- package/dist/src/modules/config/application/list-config-keys.use-case.js +26 -0
- package/dist/src/modules/config/application/list-settings.use-case.js +28 -0
- package/dist/src/modules/config/application/set-setting.use-case.js +53 -0
- package/dist/src/modules/config/application/show-config-path.use-case.js +21 -0
- package/dist/src/modules/config/application/unset-setting.use-case.js +28 -0
- package/dist/src/modules/config/config.module.js +28 -0
- package/dist/src/modules/config/presentation/config.views.js +33 -0
- package/dist/src/modules/convert/application/convert-case.use-case.js +38 -0
- package/dist/src/modules/convert/application/convert-codecs.use-case.js +92 -0
- package/dist/src/modules/convert/application/convert-input.js +16 -0
- package/dist/src/modules/convert/application/convert-json.use-case.js +51 -0
- package/dist/src/modules/convert/application/convert-time.use-case.js +39 -0
- package/dist/src/modules/convert/application/decode-jwt.use-case.js +52 -0
- package/dist/src/modules/convert/convert.module.js +29 -0
- package/dist/src/modules/convert/domain/json-syntax.js +135 -0
- package/dist/src/modules/convert/domain/jwt.js +29 -0
- package/dist/src/modules/convert/domain/moments.js +20 -0
- package/dist/src/modules/convert/presentation/convert.views.js +44 -0
- package/dist/src/modules/docker/application/clean-engine.use-case.js +77 -0
- package/dist/src/modules/docker/application/compose-project.js +45 -0
- package/dist/src/modules/docker/application/docker-engine.js +40 -0
- package/dist/src/modules/docker/application/list-containers.use-case.js +69 -0
- package/dist/src/modules/docker/application/run-compose.use-case.js +83 -0
- package/dist/src/modules/docker/application/show-logs.use-case.js +51 -0
- package/dist/src/modules/docker/application/stop-project.use-case.js +56 -0
- package/dist/src/modules/docker/docker.module.js +23 -0
- package/dist/src/modules/docker/domain/compose-file.js +105 -0
- package/dist/src/modules/docker/domain/engine-output.js +65 -0
- package/dist/src/modules/docker/presentation/docker.views.js +63 -0
- package/dist/src/modules/doctor/application/run-checks.use-case.js +128 -0
- package/dist/src/modules/doctor/doctor.module.js +13 -0
- package/dist/src/modules/doctor/domain/versions.js +22 -0
- package/dist/src/modules/doctor/presentation/doctor.view.js +30 -0
- package/dist/src/modules/env/application/check-dotenv.use-case.js +116 -0
- package/dist/src/modules/env/application/inspect-path.use-case.js +71 -0
- package/dist/src/modules/env/application/path-entries.js +28 -0
- package/dist/src/modules/env/application/show-variables.use-case.js +47 -0
- package/dist/src/modules/env/domain/dotenv.js +62 -0
- package/dist/src/modules/env/env.module.js +17 -0
- package/dist/src/modules/env/presentation/env.views.js +43 -0
- package/dist/src/modules/files/application/clean-builds.use-case.js +149 -0
- package/dist/src/modules/files/application/compare-paths.use-case.js +127 -0
- package/dist/src/modules/files/application/copy-paths.use-case.js +101 -0
- package/dist/src/modules/files/application/create-paths.use-case.js +85 -0
- package/dist/src/modules/files/application/delete-paths.use-case.js +127 -0
- package/dist/src/modules/files/application/find-duplicates.use-case.js +105 -0
- package/dist/src/modules/files/application/find-files.use-case.js +131 -0
- package/dist/src/modules/files/application/hash-files.use-case.js +73 -0
- package/dist/src/modules/files/application/list-entries.use-case.js +87 -0
- package/dist/src/modules/files/application/measure-folders.use-case.js +102 -0
- package/dist/src/modules/files/application/move-entry.js +19 -0
- package/dist/src/modules/files/application/move-paths.use-case.js +82 -0
- package/dist/src/modules/files/application/read-text.use-case.js +92 -0
- package/dist/src/modules/files/application/rename-paths.use-case.js +184 -0
- package/dist/src/modules/files/application/replace-text.use-case.js +146 -0
- package/dist/src/modules/files/application/search-text.use-case.js +115 -0
- package/dist/src/modules/files/application/show-info.use-case.js +80 -0
- package/dist/src/modules/files/application/show-tree.use-case.js +75 -0
- package/dist/src/modules/files/application/sync-folders.use-case.js +162 -0
- package/dist/src/modules/files/application/text-files.js +46 -0
- package/dist/src/modules/files/application/transfers.js +35 -0
- package/dist/src/modules/files/domain/case-style.js +18 -0
- package/dist/src/modules/files/domain/filters.js +9 -0
- package/dist/src/modules/files/domain/sync-plan.js +35 -0
- package/dist/src/modules/files/domain/text-encoding.js +75 -0
- package/dist/src/modules/files/domain/values.js +28 -0
- package/dist/src/modules/files/files.module.js +70 -0
- package/dist/src/modules/files/presentation/clean.view.js +15 -0
- package/dist/src/modules/files/presentation/compare.view.js +50 -0
- package/dist/src/modules/files/presentation/copy.view.js +17 -0
- package/dist/src/modules/files/presentation/delete.view.js +16 -0
- package/dist/src/modules/files/presentation/dupes.view.js +20 -0
- package/dist/src/modules/files/presentation/find.view.js +16 -0
- package/dist/src/modules/files/presentation/grep.view.js +27 -0
- package/dist/src/modules/files/presentation/hash.view.js +15 -0
- package/dist/src/modules/files/presentation/info.view.js +36 -0
- package/dist/src/modules/files/presentation/list.view.js +17 -0
- package/dist/src/modules/files/presentation/move.view.js +14 -0
- package/dist/src/modules/files/presentation/new.view.js +8 -0
- package/dist/src/modules/files/presentation/read.view.js +6 -0
- package/dist/src/modules/files/presentation/rename.view.js +16 -0
- package/dist/src/modules/files/presentation/replace.view.js +26 -0
- package/dist/src/modules/files/presentation/size.view.js +24 -0
- package/dist/src/modules/files/presentation/sync.view.js +37 -0
- package/dist/src/modules/files/presentation/tree.view.js +22 -0
- package/dist/src/modules/gen/application/gen-options.js +18 -0
- package/dist/src/modules/gen/application/generate-passwords.use-case.js +39 -0
- package/dist/src/modules/gen/application/generate-tokens.use-case.js +38 -0
- package/dist/src/modules/gen/application/generate-ulids.use-case.js +40 -0
- package/dist/src/modules/gen/application/generate-uuids.use-case.js +32 -0
- package/dist/src/modules/gen/domain/identifiers.js +65 -0
- package/dist/src/modules/gen/domain/secrets.js +43 -0
- package/dist/src/modules/gen/gen.module.js +20 -0
- package/dist/src/modules/git/application/fetch-repositories.use-case.js +50 -0
- package/dist/src/modules/git/application/git-client.js +62 -0
- package/dist/src/modules/git/application/pull-repositories.use-case.js +67 -0
- package/dist/src/modules/git/application/repositories.js +67 -0
- package/dist/src/modules/git/application/show-status.use-case.js +49 -0
- package/dist/src/modules/git/application/switch-branch.use-case.js +118 -0
- package/dist/src/modules/git/domain/repository-status.js +37 -0
- package/dist/src/modules/git/git.module.js +25 -0
- package/dist/src/modules/git/presentation/git.views.js +100 -0
- package/dist/src/modules/net/application/check-connection.use-case.js +71 -0
- package/dist/src/modules/net/application/list-addresses.use-case.js +29 -0
- package/dist/src/modules/net/application/lookup-name.use-case.js +55 -0
- package/dist/src/modules/net/net.module.js +17 -0
- package/dist/src/modules/net/presentation/net.views.js +39 -0
- package/dist/src/modules/open/application/open-target.use-case.js +53 -0
- package/dist/src/modules/open/domain/targets.js +45 -0
- package/dist/src/modules/open/open.module.js +12 -0
- package/dist/src/modules/open/presentation/open.view.js +6 -0
- package/dist/src/modules/port/application/end-port-owners.use-case.js +68 -0
- package/dist/src/modules/port/application/find-free-port.use-case.js +40 -0
- package/dist/src/modules/port/application/find-listeners.use-case.js +34 -0
- package/dist/src/modules/port/application/port-owners.js +25 -0
- package/dist/src/modules/port/domain/port-number.js +8 -0
- package/dist/src/modules/port/port.module.js +17 -0
- package/dist/src/modules/port/presentation/port.views.js +27 -0
- package/dist/src/modules/proc/application/end-processes.use-case.js +77 -0
- package/dist/src/modules/proc/application/find-processes.use-case.js +50 -0
- package/dist/src/modules/proc/application/list-processes.use-case.js +41 -0
- package/dist/src/modules/proc/application/show-tree.use-case.js +40 -0
- package/dist/src/modules/proc/domain/process-tree.js +55 -0
- package/dist/src/modules/proc/presentation/proc.views.js +28 -0
- package/dist/src/modules/proc/proc.module.js +19 -0
- package/dist/src/modules/sys/application/create-report.use-case.js +30 -0
- package/dist/src/modules/sys/application/find-tools.use-case.js +20 -0
- package/dist/src/modules/sys/application/machine-facts.js +44 -0
- package/dist/src/modules/sys/application/show-info.use-case.js +29 -0
- package/dist/src/modules/sys/domain/tools.js +17 -0
- package/dist/src/modules/sys/presentation/sys.views.js +67 -0
- package/dist/src/modules/sys/sys.module.js +17 -0
- package/dist/src/modules/wait/application/poll.js +26 -0
- package/dist/src/modules/wait/application/wait-file.use-case.js +55 -0
- package/dist/src/modules/wait/application/wait-port.use-case.js +76 -0
- package/dist/src/modules/wait/application/wait-url.use-case.js +71 -0
- package/dist/src/modules/wait/domain/reasons.js +14 -0
- package/dist/src/modules/wait/domain/wait-input.js +50 -0
- package/dist/src/modules/wait/presentation/wait.views.js +31 -0
- package/dist/src/modules/wait/wait.module.js +21 -0
- package/package.json +57 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { refuseProtected, requireApproval, requireTypedConfirmation } from "../../../core/application/safety.js";
|
|
3
|
+
import { done, preview } from "../../../core/domain/command.js";
|
|
4
|
+
import { KiriyaError } from "../../../core/domain/errors.js";
|
|
5
|
+
import { RawReader } from "../../../core/domain/input-schema.js";
|
|
6
|
+
import { message } from "../../../core/domain/message.js";
|
|
7
|
+
import { formatBytes } from "../../../core/domain/values/bytes.js";
|
|
8
|
+
import { expandPaths, measure, outermost } from "../../../core/application/paths.js";
|
|
9
|
+
export const deleteSpec = {
|
|
10
|
+
id: "files.delete",
|
|
11
|
+
summary: "files.delete.summary",
|
|
12
|
+
examples: [
|
|
13
|
+
'kiriya files delete "logs/*.log"',
|
|
14
|
+
"kiriya files delete build --dry-run",
|
|
15
|
+
"kiriya files delete old-backups --permanent",
|
|
16
|
+
],
|
|
17
|
+
safety: "destroy",
|
|
18
|
+
idempotent: false,
|
|
19
|
+
usesNetwork: false,
|
|
20
|
+
runsUserCommands: false,
|
|
21
|
+
input: {
|
|
22
|
+
positionals: [{ name: "paths", description: "files.delete.arg.paths", required: true, variadic: true, path: true }],
|
|
23
|
+
options: {
|
|
24
|
+
permanent: { type: "boolean", description: "files.delete.option.permanent" },
|
|
25
|
+
"dry-run": { type: "boolean", description: "files.delete.option.dry-run" },
|
|
26
|
+
yes: { type: "boolean", description: "files.delete.option.yes", short: "y", terminalOnly: true },
|
|
27
|
+
confirm: { type: "string", description: "files.delete.option.confirm", valueName: "<count>", terminalOnly: true },
|
|
28
|
+
all: { type: "boolean", description: "files.option.all", reachesHidden: true },
|
|
29
|
+
},
|
|
30
|
+
parse(raw) {
|
|
31
|
+
const reader = new RawReader(raw);
|
|
32
|
+
return {
|
|
33
|
+
paths: reader.positionalsFrom(0),
|
|
34
|
+
permanent: reader.flag("permanent"),
|
|
35
|
+
dryRun: reader.flag("dry-run"),
|
|
36
|
+
yes: reader.flag("yes"),
|
|
37
|
+
confirm: reader.string("confirm"),
|
|
38
|
+
all: reader.flag("all"),
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
export class DeletePaths {
|
|
44
|
+
fileSystem;
|
|
45
|
+
trash;
|
|
46
|
+
protectedPaths;
|
|
47
|
+
spec = deleteSpec;
|
|
48
|
+
constructor(fileSystem, trash, protectedPaths) {
|
|
49
|
+
this.fileSystem = fileSystem;
|
|
50
|
+
this.trash = trash;
|
|
51
|
+
this.protectedPaths = protectedPaths;
|
|
52
|
+
}
|
|
53
|
+
async execute(input, context) {
|
|
54
|
+
const paths = outermost(await expandPaths(this.fileSystem, input.paths, context.cwd, input.all));
|
|
55
|
+
refuseProtected(this.protectedPaths, paths, context.cwd);
|
|
56
|
+
const planned = [];
|
|
57
|
+
const warnings = [];
|
|
58
|
+
for (const target of paths) {
|
|
59
|
+
const stat = await this.fileSystem.lstat(target);
|
|
60
|
+
const size = await measure(this.fileSystem, target);
|
|
61
|
+
const isRepository = stat?.kind === "directory" && (await this.fileSystem.lstat(path.join(target, ".git"))) !== null;
|
|
62
|
+
if (isRepository)
|
|
63
|
+
warnings.push(message("files.delete.repository", { path: target }));
|
|
64
|
+
planned.push({
|
|
65
|
+
path: target,
|
|
66
|
+
kind: stat?.kind ?? "other",
|
|
67
|
+
bytes: size.bytes,
|
|
68
|
+
files: size.files,
|
|
69
|
+
isRepository,
|
|
70
|
+
outcome: "planned",
|
|
71
|
+
reason: null,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const bytes = planned.reduce((sum, item) => sum + item.bytes, 0);
|
|
75
|
+
const mode = input.permanent ? "permanent" : "trash";
|
|
76
|
+
if (input.dryRun)
|
|
77
|
+
return preview({ mode, items: planned, bytes }, "", warnings);
|
|
78
|
+
if (input.permanent) {
|
|
79
|
+
const warning = message("files.delete.warn-permanent", { count: planned.length, size: formatBytes(bytes) });
|
|
80
|
+
await requireTypedConfirmation(context, warning, String(planned.length), input.confirm);
|
|
81
|
+
return this.finish(mode, await this.removeAll(planned), bytes, warnings);
|
|
82
|
+
}
|
|
83
|
+
const question = message("files.delete.ask-trash", {
|
|
84
|
+
count: planned.length,
|
|
85
|
+
location: message(this.trash.location),
|
|
86
|
+
});
|
|
87
|
+
await requireApproval(context, question, input.yes);
|
|
88
|
+
const outcomes = await this.trash.send(planned.map((item) => item.path));
|
|
89
|
+
const items = planned.map((item, index) => {
|
|
90
|
+
const outcome = outcomes[index];
|
|
91
|
+
if (outcome?.ok === true)
|
|
92
|
+
return { ...item, outcome: "trashed" };
|
|
93
|
+
return {
|
|
94
|
+
...item,
|
|
95
|
+
outcome: "failed",
|
|
96
|
+
reason: outcome?.ok === false ? outcome.reason : message("core.trash.failed", { detail: "no result" }),
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
return this.finish(mode, items, bytes, warnings);
|
|
100
|
+
}
|
|
101
|
+
async removeAll(planned) {
|
|
102
|
+
const items = [];
|
|
103
|
+
for (const item of planned) {
|
|
104
|
+
try {
|
|
105
|
+
await this.fileSystem.remove(item.path);
|
|
106
|
+
items.push({ ...item, outcome: "deleted" });
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (!(error instanceof KiriyaError))
|
|
110
|
+
throw error;
|
|
111
|
+
items.push({ ...item, outcome: "failed", reason: error.detail });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return items;
|
|
115
|
+
}
|
|
116
|
+
finish(mode, items, bytes, warnings) {
|
|
117
|
+
// A reason from the file system already names its path; a trash reason does not.
|
|
118
|
+
const failures = items
|
|
119
|
+
.filter((item) => item.outcome === "failed")
|
|
120
|
+
.map((item) => {
|
|
121
|
+
const reason = item.reason ?? message("core.trash.failed", { detail: "" });
|
|
122
|
+
return "path" in reason.params ? reason : message("files.delete.failed", { path: item.path, reason });
|
|
123
|
+
});
|
|
124
|
+
return done({ mode, items, bytes }, { warnings, failures });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=delete-paths.use-case.js.map
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { walk } from "../../../core/application/walk.js";
|
|
3
|
+
import { done } from "../../../core/domain/command.js";
|
|
4
|
+
import { KiriyaError, NotFoundError } from "../../../core/domain/errors.js";
|
|
5
|
+
import { RawReader } from "../../../core/domain/input-schema.js";
|
|
6
|
+
import { byCodePoint } from "../../../core/domain/names.js";
|
|
7
|
+
import { parseSize } from "../domain/values.js";
|
|
8
|
+
/** Files are grouped by size, then by a hash of this many first bytes, then by a full hash. */
|
|
9
|
+
const PREFIX_BYTES = 64 * 1024;
|
|
10
|
+
async function groupBy(items, key) {
|
|
11
|
+
const groups = new Map();
|
|
12
|
+
for (const item of items) {
|
|
13
|
+
const value = await key(item);
|
|
14
|
+
const group = groups.get(value);
|
|
15
|
+
if (group === undefined)
|
|
16
|
+
groups.set(value, [item]);
|
|
17
|
+
else
|
|
18
|
+
group.push(item);
|
|
19
|
+
}
|
|
20
|
+
return [...groups.values()].filter((group) => group.length > 1);
|
|
21
|
+
}
|
|
22
|
+
const extraBytes = (group) => (group[0]?.size ?? 0) * (group.length - 1);
|
|
23
|
+
export const dupesSpec = {
|
|
24
|
+
id: "files.dupes",
|
|
25
|
+
summary: "files.dupes.summary",
|
|
26
|
+
examples: ["kiriya files dupes ~/Downloads", "kiriya files dupes photos --min-size 1MB", "kiriya files dupes --json"],
|
|
27
|
+
safety: "read",
|
|
28
|
+
idempotent: true,
|
|
29
|
+
usesNetwork: false,
|
|
30
|
+
runsUserCommands: false,
|
|
31
|
+
input: {
|
|
32
|
+
positionals: [
|
|
33
|
+
{ name: "folder", description: "files.dupes.arg.folder", required: false, variadic: false, path: true },
|
|
34
|
+
],
|
|
35
|
+
options: {
|
|
36
|
+
"min-size": { type: "string", description: "files.dupes.option.min-size", valueName: "<size>" },
|
|
37
|
+
all: { type: "boolean", description: "files.option.all" },
|
|
38
|
+
},
|
|
39
|
+
parse(raw) {
|
|
40
|
+
const reader = new RawReader(raw);
|
|
41
|
+
const minSize = reader.string("min-size");
|
|
42
|
+
return {
|
|
43
|
+
folder: reader.positional(0) ?? ".",
|
|
44
|
+
minSize: Math.max(1, minSize === undefined ? 1 : parseSize(minSize)),
|
|
45
|
+
all: reader.flag("all"),
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
export class FindDuplicates {
|
|
51
|
+
fileSystem;
|
|
52
|
+
hasher;
|
|
53
|
+
spec = dupesSpec;
|
|
54
|
+
constructor(fileSystem, hasher) {
|
|
55
|
+
this.fileSystem = fileSystem;
|
|
56
|
+
this.hasher = hasher;
|
|
57
|
+
}
|
|
58
|
+
async execute(input, context) {
|
|
59
|
+
const folder = path.resolve(context.cwd, input.folder);
|
|
60
|
+
const stat = await this.fileSystem.stat(folder);
|
|
61
|
+
if (stat === null)
|
|
62
|
+
throw new NotFoundError("core.fs.not-found", { path: folder });
|
|
63
|
+
if (stat.kind !== "directory")
|
|
64
|
+
throw new NotFoundError("core.path.not-a-folder", { path: folder });
|
|
65
|
+
const files = [];
|
|
66
|
+
for await (const entry of walk(this.fileSystem, folder, { all: input.all })) {
|
|
67
|
+
if (context.signal.aborted)
|
|
68
|
+
break;
|
|
69
|
+
if (entry.kind !== "file")
|
|
70
|
+
continue;
|
|
71
|
+
const size = (await this.fileSystem.lstat(entry.path))?.size ?? 0;
|
|
72
|
+
if (size >= input.minSize)
|
|
73
|
+
files.push({ path: entry.path, size });
|
|
74
|
+
}
|
|
75
|
+
// An unreadable file gets a key of its own, so it never joins a group.
|
|
76
|
+
const digest = async (file, limitBytes) => {
|
|
77
|
+
try {
|
|
78
|
+
return await this.hasher.hashFile(file.path, "sha256", limitBytes);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (error instanceof KiriyaError)
|
|
82
|
+
return `unreadable:${file.path}`;
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const groups = [];
|
|
87
|
+
for (const sameSize of await groupBy(files, (file) => Promise.resolve(String(file.size)))) {
|
|
88
|
+
for (const sameStart of await groupBy(sameSize, (file) => digest(file, PREFIX_BYTES))) {
|
|
89
|
+
const small = (sameStart[0]?.size ?? 0) <= PREFIX_BYTES;
|
|
90
|
+
groups.push(...(small ? [sameStart] : await groupBy(sameStart, (file) => digest(file))));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
groups.sort((a, b) => extraBytes(b) - extraBytes(a));
|
|
94
|
+
return done({
|
|
95
|
+
folder,
|
|
96
|
+
checked: files.length,
|
|
97
|
+
groups: groups.map((group) => ({
|
|
98
|
+
size: group[0]?.size ?? 0,
|
|
99
|
+
paths: group.map((file) => file.path).sort(byCodePoint),
|
|
100
|
+
})),
|
|
101
|
+
extraBytes: groups.reduce((sum, group) => sum + extraBytes(group), 0),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=find-duplicates.use-case.js.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { done } from "../../../core/domain/command.js";
|
|
3
|
+
import { NotFoundError } from "../../../core/domain/errors.js";
|
|
4
|
+
import { RawReader } from "../../../core/domain/input-schema.js";
|
|
5
|
+
import { globToRegExp } from "../../../core/domain/glob.js";
|
|
6
|
+
import { parseExtensions } from "../domain/filters.js";
|
|
7
|
+
import { parseSize, parseTime } from "../domain/values.js";
|
|
8
|
+
import { walk } from "../../../core/application/walk.js";
|
|
9
|
+
export const FIND_TYPES = ["file", "dir"];
|
|
10
|
+
export const findSpec = {
|
|
11
|
+
id: "files.find",
|
|
12
|
+
summary: "files.find.summary",
|
|
13
|
+
examples: [
|
|
14
|
+
'kiriya files find --name "*.test.ts"',
|
|
15
|
+
"kiriya files find src --ext .ts,.tsx --newer 7d",
|
|
16
|
+
"kiriya files find --type dir --empty",
|
|
17
|
+
],
|
|
18
|
+
safety: "read",
|
|
19
|
+
idempotent: true,
|
|
20
|
+
usesNetwork: false,
|
|
21
|
+
runsUserCommands: false,
|
|
22
|
+
input: {
|
|
23
|
+
positionals: [
|
|
24
|
+
{ name: "folder", description: "files.find.arg.folder", required: false, variadic: false, path: true },
|
|
25
|
+
],
|
|
26
|
+
options: {
|
|
27
|
+
name: { type: "string", description: "files.find.option.name", valueName: "<glob>" },
|
|
28
|
+
ext: { type: "string", description: "files.find.option.ext", valueName: "<extensions>", multiple: true },
|
|
29
|
+
type: { type: "string", description: "files.find.option.type", valueName: "<file|dir>" },
|
|
30
|
+
larger: { type: "string", description: "files.find.option.larger", valueName: "<size>" },
|
|
31
|
+
smaller: { type: "string", description: "files.find.option.smaller", valueName: "<size>" },
|
|
32
|
+
newer: { type: "string", description: "files.find.option.newer", valueName: "<time>" },
|
|
33
|
+
older: { type: "string", description: "files.find.option.older", valueName: "<time>" },
|
|
34
|
+
empty: { type: "boolean", description: "files.find.option.empty" },
|
|
35
|
+
all: { type: "boolean", description: "files.option.all" },
|
|
36
|
+
limit: { type: "string", description: "files.find.option.limit", valueName: "<n>" },
|
|
37
|
+
},
|
|
38
|
+
parse(raw) {
|
|
39
|
+
const reader = new RawReader(raw);
|
|
40
|
+
const type = reader.string("type") === undefined ? undefined : reader.choice("type", FIND_TYPES, "file");
|
|
41
|
+
const larger = reader.string("larger");
|
|
42
|
+
const smaller = reader.string("smaller");
|
|
43
|
+
return {
|
|
44
|
+
folder: reader.positional(0) ?? ".",
|
|
45
|
+
name: reader.string("name"),
|
|
46
|
+
extensions: parseExtensions(reader.strings("ext")),
|
|
47
|
+
type,
|
|
48
|
+
larger: larger === undefined ? undefined : parseSize(larger),
|
|
49
|
+
smaller: smaller === undefined ? undefined : parseSize(smaller),
|
|
50
|
+
newer: reader.string("newer"),
|
|
51
|
+
older: reader.string("older"),
|
|
52
|
+
empty: reader.flag("empty"),
|
|
53
|
+
all: reader.flag("all"),
|
|
54
|
+
limit: reader.positiveInteger("limit", 200),
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
export class FindFiles {
|
|
60
|
+
fileSystem;
|
|
61
|
+
clock;
|
|
62
|
+
spec = findSpec;
|
|
63
|
+
constructor(fileSystem, clock) {
|
|
64
|
+
this.fileSystem = fileSystem;
|
|
65
|
+
this.clock = clock;
|
|
66
|
+
}
|
|
67
|
+
async execute(input, context) {
|
|
68
|
+
const folder = path.resolve(context.cwd, input.folder);
|
|
69
|
+
const stat = await this.fileSystem.stat(folder);
|
|
70
|
+
if (stat === null)
|
|
71
|
+
throw new NotFoundError("core.fs.not-found", { path: folder });
|
|
72
|
+
if (stat.kind !== "directory")
|
|
73
|
+
throw new NotFoundError("core.path.not-a-folder", { path: folder });
|
|
74
|
+
const now = this.clock.now();
|
|
75
|
+
const newer = input.newer === undefined ? undefined : parseTime(input.newer, now);
|
|
76
|
+
const older = input.older === undefined ? undefined : parseTime(input.older, now);
|
|
77
|
+
const pattern = input.name === undefined ? undefined : globToRegExp(input.name);
|
|
78
|
+
const patternHasSlash = input.name?.includes("/") === true;
|
|
79
|
+
const extensions = new Set(input.extensions);
|
|
80
|
+
const sizeFilter = input.larger !== undefined || input.smaller !== undefined;
|
|
81
|
+
const matches = [];
|
|
82
|
+
let total = 0;
|
|
83
|
+
let totalBytes = 0;
|
|
84
|
+
for await (const entry of walk(this.fileSystem, folder, { all: input.all })) {
|
|
85
|
+
if (context.signal.aborted)
|
|
86
|
+
break;
|
|
87
|
+
const isDirectory = entry.kind === "directory";
|
|
88
|
+
if (input.type === "file" && isDirectory)
|
|
89
|
+
continue;
|
|
90
|
+
if (input.type === "dir" && !isDirectory)
|
|
91
|
+
continue;
|
|
92
|
+
if (pattern !== undefined && !pattern.test(patternHasSlash ? entry.rel : entry.name))
|
|
93
|
+
continue;
|
|
94
|
+
if (extensions.size > 0 && (isDirectory || !extensions.has(path.extname(entry.name).toLowerCase())))
|
|
95
|
+
continue;
|
|
96
|
+
if (sizeFilter && isDirectory)
|
|
97
|
+
continue;
|
|
98
|
+
const entryStat = await this.fileSystem.lstat(entry.path);
|
|
99
|
+
if (entryStat === null)
|
|
100
|
+
continue;
|
|
101
|
+
if (input.larger !== undefined && entryStat.size <= input.larger)
|
|
102
|
+
continue;
|
|
103
|
+
if (input.smaller !== undefined && entryStat.size >= input.smaller)
|
|
104
|
+
continue;
|
|
105
|
+
if (newer !== undefined && entryStat.modifiedMs < newer)
|
|
106
|
+
continue;
|
|
107
|
+
if (older !== undefined && entryStat.modifiedMs > older)
|
|
108
|
+
continue;
|
|
109
|
+
if (input.empty) {
|
|
110
|
+
const empty = isDirectory
|
|
111
|
+
? (await this.fileSystem.readDirectory(entry.path)).length === 0
|
|
112
|
+
: entryStat.size === 0;
|
|
113
|
+
if (!empty)
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
total += 1;
|
|
117
|
+
if (!isDirectory)
|
|
118
|
+
totalBytes += entryStat.size;
|
|
119
|
+
if (matches.length < input.limit) {
|
|
120
|
+
matches.push({
|
|
121
|
+
path: entry.path,
|
|
122
|
+
kind: entry.kind,
|
|
123
|
+
size: isDirectory ? 0 : entryStat.size,
|
|
124
|
+
modifiedMs: entryStat.modifiedMs,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return done({ folder, matches, total, totalBytes });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=find-files.use-case.js.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { expandPaths } from "../../../core/application/paths.js";
|
|
2
|
+
import { done } from "../../../core/domain/command.js";
|
|
3
|
+
import { UsageError } from "../../../core/domain/errors.js";
|
|
4
|
+
import { RawReader } from "../../../core/domain/input-schema.js";
|
|
5
|
+
import { message } from "../../../core/domain/message.js";
|
|
6
|
+
import { HASH_ALGORITHMS } from "../../../core/domain/ports/hasher.js";
|
|
7
|
+
export const hashSpec = {
|
|
8
|
+
id: "files.hash",
|
|
9
|
+
summary: "files.hash.summary",
|
|
10
|
+
examples: [
|
|
11
|
+
"kiriya files hash setup.exe",
|
|
12
|
+
'kiriya files hash "dist/*" --algo sha512',
|
|
13
|
+
"kiriya files hash node.tar.gz --check <expected-sha256>",
|
|
14
|
+
],
|
|
15
|
+
safety: "read",
|
|
16
|
+
idempotent: true,
|
|
17
|
+
usesNetwork: false,
|
|
18
|
+
runsUserCommands: false,
|
|
19
|
+
input: {
|
|
20
|
+
positionals: [{ name: "paths", description: "files.hash.arg.paths", required: true, variadic: true, path: true }],
|
|
21
|
+
options: {
|
|
22
|
+
algo: { type: "string", description: "files.hash.option.algo", valueName: `<${HASH_ALGORITHMS.join("|")}>` },
|
|
23
|
+
check: { type: "string", description: "files.hash.option.check", valueName: "<hex>" },
|
|
24
|
+
},
|
|
25
|
+
parse(raw) {
|
|
26
|
+
const reader = new RawReader(raw);
|
|
27
|
+
return {
|
|
28
|
+
paths: reader.positionalsFrom(0),
|
|
29
|
+
algorithm: reader.choice("algo", HASH_ALGORITHMS, "sha256"),
|
|
30
|
+
check: reader.string("check")?.trim().toLowerCase(),
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
export class HashFiles {
|
|
36
|
+
fileSystem;
|
|
37
|
+
hasher;
|
|
38
|
+
spec = hashSpec;
|
|
39
|
+
constructor(fileSystem, hasher) {
|
|
40
|
+
this.fileSystem = fileSystem;
|
|
41
|
+
this.hasher = hasher;
|
|
42
|
+
}
|
|
43
|
+
async execute(input, context) {
|
|
44
|
+
const warnings = [];
|
|
45
|
+
const files = [];
|
|
46
|
+
for (const target of await expandPaths(this.fileSystem, input.paths, context.cwd, false)) {
|
|
47
|
+
if ((await this.fileSystem.lstat(target))?.kind === "file")
|
|
48
|
+
files.push(target);
|
|
49
|
+
else
|
|
50
|
+
warnings.push(message("files.hash.not-a-file", { path: target }));
|
|
51
|
+
}
|
|
52
|
+
if (input.check !== undefined) {
|
|
53
|
+
const [file, ...rest] = files;
|
|
54
|
+
if (file === undefined || rest.length > 0)
|
|
55
|
+
throw new UsageError("files.hash.check-one");
|
|
56
|
+
const actual = await this.hasher.hashFile(file, input.algorithm);
|
|
57
|
+
const matches = actual === input.check;
|
|
58
|
+
const failures = matches
|
|
59
|
+
? []
|
|
60
|
+
: [message("files.hash.mismatch", { algorithm: input.algorithm, expected: input.check, actual })];
|
|
61
|
+
const check = { path: file, expected: input.check, actual, matches };
|
|
62
|
+
return done({ algorithm: input.algorithm, files: [{ path: file, hash: actual }], check }, { warnings, failures });
|
|
63
|
+
}
|
|
64
|
+
const hashed = [];
|
|
65
|
+
for (const file of files) {
|
|
66
|
+
if (context.signal.aborted)
|
|
67
|
+
break;
|
|
68
|
+
hashed.push({ path: file, hash: await this.hasher.hashFile(file, input.algorithm) });
|
|
69
|
+
}
|
|
70
|
+
return done({ algorithm: input.algorithm, files: hashed, check: null }, { warnings });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=hash-files.use-case.js.map
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { done } from "../../../core/domain/command.js";
|
|
3
|
+
import { NotFoundError } from "../../../core/domain/errors.js";
|
|
4
|
+
import { RawReader } from "../../../core/domain/input-schema.js";
|
|
5
|
+
import { byCodePoint, isHiddenName } from "../../../core/domain/names.js";
|
|
6
|
+
export const LIST_SORTS = ["name", "size", "time"];
|
|
7
|
+
export const listSpec = {
|
|
8
|
+
id: "files.list",
|
|
9
|
+
summary: "files.list.summary",
|
|
10
|
+
examples: ["kiriya files list", "kiriya files list src --sort size", "kiriya files list --all --json"],
|
|
11
|
+
safety: "read",
|
|
12
|
+
idempotent: true,
|
|
13
|
+
usesNetwork: false,
|
|
14
|
+
runsUserCommands: false,
|
|
15
|
+
input: {
|
|
16
|
+
positionals: [{ name: "path", description: "files.list.arg.path", required: false, variadic: false, path: true }],
|
|
17
|
+
options: {
|
|
18
|
+
all: { type: "boolean", description: "files.option.all" },
|
|
19
|
+
sort: { type: "string", description: "files.list.option.sort", valueName: "<name|size|time>" },
|
|
20
|
+
reverse: { type: "boolean", description: "files.list.option.reverse" },
|
|
21
|
+
},
|
|
22
|
+
parse(raw) {
|
|
23
|
+
const reader = new RawReader(raw);
|
|
24
|
+
return {
|
|
25
|
+
path: reader.positional(0) ?? ".",
|
|
26
|
+
all: reader.flag("all"),
|
|
27
|
+
sort: reader.choice("sort", LIST_SORTS, "name"),
|
|
28
|
+
reverse: reader.flag("reverse"),
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
export class ListEntries {
|
|
34
|
+
fileSystem;
|
|
35
|
+
spec = listSpec;
|
|
36
|
+
constructor(fileSystem) {
|
|
37
|
+
this.fileSystem = fileSystem;
|
|
38
|
+
}
|
|
39
|
+
async execute(input, context) {
|
|
40
|
+
const target = path.resolve(context.cwd, input.path);
|
|
41
|
+
const stat = await this.fileSystem.lstat(target);
|
|
42
|
+
if (stat === null)
|
|
43
|
+
throw new NotFoundError("core.fs.not-found", { path: target });
|
|
44
|
+
const entries = [];
|
|
45
|
+
let hidden = 0;
|
|
46
|
+
if (stat.kind === "directory") {
|
|
47
|
+
for (const entry of await this.fileSystem.readDirectory(target)) {
|
|
48
|
+
if (!input.all && isHiddenName(entry.name)) {
|
|
49
|
+
hidden += 1;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const listed = await this.describe(path.join(target, entry.name), entry.name);
|
|
53
|
+
if (listed !== null)
|
|
54
|
+
entries.push(listed);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
const listed = await this.describe(target, path.basename(target));
|
|
59
|
+
if (listed !== null)
|
|
60
|
+
entries.push(listed);
|
|
61
|
+
}
|
|
62
|
+
const byName = (a, b) => Number(b.kind === "directory") - Number(a.kind === "directory") || byCodePoint(a.name, b.name);
|
|
63
|
+
const order = input.sort === "size"
|
|
64
|
+
? (a, b) => b.size - a.size || byName(a, b)
|
|
65
|
+
: input.sort === "time"
|
|
66
|
+
? (a, b) => b.modifiedMs - a.modifiedMs || byName(a, b)
|
|
67
|
+
: byName;
|
|
68
|
+
entries.sort(order);
|
|
69
|
+
if (input.reverse)
|
|
70
|
+
entries.reverse();
|
|
71
|
+
return done({ path: target, entries, hidden });
|
|
72
|
+
}
|
|
73
|
+
async describe(entryPath, name) {
|
|
74
|
+
const stat = await this.fileSystem.lstat(entryPath);
|
|
75
|
+
if (stat === null)
|
|
76
|
+
return null;
|
|
77
|
+
return {
|
|
78
|
+
name,
|
|
79
|
+
path: entryPath,
|
|
80
|
+
kind: stat.kind,
|
|
81
|
+
size: stat.kind === "file" ? stat.size : 0,
|
|
82
|
+
modifiedMs: stat.modifiedMs,
|
|
83
|
+
target: stat.kind === "symlink" ? await this.fileSystem.readLink(entryPath) : null,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=list-entries.use-case.js.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { REBUILDABLE_DIRECTORIES } from "../../../config/rebuildable-directories.js";
|
|
3
|
+
import { done } from "../../../core/domain/command.js";
|
|
4
|
+
import { KiriyaError, NotFoundError } from "../../../core/domain/errors.js";
|
|
5
|
+
import { RawReader } from "../../../core/domain/input-schema.js";
|
|
6
|
+
import { byCodePoint } from "../../../core/domain/names.js";
|
|
7
|
+
export const sizeSpec = {
|
|
8
|
+
id: "files.size",
|
|
9
|
+
summary: "files.size.summary",
|
|
10
|
+
examples: ["kiriya files size", "kiriya files size ~/projects --top 30", "kiriya files size --json"],
|
|
11
|
+
safety: "read",
|
|
12
|
+
idempotent: true,
|
|
13
|
+
usesNetwork: false,
|
|
14
|
+
runsUserCommands: false,
|
|
15
|
+
input: {
|
|
16
|
+
positionals: [
|
|
17
|
+
{ name: "folder", description: "files.size.arg.folder", required: false, variadic: false, path: true },
|
|
18
|
+
],
|
|
19
|
+
options: { top: { type: "string", description: "files.size.option.top", valueName: "<n>" } },
|
|
20
|
+
parse(raw) {
|
|
21
|
+
const reader = new RawReader(raw);
|
|
22
|
+
return { folder: reader.positional(0) ?? ".", top: reader.positiveInteger("top", 15) };
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
export class MeasureFolders {
|
|
27
|
+
fileSystem;
|
|
28
|
+
spec = sizeSpec;
|
|
29
|
+
constructor(fileSystem) {
|
|
30
|
+
this.fileSystem = fileSystem;
|
|
31
|
+
}
|
|
32
|
+
async execute(input, context) {
|
|
33
|
+
const folder = path.resolve(context.cwd, input.folder);
|
|
34
|
+
const stat = await this.fileSystem.stat(folder);
|
|
35
|
+
if (stat === null)
|
|
36
|
+
throw new NotFoundError("core.fs.not-found", { path: folder });
|
|
37
|
+
if (stat.kind !== "directory")
|
|
38
|
+
throw new NotFoundError("core.path.not-a-folder", { path: folder });
|
|
39
|
+
const rows = [];
|
|
40
|
+
let looseBytes = 0;
|
|
41
|
+
for (const entry of await this.fileSystem.readDirectory(folder)) {
|
|
42
|
+
if (context.signal.aborted)
|
|
43
|
+
break;
|
|
44
|
+
const entryPath = path.join(folder, entry.name);
|
|
45
|
+
if (entry.kind === "directory") {
|
|
46
|
+
const size = await this.measure(entryPath, REBUILDABLE_DIRECTORIES.has(entry.name));
|
|
47
|
+
rows.push({ name: entry.name, path: entryPath, bytes: size.total, rebuildableBytes: size.rebuildable });
|
|
48
|
+
}
|
|
49
|
+
else if (entry.kind === "file") {
|
|
50
|
+
looseBytes += await this.sizeOf(entryPath);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
rows.sort((a, b) => b.bytes - a.bytes || byCodePoint(a.name, b.name));
|
|
54
|
+
return done({
|
|
55
|
+
folder,
|
|
56
|
+
bytes: rows.reduce((sum, row) => sum + row.bytes, looseBytes),
|
|
57
|
+
rebuildableBytes: rows.reduce((sum, row) => sum + row.rebuildableBytes, 0),
|
|
58
|
+
looseBytes,
|
|
59
|
+
top: input.top,
|
|
60
|
+
rows,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** Symlinks are skipped, so a link can never count a folder twice or loop. */
|
|
64
|
+
async measure(folder, rebuildable) {
|
|
65
|
+
const size = { total: 0, rebuildable: 0 };
|
|
66
|
+
let entries;
|
|
67
|
+
try {
|
|
68
|
+
entries = await this.fileSystem.readDirectory(folder);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof KiriyaError)
|
|
72
|
+
return size;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
for (const entry of entries) {
|
|
76
|
+
const entryPath = path.join(folder, entry.name);
|
|
77
|
+
if (entry.kind === "directory") {
|
|
78
|
+
const inner = await this.measure(entryPath, rebuildable || REBUILDABLE_DIRECTORIES.has(entry.name));
|
|
79
|
+
size.total += inner.total;
|
|
80
|
+
size.rebuildable += inner.rebuildable;
|
|
81
|
+
}
|
|
82
|
+
else if (entry.kind === "file") {
|
|
83
|
+
const bytes = await this.sizeOf(entryPath);
|
|
84
|
+
size.total += bytes;
|
|
85
|
+
if (rebuildable)
|
|
86
|
+
size.rebuildable += bytes;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return size;
|
|
90
|
+
}
|
|
91
|
+
async sizeOf(file) {
|
|
92
|
+
try {
|
|
93
|
+
return (await this.fileSystem.lstat(file))?.size ?? 0;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (error instanceof KiriyaError)
|
|
97
|
+
return 0;
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=measure-folders.use-case.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renames or moves without replacing anything. A change of case alone goes through a
|
|
3
|
+
* temporary name, because case-insensitive drives on Windows and macOS see one entry.
|
|
4
|
+
*/
|
|
5
|
+
export async function moveEntry(fileSystem, from, to) {
|
|
6
|
+
if (from === to)
|
|
7
|
+
return;
|
|
8
|
+
if (await fileSystem.sameEntry(from, to)) {
|
|
9
|
+
let temporary = `${to}.kiriya-rename`;
|
|
10
|
+
for (let attempt = 1; (await fileSystem.lstat(temporary)) !== null; attempt += 1) {
|
|
11
|
+
temporary = `${to}.kiriya-rename-${attempt}`;
|
|
12
|
+
}
|
|
13
|
+
await fileSystem.move(from, temporary);
|
|
14
|
+
await fileSystem.move(temporary, to);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
await fileSystem.move(from, to);
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=move-entry.js.map
|