@tomsun28/pizza 0.1.0 → 0.1.1
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/README.md +20 -143
- package/README.zh-CN.md +36 -0
- package/dist/bun/cli.d.ts +2 -0
- package/dist/bun/cli.js +6 -0
- package/dist/bun/register-bedrock.d.ts +1 -0
- package/dist/bun/register-bedrock.js +3 -0
- package/dist/cli/args.d.ts +48 -0
- package/dist/cli/args.js +330 -0
- package/dist/cli/config-selector.d.ts +13 -0
- package/dist/cli/config-selector.js +30 -0
- package/dist/cli/file-processor.d.ts +14 -0
- package/dist/cli/file-processor.js +82 -0
- package/dist/cli/initial-message.d.ts +17 -0
- package/dist/cli/initial-message.js +21 -0
- package/dist/cli/list-models.d.ts +8 -0
- package/dist/cli/list-models.js +96 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +68 -0
- package/dist/config.d.ts +79 -0
- package/dist/config.js +223 -0
- package/dist/core/agent/index.d.ts +6 -0
- package/dist/core/agent/index.js +6 -0
- package/dist/core/agent/types.d.ts +202 -0
- package/dist/core/agent/types.js +51 -0
- package/dist/core/auth-storage.d.ts +135 -0
- package/dist/core/auth-storage.js +417 -0
- package/dist/core/bash-executor.d.ts +31 -0
- package/dist/core/bash-executor.js +107 -0
- package/dist/core/compaction/branch-summarization.d.ts +87 -0
- package/dist/core/compaction/branch-summarization.js +242 -0
- package/dist/core/compaction/compaction-engine.d.ts +40 -0
- package/dist/core/compaction/compaction-engine.js +292 -0
- package/dist/core/compaction/compaction.d.ts +106 -0
- package/dist/core/compaction/compaction.js +295 -0
- package/dist/core/compaction/index.d.ts +7 -0
- package/dist/core/compaction/index.js +7 -0
- package/dist/core/compaction/utils.d.ts +37 -0
- package/dist/core/compaction/utils.js +152 -0
- package/dist/core/defaults.d.ts +2 -0
- package/dist/core/defaults.js +1 -0
- package/dist/core/diagnostics.d.ts +14 -0
- package/dist/core/diagnostics.js +1 -0
- package/dist/core/event-bus.d.ts +8 -0
- package/dist/core/event-bus.js +24 -0
- package/dist/core/event-store/events.d.ts +465 -0
- package/dist/core/event-store/events.js +6 -0
- package/dist/core/event-store/index.d.ts +10 -0
- package/dist/core/event-store/index.js +10 -0
- package/dist/core/event-store/sqlite-store.d.ts +27 -0
- package/dist/core/event-store/sqlite-store.js +275 -0
- package/dist/core/event-store/store.d.ts +91 -0
- package/dist/core/event-store/store.js +7 -0
- package/dist/core/event-store/thread-scoped-store.d.ts +29 -0
- package/dist/core/event-store/thread-scoped-store.js +50 -0
- package/dist/core/event-store/types.d.ts +70 -0
- package/dist/core/event-store/types.js +7 -0
- package/dist/core/event-store/workspace.d.ts +37 -0
- package/dist/core/event-store/workspace.js +71 -0
- package/dist/core/exec.d.ts +28 -0
- package/dist/core/exec.js +74 -0
- package/dist/core/export-html/ansi-to-html.d.ts +21 -0
- package/dist/core/export-html/ansi-to-html.js +248 -0
- package/dist/core/export-html/index.d.ts +29 -0
- package/dist/core/export-html/index.js +186 -0
- package/dist/core/export-html/template.css +1017 -0
- package/dist/core/export-html/template.html +55 -0
- package/dist/core/export-html/template.js +1742 -0
- package/dist/core/export-html/tool-renderer.d.ts +39 -0
- package/dist/core/export-html/tool-renderer.js +94 -0
- package/dist/core/export-html/vendor/highlight.min.js +1213 -0
- package/dist/core/export-html/vendor/marked.min.js +6 -0
- package/dist/core/extensions/index.d.ts +12 -0
- package/dist/core/extensions/index.js +9 -0
- package/dist/core/extensions/loader.d.ts +24 -0
- package/dist/core/extensions/loader.js +492 -0
- package/dist/core/extensions/runner.d.ts +167 -0
- package/dist/core/extensions/runner.js +898 -0
- package/dist/core/extensions/session-context.d.ts +67 -0
- package/dist/core/extensions/session-context.js +272 -0
- package/dist/core/extensions/types.d.ts +1147 -0
- package/dist/core/extensions/types.js +44 -0
- package/dist/core/extensions/wrapper.d.ts +19 -0
- package/dist/core/extensions/wrapper.js +21 -0
- package/dist/core/footer-data-provider.d.ts +47 -0
- package/dist/core/footer-data-provider.js +321 -0
- package/dist/core/goal/index.d.ts +3 -0
- package/dist/core/goal/index.js +1 -0
- package/dist/core/goal/scheduler.d.ts +83 -0
- package/dist/core/goal/scheduler.js +154 -0
- package/dist/core/goal/types.d.ts +182 -0
- package/dist/core/goal/types.js +11 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.js +8 -0
- package/dist/core/intent/classifier.d.ts +53 -0
- package/dist/core/intent/classifier.js +353 -0
- package/dist/core/intent/index.d.ts +8 -0
- package/dist/core/intent/index.js +8 -0
- package/dist/core/intent/tool-adapter.d.ts +31 -0
- package/dist/core/intent/tool-adapter.js +168 -0
- package/dist/core/intent/types.d.ts +71 -0
- package/dist/core/intent/types.js +6 -0
- package/dist/core/keybindings.d.ts +297 -0
- package/dist/core/keybindings.js +250 -0
- package/dist/core/messages.d.ts +30 -0
- package/dist/core/messages.js +134 -0
- package/dist/core/model-registry.d.ts +131 -0
- package/dist/core/model-registry.js +634 -0
- package/dist/core/model-resolver.d.ts +109 -0
- package/dist/core/model-resolver.js +486 -0
- package/dist/core/output-guard.d.ts +5 -0
- package/dist/core/output-guard.js +58 -0
- package/dist/core/package-manager.d.ts +196 -0
- package/dist/core/package-manager.js +1873 -0
- package/dist/core/projection/boundary-inferrer.d.ts +58 -0
- package/dist/core/projection/boundary-inferrer.js +122 -0
- package/dist/core/projection/event-to-message.d.ts +35 -0
- package/dist/core/projection/event-to-message.js +253 -0
- package/dist/core/projection/goal-projection.d.ts +67 -0
- package/dist/core/projection/goal-projection.js +339 -0
- package/dist/core/projection/index.d.ts +13 -0
- package/dist/core/projection/index.js +13 -0
- package/dist/core/projection/session-manager.d.ts +100 -0
- package/dist/core/projection/session-manager.js +331 -0
- package/dist/core/projection/session-projection.d.ts +58 -0
- package/dist/core/projection/session-projection.js +257 -0
- package/dist/core/projection/task-history.d.ts +13 -0
- package/dist/core/projection/task-history.js +61 -0
- package/dist/core/projection/timeline-projection.d.ts +82 -0
- package/dist/core/projection/timeline-projection.js +276 -0
- package/dist/core/projection/types.d.ts +69 -0
- package/dist/core/projection/types.js +6 -0
- package/dist/core/prompt-templates.d.ts +51 -0
- package/dist/core/prompt-templates.js +249 -0
- package/dist/core/resolve-config-value.d.ts +22 -0
- package/dist/core/resolve-config-value.js +125 -0
- package/dist/core/resource-loader.d.ts +193 -0
- package/dist/core/resource-loader.js +685 -0
- package/dist/core/runtime/ai-client.d.ts +12 -0
- package/dist/core/runtime/ai-client.js +153 -0
- package/dist/core/runtime/checkpoint.d.ts +12 -0
- package/dist/core/runtime/checkpoint.js +78 -0
- package/dist/core/runtime/index.d.ts +12 -0
- package/dist/core/runtime/index.js +12 -0
- package/dist/core/runtime/llm-types.d.ts +99 -0
- package/dist/core/runtime/llm-types.js +6 -0
- package/dist/core/runtime/local-runtime.d.ts +23 -0
- package/dist/core/runtime/local-runtime.js +59 -0
- package/dist/core/runtime/policies.d.ts +72 -0
- package/dist/core/runtime/policies.js +37 -0
- package/dist/core/runtime/reactor.d.ts +213 -0
- package/dist/core/runtime/reactor.js +992 -0
- package/dist/core/runtime/runtime.d.ts +191 -0
- package/dist/core/runtime/runtime.js +449 -0
- package/dist/core/runtime/types.d.ts +42 -0
- package/dist/core/runtime/types.js +1 -0
- package/dist/core/sdk.d.ts +12 -0
- package/dist/core/sdk.js +10 -0
- package/dist/core/session-context-builder.d.ts +9 -0
- package/dist/core/session-context-builder.js +87 -0
- package/dist/core/session-cwd.d.ts +18 -0
- package/dist/core/session-cwd.js +36 -0
- package/dist/core/session-facade-factory.d.ts +79 -0
- package/dist/core/session-facade-factory.js +460 -0
- package/dist/core/session-facade.d.ts +57 -0
- package/dist/core/session-facade.js +91 -0
- package/dist/core/session-listing.d.ts +30 -0
- package/dist/core/session-listing.js +168 -0
- package/dist/core/session-reader.d.ts +19 -0
- package/dist/core/session-reader.js +101 -0
- package/dist/core/session-ref.d.ts +7 -0
- package/dist/core/session-ref.js +25 -0
- package/dist/core/session-services.d.ts +51 -0
- package/dist/core/session-services.js +90 -0
- package/dist/core/session-stats.d.ts +43 -0
- package/dist/core/session-stats.js +41 -0
- package/dist/core/settings-manager.d.ts +236 -0
- package/dist/core/settings-manager.js +706 -0
- package/dist/core/skill-block-parser.d.ts +12 -0
- package/dist/core/skill-block-parser.js +15 -0
- package/dist/core/skills.d.ts +59 -0
- package/dist/core/skills.js +408 -0
- package/dist/core/slash-commands.d.ts +13 -0
- package/dist/core/slash-commands.js +17 -0
- package/dist/core/source-info.d.ts +17 -0
- package/dist/core/source-info.js +18 -0
- package/dist/core/system-prompt.d.ts +27 -0
- package/dist/core/system-prompt.js +179 -0
- package/dist/core/timings.d.ts +7 -0
- package/dist/core/timings.js +30 -0
- package/dist/core/tools/bash.d.ts +95 -0
- package/dist/core/tools/bash.js +513 -0
- package/dist/core/tools/builtin-commands.d.ts +75 -0
- package/dist/core/tools/builtin-commands.js +503 -0
- package/dist/core/tools/compat-commands.d.ts +12 -0
- package/dist/core/tools/compat-commands.js +1280 -0
- package/dist/core/tools/edit-diff.d.ts +59 -0
- package/dist/core/tools/edit-diff.js +263 -0
- package/dist/core/tools/edit.d.ts +49 -0
- package/dist/core/tools/edit.js +340 -0
- package/dist/core/tools/file-mutation-queue.d.ts +5 -0
- package/dist/core/tools/file-mutation-queue.js +36 -0
- package/dist/core/tools/find.d.ts +34 -0
- package/dist/core/tools/find.js +459 -0
- package/dist/core/tools/grep.d.ts +36 -0
- package/dist/core/tools/grep.js +422 -0
- package/dist/core/tools/index.d.ts +70 -0
- package/dist/core/tools/index.js +129 -0
- package/dist/core/tools/line-anchors.d.ts +33 -0
- package/dist/core/tools/line-anchors.js +151 -0
- package/dist/core/tools/ls.d.ts +36 -0
- package/dist/core/tools/ls.js +268 -0
- package/dist/core/tools/path-utils.d.ts +7 -0
- package/dist/core/tools/path-utils.js +80 -0
- package/dist/core/tools/read.d.ts +35 -0
- package/dist/core/tools/read.js +244 -0
- package/dist/core/tools/render-utils.d.ts +20 -0
- package/dist/core/tools/render-utils.js +48 -0
- package/dist/core/tools/session-split.d.ts +9 -0
- package/dist/core/tools/session-split.js +77 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts +13 -0
- package/dist/core/tools/tool-definition-wrapper.js +32 -0
- package/dist/core/tools/truncate.d.ts +69 -0
- package/dist/core/tools/truncate.js +204 -0
- package/dist/core/tools/write.d.ts +25 -0
- package/dist/core/tools/write.js +211 -0
- package/dist/core/types/session-types.d.ts +123 -0
- package/dist/core/types/session-types.js +8 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +38 -0
- package/dist/main.d.ts +11 -2
- package/dist/main.js +577 -149
- package/dist/modes/event-mapper.d.ts +115 -0
- package/dist/modes/event-mapper.js +200 -0
- package/dist/modes/gui/server.d.ts +14 -0
- package/dist/modes/gui/server.js +1032 -0
- package/dist/modes/index.d.ts +10 -0
- package/dist/modes/index.js +9 -0
- package/dist/modes/interactive/assets/clankolas.png +0 -0
- package/dist/modes/interactive/components/armin.d.ts +33 -0
- package/dist/modes/interactive/components/armin.js +328 -0
- package/dist/modes/interactive/components/assistant-message.d.ts +19 -0
- package/dist/modes/interactive/components/assistant-message.js +115 -0
- package/dist/modes/interactive/components/bash-execution.d.ts +33 -0
- package/dist/modes/interactive/components/bash-execution.js +169 -0
- package/dist/modes/interactive/components/bordered-loader.d.ts +15 -0
- package/dist/modes/interactive/components/bordered-loader.js +50 -0
- package/dist/modes/interactive/components/branch-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/branch-summary-message.js +41 -0
- package/dist/modes/interactive/components/brand-header.d.ts +33 -0
- package/dist/modes/interactive/components/brand-header.js +235 -0
- package/dist/modes/interactive/components/compaction-summary-message.d.ts +15 -0
- package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
- package/dist/modes/interactive/components/config-selector.d.ts +70 -0
- package/dist/modes/interactive/components/config-selector.js +469 -0
- package/dist/modes/interactive/components/countdown-timer.d.ts +13 -0
- package/dist/modes/interactive/components/countdown-timer.js +27 -0
- package/dist/modes/interactive/components/custom-editor.d.ts +27 -0
- package/dist/modes/interactive/components/custom-editor.js +94 -0
- package/dist/modes/interactive/components/custom-message.d.ts +19 -0
- package/dist/modes/interactive/components/custom-message.js +73 -0
- package/dist/modes/interactive/components/daxnuts.d.ts +22 -0
- package/dist/modes/interactive/components/daxnuts.js +137 -0
- package/dist/modes/interactive/components/diff.d.ts +11 -0
- package/dist/modes/interactive/components/diff.js +132 -0
- package/dist/modes/interactive/components/dynamic-border.d.ts +14 -0
- package/dist/modes/interactive/components/dynamic-border.js +19 -0
- package/dist/modes/interactive/components/earendil-announcement.d.ts +4 -0
- package/dist/modes/interactive/components/earendil-announcement.js +39 -0
- package/dist/modes/interactive/components/extension-editor.d.ts +19 -0
- package/dist/modes/interactive/components/extension-editor.js +105 -0
- package/dist/modes/interactive/components/extension-input.d.ts +22 -0
- package/dist/modes/interactive/components/extension-input.js +54 -0
- package/dist/modes/interactive/components/extension-selector.d.ts +23 -0
- package/dist/modes/interactive/components/extension-selector.js +70 -0
- package/dist/modes/interactive/components/footer.d.ts +55 -0
- package/dist/modes/interactive/components/footer.js +207 -0
- package/dist/modes/interactive/components/index.d.ts +31 -0
- package/dist/modes/interactive/components/index.js +32 -0
- package/dist/modes/interactive/components/keybinding-hints.d.ts +7 -0
- package/dist/modes/interactive/components/keybinding-hints.js +21 -0
- package/dist/modes/interactive/components/login-dialog.d.ts +41 -0
- package/dist/modes/interactive/components/login-dialog.js +138 -0
- package/dist/modes/interactive/components/model-selector.d.ts +32 -0
- package/dist/modes/interactive/components/model-selector.js +209 -0
- package/dist/modes/interactive/components/oauth-selector.d.ts +18 -0
- package/dist/modes/interactive/components/oauth-selector.js +91 -0
- package/dist/modes/interactive/components/settings-selector.d.ts +57 -0
- package/dist/modes/interactive/components/settings-selector.js +299 -0
- package/dist/modes/interactive/components/show-images-selector.d.ts +9 -0
- package/dist/modes/interactive/components/show-images-selector.js +37 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +16 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
- package/dist/modes/interactive/components/theme-selector.d.ts +10 -0
- package/dist/modes/interactive/components/theme-selector.js +47 -0
- package/dist/modes/interactive/components/thinking-selector.d.ts +10 -0
- package/dist/modes/interactive/components/thinking-selector.js +50 -0
- package/dist/modes/interactive/components/timeline-view.d.ts +31 -0
- package/dist/modes/interactive/components/timeline-view.js +81 -0
- package/dist/modes/interactive/components/tool-execution.d.ts +64 -0
- package/dist/modes/interactive/components/tool-execution.js +305 -0
- package/dist/modes/interactive/components/tree-selector.d.ts +88 -0
- package/dist/modes/interactive/components/tree-selector.js +1072 -0
- package/dist/modes/interactive/components/user-message-selector.d.ts +29 -0
- package/dist/modes/interactive/components/user-message-selector.js +110 -0
- package/dist/modes/interactive/components/user-message.d.ts +9 -0
- package/dist/modes/interactive/components/user-message.js +27 -0
- package/dist/modes/interactive/components/visual-truncate.d.ts +23 -0
- package/dist/modes/interactive/components/visual-truncate.js +32 -0
- package/dist/modes/interactive/interactive-mode.d.ts +394 -0
- package/dist/modes/interactive/interactive-mode.js +4116 -0
- package/dist/modes/interactive/theme/dark.json +85 -0
- package/dist/modes/interactive/theme/light.json +84 -0
- package/dist/modes/interactive/theme/theme-schema.json +335 -0
- package/dist/modes/interactive/theme/theme.d.ts +80 -0
- package/dist/modes/interactive/theme/theme.js +973 -0
- package/dist/modes/print-mode.d.ts +27 -0
- package/dist/modes/print-mode.js +102 -0
- package/dist/modes/rpc/jsonl.d.ts +16 -0
- package/dist/modes/rpc/jsonl.js +48 -0
- package/dist/modes/rpc/rpc-client.d.ts +219 -0
- package/dist/modes/rpc/rpc-client.js +405 -0
- package/dist/modes/rpc/rpc-mode.d.ts +19 -0
- package/dist/modes/rpc/rpc-mode.js +381 -0
- package/dist/modes/rpc/rpc-types.d.ts +406 -0
- package/dist/modes/rpc/rpc-types.js +7 -0
- package/dist/package-manager-cli.d.ts +3 -0
- package/dist/package-manager-cli.js +233 -0
- package/dist/utils/changelog.d.ts +20 -0
- package/dist/utils/changelog.js +86 -0
- package/dist/utils/child-process.d.ts +10 -0
- package/dist/utils/child-process.js +77 -0
- package/dist/utils/clipboard-image.d.ts +10 -0
- package/dist/utils/clipboard-image.js +244 -0
- package/dist/utils/clipboard-native.d.ts +7 -0
- package/dist/utils/clipboard-native.js +13 -0
- package/dist/utils/clipboard.d.ts +1 -0
- package/dist/utils/clipboard.js +77 -0
- package/dist/utils/exif-orientation.d.ts +4 -0
- package/dist/utils/exif-orientation.js +157 -0
- package/dist/utils/frontmatter.d.ts +7 -0
- package/dist/utils/frontmatter.js +25 -0
- package/dist/utils/git.d.ts +25 -0
- package/dist/utils/git.js +162 -0
- package/dist/utils/image-convert.d.ts +8 -0
- package/dist/utils/image-convert.js +38 -0
- package/dist/utils/image-resize.d.ts +35 -0
- package/dist/utils/image-resize.js +136 -0
- package/dist/utils/mime.d.ts +1 -0
- package/dist/utils/mime.js +25 -0
- package/dist/utils/path-shims.d.ts +1 -0
- package/dist/utils/path-shims.js +90 -0
- package/dist/utils/paths.d.ts +6 -0
- package/dist/utils/paths.js +18 -0
- package/dist/utils/photon.d.ts +20 -0
- package/dist/utils/photon.js +120 -0
- package/dist/utils/shell.d.ts +29 -0
- package/dist/utils/shell.js +189 -0
- package/dist/utils/sleep.d.ts +4 -0
- package/dist/utils/sleep.js +16 -0
- package/dist/utils/tools-manager.d.ts +3 -0
- package/dist/utils/tools-manager.js +101 -0
- package/dist/vendor/bin/darwin-arm64/fd +0 -0
- package/dist/vendor/bin/darwin-arm64/rg +0 -0
- package/dist/vendor/bin/darwin-x64/fd +0 -0
- package/dist/vendor/bin/darwin-x64/rg +0 -0
- package/dist/vendor/bin/linux-arm64/fd +0 -0
- package/dist/vendor/bin/linux-arm64/rg +0 -0
- package/dist/vendor/bin/linux-x64/fd +0 -0
- package/dist/vendor/bin/linux-x64/rg +0 -0
- package/dist/vendor/bin/win32-arm64/fd.exe +0 -0
- package/dist/vendor/bin/win32-arm64/rg.exe +0 -0
- package/dist/vendor/bin/win32-x64/fd.exe +0 -0
- package/dist/vendor/bin/win32-x64/rg.exe +0 -0
- package/package.json +90 -26
- package/dist/extension/core.d.ts +0 -69
- package/dist/extension/core.js +0 -270
- package/dist/extension/core.js.map +0 -1
- package/dist/extension/index.d.ts +0 -14
- package/dist/extension/index.js +0 -374
- package/dist/extension/index.js.map +0 -1
- package/dist/main.js.map +0 -1
- package/themes/pizza.json +0 -87
|
@@ -0,0 +1,1280 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { basename, isAbsolute, join, relative, resolve } from "node:path";
|
|
4
|
+
import { minimatch } from "minimatch";
|
|
5
|
+
import { getToolPath } from "../../utils/tools-manager.js";
|
|
6
|
+
export async function executeCompatCommand(command, args, context) {
|
|
7
|
+
switch (command) {
|
|
8
|
+
case "grep":
|
|
9
|
+
return executeCompatGrep(args, context);
|
|
10
|
+
case "find":
|
|
11
|
+
return executeCompatFind(args, context);
|
|
12
|
+
case "ls":
|
|
13
|
+
return executeCompatLs(args, context);
|
|
14
|
+
default:
|
|
15
|
+
return {
|
|
16
|
+
stdout: "",
|
|
17
|
+
stderr: `pizza: unsupported compat command: ${command}`,
|
|
18
|
+
exitCode: 2,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export const COMPAT_COMMANDS = ["grep", "find", "ls"];
|
|
23
|
+
function appendLine(target, line) {
|
|
24
|
+
target.push(line);
|
|
25
|
+
}
|
|
26
|
+
function result(stdoutLines, stderrLines, exitCode, stdoutSeparator = "\n") {
|
|
27
|
+
return {
|
|
28
|
+
stdout: stdoutLines.length > 0 ? `${stdoutLines.join(stdoutSeparator)}${stdoutSeparator}` : "",
|
|
29
|
+
stderr: stderrLines.length > 0 ? `${stderrLines.join("\n")}\n` : "",
|
|
30
|
+
exitCode,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function parseInteger(command, flag, value) {
|
|
34
|
+
if (value === undefined || value === "") {
|
|
35
|
+
return {
|
|
36
|
+
stdout: "",
|
|
37
|
+
stderr: `${command}: option requires an argument -- ${flag}\n`,
|
|
38
|
+
exitCode: 2,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const parsed = Number.parseInt(value, 10);
|
|
42
|
+
if (!Number.isFinite(parsed) || Number.isNaN(parsed) || parsed < 0) {
|
|
43
|
+
return {
|
|
44
|
+
stdout: "",
|
|
45
|
+
stderr: `${command}: invalid number for ${flag}: ${value}\n`,
|
|
46
|
+
exitCode: 2,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return parsed;
|
|
50
|
+
}
|
|
51
|
+
function isCompatResult(value) {
|
|
52
|
+
return !!value && typeof value === "object" && "exitCode" in value && "stdout" in value && "stderr" in value;
|
|
53
|
+
}
|
|
54
|
+
function parseGrepArgs(args) {
|
|
55
|
+
const options = {
|
|
56
|
+
patterns: [],
|
|
57
|
+
paths: [],
|
|
58
|
+
recursive: false,
|
|
59
|
+
lineNumber: false,
|
|
60
|
+
ignoreCase: false,
|
|
61
|
+
fixedStrings: false,
|
|
62
|
+
invertMatch: false,
|
|
63
|
+
count: false,
|
|
64
|
+
onlyMatching: false,
|
|
65
|
+
wordRegexp: false,
|
|
66
|
+
lineRegexp: false,
|
|
67
|
+
noMessages: false,
|
|
68
|
+
quiet: false,
|
|
69
|
+
listFiles: false,
|
|
70
|
+
filesWithoutMatch: false,
|
|
71
|
+
beforeContext: 0,
|
|
72
|
+
afterContext: 0,
|
|
73
|
+
includeGlobs: [],
|
|
74
|
+
excludeGlobs: [],
|
|
75
|
+
excludeDirGlobs: [],
|
|
76
|
+
};
|
|
77
|
+
let stopOptions = false;
|
|
78
|
+
for (let i = 0; i < args.length; i++) {
|
|
79
|
+
const arg = args[i];
|
|
80
|
+
if (stopOptions) {
|
|
81
|
+
if (options.patterns.length === 0)
|
|
82
|
+
options.patterns.push(arg);
|
|
83
|
+
else
|
|
84
|
+
options.paths.push(arg);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (arg === "--") {
|
|
88
|
+
stopOptions = true;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (arg === "--recursive" || arg === "--dereference-recursive") {
|
|
92
|
+
options.recursive = true;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (arg === "--line-number") {
|
|
96
|
+
options.lineNumber = true;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (arg === "--ignore-case") {
|
|
100
|
+
options.ignoreCase = true;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (arg === "--fixed-strings") {
|
|
104
|
+
options.fixedStrings = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (arg === "--invert-match") {
|
|
108
|
+
options.invertMatch = true;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (arg === "--count") {
|
|
112
|
+
options.count = true;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (arg === "--only-matching") {
|
|
116
|
+
options.onlyMatching = true;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (arg === "--word-regexp") {
|
|
120
|
+
options.wordRegexp = true;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (arg === "--line-regexp") {
|
|
124
|
+
options.lineRegexp = true;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (arg === "--extended-regexp" || arg === "--basic-regexp") {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (arg === "--with-filename") {
|
|
131
|
+
options.withFilename = true;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (arg === "--no-filename") {
|
|
135
|
+
options.noFilename = true;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (arg === "--quiet" || arg === "--silent") {
|
|
139
|
+
options.quiet = true;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (arg === "--no-messages") {
|
|
143
|
+
options.noMessages = true;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (arg === "--files-with-matches") {
|
|
147
|
+
options.listFiles = true;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (arg === "--files-without-match") {
|
|
151
|
+
options.filesWithoutMatch = true;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (arg === "--include" || arg === "--exclude" || arg === "--exclude-dir") {
|
|
155
|
+
const value = args[++i] ?? "";
|
|
156
|
+
if (arg === "--include")
|
|
157
|
+
options.includeGlobs.push(value);
|
|
158
|
+
else if (arg === "--exclude")
|
|
159
|
+
options.excludeGlobs.push(value);
|
|
160
|
+
else
|
|
161
|
+
options.excludeDirGlobs.push(value);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (arg.startsWith("--include=") || arg.startsWith("--exclude=") || arg.startsWith("--exclude-dir=")) {
|
|
165
|
+
const [flag, rawValue] = arg.split("=", 2);
|
|
166
|
+
const value = rawValue ?? "";
|
|
167
|
+
if (flag === "--include")
|
|
168
|
+
options.includeGlobs.push(value);
|
|
169
|
+
else if (flag === "--exclude")
|
|
170
|
+
options.excludeGlobs.push(value);
|
|
171
|
+
else
|
|
172
|
+
options.excludeDirGlobs.push(value);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (arg === "--color" || arg === "--colour" || arg.startsWith("--color=") || arg.startsWith("--colour=")) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (arg === "--binary-files") {
|
|
179
|
+
i++;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (arg.startsWith("--binary-files=")) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (arg === "--regexp") {
|
|
186
|
+
options.patterns.push(args[++i] ?? "");
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (arg.startsWith("--regexp=")) {
|
|
190
|
+
options.patterns.push(arg.slice("--regexp=".length));
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (arg === "--max-count") {
|
|
194
|
+
const parsed = parseInteger("grep", "--max-count", args[++i]);
|
|
195
|
+
if (isCompatResult(parsed))
|
|
196
|
+
return parsed;
|
|
197
|
+
options.maxCount = parsed;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (arg.startsWith("--max-count=")) {
|
|
201
|
+
const parsed = parseInteger("grep", "--max-count", arg.slice("--max-count=".length));
|
|
202
|
+
if (isCompatResult(parsed))
|
|
203
|
+
return parsed;
|
|
204
|
+
options.maxCount = parsed;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (arg === "--context" || arg === "--after-context" || arg === "--before-context") {
|
|
208
|
+
const parsed = parseInteger("grep", arg, args[++i]);
|
|
209
|
+
if (isCompatResult(parsed))
|
|
210
|
+
return parsed;
|
|
211
|
+
if (arg !== "--after-context")
|
|
212
|
+
options.beforeContext = parsed;
|
|
213
|
+
if (arg !== "--before-context")
|
|
214
|
+
options.afterContext = parsed;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (arg.startsWith("--context=") || arg.startsWith("--after-context=") || arg.startsWith("--before-context=")) {
|
|
218
|
+
const [flag, rawValue] = arg.split("=", 2);
|
|
219
|
+
const parsed = parseInteger("grep", flag, rawValue);
|
|
220
|
+
if (isCompatResult(parsed))
|
|
221
|
+
return parsed;
|
|
222
|
+
if (flag !== "--after-context")
|
|
223
|
+
options.beforeContext = parsed;
|
|
224
|
+
if (flag !== "--before-context")
|
|
225
|
+
options.afterContext = parsed;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (arg.startsWith("--")) {
|
|
229
|
+
return {
|
|
230
|
+
stdout: "",
|
|
231
|
+
stderr: `grep: unsupported option: ${arg}\n`,
|
|
232
|
+
exitCode: 2,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
if (arg.startsWith("-") && arg !== "-") {
|
|
236
|
+
const parsed = parseGrepShortOptions(arg, args, i, options);
|
|
237
|
+
if (isCompatResult(parsed))
|
|
238
|
+
return parsed;
|
|
239
|
+
i = parsed;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (options.patterns.length === 0)
|
|
243
|
+
options.patterns.push(arg);
|
|
244
|
+
else
|
|
245
|
+
options.paths.push(arg);
|
|
246
|
+
}
|
|
247
|
+
if (options.patterns.length === 0) {
|
|
248
|
+
return {
|
|
249
|
+
stdout: "",
|
|
250
|
+
stderr: "grep: missing search pattern\n",
|
|
251
|
+
exitCode: 2,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
return options;
|
|
255
|
+
}
|
|
256
|
+
function parseGrepShortOptions(arg, args, index, options) {
|
|
257
|
+
const chars = arg.slice(1);
|
|
258
|
+
for (let pos = 0; pos < chars.length; pos++) {
|
|
259
|
+
const flag = chars[pos];
|
|
260
|
+
const rest = chars.slice(pos + 1);
|
|
261
|
+
switch (flag) {
|
|
262
|
+
case "r":
|
|
263
|
+
case "R":
|
|
264
|
+
options.recursive = true;
|
|
265
|
+
break;
|
|
266
|
+
case "n":
|
|
267
|
+
options.lineNumber = true;
|
|
268
|
+
break;
|
|
269
|
+
case "i":
|
|
270
|
+
options.ignoreCase = true;
|
|
271
|
+
break;
|
|
272
|
+
case "F":
|
|
273
|
+
options.fixedStrings = true;
|
|
274
|
+
break;
|
|
275
|
+
case "v":
|
|
276
|
+
options.invertMatch = true;
|
|
277
|
+
break;
|
|
278
|
+
case "c":
|
|
279
|
+
options.count = true;
|
|
280
|
+
break;
|
|
281
|
+
case "o":
|
|
282
|
+
options.onlyMatching = true;
|
|
283
|
+
break;
|
|
284
|
+
case "w":
|
|
285
|
+
options.wordRegexp = true;
|
|
286
|
+
break;
|
|
287
|
+
case "x":
|
|
288
|
+
options.lineRegexp = true;
|
|
289
|
+
break;
|
|
290
|
+
case "E":
|
|
291
|
+
case "G":
|
|
292
|
+
break;
|
|
293
|
+
case "H":
|
|
294
|
+
options.withFilename = true;
|
|
295
|
+
break;
|
|
296
|
+
case "h":
|
|
297
|
+
options.noFilename = true;
|
|
298
|
+
break;
|
|
299
|
+
case "q":
|
|
300
|
+
options.quiet = true;
|
|
301
|
+
break;
|
|
302
|
+
case "s":
|
|
303
|
+
options.noMessages = true;
|
|
304
|
+
break;
|
|
305
|
+
case "l":
|
|
306
|
+
options.listFiles = true;
|
|
307
|
+
break;
|
|
308
|
+
case "L":
|
|
309
|
+
options.filesWithoutMatch = true;
|
|
310
|
+
break;
|
|
311
|
+
case "I":
|
|
312
|
+
break;
|
|
313
|
+
case "e": {
|
|
314
|
+
const value = rest || args[++index];
|
|
315
|
+
if (value === undefined) {
|
|
316
|
+
return { stdout: "", stderr: "grep: option requires an argument -- e\n", exitCode: 2 };
|
|
317
|
+
}
|
|
318
|
+
options.patterns.push(value);
|
|
319
|
+
return index;
|
|
320
|
+
}
|
|
321
|
+
case "m": {
|
|
322
|
+
const value = rest || args[++index];
|
|
323
|
+
const parsed = parseInteger("grep", "-m", value);
|
|
324
|
+
if (isCompatResult(parsed))
|
|
325
|
+
return parsed;
|
|
326
|
+
options.maxCount = parsed;
|
|
327
|
+
return index;
|
|
328
|
+
}
|
|
329
|
+
case "A":
|
|
330
|
+
case "B":
|
|
331
|
+
case "C": {
|
|
332
|
+
const value = rest || args[++index];
|
|
333
|
+
const parsed = parseInteger("grep", `-${flag}`, value);
|
|
334
|
+
if (isCompatResult(parsed))
|
|
335
|
+
return parsed;
|
|
336
|
+
if (flag !== "A")
|
|
337
|
+
options.beforeContext = parsed;
|
|
338
|
+
if (flag !== "B")
|
|
339
|
+
options.afterContext = parsed;
|
|
340
|
+
return index;
|
|
341
|
+
}
|
|
342
|
+
default:
|
|
343
|
+
return { stdout: "", stderr: `grep: unsupported option -- ${flag}\n`, exitCode: 2 };
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return index;
|
|
347
|
+
}
|
|
348
|
+
async function executeCompatGrep(args, context) {
|
|
349
|
+
const parsed = parseGrepArgs(args);
|
|
350
|
+
if (isCompatResult(parsed))
|
|
351
|
+
return parsed;
|
|
352
|
+
if (parsed.recursive && parsed.paths.length === 0)
|
|
353
|
+
parsed.paths.push(".");
|
|
354
|
+
const rgResult = tryRunGrepWithRg(parsed, context);
|
|
355
|
+
if (rgResult)
|
|
356
|
+
return rgResult;
|
|
357
|
+
return runGrepWithNode(parsed, context);
|
|
358
|
+
}
|
|
359
|
+
function tryRunGrepWithRg(options, context) {
|
|
360
|
+
const rgPath = getToolPath("rg");
|
|
361
|
+
if (!rgPath || options.paths.length === 0)
|
|
362
|
+
return null;
|
|
363
|
+
for (const inputPath of options.paths) {
|
|
364
|
+
try {
|
|
365
|
+
if (statSync(resolve(context.cwd, inputPath)).isDirectory() && !options.recursive)
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
const rgArgs = ["--color=never", "--no-ignore", "--hidden"];
|
|
373
|
+
if (options.lineNumber)
|
|
374
|
+
rgArgs.push("--line-number");
|
|
375
|
+
else
|
|
376
|
+
rgArgs.push("--no-line-number");
|
|
377
|
+
if (options.ignoreCase)
|
|
378
|
+
rgArgs.push("--ignore-case");
|
|
379
|
+
if (options.fixedStrings)
|
|
380
|
+
rgArgs.push("--fixed-strings");
|
|
381
|
+
if (options.invertMatch)
|
|
382
|
+
rgArgs.push("--invert-match");
|
|
383
|
+
if (options.count)
|
|
384
|
+
rgArgs.push("--count");
|
|
385
|
+
if (options.onlyMatching)
|
|
386
|
+
rgArgs.push("--only-matching");
|
|
387
|
+
if (options.wordRegexp)
|
|
388
|
+
rgArgs.push("--word-regexp");
|
|
389
|
+
if (options.lineRegexp)
|
|
390
|
+
rgArgs.push("--line-regexp");
|
|
391
|
+
if (options.quiet)
|
|
392
|
+
rgArgs.push("--quiet");
|
|
393
|
+
if (options.noMessages)
|
|
394
|
+
rgArgs.push("--no-messages");
|
|
395
|
+
if (options.listFiles)
|
|
396
|
+
rgArgs.push("--files-with-matches");
|
|
397
|
+
if (options.filesWithoutMatch)
|
|
398
|
+
rgArgs.push("--files-without-match");
|
|
399
|
+
if (options.withFilename)
|
|
400
|
+
rgArgs.push("--with-filename");
|
|
401
|
+
if (options.noFilename)
|
|
402
|
+
rgArgs.push("--no-filename");
|
|
403
|
+
if (options.maxCount !== undefined)
|
|
404
|
+
rgArgs.push("--max-count", String(options.maxCount));
|
|
405
|
+
if (options.beforeContext > 0)
|
|
406
|
+
rgArgs.push("--before-context", String(options.beforeContext));
|
|
407
|
+
if (options.afterContext > 0)
|
|
408
|
+
rgArgs.push("--after-context", String(options.afterContext));
|
|
409
|
+
for (const glob of options.includeGlobs)
|
|
410
|
+
rgArgs.push("--glob", glob);
|
|
411
|
+
for (const glob of options.excludeGlobs)
|
|
412
|
+
rgArgs.push("--glob", `!${glob}`);
|
|
413
|
+
for (const glob of options.excludeDirGlobs)
|
|
414
|
+
rgArgs.push("--glob", `!${glob.replace(/\/$/, "")}/**`);
|
|
415
|
+
for (const pattern of options.patterns)
|
|
416
|
+
rgArgs.push("--regexp", pattern);
|
|
417
|
+
rgArgs.push(...options.paths);
|
|
418
|
+
const child = spawnSync(rgPath, rgArgs, { cwd: context.cwd, encoding: "utf-8" });
|
|
419
|
+
if (child.error)
|
|
420
|
+
return null;
|
|
421
|
+
return {
|
|
422
|
+
stdout: child.stdout ?? "",
|
|
423
|
+
stderr: child.stderr ?? "",
|
|
424
|
+
exitCode: child.status ?? 2,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
async function runGrepWithNode(options, context) {
|
|
428
|
+
const stdout = [];
|
|
429
|
+
const stderr = [];
|
|
430
|
+
const matchers = buildGrepMatchers(options, stderr);
|
|
431
|
+
if (!matchers)
|
|
432
|
+
return result(stdout, stderr, 2);
|
|
433
|
+
let matched = false;
|
|
434
|
+
let hadError = false;
|
|
435
|
+
const sources = await collectGrepSources(options, context, stderr);
|
|
436
|
+
hadError ||= sources.hadError;
|
|
437
|
+
for (const source of sources.items) {
|
|
438
|
+
let fileMatched = false;
|
|
439
|
+
let selectedLineCount = 0;
|
|
440
|
+
let matchesForFile = 0;
|
|
441
|
+
const lines = source.content.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
|
|
442
|
+
const emitted = new Set();
|
|
443
|
+
for (let index = 0; index < lines.length; index++) {
|
|
444
|
+
if (options.maxCount !== undefined && matchesForFile >= options.maxCount)
|
|
445
|
+
break;
|
|
446
|
+
const line = lines[index] ?? "";
|
|
447
|
+
const rawMatch = matchesGrepLine(line, matchers, options);
|
|
448
|
+
const selected = options.invertMatch ? !rawMatch : rawMatch;
|
|
449
|
+
if (!selected)
|
|
450
|
+
continue;
|
|
451
|
+
matched = true;
|
|
452
|
+
fileMatched = true;
|
|
453
|
+
selectedLineCount++;
|
|
454
|
+
matchesForFile++;
|
|
455
|
+
if (options.quiet)
|
|
456
|
+
continue;
|
|
457
|
+
if (options.listFiles || options.filesWithoutMatch || options.count)
|
|
458
|
+
continue;
|
|
459
|
+
const start = Math.max(0, index - options.beforeContext);
|
|
460
|
+
const end = Math.min(lines.length - 1, index + options.afterContext);
|
|
461
|
+
for (let current = start; current <= end; current++) {
|
|
462
|
+
if (emitted.has(current))
|
|
463
|
+
continue;
|
|
464
|
+
emitted.add(current);
|
|
465
|
+
const isMatch = current === index;
|
|
466
|
+
if (isMatch && options.onlyMatching && !options.invertMatch) {
|
|
467
|
+
for (const match of getGrepMatches(lines[current] ?? "", matchers, options)) {
|
|
468
|
+
appendLine(stdout, formatGrepLine(source.displayName, current + 1, match, true, options, sources.forceFilename));
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
appendLine(stdout, formatGrepLine(source.displayName, current + 1, lines[current] ?? "", isMatch, options, sources.forceFilename));
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (options.quiet) {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (options.count) {
|
|
480
|
+
appendLine(stdout, formatGrepLine(source.displayName, 0, String(selectedLineCount), true, { ...options, lineNumber: false }, sources.forceFilename));
|
|
481
|
+
}
|
|
482
|
+
else if (fileMatched && options.listFiles) {
|
|
483
|
+
appendLine(stdout, source.displayName ?? "");
|
|
484
|
+
}
|
|
485
|
+
else if (!fileMatched && options.filesWithoutMatch) {
|
|
486
|
+
matched = true;
|
|
487
|
+
appendLine(stdout, source.displayName ?? "");
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return result(stdout, stderr, hadError ? 2 : matched ? 0 : 1);
|
|
491
|
+
}
|
|
492
|
+
function buildGrepMatchers(options, stderr) {
|
|
493
|
+
if (options.fixedStrings) {
|
|
494
|
+
return options.patterns.map((pattern) => ({
|
|
495
|
+
kind: "fixed",
|
|
496
|
+
pattern,
|
|
497
|
+
comparePattern: options.ignoreCase ? pattern.toLowerCase() : pattern,
|
|
498
|
+
}));
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
const flags = options.ignoreCase ? "i" : "";
|
|
502
|
+
const globalFlags = options.ignoreCase ? "gi" : "g";
|
|
503
|
+
return options.patterns.map((pattern) => {
|
|
504
|
+
const wrapped = wrapGrepPattern(pattern, options);
|
|
505
|
+
return {
|
|
506
|
+
kind: "regex",
|
|
507
|
+
regex: new RegExp(wrapped, flags),
|
|
508
|
+
globalRegex: new RegExp(wrapped, globalFlags),
|
|
509
|
+
};
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
catch (error) {
|
|
513
|
+
appendLine(stderr, `grep: ${error instanceof Error ? error.message : String(error)}`);
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function wrapGrepPattern(pattern, options) {
|
|
518
|
+
let wrapped = pattern;
|
|
519
|
+
if (options.wordRegexp)
|
|
520
|
+
wrapped = `\\b(?:${wrapped})\\b`;
|
|
521
|
+
if (options.lineRegexp)
|
|
522
|
+
wrapped = `^(?:${wrapped})$`;
|
|
523
|
+
return wrapped;
|
|
524
|
+
}
|
|
525
|
+
function matchesGrepLine(line, matchers, options) {
|
|
526
|
+
return matchers.some((matcher) => {
|
|
527
|
+
if (matcher.kind === "fixed") {
|
|
528
|
+
return fixedPatternMatches(line, matcher, options);
|
|
529
|
+
}
|
|
530
|
+
matcher.regex.lastIndex = 0;
|
|
531
|
+
return matcher.regex.test(line);
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
function getGrepMatches(line, matchers, options) {
|
|
535
|
+
const matches = [];
|
|
536
|
+
for (const matcher of matchers) {
|
|
537
|
+
if (matcher.kind === "fixed") {
|
|
538
|
+
matches.push(...getFixedMatches(line, matcher, options));
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
matcher.globalRegex.lastIndex = 0;
|
|
542
|
+
for (const match of line.matchAll(matcher.globalRegex)) {
|
|
543
|
+
matches.push(match[0]);
|
|
544
|
+
if (match[0] === "")
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return matches;
|
|
549
|
+
}
|
|
550
|
+
function fixedPatternMatches(line, matcher, options) {
|
|
551
|
+
return getFixedMatches(line, matcher, options).length > 0;
|
|
552
|
+
}
|
|
553
|
+
function getFixedMatches(line, matcher, options) {
|
|
554
|
+
const haystack = options.ignoreCase ? line.toLowerCase() : line;
|
|
555
|
+
const pattern = matcher.comparePattern;
|
|
556
|
+
const matches = [];
|
|
557
|
+
if (pattern === "")
|
|
558
|
+
return [""];
|
|
559
|
+
let index = 0;
|
|
560
|
+
while (index <= haystack.length) {
|
|
561
|
+
const found = haystack.indexOf(pattern, index);
|
|
562
|
+
if (found === -1)
|
|
563
|
+
break;
|
|
564
|
+
const end = found + pattern.length;
|
|
565
|
+
if ((!options.wordRegexp || (isWordBoundary(line, found - 1) && isWordBoundary(line, end))) &&
|
|
566
|
+
(!options.lineRegexp || (found === 0 && end === line.length))) {
|
|
567
|
+
matches.push(line.slice(found, end));
|
|
568
|
+
}
|
|
569
|
+
index = Math.max(end, found + 1);
|
|
570
|
+
}
|
|
571
|
+
return matches;
|
|
572
|
+
}
|
|
573
|
+
function isWordBoundary(value, index) {
|
|
574
|
+
if (index < 0 || index >= value.length)
|
|
575
|
+
return true;
|
|
576
|
+
return !/[A-Za-z0-9_]/.test(value[index] ?? "");
|
|
577
|
+
}
|
|
578
|
+
async function collectGrepSources(options, context, stderr) {
|
|
579
|
+
if (options.paths.length === 0) {
|
|
580
|
+
return {
|
|
581
|
+
items: [{ content: await readStdin() }],
|
|
582
|
+
forceFilename: options.withFilename === true,
|
|
583
|
+
hadError: false,
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
const items = [];
|
|
587
|
+
let hadError = false;
|
|
588
|
+
for (const inputPath of options.paths) {
|
|
589
|
+
const absolutePath = resolve(context.cwd, inputPath);
|
|
590
|
+
let stats;
|
|
591
|
+
try {
|
|
592
|
+
stats = statSync(absolutePath);
|
|
593
|
+
}
|
|
594
|
+
catch {
|
|
595
|
+
if (!options.noMessages)
|
|
596
|
+
appendLine(stderr, `grep: ${inputPath}: No such file or directory`);
|
|
597
|
+
hadError = true;
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
if (stats.isDirectory()) {
|
|
601
|
+
if (!options.recursive) {
|
|
602
|
+
if (!options.noMessages)
|
|
603
|
+
appendLine(stderr, `grep: ${inputPath}: Is a directory`);
|
|
604
|
+
hadError = true;
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
for (const filePath of walkFiles(absolutePath)) {
|
|
608
|
+
const displayName = formatRecursiveDisplayName(inputPath, absolutePath, filePath);
|
|
609
|
+
if (!shouldIncludeGrepFile(displayName, options))
|
|
610
|
+
continue;
|
|
611
|
+
const content = readTextFile(filePath, stderr, "grep", displayName, options.noMessages);
|
|
612
|
+
if (content === undefined) {
|
|
613
|
+
hadError = true;
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
items.push({ displayName, content });
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
if (!shouldIncludeGrepFile(inputPath, options))
|
|
621
|
+
continue;
|
|
622
|
+
const content = readTextFile(absolutePath, stderr, "grep", inputPath, options.noMessages);
|
|
623
|
+
if (content === undefined) {
|
|
624
|
+
hadError = true;
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
items.push({ displayName: inputPath, content });
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
const forceFilename = options.withFilename === true ||
|
|
631
|
+
(!options.noFilename && (items.length > 1 || options.recursive || options.paths.length > 1));
|
|
632
|
+
return { items, forceFilename, hadError };
|
|
633
|
+
}
|
|
634
|
+
function shouldIncludeGrepFile(displayName, options) {
|
|
635
|
+
const normalized = displayName.replace(/^\.\//, "");
|
|
636
|
+
const base = basename(normalized);
|
|
637
|
+
if (options.includeGlobs.length > 0 && !options.includeGlobs.some((glob) => matchesPathOrBase(normalized, base, glob))) {
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
if (options.excludeGlobs.some((glob) => matchesPathOrBase(normalized, base, glob))) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
if (options.excludeDirGlobs.some((glob) => normalized.split("/").some((part) => minimatch(part, glob, { dot: true })))) {
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
function matchesPathOrBase(pathValue, base, glob) {
|
|
649
|
+
return minimatch(pathValue, glob, { dot: true }) || minimatch(base, glob, { dot: true });
|
|
650
|
+
}
|
|
651
|
+
function formatRecursiveDisplayName(inputRoot, absoluteRoot, filePath) {
|
|
652
|
+
const relativePath = toPosix(relative(absoluteRoot, filePath));
|
|
653
|
+
if (inputRoot === "." || inputRoot === "./")
|
|
654
|
+
return `./${relativePath}`;
|
|
655
|
+
return `${inputRoot.replace(/\/$/, "")}/${relativePath}`;
|
|
656
|
+
}
|
|
657
|
+
function formatGrepLine(displayName, lineNumber, line, isMatch, options, forceFilename) {
|
|
658
|
+
const parts = [];
|
|
659
|
+
const separator = isMatch ? ":" : "-";
|
|
660
|
+
if (forceFilename && !options.noFilename && displayName)
|
|
661
|
+
parts.push(displayName);
|
|
662
|
+
if (options.lineNumber)
|
|
663
|
+
parts.push(String(lineNumber));
|
|
664
|
+
if (parts.length === 0)
|
|
665
|
+
return line;
|
|
666
|
+
return `${parts.join(separator)}${separator}${line}`;
|
|
667
|
+
}
|
|
668
|
+
function parseFindArgs(args) {
|
|
669
|
+
const options = { paths: [], clauses: [[]], outputSeparator: "\n" };
|
|
670
|
+
let expressionStarted = false;
|
|
671
|
+
let negateNext = false;
|
|
672
|
+
for (let i = 0; i < args.length; i++) {
|
|
673
|
+
const arg = args[i];
|
|
674
|
+
if (!expressionStarted && !arg.startsWith("-") && arg !== "(" && arg !== "!") {
|
|
675
|
+
options.paths.push(arg);
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
expressionStarted = true;
|
|
679
|
+
switch (arg) {
|
|
680
|
+
case "-name":
|
|
681
|
+
addFindPredicate(options, { kind: "name", value: args[++i] ?? "", negate: negateNext });
|
|
682
|
+
negateNext = false;
|
|
683
|
+
break;
|
|
684
|
+
case "-iname":
|
|
685
|
+
addFindPredicate(options, { kind: "iname", value: args[++i] ?? "", negate: negateNext });
|
|
686
|
+
negateNext = false;
|
|
687
|
+
break;
|
|
688
|
+
case "-path":
|
|
689
|
+
case "-wholename":
|
|
690
|
+
addFindPredicate(options, { kind: "path", value: args[++i] ?? "", negate: negateNext });
|
|
691
|
+
negateNext = false;
|
|
692
|
+
break;
|
|
693
|
+
case "-ipath":
|
|
694
|
+
case "-iwholename":
|
|
695
|
+
addFindPredicate(options, { kind: "ipath", value: args[++i] ?? "", negate: negateNext });
|
|
696
|
+
negateNext = false;
|
|
697
|
+
break;
|
|
698
|
+
case "-type": {
|
|
699
|
+
const value = args[++i];
|
|
700
|
+
if (value !== "f" && value !== "d" && value !== "l") {
|
|
701
|
+
return { stdout: "", stderr: `find: unsupported -type value: ${value ?? ""}\n`, exitCode: 1 };
|
|
702
|
+
}
|
|
703
|
+
addFindPredicate(options, { kind: "type", value, negate: negateNext });
|
|
704
|
+
negateNext = false;
|
|
705
|
+
break;
|
|
706
|
+
}
|
|
707
|
+
case "-empty":
|
|
708
|
+
addFindPredicate(options, { kind: "empty", negate: negateNext });
|
|
709
|
+
negateNext = false;
|
|
710
|
+
break;
|
|
711
|
+
case "-maxdepth": {
|
|
712
|
+
const parsed = parseInteger("find", "-maxdepth", args[++i]);
|
|
713
|
+
if (isCompatResult(parsed))
|
|
714
|
+
return parsed;
|
|
715
|
+
options.maxDepth = parsed;
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
case "-mindepth": {
|
|
719
|
+
const parsed = parseInteger("find", "-mindepth", args[++i]);
|
|
720
|
+
if (isCompatResult(parsed))
|
|
721
|
+
return parsed;
|
|
722
|
+
options.minDepth = parsed;
|
|
723
|
+
break;
|
|
724
|
+
}
|
|
725
|
+
case "!":
|
|
726
|
+
case "-not":
|
|
727
|
+
negateNext = !negateNext;
|
|
728
|
+
break;
|
|
729
|
+
case "-o":
|
|
730
|
+
case "-or":
|
|
731
|
+
options.clauses.push([]);
|
|
732
|
+
negateNext = false;
|
|
733
|
+
break;
|
|
734
|
+
case "-print":
|
|
735
|
+
options.outputSeparator = "\n";
|
|
736
|
+
break;
|
|
737
|
+
case "-print0":
|
|
738
|
+
options.outputSeparator = "\0";
|
|
739
|
+
break;
|
|
740
|
+
case "-a":
|
|
741
|
+
case "-and":
|
|
742
|
+
case "(":
|
|
743
|
+
case ")":
|
|
744
|
+
break;
|
|
745
|
+
default:
|
|
746
|
+
return { stdout: "", stderr: `find: unsupported expression: ${arg}\n`, exitCode: 1 };
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
if (options.paths.length === 0)
|
|
750
|
+
options.paths.push(".");
|
|
751
|
+
return options;
|
|
752
|
+
}
|
|
753
|
+
function addFindPredicate(options, predicate) {
|
|
754
|
+
options.clauses[options.clauses.length - 1]?.push(predicate);
|
|
755
|
+
}
|
|
756
|
+
function executeCompatFind(args, context) {
|
|
757
|
+
const parsed = parseFindArgs(args);
|
|
758
|
+
if (isCompatResult(parsed))
|
|
759
|
+
return parsed;
|
|
760
|
+
const fdResult = tryRunFindWithFd(parsed, context);
|
|
761
|
+
if (fdResult)
|
|
762
|
+
return fdResult;
|
|
763
|
+
return runFindWithNode(parsed, context);
|
|
764
|
+
}
|
|
765
|
+
function tryRunFindWithFd(options, context) {
|
|
766
|
+
const fdPath = getToolPath("fd");
|
|
767
|
+
if (!fdPath || options.outputSeparator !== "\n")
|
|
768
|
+
return null;
|
|
769
|
+
if (options.clauses.length !== 1)
|
|
770
|
+
return null;
|
|
771
|
+
const predicates = options.clauses[0] ?? [];
|
|
772
|
+
const namePredicate = predicates.find(isPositiveFindNamePredicate);
|
|
773
|
+
const typePredicate = predicates.find(isPositiveFindTypePredicate);
|
|
774
|
+
const unsupported = predicates.some((predicate) => predicate.negate ||
|
|
775
|
+
(predicate.kind !== "name" && predicate.kind !== "iname" && predicate.kind !== "type") ||
|
|
776
|
+
((predicate.kind === "name" || predicate.kind === "iname") && predicate !== namePredicate) ||
|
|
777
|
+
(predicate.kind === "type" && predicate !== typePredicate));
|
|
778
|
+
if (!namePredicate || unsupported)
|
|
779
|
+
return null;
|
|
780
|
+
const fdArgs = ["--hidden", "--no-ignore", "--color=never", "--glob"];
|
|
781
|
+
if (namePredicate.kind === "iname")
|
|
782
|
+
fdArgs.push("--ignore-case");
|
|
783
|
+
if (options.maxDepth !== undefined)
|
|
784
|
+
fdArgs.push("--max-depth", String(options.maxDepth));
|
|
785
|
+
if (options.minDepth !== undefined)
|
|
786
|
+
fdArgs.push("--min-depth", String(options.minDepth));
|
|
787
|
+
if (typePredicate?.kind === "type" && typePredicate.value === "f")
|
|
788
|
+
fdArgs.push("--type", "file");
|
|
789
|
+
if (typePredicate?.kind === "type" && typePredicate.value === "d")
|
|
790
|
+
fdArgs.push("--type", "directory");
|
|
791
|
+
if (typePredicate?.kind === "type" && typePredicate.value === "l")
|
|
792
|
+
fdArgs.push("--type", "symlink");
|
|
793
|
+
fdArgs.push(namePredicate.value);
|
|
794
|
+
for (const inputPath of options.paths)
|
|
795
|
+
fdArgs.push(resolve(context.cwd, inputPath));
|
|
796
|
+
const child = spawnSync(fdPath, fdArgs, { cwd: context.cwd, encoding: "utf-8" });
|
|
797
|
+
if (child.error || (child.status !== 0 && child.status !== 1))
|
|
798
|
+
return null;
|
|
799
|
+
const lines = (child.stdout ?? "")
|
|
800
|
+
.split(/\r?\n/)
|
|
801
|
+
.map((line) => line.trim())
|
|
802
|
+
.filter(Boolean)
|
|
803
|
+
.map((line) => normalizeFindEnginePath(line, options, context));
|
|
804
|
+
return result(lines, child.stderr ? [child.stderr.trim()] : [], child.status ?? 0, options.outputSeparator);
|
|
805
|
+
}
|
|
806
|
+
function isPositiveFindNamePredicate(predicate) {
|
|
807
|
+
return !predicate.negate && (predicate.kind === "name" || predicate.kind === "iname");
|
|
808
|
+
}
|
|
809
|
+
function isPositiveFindTypePredicate(predicate) {
|
|
810
|
+
return !predicate.negate && predicate.kind === "type";
|
|
811
|
+
}
|
|
812
|
+
function normalizeFindEnginePath(rawLine, options, context) {
|
|
813
|
+
const absolutePath = isAbsolute(rawLine) ? rawLine : resolve(context.cwd, rawLine);
|
|
814
|
+
for (const inputPath of options.paths) {
|
|
815
|
+
const absoluteRoot = resolve(context.cwd, inputPath);
|
|
816
|
+
const relativePath = relative(absoluteRoot, absolutePath);
|
|
817
|
+
if (!relativePath.startsWith("..") && !isAbsolute(relativePath)) {
|
|
818
|
+
return formatFindDisplayName(inputPath, relativePath);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return toPosix(relative(context.cwd, absolutePath));
|
|
822
|
+
}
|
|
823
|
+
function runFindWithNode(options, context) {
|
|
824
|
+
const stdout = [];
|
|
825
|
+
const stderr = [];
|
|
826
|
+
let hadError = false;
|
|
827
|
+
for (const inputPath of options.paths) {
|
|
828
|
+
const absoluteRoot = resolve(context.cwd, inputPath);
|
|
829
|
+
if (!existsSync(absoluteRoot)) {
|
|
830
|
+
appendLine(stderr, `find: ${inputPath}: No such file or directory`);
|
|
831
|
+
hadError = true;
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
walkFind(inputPath, absoluteRoot, absoluteRoot, 0, options, stdout, stderr);
|
|
835
|
+
}
|
|
836
|
+
return result(stdout, stderr, hadError ? 1 : 0, options.outputSeparator);
|
|
837
|
+
}
|
|
838
|
+
function walkFind(inputRoot, absoluteRoot, currentPath, depth, options, stdout, stderr) {
|
|
839
|
+
const minDepth = options.minDepth ?? 0;
|
|
840
|
+
const maxDepth = options.maxDepth ?? Number.POSITIVE_INFINITY;
|
|
841
|
+
let stats;
|
|
842
|
+
try {
|
|
843
|
+
stats = lstatSync(currentPath);
|
|
844
|
+
}
|
|
845
|
+
catch (error) {
|
|
846
|
+
appendLine(stderr, `find: ${currentPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
const displayName = formatFindDisplayName(inputRoot, relative(absoluteRoot, currentPath));
|
|
850
|
+
if (depth >= minDepth && depth <= maxDepth && matchesFindEntry(currentPath, displayName, stats, options)) {
|
|
851
|
+
appendLine(stdout, displayName);
|
|
852
|
+
}
|
|
853
|
+
if (!stats.isDirectory() || depth >= maxDepth)
|
|
854
|
+
return;
|
|
855
|
+
let entries;
|
|
856
|
+
try {
|
|
857
|
+
entries = readdirSync(currentPath).sort((a, b) => a.localeCompare(b));
|
|
858
|
+
}
|
|
859
|
+
catch (error) {
|
|
860
|
+
appendLine(stderr, `find: ${currentPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
for (const entry of entries) {
|
|
864
|
+
walkFind(inputRoot, absoluteRoot, join(currentPath, entry), depth + 1, options, stdout, stderr);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
function matchesFindEntry(path, displayName, stats, options) {
|
|
868
|
+
const activeClauses = options.clauses.length > 0 ? options.clauses : [[]];
|
|
869
|
+
return activeClauses.some((clause) => clause.every((predicate) => matchesFindPredicate(path, displayName, stats, predicate)));
|
|
870
|
+
}
|
|
871
|
+
function matchesFindPredicate(path, displayName, stats, predicate) {
|
|
872
|
+
let matched;
|
|
873
|
+
const entryName = basename(path);
|
|
874
|
+
switch (predicate.kind) {
|
|
875
|
+
case "name":
|
|
876
|
+
matched = minimatch(entryName, predicate.value, { dot: true });
|
|
877
|
+
break;
|
|
878
|
+
case "iname":
|
|
879
|
+
matched = minimatch(entryName.toLowerCase(), predicate.value.toLowerCase(), { dot: true });
|
|
880
|
+
break;
|
|
881
|
+
case "path":
|
|
882
|
+
matched = minimatch(displayName, predicate.value, { dot: true });
|
|
883
|
+
break;
|
|
884
|
+
case "ipath":
|
|
885
|
+
matched = minimatch(displayName.toLowerCase(), predicate.value.toLowerCase(), { dot: true });
|
|
886
|
+
break;
|
|
887
|
+
case "type":
|
|
888
|
+
matched =
|
|
889
|
+
(predicate.value === "f" && stats.isFile()) ||
|
|
890
|
+
(predicate.value === "d" && stats.isDirectory()) ||
|
|
891
|
+
(predicate.value === "l" && stats.isSymbolicLink());
|
|
892
|
+
break;
|
|
893
|
+
case "empty":
|
|
894
|
+
matched = isFindEntryEmpty(path, stats);
|
|
895
|
+
break;
|
|
896
|
+
}
|
|
897
|
+
return predicate.negate ? !matched : matched;
|
|
898
|
+
}
|
|
899
|
+
function isFindEntryEmpty(path, stats) {
|
|
900
|
+
if (stats.isDirectory()) {
|
|
901
|
+
try {
|
|
902
|
+
return readdirSync(path).length === 0;
|
|
903
|
+
}
|
|
904
|
+
catch {
|
|
905
|
+
return false;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
return stats.isFile() && stats.size === 0;
|
|
909
|
+
}
|
|
910
|
+
function formatFindDisplayName(inputRoot, relativePath) {
|
|
911
|
+
const normalized = toPosix(relativePath);
|
|
912
|
+
if (!normalized)
|
|
913
|
+
return inputRoot;
|
|
914
|
+
if (inputRoot === "." || inputRoot === "./")
|
|
915
|
+
return `./${normalized}`;
|
|
916
|
+
return `${inputRoot.replace(/\/$/, "")}/${normalized}`;
|
|
917
|
+
}
|
|
918
|
+
function parseLsArgs(args) {
|
|
919
|
+
const options = {
|
|
920
|
+
all: false,
|
|
921
|
+
almostAll: false,
|
|
922
|
+
long: false,
|
|
923
|
+
human: false,
|
|
924
|
+
directory: false,
|
|
925
|
+
onePerLine: false,
|
|
926
|
+
recursive: false,
|
|
927
|
+
reverse: false,
|
|
928
|
+
sortBy: "name",
|
|
929
|
+
classify: false,
|
|
930
|
+
appendSlash: false,
|
|
931
|
+
groupDirectoriesFirst: false,
|
|
932
|
+
paths: [],
|
|
933
|
+
};
|
|
934
|
+
let stopOptions = false;
|
|
935
|
+
for (const arg of args) {
|
|
936
|
+
if (stopOptions) {
|
|
937
|
+
options.paths.push(arg);
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
if (arg === "--") {
|
|
941
|
+
stopOptions = true;
|
|
942
|
+
continue;
|
|
943
|
+
}
|
|
944
|
+
if (arg === "--all") {
|
|
945
|
+
options.all = true;
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
if (arg === "--almost-all") {
|
|
949
|
+
options.almostAll = true;
|
|
950
|
+
continue;
|
|
951
|
+
}
|
|
952
|
+
if (arg === "--long") {
|
|
953
|
+
options.long = true;
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
if (arg === "--human-readable") {
|
|
957
|
+
options.human = true;
|
|
958
|
+
continue;
|
|
959
|
+
}
|
|
960
|
+
if (arg === "--directory") {
|
|
961
|
+
options.directory = true;
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
if (arg === "--recursive") {
|
|
965
|
+
options.recursive = true;
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
if (arg === "--reverse") {
|
|
969
|
+
options.reverse = true;
|
|
970
|
+
continue;
|
|
971
|
+
}
|
|
972
|
+
if (arg === "--classify") {
|
|
973
|
+
options.classify = true;
|
|
974
|
+
continue;
|
|
975
|
+
}
|
|
976
|
+
if (arg === "--indicator-style=slash") {
|
|
977
|
+
options.appendSlash = true;
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (arg === "--group-directories-first") {
|
|
981
|
+
options.groupDirectoriesFirst = true;
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
if (arg === "--color" || arg.startsWith("--color=")) {
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
if (arg.startsWith("--sort=")) {
|
|
988
|
+
const sort = arg.slice("--sort=".length);
|
|
989
|
+
if (sort === "time")
|
|
990
|
+
options.sortBy = "time";
|
|
991
|
+
else if (sort === "size")
|
|
992
|
+
options.sortBy = "size";
|
|
993
|
+
else if (sort === "none")
|
|
994
|
+
options.sortBy = "none";
|
|
995
|
+
else if (sort === "name" || sort === "extension" || sort === "version")
|
|
996
|
+
options.sortBy = "name";
|
|
997
|
+
else
|
|
998
|
+
return { stdout: "", stderr: `ls: unsupported sort key: ${sort}\n`, exitCode: 2 };
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
if (arg.startsWith("--")) {
|
|
1002
|
+
return { stdout: "", stderr: `ls: unsupported option: ${arg}\n`, exitCode: 2 };
|
|
1003
|
+
}
|
|
1004
|
+
if (arg.startsWith("-") && arg !== "-") {
|
|
1005
|
+
for (const flag of arg.slice(1)) {
|
|
1006
|
+
switch (flag) {
|
|
1007
|
+
case "a":
|
|
1008
|
+
options.all = true;
|
|
1009
|
+
break;
|
|
1010
|
+
case "A":
|
|
1011
|
+
options.almostAll = true;
|
|
1012
|
+
break;
|
|
1013
|
+
case "l":
|
|
1014
|
+
options.long = true;
|
|
1015
|
+
break;
|
|
1016
|
+
case "h":
|
|
1017
|
+
options.human = true;
|
|
1018
|
+
break;
|
|
1019
|
+
case "d":
|
|
1020
|
+
options.directory = true;
|
|
1021
|
+
break;
|
|
1022
|
+
case "1":
|
|
1023
|
+
options.onePerLine = true;
|
|
1024
|
+
break;
|
|
1025
|
+
case "R":
|
|
1026
|
+
options.recursive = true;
|
|
1027
|
+
break;
|
|
1028
|
+
case "r":
|
|
1029
|
+
options.reverse = true;
|
|
1030
|
+
break;
|
|
1031
|
+
case "t":
|
|
1032
|
+
options.sortBy = "time";
|
|
1033
|
+
break;
|
|
1034
|
+
case "S":
|
|
1035
|
+
options.sortBy = "size";
|
|
1036
|
+
break;
|
|
1037
|
+
case "f":
|
|
1038
|
+
options.all = true;
|
|
1039
|
+
options.sortBy = "none";
|
|
1040
|
+
break;
|
|
1041
|
+
case "F":
|
|
1042
|
+
options.classify = true;
|
|
1043
|
+
break;
|
|
1044
|
+
case "p":
|
|
1045
|
+
options.appendSlash = true;
|
|
1046
|
+
break;
|
|
1047
|
+
case "G":
|
|
1048
|
+
case "C":
|
|
1049
|
+
case "x":
|
|
1050
|
+
case "q":
|
|
1051
|
+
break;
|
|
1052
|
+
default:
|
|
1053
|
+
return { stdout: "", stderr: `ls: unsupported option -- ${flag}\n`, exitCode: 2 };
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
continue;
|
|
1057
|
+
}
|
|
1058
|
+
options.paths.push(arg);
|
|
1059
|
+
}
|
|
1060
|
+
if (options.paths.length === 0)
|
|
1061
|
+
options.paths.push(".");
|
|
1062
|
+
return options;
|
|
1063
|
+
}
|
|
1064
|
+
function executeCompatLs(args, context) {
|
|
1065
|
+
const parsed = parseLsArgs(args);
|
|
1066
|
+
if (isCompatResult(parsed))
|
|
1067
|
+
return parsed;
|
|
1068
|
+
const stdout = [];
|
|
1069
|
+
const stderr = [];
|
|
1070
|
+
let hadError = false;
|
|
1071
|
+
const multiplePaths = parsed.paths.length > 1;
|
|
1072
|
+
for (let pathIndex = 0; pathIndex < parsed.paths.length; pathIndex++) {
|
|
1073
|
+
const inputPath = parsed.paths[pathIndex];
|
|
1074
|
+
const absolutePath = resolve(context.cwd, inputPath);
|
|
1075
|
+
let stats;
|
|
1076
|
+
try {
|
|
1077
|
+
stats = statSync(absolutePath);
|
|
1078
|
+
}
|
|
1079
|
+
catch {
|
|
1080
|
+
appendLine(stderr, `ls: ${inputPath}: No such file or directory`);
|
|
1081
|
+
hadError = true;
|
|
1082
|
+
continue;
|
|
1083
|
+
}
|
|
1084
|
+
if (stats.isDirectory() && !parsed.directory) {
|
|
1085
|
+
if (multiplePaths || parsed.recursive) {
|
|
1086
|
+
if (stdout.length > 0)
|
|
1087
|
+
appendLine(stdout, "");
|
|
1088
|
+
appendLine(stdout, `${inputPath}:`);
|
|
1089
|
+
}
|
|
1090
|
+
emitLsDirectory(stdout, inputPath, absolutePath, parsed, multiplePaths || parsed.recursive);
|
|
1091
|
+
}
|
|
1092
|
+
else {
|
|
1093
|
+
appendLine(stdout, formatLsEntry(inputPath, absolutePath, parsed));
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
return result(stdout, stderr, hadError ? 2 : 0);
|
|
1097
|
+
}
|
|
1098
|
+
function emitLsDirectory(stdout, displayPath, absolutePath, options, includeRecursiveHeaders) {
|
|
1099
|
+
const entries = listDirectoryEntries(absolutePath, options);
|
|
1100
|
+
for (const entry of entries) {
|
|
1101
|
+
const entryPath = join(absolutePath, entry);
|
|
1102
|
+
appendLine(stdout, formatLsEntry(entry, entryPath, options));
|
|
1103
|
+
}
|
|
1104
|
+
if (!options.recursive)
|
|
1105
|
+
return;
|
|
1106
|
+
for (const entry of entries) {
|
|
1107
|
+
if (entry === "." || entry === "..")
|
|
1108
|
+
continue;
|
|
1109
|
+
const entryPath = join(absolutePath, entry);
|
|
1110
|
+
let stats;
|
|
1111
|
+
try {
|
|
1112
|
+
stats = statSync(entryPath);
|
|
1113
|
+
}
|
|
1114
|
+
catch {
|
|
1115
|
+
continue;
|
|
1116
|
+
}
|
|
1117
|
+
if (!stats.isDirectory())
|
|
1118
|
+
continue;
|
|
1119
|
+
const childDisplayPath = `${displayPath.replace(/\/$/, "")}/${entry}`;
|
|
1120
|
+
if (includeRecursiveHeaders) {
|
|
1121
|
+
appendLine(stdout, "");
|
|
1122
|
+
appendLine(stdout, `${childDisplayPath}:`);
|
|
1123
|
+
}
|
|
1124
|
+
emitLsDirectory(stdout, childDisplayPath, entryPath, options, includeRecursiveHeaders);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
function listDirectoryEntries(path, options) {
|
|
1128
|
+
let entries = readdirSync(path);
|
|
1129
|
+
if (options.all) {
|
|
1130
|
+
entries = [".", "..", ...entries];
|
|
1131
|
+
}
|
|
1132
|
+
else if (!options.almostAll) {
|
|
1133
|
+
entries = entries.filter((entry) => !entry.startsWith("."));
|
|
1134
|
+
}
|
|
1135
|
+
return sortLsEntries(path, entries, options);
|
|
1136
|
+
}
|
|
1137
|
+
function sortLsEntries(path, entries, options) {
|
|
1138
|
+
if (options.sortBy === "none")
|
|
1139
|
+
return options.reverse ? entries.reverse() : entries;
|
|
1140
|
+
const sorted = [...entries].sort((a, b) => {
|
|
1141
|
+
const aPath = join(path, a);
|
|
1142
|
+
const bPath = join(path, b);
|
|
1143
|
+
const aStats = safeStat(aPath);
|
|
1144
|
+
const bStats = safeStat(bPath);
|
|
1145
|
+
if (options.groupDirectoriesFirst && aStats && bStats && aStats.isDirectory() !== bStats.isDirectory()) {
|
|
1146
|
+
return aStats.isDirectory() ? -1 : 1;
|
|
1147
|
+
}
|
|
1148
|
+
if (options.sortBy === "time" && aStats && bStats) {
|
|
1149
|
+
const diff = bStats.mtimeMs - aStats.mtimeMs;
|
|
1150
|
+
if (diff !== 0)
|
|
1151
|
+
return diff;
|
|
1152
|
+
}
|
|
1153
|
+
if (options.sortBy === "size" && aStats && bStats) {
|
|
1154
|
+
const diff = bStats.size - aStats.size;
|
|
1155
|
+
if (diff !== 0)
|
|
1156
|
+
return diff;
|
|
1157
|
+
}
|
|
1158
|
+
return a.localeCompare(b);
|
|
1159
|
+
});
|
|
1160
|
+
if (options.reverse)
|
|
1161
|
+
sorted.reverse();
|
|
1162
|
+
return sorted;
|
|
1163
|
+
}
|
|
1164
|
+
function safeStat(path) {
|
|
1165
|
+
try {
|
|
1166
|
+
return statSync(path);
|
|
1167
|
+
}
|
|
1168
|
+
catch {
|
|
1169
|
+
return undefined;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
function formatLsEntry(displayName, absolutePath, options) {
|
|
1173
|
+
const name = formatLsDisplayName(displayName, absolutePath, options);
|
|
1174
|
+
if (!options.long)
|
|
1175
|
+
return name;
|
|
1176
|
+
const stats = statSync(absolutePath);
|
|
1177
|
+
const mode = formatMode(stats.mode, stats.isDirectory());
|
|
1178
|
+
const size = options.human ? humanSize(stats.size) : String(stats.size);
|
|
1179
|
+
const modified = stats.mtime.toISOString().slice(0, 16).replace("T", " ");
|
|
1180
|
+
return `${mode} ${String(stats.nlink).padStart(2, " ")} ${String(stats.uid).padStart(5, " ")} ${String(stats.gid).padStart(5, " ")} ${size.padStart(6, " ")} ${modified} ${name}`;
|
|
1181
|
+
}
|
|
1182
|
+
function formatLsDisplayName(displayName, absolutePath, options) {
|
|
1183
|
+
let stats;
|
|
1184
|
+
let linkStats;
|
|
1185
|
+
try {
|
|
1186
|
+
stats = statSync(absolutePath);
|
|
1187
|
+
linkStats = lstatSync(absolutePath);
|
|
1188
|
+
}
|
|
1189
|
+
catch {
|
|
1190
|
+
return displayName;
|
|
1191
|
+
}
|
|
1192
|
+
if ((options.appendSlash || options.classify) && stats.isDirectory() && !displayName.endsWith("/")) {
|
|
1193
|
+
return `${displayName}/`;
|
|
1194
|
+
}
|
|
1195
|
+
if (options.classify) {
|
|
1196
|
+
if (linkStats.isSymbolicLink())
|
|
1197
|
+
return `${displayName}@`;
|
|
1198
|
+
if (stats.mode & 0o111)
|
|
1199
|
+
return `${displayName}*`;
|
|
1200
|
+
}
|
|
1201
|
+
return displayName;
|
|
1202
|
+
}
|
|
1203
|
+
function formatMode(mode, directory) {
|
|
1204
|
+
const chars = directory ? ["d"] : ["-"];
|
|
1205
|
+
const masks = [0o400, 0o200, 0o100, 0o040, 0o020, 0o010, 0o004, 0o002, 0o001];
|
|
1206
|
+
for (let i = 0; i < masks.length; i++) {
|
|
1207
|
+
const bit = masks[i];
|
|
1208
|
+
const type = i % 3 === 0 ? "r" : i % 3 === 1 ? "w" : "x";
|
|
1209
|
+
chars.push(mode & bit ? type : "-");
|
|
1210
|
+
}
|
|
1211
|
+
return chars.join("");
|
|
1212
|
+
}
|
|
1213
|
+
function humanSize(size) {
|
|
1214
|
+
const units = ["B", "K", "M", "G", "T"];
|
|
1215
|
+
let value = size;
|
|
1216
|
+
let unitIndex = 0;
|
|
1217
|
+
while (value >= 1024 && unitIndex < units.length - 1) {
|
|
1218
|
+
value /= 1024;
|
|
1219
|
+
unitIndex++;
|
|
1220
|
+
}
|
|
1221
|
+
if (unitIndex === 0)
|
|
1222
|
+
return String(size);
|
|
1223
|
+
return `${value >= 10 ? value.toFixed(0) : value.toFixed(1)}${units[unitIndex]}`;
|
|
1224
|
+
}
|
|
1225
|
+
// ============================================================================
|
|
1226
|
+
// Shared helpers
|
|
1227
|
+
// ============================================================================
|
|
1228
|
+
async function readStdin() {
|
|
1229
|
+
const chunks = [];
|
|
1230
|
+
return await new Promise((resolve) => {
|
|
1231
|
+
let settled = false;
|
|
1232
|
+
const finish = () => {
|
|
1233
|
+
if (settled)
|
|
1234
|
+
return;
|
|
1235
|
+
settled = true;
|
|
1236
|
+
resolve(Buffer.concat(chunks).toString("utf-8"));
|
|
1237
|
+
};
|
|
1238
|
+
process.stdin.on("data", (chunk) => {
|
|
1239
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
1240
|
+
});
|
|
1241
|
+
process.stdin.once("end", finish);
|
|
1242
|
+
process.stdin.once("error", finish);
|
|
1243
|
+
process.stdin.resume();
|
|
1244
|
+
});
|
|
1245
|
+
}
|
|
1246
|
+
function readTextFile(path, stderr, command, displayName, suppressErrors = false) {
|
|
1247
|
+
try {
|
|
1248
|
+
return readFileSync(path, "utf-8");
|
|
1249
|
+
}
|
|
1250
|
+
catch (error) {
|
|
1251
|
+
if (!suppressErrors) {
|
|
1252
|
+
appendLine(stderr, `${command}: ${displayName}: ${error instanceof Error ? error.message : String(error)}`);
|
|
1253
|
+
}
|
|
1254
|
+
return undefined;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
function walkFiles(root) {
|
|
1258
|
+
const results = [];
|
|
1259
|
+
const walk = (dir) => {
|
|
1260
|
+
let entries;
|
|
1261
|
+
try {
|
|
1262
|
+
entries = readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
1263
|
+
}
|
|
1264
|
+
catch {
|
|
1265
|
+
return;
|
|
1266
|
+
}
|
|
1267
|
+
for (const entry of entries) {
|
|
1268
|
+
const fullPath = join(dir, entry.name);
|
|
1269
|
+
if (entry.isDirectory())
|
|
1270
|
+
walk(fullPath);
|
|
1271
|
+
else if (entry.isFile())
|
|
1272
|
+
results.push(fullPath);
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
walk(root);
|
|
1276
|
+
return results;
|
|
1277
|
+
}
|
|
1278
|
+
function toPosix(value) {
|
|
1279
|
+
return value.replace(/\\/g, "/");
|
|
1280
|
+
}
|