alepha 0.15.0 → 0.15.2
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 +43 -98
- package/dist/api/audits/index.d.ts +630 -653
- package/dist/api/audits/index.d.ts.map +1 -1
- package/dist/api/audits/index.js +12 -35
- package/dist/api/audits/index.js.map +1 -1
- package/dist/api/files/index.d.ts +365 -358
- package/dist/api/files/index.d.ts.map +1 -1
- package/dist/api/files/index.js +12 -5
- package/dist/api/files/index.js.map +1 -1
- package/dist/api/jobs/index.d.ts +255 -248
- package/dist/api/jobs/index.d.ts.map +1 -1
- package/dist/api/jobs/index.js +10 -3
- package/dist/api/jobs/index.js.map +1 -1
- package/dist/api/keys/index.d.ts +413 -0
- package/dist/api/keys/index.d.ts.map +1 -0
- package/dist/api/keys/index.js +476 -0
- package/dist/api/keys/index.js.map +1 -0
- package/dist/api/notifications/index.browser.js +4 -4
- package/dist/api/notifications/index.browser.js.map +1 -1
- package/dist/api/notifications/index.d.ts +84 -78
- package/dist/api/notifications/index.d.ts.map +1 -1
- package/dist/api/notifications/index.js +14 -8
- package/dist/api/notifications/index.js.map +1 -1
- package/dist/api/parameters/index.d.ts +528 -535
- package/dist/api/parameters/index.d.ts.map +1 -1
- package/dist/api/parameters/index.js +30 -37
- package/dist/api/parameters/index.js.map +1 -1
- package/dist/api/users/index.d.ts +1221 -910
- package/dist/api/users/index.d.ts.map +1 -1
- package/dist/api/users/index.js +2556 -248
- package/dist/api/users/index.js.map +1 -1
- package/dist/api/verifications/index.d.ts +142 -136
- package/dist/api/verifications/index.d.ts.map +1 -1
- package/dist/api/verifications/index.js +12 -4
- package/dist/api/verifications/index.js.map +1 -1
- package/dist/batch/index.d.ts +142 -162
- package/dist/batch/index.d.ts.map +1 -1
- package/dist/batch/index.js +31 -44
- package/dist/batch/index.js.map +1 -1
- package/dist/bucket/index.d.ts +595 -171
- package/dist/bucket/index.d.ts.map +1 -1
- package/dist/bucket/index.js +1856 -12
- package/dist/bucket/index.js.map +1 -1
- package/dist/cache/core/index.d.ts +225 -53
- package/dist/cache/core/index.d.ts.map +1 -1
- package/dist/cache/core/index.js +213 -7
- package/dist/cache/core/index.js.map +1 -1
- package/dist/cache/redis/index.d.ts +1 -0
- package/dist/cache/redis/index.d.ts.map +1 -1
- package/dist/cache/redis/index.js +6 -2
- package/dist/cache/redis/index.js.map +1 -1
- package/dist/cli/index.d.ts +834 -226
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +2872 -417
- package/dist/cli/index.js.map +1 -1
- package/dist/command/index.d.ts +458 -310
- package/dist/command/index.d.ts.map +1 -1
- package/dist/command/index.js +2011 -76
- package/dist/command/index.js.map +1 -1
- package/dist/core/index.browser.js +309 -97
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +796 -701
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +329 -97
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +309 -97
- package/dist/core/index.native.js.map +1 -1
- package/dist/datetime/index.d.ts +59 -44
- package/dist/datetime/index.d.ts.map +1 -1
- package/dist/datetime/index.js +15 -0
- package/dist/datetime/index.js.map +1 -1
- package/dist/email/index.d.ts +314 -19
- package/dist/email/index.d.ts.map +1 -1
- package/dist/email/index.js +1852 -7
- package/dist/email/index.js.map +1 -1
- package/dist/fake/index.d.ts +5500 -5418
- package/dist/fake/index.d.ts.map +1 -1
- package/dist/fake/index.js +113 -42
- package/dist/fake/index.js.map +1 -1
- package/dist/lock/core/index.d.ts +219 -212
- package/dist/lock/core/index.d.ts.map +1 -1
- package/dist/lock/core/index.js +11 -4
- package/dist/lock/core/index.js.map +1 -1
- package/dist/lock/redis/index.d.ts.map +1 -1
- package/dist/logger/index.d.ts +41 -90
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +15 -68
- package/dist/logger/index.js.map +1 -1
- package/dist/mcp/index.d.ts +228 -230
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +32 -31
- package/dist/mcp/index.js.map +1 -1
- package/dist/orm/index.browser.js +12 -12
- package/dist/orm/index.browser.js.map +1 -1
- package/dist/orm/index.bun.js +90 -80
- package/dist/orm/index.bun.js.map +1 -1
- package/dist/orm/index.d.ts +1434 -1459
- package/dist/orm/index.d.ts.map +1 -1
- package/dist/orm/index.js +112 -130
- package/dist/orm/index.js.map +1 -1
- package/dist/queue/core/index.d.ts +262 -254
- package/dist/queue/core/index.d.ts.map +1 -1
- package/dist/queue/core/index.js +14 -6
- package/dist/queue/core/index.js.map +1 -1
- package/dist/queue/redis/index.d.ts.map +1 -1
- package/dist/react/auth/index.browser.js +108 -0
- package/dist/react/auth/index.browser.js.map +1 -0
- package/dist/react/auth/index.d.ts +100 -0
- package/dist/react/auth/index.d.ts.map +1 -0
- package/dist/react/auth/index.js +145 -0
- package/dist/react/auth/index.js.map +1 -0
- package/dist/react/core/index.d.ts +469 -0
- package/dist/react/core/index.d.ts.map +1 -0
- package/dist/react/core/index.js +464 -0
- package/dist/react/core/index.js.map +1 -0
- package/dist/react/form/index.d.ts +232 -0
- package/dist/react/form/index.d.ts.map +1 -0
- package/dist/react/form/index.js +432 -0
- package/dist/react/form/index.js.map +1 -0
- package/dist/react/head/index.browser.js +423 -0
- package/dist/react/head/index.browser.js.map +1 -0
- package/dist/react/head/index.d.ts +288 -0
- package/dist/react/head/index.d.ts.map +1 -0
- package/dist/react/head/index.js +465 -0
- package/dist/react/head/index.js.map +1 -0
- package/dist/react/i18n/index.d.ts +175 -0
- package/dist/react/i18n/index.d.ts.map +1 -0
- package/dist/react/i18n/index.js +224 -0
- package/dist/react/i18n/index.js.map +1 -0
- package/dist/react/router/index.browser.js +1980 -0
- package/dist/react/router/index.browser.js.map +1 -0
- package/dist/react/router/index.d.ts +2068 -0
- package/dist/react/router/index.d.ts.map +1 -0
- package/dist/react/router/index.js +4932 -0
- package/dist/react/router/index.js.map +1 -0
- package/dist/react/websocket/index.d.ts +117 -0
- package/dist/react/websocket/index.d.ts.map +1 -0
- package/dist/react/websocket/index.js +107 -0
- package/dist/react/websocket/index.js.map +1 -0
- package/dist/redis/index.bun.js +4 -0
- package/dist/redis/index.bun.js.map +1 -1
- package/dist/redis/index.d.ts +127 -130
- package/dist/redis/index.d.ts.map +1 -1
- package/dist/redis/index.js +16 -25
- package/dist/redis/index.js.map +1 -1
- package/dist/retry/index.d.ts +80 -71
- package/dist/retry/index.d.ts.map +1 -1
- package/dist/retry/index.js +11 -2
- package/dist/retry/index.js.map +1 -1
- package/dist/router/index.d.ts +6 -6
- package/dist/router/index.d.ts.map +1 -1
- package/dist/scheduler/index.d.ts +119 -28
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/scheduler/index.js +404 -3
- package/dist/scheduler/index.js.map +1 -1
- package/dist/security/index.d.ts +642 -228
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/index.js +1579 -37
- package/dist/security/index.js.map +1 -1
- package/dist/server/auth/index.d.ts +1141 -111
- package/dist/server/auth/index.d.ts.map +1 -1
- package/dist/server/auth/index.js +1261 -25
- package/dist/server/auth/index.js.map +1 -1
- package/dist/server/cache/index.d.ts +63 -78
- package/dist/server/cache/index.d.ts.map +1 -1
- package/dist/server/cache/index.js +7 -22
- package/dist/server/cache/index.js.map +1 -1
- package/dist/server/compress/index.d.ts +13 -5
- package/dist/server/compress/index.d.ts.map +1 -1
- package/dist/server/compress/index.js +10 -2
- package/dist/server/compress/index.js.map +1 -1
- package/dist/server/cookies/index.d.ts +46 -22
- package/dist/server/cookies/index.d.ts.map +1 -1
- package/dist/server/cookies/index.js +7 -5
- package/dist/server/cookies/index.js.map +1 -1
- package/dist/server/core/index.d.ts +307 -196
- package/dist/server/core/index.d.ts.map +1 -1
- package/dist/server/core/index.js +271 -38
- package/dist/server/core/index.js.map +1 -1
- package/dist/server/cors/index.d.ts +24 -34
- package/dist/server/cors/index.d.ts.map +1 -1
- package/dist/server/cors/index.js +7 -21
- package/dist/server/cors/index.js.map +1 -1
- package/dist/server/health/index.d.ts +25 -19
- package/dist/server/health/index.d.ts.map +1 -1
- package/dist/server/health/index.js +8 -2
- package/dist/server/health/index.js.map +1 -1
- package/dist/server/helmet/index.d.ts +13 -5
- package/dist/server/helmet/index.d.ts.map +1 -1
- package/dist/server/helmet/index.js +11 -3
- package/dist/server/helmet/index.js.map +1 -1
- package/dist/server/links/index.browser.js +9 -1
- package/dist/server/links/index.browser.js.map +1 -1
- package/dist/server/links/index.d.ts +133 -128
- package/dist/server/links/index.d.ts.map +1 -1
- package/dist/server/links/index.js +24 -11
- package/dist/server/links/index.js.map +1 -1
- package/dist/server/metrics/index.d.ts +524 -4
- package/dist/server/metrics/index.d.ts.map +1 -1
- package/dist/server/metrics/index.js +4472 -7
- package/dist/server/metrics/index.js.map +1 -1
- package/dist/server/multipart/index.d.ts +15 -9
- package/dist/server/multipart/index.d.ts.map +1 -1
- package/dist/server/multipart/index.js +9 -3
- package/dist/server/multipart/index.js.map +1 -1
- package/dist/server/proxy/index.d.ts +110 -104
- package/dist/server/proxy/index.d.ts.map +1 -1
- package/dist/server/proxy/index.js +8 -2
- package/dist/server/proxy/index.js.map +1 -1
- package/dist/server/rate-limit/index.d.ts +46 -51
- package/dist/server/rate-limit/index.d.ts.map +1 -1
- package/dist/server/rate-limit/index.js +18 -55
- package/dist/server/rate-limit/index.js.map +1 -1
- package/dist/server/static/index.d.ts +181 -48
- package/dist/server/static/index.d.ts.map +1 -1
- package/dist/server/static/index.js +1848 -5
- package/dist/server/static/index.js.map +1 -1
- package/dist/server/swagger/index.d.ts +348 -53
- package/dist/server/swagger/index.d.ts.map +1 -1
- package/dist/server/swagger/index.js +1849 -6
- package/dist/server/swagger/index.js.map +1 -1
- package/dist/sms/index.d.ts +312 -18
- package/dist/sms/index.d.ts.map +1 -1
- package/dist/sms/index.js +1854 -10
- package/dist/sms/index.js.map +1 -1
- package/dist/system/index.browser.js +496 -0
- package/dist/system/index.browser.js.map +1 -0
- package/dist/system/index.d.ts +1158 -0
- package/dist/system/index.d.ts.map +1 -0
- package/dist/{file → system}/index.js +412 -20
- package/dist/system/index.js.map +1 -0
- package/dist/thread/index.d.ts +82 -73
- package/dist/thread/index.d.ts.map +1 -1
- package/dist/thread/index.js +13 -4
- package/dist/thread/index.js.map +1 -1
- package/dist/topic/core/index.d.ts +330 -323
- package/dist/topic/core/index.d.ts.map +1 -1
- package/dist/topic/core/index.js +12 -5
- package/dist/topic/core/index.js.map +1 -1
- package/dist/topic/redis/index.d.ts +6 -6
- package/dist/topic/redis/index.d.ts.map +1 -1
- package/dist/vite/index.d.ts +163 -5825
- package/dist/vite/index.d.ts.map +1 -1
- package/dist/vite/index.js +130 -477
- package/dist/vite/index.js.map +1 -1
- package/dist/websocket/index.browser.js +3 -3
- package/dist/websocket/index.browser.js.map +1 -1
- package/dist/websocket/index.d.ts +287 -283
- package/dist/websocket/index.d.ts.map +1 -1
- package/dist/websocket/index.js +15 -11
- package/dist/websocket/index.js.map +1 -1
- package/package.json +86 -17
- package/src/api/audits/index.ts +10 -33
- package/src/api/files/__tests__/$bucket.spec.ts +1 -1
- package/src/api/files/controllers/AdminFileStatsController.spec.ts +1 -1
- package/src/api/files/controllers/FileController.spec.ts +1 -1
- package/src/api/files/index.ts +10 -3
- package/src/api/files/jobs/FileJobs.spec.ts +1 -1
- package/src/api/files/services/FileService.spec.ts +1 -1
- package/src/api/jobs/index.ts +10 -3
- package/src/api/keys/controllers/AdminApiKeyController.ts +75 -0
- package/src/api/keys/controllers/ApiKeyController.ts +103 -0
- package/src/api/keys/entities/apiKeyEntity.ts +41 -0
- package/src/api/keys/index.ts +49 -0
- package/src/api/keys/schemas/adminApiKeyQuerySchema.ts +7 -0
- package/src/api/keys/schemas/adminApiKeyResourceSchema.ts +17 -0
- package/src/api/keys/schemas/createApiKeyBodySchema.ts +7 -0
- package/src/api/keys/schemas/createApiKeyResponseSchema.ts +11 -0
- package/src/api/keys/schemas/listApiKeyResponseSchema.ts +15 -0
- package/src/api/keys/schemas/revokeApiKeyParamsSchema.ts +5 -0
- package/src/api/keys/schemas/revokeApiKeyResponseSchema.ts +5 -0
- package/src/api/keys/services/ApiKeyService.spec.ts +553 -0
- package/src/api/keys/services/ApiKeyService.ts +306 -0
- package/src/api/logs/TODO.md +52 -0
- package/src/api/notifications/index.ts +10 -4
- package/src/api/parameters/index.ts +9 -30
- package/src/api/parameters/primitives/$config.ts +12 -4
- package/src/api/parameters/services/ConfigStore.ts +9 -3
- package/src/api/users/__tests__/ApiKeys-integration.spec.ts +1035 -0
- package/src/api/users/__tests__/ApiKeys.spec.ts +401 -0
- package/src/api/users/index.ts +14 -3
- package/src/api/users/primitives/$realm.ts +33 -5
- package/src/api/users/providers/RealmProvider.ts +1 -12
- package/src/api/users/services/SessionService.ts +1 -11
- package/src/api/verifications/controllers/VerificationController.ts +2 -0
- package/src/api/verifications/index.ts +10 -4
- package/src/batch/index.ts +9 -36
- package/src/batch/primitives/$batch.ts +0 -8
- package/src/batch/providers/BatchProvider.ts +29 -2
- package/src/bucket/__tests__/shared.ts +1 -1
- package/src/bucket/index.ts +13 -6
- package/src/bucket/primitives/$bucket.ts +1 -1
- package/src/bucket/providers/LocalFileStorageProvider.ts +1 -1
- package/src/bucket/providers/MemoryFileStorageProvider.ts +1 -1
- package/src/cache/core/__tests__/shared.ts +30 -0
- package/src/cache/core/index.ts +11 -6
- package/src/cache/core/primitives/$cache.spec.ts +5 -0
- package/src/cache/core/providers/CacheProvider.ts +17 -0
- package/src/cache/core/providers/MemoryCacheProvider.ts +300 -1
- package/src/cache/redis/__tests__/cache-redis.spec.ts +5 -0
- package/src/cache/redis/providers/RedisCacheProvider.ts +9 -0
- package/src/cli/apps/AlephaCli.ts +3 -16
- package/src/cli/apps/AlephaPackageBuilderCli.ts +10 -2
- package/src/cli/atoms/appEntryOptions.ts +13 -0
- package/src/cli/atoms/buildOptions.ts +1 -1
- package/src/cli/atoms/changelogOptions.ts +1 -1
- package/src/cli/commands/build.ts +64 -52
- package/src/cli/commands/db.ts +17 -11
- package/src/cli/commands/deploy.ts +1 -1
- package/src/cli/commands/dev.ts +13 -49
- package/src/cli/commands/gen/env.ts +6 -3
- package/src/cli/commands/gen/openapi.ts +5 -2
- package/src/cli/commands/init.spec.ts +544 -0
- package/src/cli/commands/init.ts +101 -58
- package/src/cli/commands/lint.ts +8 -2
- package/src/cli/commands/typecheck.ts +11 -0
- package/src/cli/defineConfig.ts +9 -0
- package/src/cli/index.ts +2 -1
- package/src/cli/providers/AppEntryProvider.ts +131 -0
- package/src/cli/providers/ViteBuildProvider.ts +40 -0
- package/src/cli/providers/ViteDevServerProvider.ts +378 -0
- package/src/cli/services/AlephaCliUtils.ts +39 -93
- package/src/cli/services/PackageManagerUtils.ts +140 -17
- package/src/cli/services/ProjectScaffolder.ts +169 -101
- package/src/cli/services/ViteUtils.ts +82 -0
- package/src/cli/{assets/claudeMd.ts → templates/agentMd.ts} +41 -28
- package/src/cli/{assets → templates}/apiHelloControllerTs.ts +2 -1
- package/src/cli/{assets → templates}/biomeJson.ts +2 -1
- package/src/cli/{assets → templates}/dummySpecTs.ts +2 -1
- package/src/cli/{assets → templates}/editorconfig.ts +2 -1
- package/src/cli/templates/gitignore.ts +39 -0
- package/src/cli/{assets → templates}/mainBrowserTs.ts +2 -1
- package/src/cli/templates/mainCss.ts +33 -0
- package/src/cli/templates/mainServerTs.ts +33 -0
- package/src/cli/{assets → templates}/tsconfigJson.ts +2 -1
- package/src/cli/templates/webAppRouterTs.ts +50 -0
- package/src/cli/templates/webHelloComponentTsx.ts +20 -0
- package/src/command/helpers/Runner.spec.ts +4 -0
- package/src/command/helpers/Runner.ts +3 -21
- package/src/command/index.ts +12 -4
- package/src/command/providers/CliProvider.spec.ts +1067 -0
- package/src/command/providers/CliProvider.ts +203 -40
- package/src/core/Alepha.ts +3 -9
- package/src/core/__tests__/Alepha-start.spec.ts +4 -4
- package/src/core/helpers/jsonSchemaToTypeBox.spec.ts +771 -0
- package/src/core/helpers/jsonSchemaToTypeBox.ts +62 -10
- package/src/core/index.shared.ts +1 -0
- package/src/core/index.ts +20 -0
- package/src/core/primitives/$module.ts +12 -0
- package/src/core/providers/EventManager.spec.ts +0 -71
- package/src/core/providers/EventManager.ts +3 -15
- package/src/core/providers/Json.ts +2 -14
- package/src/core/providers/KeylessJsonSchemaCodec.spec.ts +257 -0
- package/src/core/providers/KeylessJsonSchemaCodec.ts +396 -14
- package/src/core/providers/SchemaValidator.spec.ts +236 -0
- package/src/datetime/index.ts +15 -0
- package/src/email/index.ts +10 -5
- package/src/email/providers/LocalEmailProvider.spec.ts +1 -1
- package/src/email/providers/LocalEmailProvider.ts +1 -1
- package/src/fake/__tests__/keyName.example.ts +1 -1
- package/src/fake/__tests__/keyName.spec.ts +5 -5
- package/src/fake/index.ts +9 -6
- package/src/fake/providers/FakeProvider.spec.ts +258 -40
- package/src/fake/providers/FakeProvider.ts +133 -19
- package/src/lock/core/index.ts +11 -4
- package/src/logger/index.ts +17 -66
- package/src/logger/providers/PrettyFormatterProvider.ts +0 -9
- package/src/mcp/errors/McpError.ts +30 -0
- package/src/mcp/index.ts +13 -27
- package/src/mcp/transports/SseMcpTransport.ts +6 -7
- package/src/orm/__tests__/PostgresProvider.spec.ts +2 -2
- package/src/orm/index.browser.ts +2 -2
- package/src/orm/index.bun.ts +4 -2
- package/src/orm/index.ts +21 -47
- package/src/orm/providers/DrizzleKitProvider.ts +3 -5
- package/src/orm/providers/drivers/BunSqliteProvider.ts +1 -0
- package/src/orm/services/Repository.ts +18 -3
- package/src/queue/core/index.ts +14 -6
- package/src/react/auth/__tests__/$auth.spec.ts +202 -0
- package/src/react/auth/hooks/useAuth.ts +32 -0
- package/src/react/auth/index.browser.ts +13 -0
- package/src/react/auth/index.shared.ts +2 -0
- package/src/react/auth/index.ts +48 -0
- package/src/react/auth/providers/ReactAuthProvider.ts +16 -0
- package/src/react/auth/services/ReactAuth.ts +135 -0
- package/src/react/core/__tests__/Router.spec.tsx +169 -0
- package/src/react/core/components/ClientOnly.tsx +49 -0
- package/src/react/core/components/ErrorBoundary.tsx +73 -0
- package/src/react/core/contexts/AlephaContext.ts +7 -0
- package/src/react/core/contexts/AlephaProvider.tsx +42 -0
- package/src/react/core/hooks/useAction.browser.spec.tsx +569 -0
- package/src/react/core/hooks/useAction.ts +480 -0
- package/src/react/core/hooks/useAlepha.ts +26 -0
- package/src/react/core/hooks/useClient.ts +17 -0
- package/src/react/core/hooks/useEvents.ts +51 -0
- package/src/react/core/hooks/useInject.ts +12 -0
- package/src/react/core/hooks/useStore.ts +52 -0
- package/src/react/core/index.ts +90 -0
- package/src/react/form/components/FormState.tsx +17 -0
- package/src/react/form/errors/FormValidationError.ts +18 -0
- package/src/react/form/hooks/useForm.browser.spec.tsx +366 -0
- package/src/react/form/hooks/useForm.ts +47 -0
- package/src/react/form/hooks/useFormState.ts +130 -0
- package/src/react/form/index.ts +44 -0
- package/src/react/form/services/FormModel.ts +614 -0
- package/src/react/head/helpers/SeoExpander.spec.ts +203 -0
- package/src/react/head/helpers/SeoExpander.ts +142 -0
- package/src/react/head/hooks/useHead.spec.tsx +288 -0
- package/src/react/head/hooks/useHead.ts +62 -0
- package/src/react/head/index.browser.ts +26 -0
- package/src/react/head/index.ts +44 -0
- package/src/react/head/interfaces/Head.ts +105 -0
- package/src/react/head/primitives/$head.ts +25 -0
- package/src/react/head/providers/BrowserHeadProvider.browser.spec.ts +196 -0
- package/src/react/head/providers/BrowserHeadProvider.ts +212 -0
- package/src/react/head/providers/HeadProvider.ts +168 -0
- package/src/react/head/providers/ServerHeadProvider.ts +31 -0
- package/src/react/i18n/__tests__/integration.spec.tsx +239 -0
- package/src/react/i18n/components/Localize.spec.tsx +357 -0
- package/src/react/i18n/components/Localize.tsx +35 -0
- package/src/react/i18n/hooks/useI18n.browser.spec.tsx +438 -0
- package/src/react/i18n/hooks/useI18n.ts +18 -0
- package/src/react/i18n/index.ts +41 -0
- package/src/react/i18n/primitives/$dictionary.ts +69 -0
- package/src/react/i18n/providers/I18nProvider.spec.ts +389 -0
- package/src/react/i18n/providers/I18nProvider.ts +278 -0
- package/src/react/router/__tests__/page-head-browser.browser.spec.ts +95 -0
- package/src/react/router/__tests__/page-head.spec.ts +48 -0
- package/src/react/router/__tests__/seo-head.spec.ts +125 -0
- package/src/react/router/atoms/ssrManifestAtom.ts +58 -0
- package/src/react/router/components/ErrorViewer.tsx +872 -0
- package/src/react/router/components/Link.tsx +23 -0
- package/src/react/router/components/NestedView.tsx +223 -0
- package/src/react/router/components/NotFound.tsx +30 -0
- package/src/react/router/constants/PAGE_PRELOAD_KEY.ts +6 -0
- package/src/react/router/contexts/RouterLayerContext.ts +12 -0
- package/src/react/router/errors/Redirection.ts +28 -0
- package/src/react/router/hooks/useActive.ts +52 -0
- package/src/react/router/hooks/useQueryParams.ts +63 -0
- package/src/react/router/hooks/useRouter.ts +20 -0
- package/src/react/router/hooks/useRouterState.ts +11 -0
- package/src/react/router/index.browser.ts +45 -0
- package/src/react/router/index.shared.ts +19 -0
- package/src/react/router/index.ts +142 -0
- package/src/react/router/primitives/$page.browser.spec.tsx +851 -0
- package/src/react/router/primitives/$page.spec.tsx +708 -0
- package/src/react/router/primitives/$page.ts +497 -0
- package/src/react/router/providers/ReactBrowserProvider.ts +309 -0
- package/src/react/router/providers/ReactBrowserRendererProvider.ts +25 -0
- package/src/react/router/providers/ReactBrowserRouterProvider.ts +168 -0
- package/src/react/router/providers/ReactPageProvider.ts +726 -0
- package/src/react/router/providers/ReactServerProvider.spec.tsx +316 -0
- package/src/react/router/providers/ReactServerProvider.ts +558 -0
- package/src/react/router/providers/ReactServerTemplateProvider.ts +979 -0
- package/src/react/router/providers/SSRManifestProvider.ts +334 -0
- package/src/react/router/services/ReactPageServerService.ts +48 -0
- package/src/react/router/services/ReactPageService.ts +27 -0
- package/src/react/router/services/ReactRouter.ts +262 -0
- package/src/react/websocket/hooks/useRoom.tsx +242 -0
- package/src/react/websocket/index.ts +7 -0
- package/src/redis/__tests__/redis.spec.ts +13 -0
- package/src/redis/index.ts +9 -25
- package/src/redis/providers/BunRedisProvider.ts +9 -0
- package/src/redis/providers/NodeRedisProvider.ts +8 -0
- package/src/redis/providers/RedisProvider.ts +16 -0
- package/src/retry/index.ts +11 -2
- package/src/router/index.ts +15 -0
- package/src/scheduler/index.ts +11 -2
- package/src/security/__tests__/BasicAuth.spec.ts +2 -0
- package/src/security/__tests__/ServerSecurityProvider.spec.ts +13 -5
- package/src/security/index.ts +15 -10
- package/src/security/interfaces/IssuerResolver.ts +27 -0
- package/src/security/primitives/$issuer.ts +55 -0
- package/src/security/providers/SecurityProvider.ts +179 -0
- package/src/security/providers/ServerBasicAuthProvider.ts +6 -2
- package/src/security/providers/ServerSecurityProvider.ts +36 -22
- package/src/server/auth/index.ts +12 -7
- package/src/server/cache/index.ts +7 -22
- package/src/server/compress/index.ts +10 -2
- package/src/server/cookies/index.ts +7 -5
- package/src/server/cookies/primitives/$cookie.ts +33 -11
- package/src/server/core/index.ts +17 -7
- package/src/server/core/interfaces/ServerRequest.ts +83 -1
- package/src/server/core/primitives/$action.spec.ts +1 -1
- package/src/server/core/primitives/$action.ts +8 -3
- package/src/server/core/providers/BunHttpServerProvider.ts +1 -1
- package/src/server/core/providers/NodeHttpServerProvider.spec.ts +125 -0
- package/src/server/core/providers/NodeHttpServerProvider.ts +77 -22
- package/src/server/core/providers/ServerLoggerProvider.ts +2 -2
- package/src/server/core/providers/ServerProvider.ts +9 -12
- package/src/server/core/services/ServerRequestParser.spec.ts +520 -0
- package/src/server/core/services/ServerRequestParser.ts +306 -13
- package/src/server/cors/index.ts +7 -21
- package/src/server/cors/primitives/$cors.ts +6 -2
- package/src/server/health/index.ts +8 -2
- package/src/server/helmet/index.ts +11 -3
- package/src/server/links/atoms/apiLinksAtom.ts +7 -0
- package/src/server/links/index.browser.ts +2 -0
- package/src/server/links/index.ts +13 -6
- package/src/server/metrics/index.ts +10 -3
- package/src/server/multipart/index.ts +9 -3
- package/src/server/proxy/index.ts +8 -2
- package/src/server/rate-limit/index.ts +21 -25
- package/src/server/rate-limit/primitives/$rateLimit.ts +6 -2
- package/src/server/rate-limit/providers/ServerRateLimitProvider.spec.ts +38 -14
- package/src/server/rate-limit/providers/ServerRateLimitProvider.ts +22 -56
- package/src/server/static/index.ts +8 -2
- package/src/server/static/providers/ServerStaticProvider.ts +1 -1
- package/src/server/swagger/index.ts +9 -4
- package/src/server/swagger/providers/ServerSwaggerProvider.ts +1 -1
- package/src/sms/index.ts +9 -5
- package/src/sms/providers/LocalSmsProvider.spec.ts +1 -1
- package/src/sms/providers/LocalSmsProvider.ts +1 -1
- package/src/system/index.browser.ts +11 -0
- package/src/system/index.ts +62 -0
- package/src/{file → system}/providers/FileSystemProvider.ts +16 -0
- package/src/{file → system}/providers/MemoryFileSystemProvider.ts +116 -3
- package/src/system/providers/MemoryShellProvider.ts +164 -0
- package/src/{file → system}/providers/NodeFileSystemProvider.spec.ts +2 -2
- package/src/{file → system}/providers/NodeFileSystemProvider.ts +36 -0
- package/src/system/providers/NodeShellProvider.ts +184 -0
- package/src/system/providers/ShellProvider.ts +74 -0
- package/src/{file → system}/services/FileDetector.spec.ts +2 -2
- package/src/thread/index.ts +11 -2
- package/src/topic/core/index.ts +12 -5
- package/src/vite/index.ts +3 -2
- package/src/vite/tasks/buildClient.ts +2 -8
- package/src/vite/tasks/buildServer.ts +84 -21
- package/src/vite/tasks/copyAssets.ts +5 -4
- package/src/vite/tasks/generateSitemap.ts +64 -23
- package/src/vite/tasks/index.ts +0 -2
- package/src/vite/tasks/prerenderPages.ts +49 -24
- package/src/websocket/index.ts +12 -8
- package/dist/file/index.d.ts +0 -839
- package/dist/file/index.d.ts.map +0 -1
- package/dist/file/index.js.map +0 -1
- package/src/cli/assets/indexHtml.ts +0 -15
- package/src/cli/assets/mainServerTs.ts +0 -24
- package/src/cli/assets/webAppRouterTs.ts +0 -15
- package/src/cli/assets/webHelloComponentTsx.ts +0 -16
- package/src/cli/commands/format.ts +0 -23
- package/src/file/index.ts +0 -43
- package/src/vite/helpers/boot.ts +0 -117
- package/src/vite/plugins/viteAlephaDev.ts +0 -177
- package/src/vite/tasks/devServer.ts +0 -71
- package/src/vite/tasks/runAlepha.ts +0 -270
- /package/dist/orm/{chunk-DtkW-qnP.js → chunk-DH6iiROE.js} +0 -0
- /package/src/cli/{assets → templates}/apiIndexTs.ts +0 -0
- /package/src/cli/{assets → templates}/webIndexTs.ts +0 -0
- /package/src/{file → system}/errors/FileError.ts +0 -0
- /package/src/{file → system}/services/FileDetector.ts +0 -0
package/dist/command/index.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { $atom, $env, $hook, $inject, $module, $use, Alepha, AlephaError, KIND, Primitive, TypeBoxError, createPrimitive, t } from "alepha";
|
|
1
|
+
import { $atom, $env, $hook, $inject, $module, $use, Alepha, AlephaError, Json, KIND, Primitive, TypeBoxError, createPrimitive, isFileLike, t } from "alepha";
|
|
2
2
|
import { stdin, stdout } from "node:process";
|
|
3
3
|
import { createInterface } from "node:readline/promises";
|
|
4
4
|
import { $logger } from "alepha/logger";
|
|
5
5
|
import * as fs from "node:fs/promises";
|
|
6
|
-
import { cp, glob, readFile, rm } from "node:fs/promises";
|
|
6
|
+
import { access, copyFile, cp, glob, mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { DateTimeProvider } from "alepha/datetime";
|
|
9
|
-
import {
|
|
9
|
+
import { createReadStream } from "node:fs";
|
|
10
|
+
import { PassThrough, Readable } from "node:stream";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { exec, spawn } from "node:child_process";
|
|
10
13
|
|
|
11
14
|
//#region ../../src/command/helpers/Asker.ts
|
|
12
15
|
var Asker = class {
|
|
@@ -225,6 +228,1841 @@ var PrettyPrint = class {
|
|
|
225
228
|
}
|
|
226
229
|
};
|
|
227
230
|
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region ../../src/system/providers/FileSystemProvider.ts
|
|
233
|
+
/**
|
|
234
|
+
* FileSystem interface providing utilities for working with files.
|
|
235
|
+
*/
|
|
236
|
+
var FileSystemProvider = class {};
|
|
237
|
+
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region ../../src/system/providers/MemoryFileSystemProvider.ts
|
|
240
|
+
/**
|
|
241
|
+
* In-memory implementation of FileSystemProvider for testing.
|
|
242
|
+
*
|
|
243
|
+
* This provider stores all files and directories in memory, making it ideal for
|
|
244
|
+
* unit tests that need to verify file operations without touching the real file system.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* // In tests, substitute the real FileSystemProvider with MemoryFileSystemProvider
|
|
249
|
+
* const alepha = Alepha.create().with({
|
|
250
|
+
* provide: FileSystemProvider,
|
|
251
|
+
* use: MemoryFileSystemProvider,
|
|
252
|
+
* });
|
|
253
|
+
*
|
|
254
|
+
* // Run code that uses FileSystemProvider
|
|
255
|
+
* const service = alepha.inject(MyService);
|
|
256
|
+
* await service.saveFile("test.txt", "Hello World");
|
|
257
|
+
*
|
|
258
|
+
* // Verify the file was written
|
|
259
|
+
* const memoryFs = alepha.inject(MemoryFileSystemProvider);
|
|
260
|
+
* expect(memoryFs.files.get("test.txt")?.toString()).toBe("Hello World");
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
var MemoryFileSystemProvider = class {
|
|
264
|
+
json = $inject(Json);
|
|
265
|
+
/**
|
|
266
|
+
* In-memory storage for files (path -> content)
|
|
267
|
+
*/
|
|
268
|
+
files = /* @__PURE__ */ new Map();
|
|
269
|
+
/**
|
|
270
|
+
* In-memory storage for directories
|
|
271
|
+
*/
|
|
272
|
+
directories = /* @__PURE__ */ new Set();
|
|
273
|
+
/**
|
|
274
|
+
* Track mkdir calls for test assertions
|
|
275
|
+
*/
|
|
276
|
+
mkdirCalls = [];
|
|
277
|
+
/**
|
|
278
|
+
* Track writeFile calls for test assertions
|
|
279
|
+
*/
|
|
280
|
+
writeFileCalls = [];
|
|
281
|
+
/**
|
|
282
|
+
* Track readFile calls for test assertions
|
|
283
|
+
*/
|
|
284
|
+
readFileCalls = [];
|
|
285
|
+
/**
|
|
286
|
+
* Track rm calls for test assertions
|
|
287
|
+
*/
|
|
288
|
+
rmCalls = [];
|
|
289
|
+
/**
|
|
290
|
+
* Track join calls for test assertions
|
|
291
|
+
*/
|
|
292
|
+
joinCalls = [];
|
|
293
|
+
/**
|
|
294
|
+
* Error to throw on mkdir (for testing error handling)
|
|
295
|
+
*/
|
|
296
|
+
mkdirError = null;
|
|
297
|
+
/**
|
|
298
|
+
* Error to throw on writeFile (for testing error handling)
|
|
299
|
+
*/
|
|
300
|
+
writeFileError = null;
|
|
301
|
+
/**
|
|
302
|
+
* Error to throw on readFile (for testing error handling)
|
|
303
|
+
*/
|
|
304
|
+
readFileError = null;
|
|
305
|
+
constructor(options = {}) {
|
|
306
|
+
this.mkdirError = options.mkdirError ?? null;
|
|
307
|
+
this.writeFileError = options.writeFileError ?? null;
|
|
308
|
+
this.readFileError = options.readFileError ?? null;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Join path segments using forward slashes.
|
|
312
|
+
* Uses Node's path.join for proper normalization (handles .. and .)
|
|
313
|
+
*/
|
|
314
|
+
join(...paths) {
|
|
315
|
+
this.joinCalls.push(paths);
|
|
316
|
+
return join(...paths);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Create a FileLike object from various sources.
|
|
320
|
+
*/
|
|
321
|
+
createFile(options) {
|
|
322
|
+
if ("path" in options) {
|
|
323
|
+
const filePath = options.path;
|
|
324
|
+
const buffer = this.files.get(filePath);
|
|
325
|
+
if (buffer === void 0) throw new Error(`ENOENT: no such file or directory, open '${filePath}'`);
|
|
326
|
+
return {
|
|
327
|
+
name: options.name ?? filePath.split("/").pop() ?? "file",
|
|
328
|
+
type: options.type ?? "application/octet-stream",
|
|
329
|
+
size: buffer.byteLength,
|
|
330
|
+
lastModified: Date.now(),
|
|
331
|
+
stream: () => {
|
|
332
|
+
throw new Error("Stream not implemented in MemoryFileSystemProvider");
|
|
333
|
+
},
|
|
334
|
+
arrayBuffer: async () => buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
|
|
335
|
+
text: async () => buffer.toString("utf-8")
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
if ("buffer" in options) {
|
|
339
|
+
const buffer = options.buffer;
|
|
340
|
+
return {
|
|
341
|
+
name: options.name ?? "file",
|
|
342
|
+
type: options.type ?? "application/octet-stream",
|
|
343
|
+
size: buffer.byteLength,
|
|
344
|
+
lastModified: Date.now(),
|
|
345
|
+
stream: () => {
|
|
346
|
+
throw new Error("Stream not implemented in MemoryFileSystemProvider");
|
|
347
|
+
},
|
|
348
|
+
arrayBuffer: async () => buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
|
|
349
|
+
text: async () => buffer.toString("utf-8")
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if ("text" in options) {
|
|
353
|
+
const buffer = Buffer.from(options.text, "utf-8");
|
|
354
|
+
return {
|
|
355
|
+
name: options.name ?? "file.txt",
|
|
356
|
+
type: options.type ?? "text/plain",
|
|
357
|
+
size: buffer.byteLength,
|
|
358
|
+
lastModified: Date.now(),
|
|
359
|
+
stream: () => {
|
|
360
|
+
throw new Error("Stream not implemented in MemoryFileSystemProvider");
|
|
361
|
+
},
|
|
362
|
+
arrayBuffer: async () => buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
|
|
363
|
+
text: async () => options.text
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
throw new Error("MemoryFileSystemProvider.createFile: unsupported options. Only buffer and text are supported.");
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Remove a file or directory from memory.
|
|
370
|
+
*/
|
|
371
|
+
async rm(path, options) {
|
|
372
|
+
this.rmCalls.push({
|
|
373
|
+
path,
|
|
374
|
+
options
|
|
375
|
+
});
|
|
376
|
+
if (!(this.files.has(path) || this.directories.has(path)) && !options?.force) throw new Error(`ENOENT: no such file or directory, rm '${path}'`);
|
|
377
|
+
if (this.directories.has(path)) if (options?.recursive) {
|
|
378
|
+
this.directories.delete(path);
|
|
379
|
+
for (const filePath of this.files.keys()) if (filePath.startsWith(`${path}/`)) this.files.delete(filePath);
|
|
380
|
+
for (const dirPath of this.directories) if (dirPath.startsWith(`${path}/`)) this.directories.delete(dirPath);
|
|
381
|
+
} else throw new Error(`EISDIR: illegal operation on a directory, rm '${path}'`);
|
|
382
|
+
else this.files.delete(path);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Copy a file or directory in memory.
|
|
386
|
+
*/
|
|
387
|
+
async cp(src, dest, options) {
|
|
388
|
+
if (this.directories.has(src)) {
|
|
389
|
+
if (!options?.recursive) throw new Error(`Cannot copy directory without recursive option: ${src}`);
|
|
390
|
+
this.directories.add(dest);
|
|
391
|
+
for (const [filePath, content] of this.files) if (filePath.startsWith(`${src}/`)) {
|
|
392
|
+
const newPath = filePath.replace(src, dest);
|
|
393
|
+
this.files.set(newPath, Buffer.from(content));
|
|
394
|
+
}
|
|
395
|
+
} else if (this.files.has(src)) {
|
|
396
|
+
const content = this.files.get(src);
|
|
397
|
+
this.files.set(dest, Buffer.from(content));
|
|
398
|
+
} else throw new Error(`ENOENT: no such file or directory, cp '${src}'`);
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Move/rename a file or directory in memory.
|
|
402
|
+
*/
|
|
403
|
+
async mv(src, dest) {
|
|
404
|
+
if (this.directories.has(src)) {
|
|
405
|
+
this.directories.delete(src);
|
|
406
|
+
this.directories.add(dest);
|
|
407
|
+
for (const [filePath, content] of this.files) if (filePath.startsWith(`${src}/`)) {
|
|
408
|
+
const newPath = filePath.replace(src, dest);
|
|
409
|
+
this.files.delete(filePath);
|
|
410
|
+
this.files.set(newPath, content);
|
|
411
|
+
}
|
|
412
|
+
} else if (this.files.has(src)) {
|
|
413
|
+
const content = this.files.get(src);
|
|
414
|
+
this.files.delete(src);
|
|
415
|
+
this.files.set(dest, content);
|
|
416
|
+
} else throw new Error(`ENOENT: no such file or directory, mv '${src}'`);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Create a directory in memory.
|
|
420
|
+
*/
|
|
421
|
+
async mkdir(path, options) {
|
|
422
|
+
this.mkdirCalls.push({
|
|
423
|
+
path,
|
|
424
|
+
options
|
|
425
|
+
});
|
|
426
|
+
if (this.mkdirError) throw this.mkdirError;
|
|
427
|
+
if (this.directories.has(path) && !options?.recursive) throw new Error(`EEXIST: file already exists, mkdir '${path}'`);
|
|
428
|
+
this.directories.add(path);
|
|
429
|
+
if (options?.recursive) {
|
|
430
|
+
const parts = path.split("/").filter(Boolean);
|
|
431
|
+
let current = "";
|
|
432
|
+
for (const part of parts) {
|
|
433
|
+
current = current ? `${current}/${part}` : part;
|
|
434
|
+
this.directories.add(current);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* List files in a directory.
|
|
440
|
+
*/
|
|
441
|
+
async ls(path, options) {
|
|
442
|
+
const normalizedPath = path.replace(/\/$/, "");
|
|
443
|
+
const entries = /* @__PURE__ */ new Set();
|
|
444
|
+
for (const filePath of this.files.keys()) if (filePath.startsWith(`${normalizedPath}/`)) {
|
|
445
|
+
const relativePath = filePath.slice(normalizedPath.length + 1);
|
|
446
|
+
const parts = relativePath.split("/");
|
|
447
|
+
if (options?.recursive) entries.add(relativePath);
|
|
448
|
+
else entries.add(parts[0]);
|
|
449
|
+
}
|
|
450
|
+
for (const dirPath of this.directories) if (dirPath.startsWith(`${normalizedPath}/`) && dirPath !== normalizedPath) {
|
|
451
|
+
const relativePath = dirPath.slice(normalizedPath.length + 1);
|
|
452
|
+
const parts = relativePath.split("/");
|
|
453
|
+
if (options?.recursive) entries.add(relativePath);
|
|
454
|
+
else if (parts.length === 1) entries.add(parts[0]);
|
|
455
|
+
}
|
|
456
|
+
let result = Array.from(entries);
|
|
457
|
+
if (!options?.hidden) result = result.filter((entry) => !entry.startsWith("."));
|
|
458
|
+
return result.sort();
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Check if a file or directory exists in memory.
|
|
462
|
+
*/
|
|
463
|
+
async exists(path) {
|
|
464
|
+
return this.files.has(path) || this.directories.has(path);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Read a file from memory.
|
|
468
|
+
*/
|
|
469
|
+
async readFile(path) {
|
|
470
|
+
this.readFileCalls.push(path);
|
|
471
|
+
if (this.readFileError) throw this.readFileError;
|
|
472
|
+
const content = this.files.get(path);
|
|
473
|
+
if (!content) throw new Error(`ENOENT: no such file or directory, open '${path}'`);
|
|
474
|
+
return content;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Read a file from memory as text.
|
|
478
|
+
*/
|
|
479
|
+
async readTextFile(path) {
|
|
480
|
+
return (await this.readFile(path)).toString("utf-8");
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Read a file from memory as JSON.
|
|
484
|
+
*/
|
|
485
|
+
async readJsonFile(path) {
|
|
486
|
+
const text = await this.readTextFile(path);
|
|
487
|
+
return this.json.parse(text);
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Write a file to memory.
|
|
491
|
+
*/
|
|
492
|
+
async writeFile(path, data) {
|
|
493
|
+
const dataStr = typeof data === "string" ? data : data instanceof Buffer || data instanceof Uint8Array ? data.toString("utf-8") : await data.text();
|
|
494
|
+
this.writeFileCalls.push({
|
|
495
|
+
path,
|
|
496
|
+
data: dataStr
|
|
497
|
+
});
|
|
498
|
+
if (this.writeFileError) throw this.writeFileError;
|
|
499
|
+
const buffer = typeof data === "string" ? Buffer.from(data, "utf-8") : data instanceof Buffer ? data : data instanceof Uint8Array ? Buffer.from(data) : Buffer.from(await data.text(), "utf-8");
|
|
500
|
+
this.files.set(path, buffer);
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Reset all in-memory state (useful between tests).
|
|
504
|
+
*/
|
|
505
|
+
reset() {
|
|
506
|
+
this.files.clear();
|
|
507
|
+
this.directories.clear();
|
|
508
|
+
this.mkdirCalls = [];
|
|
509
|
+
this.writeFileCalls = [];
|
|
510
|
+
this.readFileCalls = [];
|
|
511
|
+
this.rmCalls = [];
|
|
512
|
+
this.joinCalls = [];
|
|
513
|
+
this.mkdirError = null;
|
|
514
|
+
this.writeFileError = null;
|
|
515
|
+
this.readFileError = null;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Check if a file was written during the test.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```typescript
|
|
522
|
+
* expect(fs.wasWritten("/project/tsconfig.json")).toBe(true);
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
wasWritten(path) {
|
|
526
|
+
return this.writeFileCalls.some((call) => call.path === path);
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Check if a file was written with content matching a pattern.
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```typescript
|
|
533
|
+
* expect(fs.wasWrittenMatching("/project/tsconfig.json", /extends/)).toBe(true);
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
wasWrittenMatching(path, pattern) {
|
|
537
|
+
const call = this.writeFileCalls.find((c) => c.path === path);
|
|
538
|
+
return call ? pattern.test(call.data) : false;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Check if a file was read during the test.
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```typescript
|
|
545
|
+
* expect(fs.wasRead("/project/package.json")).toBe(true);
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
wasRead(path) {
|
|
549
|
+
return this.readFileCalls.includes(path);
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Check if a file was deleted during the test.
|
|
553
|
+
*
|
|
554
|
+
* @example
|
|
555
|
+
* ```typescript
|
|
556
|
+
* expect(fs.wasDeleted("/project/old-file.txt")).toBe(true);
|
|
557
|
+
* ```
|
|
558
|
+
*/
|
|
559
|
+
wasDeleted(path) {
|
|
560
|
+
return this.rmCalls.some((call) => call.path === path);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Get the content of a file as a string (convenience method for testing).
|
|
564
|
+
*/
|
|
565
|
+
getFileContent(path) {
|
|
566
|
+
return this.files.get(path)?.toString("utf-8");
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
//#endregion
|
|
571
|
+
//#region ../../src/system/providers/MemoryShellProvider.ts
|
|
572
|
+
/**
|
|
573
|
+
* In-memory implementation of ShellProvider for testing.
|
|
574
|
+
*
|
|
575
|
+
* Records all commands that would be executed without actually running them.
|
|
576
|
+
* Can be configured to return specific outputs or throw errors for testing.
|
|
577
|
+
*
|
|
578
|
+
* @example
|
|
579
|
+
* ```typescript
|
|
580
|
+
* // In tests, substitute the real ShellProvider with MemoryShellProvider
|
|
581
|
+
* const alepha = Alepha.create().with({
|
|
582
|
+
* provide: ShellProvider,
|
|
583
|
+
* use: MemoryShellProvider,
|
|
584
|
+
* });
|
|
585
|
+
*
|
|
586
|
+
* // Configure mock behavior
|
|
587
|
+
* const shell = alepha.inject(MemoryShellProvider);
|
|
588
|
+
* shell.configure({
|
|
589
|
+
* outputs: { "echo hello": "hello\n" },
|
|
590
|
+
* errors: { "failing-cmd": "Command failed" },
|
|
591
|
+
* });
|
|
592
|
+
*
|
|
593
|
+
* // Or use the fluent API
|
|
594
|
+
* shell.outputs.set("another-cmd", "output");
|
|
595
|
+
* shell.errors.set("another-error", "Error message");
|
|
596
|
+
*
|
|
597
|
+
* // Run code that uses ShellProvider
|
|
598
|
+
* const service = alepha.inject(MyService);
|
|
599
|
+
* await service.doSomething();
|
|
600
|
+
*
|
|
601
|
+
* // Verify commands were called
|
|
602
|
+
* expect(shell.calls).toHaveLength(2);
|
|
603
|
+
* expect(shell.calls[0].command).toBe("yarn install");
|
|
604
|
+
* ```
|
|
605
|
+
*/
|
|
606
|
+
var MemoryShellProvider = class {
|
|
607
|
+
/**
|
|
608
|
+
* All recorded shell calls.
|
|
609
|
+
*/
|
|
610
|
+
calls = [];
|
|
611
|
+
/**
|
|
612
|
+
* Simulated outputs for specific commands.
|
|
613
|
+
*/
|
|
614
|
+
outputs = /* @__PURE__ */ new Map();
|
|
615
|
+
/**
|
|
616
|
+
* Commands that should throw an error.
|
|
617
|
+
*/
|
|
618
|
+
errors = /* @__PURE__ */ new Map();
|
|
619
|
+
/**
|
|
620
|
+
* Commands considered installed in the system PATH.
|
|
621
|
+
*/
|
|
622
|
+
installedCommands = /* @__PURE__ */ new Set();
|
|
623
|
+
/**
|
|
624
|
+
* Configure the mock with predefined outputs, errors, and installed commands.
|
|
625
|
+
*/
|
|
626
|
+
configure(options) {
|
|
627
|
+
if (options.outputs) for (const [cmd, output] of Object.entries(options.outputs)) this.outputs.set(cmd, output);
|
|
628
|
+
if (options.errors) for (const [cmd, error] of Object.entries(options.errors)) this.errors.set(cmd, error);
|
|
629
|
+
if (options.installedCommands) for (const cmd of options.installedCommands) this.installedCommands.add(cmd);
|
|
630
|
+
return this;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Record command and return simulated output.
|
|
634
|
+
*/
|
|
635
|
+
async run(command, options = {}) {
|
|
636
|
+
this.calls.push({
|
|
637
|
+
command,
|
|
638
|
+
options
|
|
639
|
+
});
|
|
640
|
+
const errorMsg = this.errors.get(command);
|
|
641
|
+
if (errorMsg) throw new Error(errorMsg);
|
|
642
|
+
return this.outputs.get(command) ?? "";
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Check if a specific command was called.
|
|
646
|
+
*/
|
|
647
|
+
wasCalled(command) {
|
|
648
|
+
return this.calls.some((call) => call.command === command);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Check if a command matching a pattern was called.
|
|
652
|
+
*/
|
|
653
|
+
wasCalledMatching(pattern) {
|
|
654
|
+
return this.calls.some((call) => pattern.test(call.command));
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Get all calls matching a pattern.
|
|
658
|
+
*/
|
|
659
|
+
getCallsMatching(pattern) {
|
|
660
|
+
return this.calls.filter((call) => pattern.test(call.command));
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Check if a command is installed.
|
|
664
|
+
*/
|
|
665
|
+
async isInstalled(command) {
|
|
666
|
+
return this.installedCommands.has(command);
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Reset all recorded state.
|
|
670
|
+
*/
|
|
671
|
+
reset() {
|
|
672
|
+
this.calls = [];
|
|
673
|
+
this.outputs.clear();
|
|
674
|
+
this.errors.clear();
|
|
675
|
+
this.installedCommands.clear();
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
//#endregion
|
|
680
|
+
//#region ../../src/system/services/FileDetector.ts
|
|
681
|
+
/**
|
|
682
|
+
* Service for detecting file types and getting content types.
|
|
683
|
+
*
|
|
684
|
+
* @example
|
|
685
|
+
* ```typescript
|
|
686
|
+
* const detector = alepha.inject(FileDetector);
|
|
687
|
+
*
|
|
688
|
+
* // Get content type from filename
|
|
689
|
+
* const mimeType = detector.getContentType("image.png"); // "image/png"
|
|
690
|
+
*
|
|
691
|
+
* // Detect file type by magic bytes
|
|
692
|
+
* const stream = createReadStream('image.png');
|
|
693
|
+
* const result = await detector.detectFileType(stream, 'image.png');
|
|
694
|
+
* console.log(result.mimeType); // 'image/png'
|
|
695
|
+
* console.log(result.verified); // true if magic bytes match
|
|
696
|
+
* ```
|
|
697
|
+
*/
|
|
698
|
+
var FileDetector = class FileDetector {
|
|
699
|
+
/**
|
|
700
|
+
* Magic byte signatures for common file formats.
|
|
701
|
+
* Each signature is represented as an array of bytes or null (wildcard).
|
|
702
|
+
*/
|
|
703
|
+
static MAGIC_BYTES = {
|
|
704
|
+
png: [{
|
|
705
|
+
signature: [
|
|
706
|
+
137,
|
|
707
|
+
80,
|
|
708
|
+
78,
|
|
709
|
+
71,
|
|
710
|
+
13,
|
|
711
|
+
10,
|
|
712
|
+
26,
|
|
713
|
+
10
|
|
714
|
+
],
|
|
715
|
+
mimeType: "image/png"
|
|
716
|
+
}],
|
|
717
|
+
jpg: [
|
|
718
|
+
{
|
|
719
|
+
signature: [
|
|
720
|
+
255,
|
|
721
|
+
216,
|
|
722
|
+
255,
|
|
723
|
+
224
|
|
724
|
+
],
|
|
725
|
+
mimeType: "image/jpeg"
|
|
726
|
+
},
|
|
727
|
+
{
|
|
728
|
+
signature: [
|
|
729
|
+
255,
|
|
730
|
+
216,
|
|
731
|
+
255,
|
|
732
|
+
225
|
|
733
|
+
],
|
|
734
|
+
mimeType: "image/jpeg"
|
|
735
|
+
},
|
|
736
|
+
{
|
|
737
|
+
signature: [
|
|
738
|
+
255,
|
|
739
|
+
216,
|
|
740
|
+
255,
|
|
741
|
+
226
|
|
742
|
+
],
|
|
743
|
+
mimeType: "image/jpeg"
|
|
744
|
+
},
|
|
745
|
+
{
|
|
746
|
+
signature: [
|
|
747
|
+
255,
|
|
748
|
+
216,
|
|
749
|
+
255,
|
|
750
|
+
227
|
|
751
|
+
],
|
|
752
|
+
mimeType: "image/jpeg"
|
|
753
|
+
},
|
|
754
|
+
{
|
|
755
|
+
signature: [
|
|
756
|
+
255,
|
|
757
|
+
216,
|
|
758
|
+
255,
|
|
759
|
+
232
|
|
760
|
+
],
|
|
761
|
+
mimeType: "image/jpeg"
|
|
762
|
+
}
|
|
763
|
+
],
|
|
764
|
+
jpeg: [
|
|
765
|
+
{
|
|
766
|
+
signature: [
|
|
767
|
+
255,
|
|
768
|
+
216,
|
|
769
|
+
255,
|
|
770
|
+
224
|
|
771
|
+
],
|
|
772
|
+
mimeType: "image/jpeg"
|
|
773
|
+
},
|
|
774
|
+
{
|
|
775
|
+
signature: [
|
|
776
|
+
255,
|
|
777
|
+
216,
|
|
778
|
+
255,
|
|
779
|
+
225
|
|
780
|
+
],
|
|
781
|
+
mimeType: "image/jpeg"
|
|
782
|
+
},
|
|
783
|
+
{
|
|
784
|
+
signature: [
|
|
785
|
+
255,
|
|
786
|
+
216,
|
|
787
|
+
255,
|
|
788
|
+
226
|
|
789
|
+
],
|
|
790
|
+
mimeType: "image/jpeg"
|
|
791
|
+
},
|
|
792
|
+
{
|
|
793
|
+
signature: [
|
|
794
|
+
255,
|
|
795
|
+
216,
|
|
796
|
+
255,
|
|
797
|
+
227
|
|
798
|
+
],
|
|
799
|
+
mimeType: "image/jpeg"
|
|
800
|
+
},
|
|
801
|
+
{
|
|
802
|
+
signature: [
|
|
803
|
+
255,
|
|
804
|
+
216,
|
|
805
|
+
255,
|
|
806
|
+
232
|
|
807
|
+
],
|
|
808
|
+
mimeType: "image/jpeg"
|
|
809
|
+
}
|
|
810
|
+
],
|
|
811
|
+
gif: [{
|
|
812
|
+
signature: [
|
|
813
|
+
71,
|
|
814
|
+
73,
|
|
815
|
+
70,
|
|
816
|
+
56,
|
|
817
|
+
55,
|
|
818
|
+
97
|
|
819
|
+
],
|
|
820
|
+
mimeType: "image/gif"
|
|
821
|
+
}, {
|
|
822
|
+
signature: [
|
|
823
|
+
71,
|
|
824
|
+
73,
|
|
825
|
+
70,
|
|
826
|
+
56,
|
|
827
|
+
57,
|
|
828
|
+
97
|
|
829
|
+
],
|
|
830
|
+
mimeType: "image/gif"
|
|
831
|
+
}],
|
|
832
|
+
webp: [{
|
|
833
|
+
signature: [
|
|
834
|
+
82,
|
|
835
|
+
73,
|
|
836
|
+
70,
|
|
837
|
+
70,
|
|
838
|
+
null,
|
|
839
|
+
null,
|
|
840
|
+
null,
|
|
841
|
+
null,
|
|
842
|
+
87,
|
|
843
|
+
69,
|
|
844
|
+
66,
|
|
845
|
+
80
|
|
846
|
+
],
|
|
847
|
+
mimeType: "image/webp"
|
|
848
|
+
}],
|
|
849
|
+
bmp: [{
|
|
850
|
+
signature: [66, 77],
|
|
851
|
+
mimeType: "image/bmp"
|
|
852
|
+
}],
|
|
853
|
+
ico: [{
|
|
854
|
+
signature: [
|
|
855
|
+
0,
|
|
856
|
+
0,
|
|
857
|
+
1,
|
|
858
|
+
0
|
|
859
|
+
],
|
|
860
|
+
mimeType: "image/x-icon"
|
|
861
|
+
}],
|
|
862
|
+
tiff: [{
|
|
863
|
+
signature: [
|
|
864
|
+
73,
|
|
865
|
+
73,
|
|
866
|
+
42,
|
|
867
|
+
0
|
|
868
|
+
],
|
|
869
|
+
mimeType: "image/tiff"
|
|
870
|
+
}, {
|
|
871
|
+
signature: [
|
|
872
|
+
77,
|
|
873
|
+
77,
|
|
874
|
+
0,
|
|
875
|
+
42
|
|
876
|
+
],
|
|
877
|
+
mimeType: "image/tiff"
|
|
878
|
+
}],
|
|
879
|
+
tif: [{
|
|
880
|
+
signature: [
|
|
881
|
+
73,
|
|
882
|
+
73,
|
|
883
|
+
42,
|
|
884
|
+
0
|
|
885
|
+
],
|
|
886
|
+
mimeType: "image/tiff"
|
|
887
|
+
}, {
|
|
888
|
+
signature: [
|
|
889
|
+
77,
|
|
890
|
+
77,
|
|
891
|
+
0,
|
|
892
|
+
42
|
|
893
|
+
],
|
|
894
|
+
mimeType: "image/tiff"
|
|
895
|
+
}],
|
|
896
|
+
pdf: [{
|
|
897
|
+
signature: [
|
|
898
|
+
37,
|
|
899
|
+
80,
|
|
900
|
+
68,
|
|
901
|
+
70,
|
|
902
|
+
45
|
|
903
|
+
],
|
|
904
|
+
mimeType: "application/pdf"
|
|
905
|
+
}],
|
|
906
|
+
zip: [
|
|
907
|
+
{
|
|
908
|
+
signature: [
|
|
909
|
+
80,
|
|
910
|
+
75,
|
|
911
|
+
3,
|
|
912
|
+
4
|
|
913
|
+
],
|
|
914
|
+
mimeType: "application/zip"
|
|
915
|
+
},
|
|
916
|
+
{
|
|
917
|
+
signature: [
|
|
918
|
+
80,
|
|
919
|
+
75,
|
|
920
|
+
5,
|
|
921
|
+
6
|
|
922
|
+
],
|
|
923
|
+
mimeType: "application/zip"
|
|
924
|
+
},
|
|
925
|
+
{
|
|
926
|
+
signature: [
|
|
927
|
+
80,
|
|
928
|
+
75,
|
|
929
|
+
7,
|
|
930
|
+
8
|
|
931
|
+
],
|
|
932
|
+
mimeType: "application/zip"
|
|
933
|
+
}
|
|
934
|
+
],
|
|
935
|
+
rar: [{
|
|
936
|
+
signature: [
|
|
937
|
+
82,
|
|
938
|
+
97,
|
|
939
|
+
114,
|
|
940
|
+
33,
|
|
941
|
+
26,
|
|
942
|
+
7
|
|
943
|
+
],
|
|
944
|
+
mimeType: "application/vnd.rar"
|
|
945
|
+
}],
|
|
946
|
+
"7z": [{
|
|
947
|
+
signature: [
|
|
948
|
+
55,
|
|
949
|
+
122,
|
|
950
|
+
188,
|
|
951
|
+
175,
|
|
952
|
+
39,
|
|
953
|
+
28
|
|
954
|
+
],
|
|
955
|
+
mimeType: "application/x-7z-compressed"
|
|
956
|
+
}],
|
|
957
|
+
tar: [{
|
|
958
|
+
signature: [
|
|
959
|
+
117,
|
|
960
|
+
115,
|
|
961
|
+
116,
|
|
962
|
+
97,
|
|
963
|
+
114
|
|
964
|
+
],
|
|
965
|
+
mimeType: "application/x-tar"
|
|
966
|
+
}],
|
|
967
|
+
gz: [{
|
|
968
|
+
signature: [31, 139],
|
|
969
|
+
mimeType: "application/gzip"
|
|
970
|
+
}],
|
|
971
|
+
tgz: [{
|
|
972
|
+
signature: [31, 139],
|
|
973
|
+
mimeType: "application/gzip"
|
|
974
|
+
}],
|
|
975
|
+
mp3: [
|
|
976
|
+
{
|
|
977
|
+
signature: [255, 251],
|
|
978
|
+
mimeType: "audio/mpeg"
|
|
979
|
+
},
|
|
980
|
+
{
|
|
981
|
+
signature: [255, 243],
|
|
982
|
+
mimeType: "audio/mpeg"
|
|
983
|
+
},
|
|
984
|
+
{
|
|
985
|
+
signature: [255, 242],
|
|
986
|
+
mimeType: "audio/mpeg"
|
|
987
|
+
},
|
|
988
|
+
{
|
|
989
|
+
signature: [
|
|
990
|
+
73,
|
|
991
|
+
68,
|
|
992
|
+
51
|
|
993
|
+
],
|
|
994
|
+
mimeType: "audio/mpeg"
|
|
995
|
+
}
|
|
996
|
+
],
|
|
997
|
+
wav: [{
|
|
998
|
+
signature: [
|
|
999
|
+
82,
|
|
1000
|
+
73,
|
|
1001
|
+
70,
|
|
1002
|
+
70,
|
|
1003
|
+
null,
|
|
1004
|
+
null,
|
|
1005
|
+
null,
|
|
1006
|
+
null,
|
|
1007
|
+
87,
|
|
1008
|
+
65,
|
|
1009
|
+
86,
|
|
1010
|
+
69
|
|
1011
|
+
],
|
|
1012
|
+
mimeType: "audio/wav"
|
|
1013
|
+
}],
|
|
1014
|
+
ogg: [{
|
|
1015
|
+
signature: [
|
|
1016
|
+
79,
|
|
1017
|
+
103,
|
|
1018
|
+
103,
|
|
1019
|
+
83
|
|
1020
|
+
],
|
|
1021
|
+
mimeType: "audio/ogg"
|
|
1022
|
+
}],
|
|
1023
|
+
flac: [{
|
|
1024
|
+
signature: [
|
|
1025
|
+
102,
|
|
1026
|
+
76,
|
|
1027
|
+
97,
|
|
1028
|
+
67
|
|
1029
|
+
],
|
|
1030
|
+
mimeType: "audio/flac"
|
|
1031
|
+
}],
|
|
1032
|
+
mp4: [
|
|
1033
|
+
{
|
|
1034
|
+
signature: [
|
|
1035
|
+
null,
|
|
1036
|
+
null,
|
|
1037
|
+
null,
|
|
1038
|
+
null,
|
|
1039
|
+
102,
|
|
1040
|
+
116,
|
|
1041
|
+
121,
|
|
1042
|
+
112
|
|
1043
|
+
],
|
|
1044
|
+
mimeType: "video/mp4"
|
|
1045
|
+
},
|
|
1046
|
+
{
|
|
1047
|
+
signature: [
|
|
1048
|
+
null,
|
|
1049
|
+
null,
|
|
1050
|
+
null,
|
|
1051
|
+
null,
|
|
1052
|
+
102,
|
|
1053
|
+
116,
|
|
1054
|
+
121,
|
|
1055
|
+
112,
|
|
1056
|
+
105,
|
|
1057
|
+
115,
|
|
1058
|
+
111,
|
|
1059
|
+
109
|
|
1060
|
+
],
|
|
1061
|
+
mimeType: "video/mp4"
|
|
1062
|
+
},
|
|
1063
|
+
{
|
|
1064
|
+
signature: [
|
|
1065
|
+
null,
|
|
1066
|
+
null,
|
|
1067
|
+
null,
|
|
1068
|
+
null,
|
|
1069
|
+
102,
|
|
1070
|
+
116,
|
|
1071
|
+
121,
|
|
1072
|
+
112,
|
|
1073
|
+
109,
|
|
1074
|
+
112,
|
|
1075
|
+
52,
|
|
1076
|
+
50
|
|
1077
|
+
],
|
|
1078
|
+
mimeType: "video/mp4"
|
|
1079
|
+
}
|
|
1080
|
+
],
|
|
1081
|
+
webm: [{
|
|
1082
|
+
signature: [
|
|
1083
|
+
26,
|
|
1084
|
+
69,
|
|
1085
|
+
223,
|
|
1086
|
+
163
|
|
1087
|
+
],
|
|
1088
|
+
mimeType: "video/webm"
|
|
1089
|
+
}],
|
|
1090
|
+
avi: [{
|
|
1091
|
+
signature: [
|
|
1092
|
+
82,
|
|
1093
|
+
73,
|
|
1094
|
+
70,
|
|
1095
|
+
70,
|
|
1096
|
+
null,
|
|
1097
|
+
null,
|
|
1098
|
+
null,
|
|
1099
|
+
null,
|
|
1100
|
+
65,
|
|
1101
|
+
86,
|
|
1102
|
+
73,
|
|
1103
|
+
32
|
|
1104
|
+
],
|
|
1105
|
+
mimeType: "video/x-msvideo"
|
|
1106
|
+
}],
|
|
1107
|
+
mov: [{
|
|
1108
|
+
signature: [
|
|
1109
|
+
null,
|
|
1110
|
+
null,
|
|
1111
|
+
null,
|
|
1112
|
+
null,
|
|
1113
|
+
102,
|
|
1114
|
+
116,
|
|
1115
|
+
121,
|
|
1116
|
+
112,
|
|
1117
|
+
113,
|
|
1118
|
+
116,
|
|
1119
|
+
32,
|
|
1120
|
+
32
|
|
1121
|
+
],
|
|
1122
|
+
mimeType: "video/quicktime"
|
|
1123
|
+
}],
|
|
1124
|
+
mkv: [{
|
|
1125
|
+
signature: [
|
|
1126
|
+
26,
|
|
1127
|
+
69,
|
|
1128
|
+
223,
|
|
1129
|
+
163
|
|
1130
|
+
],
|
|
1131
|
+
mimeType: "video/x-matroska"
|
|
1132
|
+
}],
|
|
1133
|
+
docx: [{
|
|
1134
|
+
signature: [
|
|
1135
|
+
80,
|
|
1136
|
+
75,
|
|
1137
|
+
3,
|
|
1138
|
+
4
|
|
1139
|
+
],
|
|
1140
|
+
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
1141
|
+
}],
|
|
1142
|
+
xlsx: [{
|
|
1143
|
+
signature: [
|
|
1144
|
+
80,
|
|
1145
|
+
75,
|
|
1146
|
+
3,
|
|
1147
|
+
4
|
|
1148
|
+
],
|
|
1149
|
+
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
1150
|
+
}],
|
|
1151
|
+
pptx: [{
|
|
1152
|
+
signature: [
|
|
1153
|
+
80,
|
|
1154
|
+
75,
|
|
1155
|
+
3,
|
|
1156
|
+
4
|
|
1157
|
+
],
|
|
1158
|
+
mimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
1159
|
+
}],
|
|
1160
|
+
doc: [{
|
|
1161
|
+
signature: [
|
|
1162
|
+
208,
|
|
1163
|
+
207,
|
|
1164
|
+
17,
|
|
1165
|
+
224,
|
|
1166
|
+
161,
|
|
1167
|
+
177,
|
|
1168
|
+
26,
|
|
1169
|
+
225
|
|
1170
|
+
],
|
|
1171
|
+
mimeType: "application/msword"
|
|
1172
|
+
}],
|
|
1173
|
+
xls: [{
|
|
1174
|
+
signature: [
|
|
1175
|
+
208,
|
|
1176
|
+
207,
|
|
1177
|
+
17,
|
|
1178
|
+
224,
|
|
1179
|
+
161,
|
|
1180
|
+
177,
|
|
1181
|
+
26,
|
|
1182
|
+
225
|
|
1183
|
+
],
|
|
1184
|
+
mimeType: "application/vnd.ms-excel"
|
|
1185
|
+
}],
|
|
1186
|
+
ppt: [{
|
|
1187
|
+
signature: [
|
|
1188
|
+
208,
|
|
1189
|
+
207,
|
|
1190
|
+
17,
|
|
1191
|
+
224,
|
|
1192
|
+
161,
|
|
1193
|
+
177,
|
|
1194
|
+
26,
|
|
1195
|
+
225
|
|
1196
|
+
],
|
|
1197
|
+
mimeType: "application/vnd.ms-powerpoint"
|
|
1198
|
+
}]
|
|
1199
|
+
};
|
|
1200
|
+
/**
|
|
1201
|
+
* All possible format signatures for checking against actual file content
|
|
1202
|
+
*/
|
|
1203
|
+
static ALL_SIGNATURES = Object.entries(FileDetector.MAGIC_BYTES).flatMap(([ext, signatures]) => signatures.map((sig) => ({
|
|
1204
|
+
ext,
|
|
1205
|
+
...sig
|
|
1206
|
+
})));
|
|
1207
|
+
/**
|
|
1208
|
+
* MIME type map for file extensions.
|
|
1209
|
+
*
|
|
1210
|
+
* Can be used to get the content type of file based on its extension.
|
|
1211
|
+
* Feel free to add more mime types in your project!
|
|
1212
|
+
*/
|
|
1213
|
+
static mimeMap = {
|
|
1214
|
+
json: "application/json",
|
|
1215
|
+
txt: "text/plain",
|
|
1216
|
+
html: "text/html",
|
|
1217
|
+
htm: "text/html",
|
|
1218
|
+
xml: "application/xml",
|
|
1219
|
+
csv: "text/csv",
|
|
1220
|
+
pdf: "application/pdf",
|
|
1221
|
+
md: "text/markdown",
|
|
1222
|
+
markdown: "text/markdown",
|
|
1223
|
+
rtf: "application/rtf",
|
|
1224
|
+
css: "text/css",
|
|
1225
|
+
js: "application/javascript",
|
|
1226
|
+
mjs: "application/javascript",
|
|
1227
|
+
ts: "application/typescript",
|
|
1228
|
+
jsx: "text/jsx",
|
|
1229
|
+
tsx: "text/tsx",
|
|
1230
|
+
zip: "application/zip",
|
|
1231
|
+
rar: "application/vnd.rar",
|
|
1232
|
+
"7z": "application/x-7z-compressed",
|
|
1233
|
+
tar: "application/x-tar",
|
|
1234
|
+
gz: "application/gzip",
|
|
1235
|
+
tgz: "application/gzip",
|
|
1236
|
+
png: "image/png",
|
|
1237
|
+
jpg: "image/jpeg",
|
|
1238
|
+
jpeg: "image/jpeg",
|
|
1239
|
+
gif: "image/gif",
|
|
1240
|
+
webp: "image/webp",
|
|
1241
|
+
svg: "image/svg+xml",
|
|
1242
|
+
bmp: "image/bmp",
|
|
1243
|
+
ico: "image/x-icon",
|
|
1244
|
+
tiff: "image/tiff",
|
|
1245
|
+
tif: "image/tiff",
|
|
1246
|
+
mp3: "audio/mpeg",
|
|
1247
|
+
wav: "audio/wav",
|
|
1248
|
+
ogg: "audio/ogg",
|
|
1249
|
+
m4a: "audio/mp4",
|
|
1250
|
+
aac: "audio/aac",
|
|
1251
|
+
flac: "audio/flac",
|
|
1252
|
+
mp4: "video/mp4",
|
|
1253
|
+
webm: "video/webm",
|
|
1254
|
+
avi: "video/x-msvideo",
|
|
1255
|
+
mov: "video/quicktime",
|
|
1256
|
+
wmv: "video/x-ms-wmv",
|
|
1257
|
+
flv: "video/x-flv",
|
|
1258
|
+
mkv: "video/x-matroska",
|
|
1259
|
+
doc: "application/msword",
|
|
1260
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1261
|
+
xls: "application/vnd.ms-excel",
|
|
1262
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1263
|
+
ppt: "application/vnd.ms-powerpoint",
|
|
1264
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
1265
|
+
woff: "font/woff",
|
|
1266
|
+
woff2: "font/woff2",
|
|
1267
|
+
ttf: "font/ttf",
|
|
1268
|
+
otf: "font/otf",
|
|
1269
|
+
eot: "application/vnd.ms-fontobject"
|
|
1270
|
+
};
|
|
1271
|
+
/**
|
|
1272
|
+
* Reverse MIME type map for looking up extensions from MIME types.
|
|
1273
|
+
* Prefers shorter, more common extensions when multiple exist.
|
|
1274
|
+
*/
|
|
1275
|
+
static reverseMimeMap = (() => {
|
|
1276
|
+
const reverse = {};
|
|
1277
|
+
for (const [ext, mimeType] of Object.entries(FileDetector.mimeMap)) if (!reverse[mimeType]) reverse[mimeType] = ext;
|
|
1278
|
+
return reverse;
|
|
1279
|
+
})();
|
|
1280
|
+
/**
|
|
1281
|
+
* Returns the file extension for a given MIME type.
|
|
1282
|
+
*
|
|
1283
|
+
* @param mimeType - The MIME type to look up
|
|
1284
|
+
* @returns The file extension (without dot), or "bin" if not found
|
|
1285
|
+
*
|
|
1286
|
+
* @example
|
|
1287
|
+
* ```typescript
|
|
1288
|
+
* const detector = alepha.inject(FileDetector);
|
|
1289
|
+
* const ext = detector.getExtensionFromMimeType("image/png"); // "png"
|
|
1290
|
+
* const ext2 = detector.getExtensionFromMimeType("application/octet-stream"); // "bin"
|
|
1291
|
+
* ```
|
|
1292
|
+
*/
|
|
1293
|
+
getExtensionFromMimeType(mimeType) {
|
|
1294
|
+
return FileDetector.reverseMimeMap[mimeType] || "bin";
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Returns the content type of file based on its filename.
|
|
1298
|
+
*
|
|
1299
|
+
* @param filename - The filename to check
|
|
1300
|
+
* @returns The MIME type
|
|
1301
|
+
*
|
|
1302
|
+
* @example
|
|
1303
|
+
* ```typescript
|
|
1304
|
+
* const detector = alepha.inject(FileDetector);
|
|
1305
|
+
* const mimeType = detector.getContentType("image.png"); // "image/png"
|
|
1306
|
+
* ```
|
|
1307
|
+
*/
|
|
1308
|
+
getContentType(filename) {
|
|
1309
|
+
const ext = filename.toLowerCase().split(".").pop() || "";
|
|
1310
|
+
return FileDetector.mimeMap[ext] || "application/octet-stream";
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
* Detects the file type by checking magic bytes against the stream content.
|
|
1314
|
+
*
|
|
1315
|
+
* @param stream - The readable stream to check
|
|
1316
|
+
* @param filename - The filename (used to get the extension)
|
|
1317
|
+
* @returns File type information including MIME type, extension, and verification status
|
|
1318
|
+
*
|
|
1319
|
+
* @example
|
|
1320
|
+
* ```typescript
|
|
1321
|
+
* const detector = alepha.inject(FileDetector);
|
|
1322
|
+
* const stream = createReadStream('image.png');
|
|
1323
|
+
* const result = await detector.detectFileType(stream, 'image.png');
|
|
1324
|
+
* console.log(result.mimeType); // 'image/png'
|
|
1325
|
+
* console.log(result.verified); // true if magic bytes match
|
|
1326
|
+
* ```
|
|
1327
|
+
*/
|
|
1328
|
+
async detectFileType(stream, filename) {
|
|
1329
|
+
const expectedMimeType = this.getContentType(filename);
|
|
1330
|
+
const lastDotIndex = filename.lastIndexOf(".");
|
|
1331
|
+
const ext = lastDotIndex > 0 ? filename.substring(lastDotIndex + 1).toLowerCase() : "";
|
|
1332
|
+
const { buffer, stream: newStream } = await this.peekBytes(stream, 16);
|
|
1333
|
+
const expectedSignatures = FileDetector.MAGIC_BYTES[ext];
|
|
1334
|
+
if (expectedSignatures) {
|
|
1335
|
+
for (const { signature, mimeType } of expectedSignatures) if (this.matchesSignature(buffer, signature)) return {
|
|
1336
|
+
mimeType,
|
|
1337
|
+
extension: ext,
|
|
1338
|
+
verified: true,
|
|
1339
|
+
stream: newStream
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
for (const { ext: detectedExt, signature, mimeType } of FileDetector.ALL_SIGNATURES) if (detectedExt !== ext && this.matchesSignature(buffer, signature)) return {
|
|
1343
|
+
mimeType,
|
|
1344
|
+
extension: detectedExt,
|
|
1345
|
+
verified: true,
|
|
1346
|
+
stream: newStream
|
|
1347
|
+
};
|
|
1348
|
+
return {
|
|
1349
|
+
mimeType: expectedMimeType,
|
|
1350
|
+
extension: ext,
|
|
1351
|
+
verified: false,
|
|
1352
|
+
stream: newStream
|
|
1353
|
+
};
|
|
1354
|
+
}
|
|
1355
|
+
/**
|
|
1356
|
+
* Reads all bytes from a stream and returns the first N bytes along with a new stream containing all data.
|
|
1357
|
+
* This approach reads the entire stream upfront to avoid complex async handling issues.
|
|
1358
|
+
*
|
|
1359
|
+
* @protected
|
|
1360
|
+
*/
|
|
1361
|
+
async peekBytes(stream, numBytes) {
|
|
1362
|
+
const chunks = [];
|
|
1363
|
+
for await (const chunk of stream) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
1364
|
+
const allData = Buffer.concat(chunks);
|
|
1365
|
+
return {
|
|
1366
|
+
buffer: allData.subarray(0, numBytes),
|
|
1367
|
+
stream: Readable.from(allData)
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Checks if a buffer matches a magic byte signature.
|
|
1372
|
+
*
|
|
1373
|
+
* @protected
|
|
1374
|
+
*/
|
|
1375
|
+
matchesSignature(buffer, signature) {
|
|
1376
|
+
if (buffer.length < signature.length) return false;
|
|
1377
|
+
for (let i = 0; i < signature.length; i++) if (signature[i] !== null && buffer[i] !== signature[i]) return false;
|
|
1378
|
+
return true;
|
|
1379
|
+
}
|
|
1380
|
+
};
|
|
1381
|
+
|
|
1382
|
+
//#endregion
|
|
1383
|
+
//#region ../../src/system/providers/NodeFileSystemProvider.ts
|
|
1384
|
+
/**
|
|
1385
|
+
* Node.js implementation of FileSystem interface.
|
|
1386
|
+
*
|
|
1387
|
+
* @example
|
|
1388
|
+
* ```typescript
|
|
1389
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1390
|
+
*
|
|
1391
|
+
* // Create from URL
|
|
1392
|
+
* const file1 = fs.createFile({ url: "file:///path/to/file.png" });
|
|
1393
|
+
*
|
|
1394
|
+
* // Create from Buffer
|
|
1395
|
+
* const file2 = fs.createFile({ buffer: Buffer.from("hello"), name: "hello.txt" });
|
|
1396
|
+
*
|
|
1397
|
+
* // Create from text
|
|
1398
|
+
* const file3 = fs.createFile({ text: "Hello, world!", name: "greeting.txt" });
|
|
1399
|
+
*
|
|
1400
|
+
* // File operations
|
|
1401
|
+
* await fs.mkdir("/tmp/mydir", { recursive: true });
|
|
1402
|
+
* await fs.cp("/src/file.txt", "/dest/file.txt");
|
|
1403
|
+
* await fs.mv("/old/path.txt", "/new/path.txt");
|
|
1404
|
+
* const files = await fs.ls("/tmp");
|
|
1405
|
+
* await fs.rm("/tmp/file.txt");
|
|
1406
|
+
* ```
|
|
1407
|
+
*/
|
|
1408
|
+
var NodeFileSystemProvider = class {
|
|
1409
|
+
detector = $inject(FileDetector);
|
|
1410
|
+
json = $inject(Json);
|
|
1411
|
+
join(...paths) {
|
|
1412
|
+
return join(...paths);
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Creates a FileLike object from various sources.
|
|
1416
|
+
*
|
|
1417
|
+
* @param options - Options for creating the file
|
|
1418
|
+
* @returns A FileLike object
|
|
1419
|
+
*
|
|
1420
|
+
* @example
|
|
1421
|
+
* ```typescript
|
|
1422
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1423
|
+
*
|
|
1424
|
+
* // From URL
|
|
1425
|
+
* const file1 = fs.createFile({ url: "https://example.com/image.png" });
|
|
1426
|
+
*
|
|
1427
|
+
* // From Buffer
|
|
1428
|
+
* const file2 = fs.createFile({
|
|
1429
|
+
* buffer: Buffer.from("hello"),
|
|
1430
|
+
* name: "hello.txt",
|
|
1431
|
+
* type: "text/plain"
|
|
1432
|
+
* });
|
|
1433
|
+
*
|
|
1434
|
+
* // From text
|
|
1435
|
+
* const file3 = fs.createFile({ text: "Hello!", name: "greeting.txt" });
|
|
1436
|
+
*
|
|
1437
|
+
* // From stream with detection
|
|
1438
|
+
* const stream = createReadStream("/path/to/file.png");
|
|
1439
|
+
* const file4 = fs.createFile({ stream, name: "image.png" });
|
|
1440
|
+
* ```
|
|
1441
|
+
*/
|
|
1442
|
+
createFile(options) {
|
|
1443
|
+
if ("path" in options) {
|
|
1444
|
+
const path = options.path;
|
|
1445
|
+
const filename = path.split("/").pop() || "file";
|
|
1446
|
+
return this.createFileFromUrl(`file://${path}`, {
|
|
1447
|
+
type: options.type,
|
|
1448
|
+
name: options.name || filename
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1451
|
+
if ("url" in options) return this.createFileFromUrl(options.url, {
|
|
1452
|
+
type: options.type,
|
|
1453
|
+
name: options.name
|
|
1454
|
+
});
|
|
1455
|
+
if ("response" in options) {
|
|
1456
|
+
if (!options.response.body) throw new AlephaError("Response has no body stream");
|
|
1457
|
+
const res = options.response;
|
|
1458
|
+
const sizeHeader = res.headers.get("content-length");
|
|
1459
|
+
const size = sizeHeader ? parseInt(sizeHeader, 10) : void 0;
|
|
1460
|
+
let name = options.name;
|
|
1461
|
+
const contentDisposition = res.headers.get("content-disposition");
|
|
1462
|
+
if (contentDisposition && !name) {
|
|
1463
|
+
const match = contentDisposition.match(/filename="?([^"]+)"?/);
|
|
1464
|
+
if (match) name = match[1];
|
|
1465
|
+
}
|
|
1466
|
+
const type = options.type || res.headers.get("content-type") || void 0;
|
|
1467
|
+
return this.createFileFromStream(options.response.body, {
|
|
1468
|
+
type,
|
|
1469
|
+
name,
|
|
1470
|
+
size
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
if ("file" in options) return this.createFileFromWebFile(options.file, {
|
|
1474
|
+
type: options.type,
|
|
1475
|
+
name: options.name,
|
|
1476
|
+
size: options.size
|
|
1477
|
+
});
|
|
1478
|
+
if ("buffer" in options) return this.createFileFromBuffer(options.buffer, {
|
|
1479
|
+
type: options.type,
|
|
1480
|
+
name: options.name
|
|
1481
|
+
});
|
|
1482
|
+
if ("arrayBuffer" in options) return this.createFileFromBuffer(Buffer.from(options.arrayBuffer), {
|
|
1483
|
+
type: options.type,
|
|
1484
|
+
name: options.name
|
|
1485
|
+
});
|
|
1486
|
+
if ("text" in options) return this.createFileFromBuffer(Buffer.from(options.text, "utf-8"), {
|
|
1487
|
+
type: options.type || "text/plain",
|
|
1488
|
+
name: options.name || "file.txt"
|
|
1489
|
+
});
|
|
1490
|
+
if ("stream" in options) return this.createFileFromStream(options.stream, {
|
|
1491
|
+
type: options.type,
|
|
1492
|
+
name: options.name,
|
|
1493
|
+
size: options.size
|
|
1494
|
+
});
|
|
1495
|
+
throw new AlephaError("Invalid createFile options: no valid source provided");
|
|
1496
|
+
}
|
|
1497
|
+
/**
|
|
1498
|
+
* Removes a file or directory.
|
|
1499
|
+
*
|
|
1500
|
+
* @param path - The path to remove
|
|
1501
|
+
* @param options - Remove options
|
|
1502
|
+
*
|
|
1503
|
+
* @example
|
|
1504
|
+
* ```typescript
|
|
1505
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1506
|
+
*
|
|
1507
|
+
* // Remove a file
|
|
1508
|
+
* await fs.rm("/tmp/file.txt");
|
|
1509
|
+
*
|
|
1510
|
+
* // Remove a directory recursively
|
|
1511
|
+
* await fs.rm("/tmp/mydir", { recursive: true });
|
|
1512
|
+
*
|
|
1513
|
+
* // Remove with force (no error if doesn't exist)
|
|
1514
|
+
* await fs.rm("/tmp/maybe-exists.txt", { force: true });
|
|
1515
|
+
* ```
|
|
1516
|
+
*/
|
|
1517
|
+
async rm(path, options) {
|
|
1518
|
+
await rm(path, options);
|
|
1519
|
+
}
|
|
1520
|
+
/**
|
|
1521
|
+
* Copies a file or directory.
|
|
1522
|
+
*
|
|
1523
|
+
* @param src - Source path
|
|
1524
|
+
* @param dest - Destination path
|
|
1525
|
+
* @param options - Copy options
|
|
1526
|
+
*
|
|
1527
|
+
* @example
|
|
1528
|
+
* ```typescript
|
|
1529
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1530
|
+
*
|
|
1531
|
+
* // Copy a file
|
|
1532
|
+
* await fs.cp("/src/file.txt", "/dest/file.txt");
|
|
1533
|
+
*
|
|
1534
|
+
* // Copy a directory recursively
|
|
1535
|
+
* await fs.cp("/src/dir", "/dest/dir", { recursive: true });
|
|
1536
|
+
*
|
|
1537
|
+
* // Copy with force (overwrite existing)
|
|
1538
|
+
* await fs.cp("/src/file.txt", "/dest/file.txt", { force: true });
|
|
1539
|
+
* ```
|
|
1540
|
+
*/
|
|
1541
|
+
async cp(src, dest, options) {
|
|
1542
|
+
if ((await stat(src)).isDirectory()) {
|
|
1543
|
+
if (!options?.recursive) throw new Error(`Cannot copy directory without recursive option: ${src}`);
|
|
1544
|
+
await cp(src, dest, {
|
|
1545
|
+
recursive: true,
|
|
1546
|
+
force: options?.force ?? false
|
|
1547
|
+
});
|
|
1548
|
+
} else await copyFile(src, dest);
|
|
1549
|
+
}
|
|
1550
|
+
/**
|
|
1551
|
+
* Moves/renames a file or directory.
|
|
1552
|
+
*
|
|
1553
|
+
* @param src - Source path
|
|
1554
|
+
* @param dest - Destination path
|
|
1555
|
+
*
|
|
1556
|
+
* @example
|
|
1557
|
+
* ```typescript
|
|
1558
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1559
|
+
*
|
|
1560
|
+
* // Move/rename a file
|
|
1561
|
+
* await fs.mv("/old/path.txt", "/new/path.txt");
|
|
1562
|
+
*
|
|
1563
|
+
* // Move a directory
|
|
1564
|
+
* await fs.mv("/old/dir", "/new/dir");
|
|
1565
|
+
* ```
|
|
1566
|
+
*/
|
|
1567
|
+
async mv(src, dest) {
|
|
1568
|
+
await rename(src, dest);
|
|
1569
|
+
}
|
|
1570
|
+
/**
|
|
1571
|
+
* Creates a directory.
|
|
1572
|
+
*
|
|
1573
|
+
* @param path - The directory path to create
|
|
1574
|
+
* @param options - Mkdir options
|
|
1575
|
+
*
|
|
1576
|
+
* @example
|
|
1577
|
+
* ```typescript
|
|
1578
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1579
|
+
*
|
|
1580
|
+
* // Create a directory
|
|
1581
|
+
* await fs.mkdir("/tmp/mydir");
|
|
1582
|
+
*
|
|
1583
|
+
* // Create nested directories
|
|
1584
|
+
* await fs.mkdir("/tmp/path/to/dir", { recursive: true });
|
|
1585
|
+
*
|
|
1586
|
+
* // Create with specific permissions
|
|
1587
|
+
* await fs.mkdir("/tmp/mydir", { mode: 0o755 });
|
|
1588
|
+
* ```
|
|
1589
|
+
*/
|
|
1590
|
+
async mkdir(path, options) {
|
|
1591
|
+
await mkdir(path, options);
|
|
1592
|
+
}
|
|
1593
|
+
/**
|
|
1594
|
+
* Lists files in a directory.
|
|
1595
|
+
*
|
|
1596
|
+
* @param path - The directory path to list
|
|
1597
|
+
* @param options - List options
|
|
1598
|
+
* @returns Array of filenames
|
|
1599
|
+
*
|
|
1600
|
+
* @example
|
|
1601
|
+
* ```typescript
|
|
1602
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1603
|
+
*
|
|
1604
|
+
* // List files in a directory
|
|
1605
|
+
* const files = await fs.ls("/tmp");
|
|
1606
|
+
* console.log(files); // ["file1.txt", "file2.txt", "subdir"]
|
|
1607
|
+
*
|
|
1608
|
+
* // List with hidden files
|
|
1609
|
+
* const allFiles = await fs.ls("/tmp", { hidden: true });
|
|
1610
|
+
*
|
|
1611
|
+
* // List recursively
|
|
1612
|
+
* const allFilesRecursive = await fs.ls("/tmp", { recursive: true });
|
|
1613
|
+
* ```
|
|
1614
|
+
*/
|
|
1615
|
+
async ls(path, options) {
|
|
1616
|
+
const entries = await readdir(path);
|
|
1617
|
+
const filteredEntries = options?.hidden ? entries : entries.filter((e) => !e.startsWith("."));
|
|
1618
|
+
if (options?.recursive) {
|
|
1619
|
+
const allFiles = [];
|
|
1620
|
+
for (const entry of filteredEntries) {
|
|
1621
|
+
const fullPath = join(path, entry);
|
|
1622
|
+
if ((await stat(fullPath)).isDirectory()) {
|
|
1623
|
+
allFiles.push(entry);
|
|
1624
|
+
const subFiles = await this.ls(fullPath, options);
|
|
1625
|
+
allFiles.push(...subFiles.map((f) => join(entry, f)));
|
|
1626
|
+
} else allFiles.push(entry);
|
|
1627
|
+
}
|
|
1628
|
+
return allFiles;
|
|
1629
|
+
}
|
|
1630
|
+
return filteredEntries;
|
|
1631
|
+
}
|
|
1632
|
+
/**
|
|
1633
|
+
* Checks if a file or directory exists.
|
|
1634
|
+
*
|
|
1635
|
+
* @param path - The path to check
|
|
1636
|
+
* @returns True if the path exists, false otherwise
|
|
1637
|
+
*
|
|
1638
|
+
* @example
|
|
1639
|
+
* ```typescript
|
|
1640
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1641
|
+
*
|
|
1642
|
+
* if (await fs.exists("/tmp/file.txt")) {
|
|
1643
|
+
* console.log("File exists");
|
|
1644
|
+
* }
|
|
1645
|
+
* ```
|
|
1646
|
+
*/
|
|
1647
|
+
async exists(path) {
|
|
1648
|
+
try {
|
|
1649
|
+
await access(path);
|
|
1650
|
+
return true;
|
|
1651
|
+
} catch {
|
|
1652
|
+
return false;
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
/**
|
|
1656
|
+
* Reads the content of a file.
|
|
1657
|
+
*
|
|
1658
|
+
* @param path - The file path to read
|
|
1659
|
+
* @returns The file content as a Buffer
|
|
1660
|
+
*
|
|
1661
|
+
* @example
|
|
1662
|
+
* ```typescript
|
|
1663
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1664
|
+
*
|
|
1665
|
+
* const buffer = await fs.readFile("/tmp/file.txt");
|
|
1666
|
+
* console.log(buffer.toString("utf-8"));
|
|
1667
|
+
* ```
|
|
1668
|
+
*/
|
|
1669
|
+
async readFile(path) {
|
|
1670
|
+
return await readFile(path);
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Writes data to a file.
|
|
1674
|
+
*
|
|
1675
|
+
* @param path - The file path to write to
|
|
1676
|
+
* @param data - The data to write (Buffer or string)
|
|
1677
|
+
*
|
|
1678
|
+
* @example
|
|
1679
|
+
* ```typescript
|
|
1680
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1681
|
+
*
|
|
1682
|
+
* // Write string
|
|
1683
|
+
* await fs.writeFile("/tmp/file.txt", "Hello, world!");
|
|
1684
|
+
*
|
|
1685
|
+
* // Write Buffer
|
|
1686
|
+
* await fs.writeFile("/tmp/file.bin", Buffer.from([0x01, 0x02, 0x03]));
|
|
1687
|
+
* ```
|
|
1688
|
+
*/
|
|
1689
|
+
async writeFile(path, data) {
|
|
1690
|
+
if (isFileLike(data)) {
|
|
1691
|
+
await writeFile(path, Readable.from(data.stream()));
|
|
1692
|
+
return;
|
|
1693
|
+
}
|
|
1694
|
+
await writeFile(path, data);
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Reads the content of a file as a string.
|
|
1698
|
+
*
|
|
1699
|
+
* @param path - The file path to read
|
|
1700
|
+
* @returns The file content as a string
|
|
1701
|
+
*
|
|
1702
|
+
* @example
|
|
1703
|
+
* ```typescript
|
|
1704
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1705
|
+
* const content = await fs.readTextFile("/tmp/file.txt");
|
|
1706
|
+
* ```
|
|
1707
|
+
*/
|
|
1708
|
+
async readTextFile(path) {
|
|
1709
|
+
return (await this.readFile(path)).toString("utf-8");
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* Reads the content of a file as JSON.
|
|
1713
|
+
*
|
|
1714
|
+
* @param path - The file path to read
|
|
1715
|
+
* @returns The parsed JSON content
|
|
1716
|
+
*
|
|
1717
|
+
* @example
|
|
1718
|
+
* ```typescript
|
|
1719
|
+
* const fs = alepha.inject(NodeFileSystemProvider);
|
|
1720
|
+
* const config = await fs.readJsonFile<{ name: string }>("/tmp/config.json");
|
|
1721
|
+
* ```
|
|
1722
|
+
*/
|
|
1723
|
+
async readJsonFile(path) {
|
|
1724
|
+
const text = await this.readTextFile(path);
|
|
1725
|
+
return this.json.parse(text);
|
|
1726
|
+
}
|
|
1727
|
+
/**
|
|
1728
|
+
* Creates a FileLike object from a Web File.
|
|
1729
|
+
*
|
|
1730
|
+
* @protected
|
|
1731
|
+
*/
|
|
1732
|
+
createFileFromWebFile(source, options = {}) {
|
|
1733
|
+
const name = options.name ?? source.name;
|
|
1734
|
+
return {
|
|
1735
|
+
name,
|
|
1736
|
+
type: options.type ?? (source.type || this.detector.getContentType(name)),
|
|
1737
|
+
size: options.size ?? source.size ?? 0,
|
|
1738
|
+
lastModified: source.lastModified || Date.now(),
|
|
1739
|
+
stream: () => source.stream(),
|
|
1740
|
+
arrayBuffer: async () => {
|
|
1741
|
+
return await source.arrayBuffer();
|
|
1742
|
+
},
|
|
1743
|
+
text: async () => {
|
|
1744
|
+
return await source.text();
|
|
1745
|
+
}
|
|
1746
|
+
};
|
|
1747
|
+
}
|
|
1748
|
+
/**
|
|
1749
|
+
* Creates a FileLike object from a Buffer.
|
|
1750
|
+
*
|
|
1751
|
+
* @protected
|
|
1752
|
+
*/
|
|
1753
|
+
createFileFromBuffer(source, options = {}) {
|
|
1754
|
+
const name = options.name ?? "file";
|
|
1755
|
+
return {
|
|
1756
|
+
name,
|
|
1757
|
+
type: options.type ?? this.detector.getContentType(options.name ?? name),
|
|
1758
|
+
size: source.byteLength,
|
|
1759
|
+
lastModified: Date.now(),
|
|
1760
|
+
stream: () => Readable.from(source),
|
|
1761
|
+
arrayBuffer: async () => {
|
|
1762
|
+
return this.bufferToArrayBuffer(source);
|
|
1763
|
+
},
|
|
1764
|
+
text: async () => {
|
|
1765
|
+
return source.toString("utf-8");
|
|
1766
|
+
}
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
/**
|
|
1770
|
+
* Creates a FileLike object from a stream.
|
|
1771
|
+
*
|
|
1772
|
+
* @protected
|
|
1773
|
+
*/
|
|
1774
|
+
createFileFromStream(source, options = {}) {
|
|
1775
|
+
let buffer = null;
|
|
1776
|
+
return {
|
|
1777
|
+
name: options.name ?? "file",
|
|
1778
|
+
type: options.type ?? this.detector.getContentType(options.name ?? "file"),
|
|
1779
|
+
size: options.size ?? 0,
|
|
1780
|
+
lastModified: Date.now(),
|
|
1781
|
+
stream: () => source,
|
|
1782
|
+
_buffer: null,
|
|
1783
|
+
arrayBuffer: async () => {
|
|
1784
|
+
buffer ??= await this.streamToBuffer(source);
|
|
1785
|
+
return this.bufferToArrayBuffer(buffer);
|
|
1786
|
+
},
|
|
1787
|
+
text: async () => {
|
|
1788
|
+
buffer ??= await this.streamToBuffer(source);
|
|
1789
|
+
return buffer.toString("utf-8");
|
|
1790
|
+
}
|
|
1791
|
+
};
|
|
1792
|
+
}
|
|
1793
|
+
/**
|
|
1794
|
+
* Creates a FileLike object from a URL.
|
|
1795
|
+
*
|
|
1796
|
+
* @protected
|
|
1797
|
+
*/
|
|
1798
|
+
createFileFromUrl(url, options = {}) {
|
|
1799
|
+
const parsedUrl = new URL(url);
|
|
1800
|
+
const filename = options.name || parsedUrl.pathname.split("/").pop() || "file";
|
|
1801
|
+
let buffer = null;
|
|
1802
|
+
return {
|
|
1803
|
+
name: filename,
|
|
1804
|
+
type: options.type ?? this.detector.getContentType(filename),
|
|
1805
|
+
size: 0,
|
|
1806
|
+
lastModified: Date.now(),
|
|
1807
|
+
stream: () => this.createStreamFromUrl(url),
|
|
1808
|
+
arrayBuffer: async () => {
|
|
1809
|
+
buffer ??= await this.loadFromUrl(url);
|
|
1810
|
+
return this.bufferToArrayBuffer(buffer);
|
|
1811
|
+
},
|
|
1812
|
+
text: async () => {
|
|
1813
|
+
buffer ??= await this.loadFromUrl(url);
|
|
1814
|
+
return buffer.toString("utf-8");
|
|
1815
|
+
},
|
|
1816
|
+
filepath: url
|
|
1817
|
+
};
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Gets a streaming response from a URL.
|
|
1821
|
+
*
|
|
1822
|
+
* @protected
|
|
1823
|
+
*/
|
|
1824
|
+
getStreamingResponse(url) {
|
|
1825
|
+
const stream = new PassThrough();
|
|
1826
|
+
fetch(url).then((res) => Readable.fromWeb(res.body).pipe(stream)).catch((err) => stream.destroy(err));
|
|
1827
|
+
return stream;
|
|
1828
|
+
}
|
|
1829
|
+
/**
|
|
1830
|
+
* Loads data from a URL.
|
|
1831
|
+
*
|
|
1832
|
+
* @protected
|
|
1833
|
+
*/
|
|
1834
|
+
async loadFromUrl(url) {
|
|
1835
|
+
const parsedUrl = new URL(url);
|
|
1836
|
+
if (parsedUrl.protocol === "file:") return await readFile(fileURLToPath(url));
|
|
1837
|
+
else if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") {
|
|
1838
|
+
const response = await fetch(url);
|
|
1839
|
+
if (!response.ok) throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
1840
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
1841
|
+
return Buffer.from(arrayBuffer);
|
|
1842
|
+
} else throw new Error(`Unsupported protocol: ${parsedUrl.protocol}`);
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* Creates a stream from a URL.
|
|
1846
|
+
*
|
|
1847
|
+
* @protected
|
|
1848
|
+
*/
|
|
1849
|
+
createStreamFromUrl(url) {
|
|
1850
|
+
const parsedUrl = new URL(url);
|
|
1851
|
+
if (parsedUrl.protocol === "file:") return createReadStream(fileURLToPath(url));
|
|
1852
|
+
else if (parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:") return this.getStreamingResponse(url);
|
|
1853
|
+
else throw new AlephaError(`Unsupported protocol: ${parsedUrl.protocol}`);
|
|
1854
|
+
}
|
|
1855
|
+
/**
|
|
1856
|
+
* Converts a stream-like object to a Buffer.
|
|
1857
|
+
*
|
|
1858
|
+
* @protected
|
|
1859
|
+
*/
|
|
1860
|
+
async streamToBuffer(streamLike) {
|
|
1861
|
+
const stream = streamLike instanceof Readable ? streamLike : Readable.fromWeb(streamLike);
|
|
1862
|
+
return new Promise((resolve, reject) => {
|
|
1863
|
+
const buffer = [];
|
|
1864
|
+
stream.on("data", (chunk) => buffer.push(Buffer.from(chunk)));
|
|
1865
|
+
stream.on("end", () => resolve(Buffer.concat(buffer)));
|
|
1866
|
+
stream.on("error", (err) => reject(new AlephaError("Error converting stream", { cause: err })));
|
|
1867
|
+
});
|
|
1868
|
+
}
|
|
1869
|
+
/**
|
|
1870
|
+
* Converts a Node.js Buffer to an ArrayBuffer.
|
|
1871
|
+
*
|
|
1872
|
+
* @protected
|
|
1873
|
+
*/
|
|
1874
|
+
bufferToArrayBuffer(buffer) {
|
|
1875
|
+
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
1876
|
+
}
|
|
1877
|
+
};
|
|
1878
|
+
|
|
1879
|
+
//#endregion
|
|
1880
|
+
//#region ../../src/system/providers/NodeShellProvider.ts
|
|
1881
|
+
/**
|
|
1882
|
+
* Node.js implementation of ShellProvider.
|
|
1883
|
+
*
|
|
1884
|
+
* Executes shell commands using Node.js child_process module.
|
|
1885
|
+
* Supports binary resolution from node_modules/.bin for local packages.
|
|
1886
|
+
*/
|
|
1887
|
+
var NodeShellProvider = class {
|
|
1888
|
+
log = $logger();
|
|
1889
|
+
fs = $inject(FileSystemProvider);
|
|
1890
|
+
/**
|
|
1891
|
+
* Run a shell command or binary.
|
|
1892
|
+
*/
|
|
1893
|
+
async run(command, options = {}) {
|
|
1894
|
+
const { resolve = false, capture = false, root, env } = options;
|
|
1895
|
+
const cwd = root ?? process.cwd();
|
|
1896
|
+
this.log.debug(`Shell: ${command}`, {
|
|
1897
|
+
cwd,
|
|
1898
|
+
resolve,
|
|
1899
|
+
capture
|
|
1900
|
+
});
|
|
1901
|
+
let executable;
|
|
1902
|
+
let args;
|
|
1903
|
+
if (resolve) {
|
|
1904
|
+
const [bin, ...rest] = command.split(" ");
|
|
1905
|
+
executable = await this.resolveExecutable(bin, cwd);
|
|
1906
|
+
args = rest;
|
|
1907
|
+
} else [executable, ...args] = command.split(" ");
|
|
1908
|
+
if (capture) return this.execCapture(command, {
|
|
1909
|
+
cwd,
|
|
1910
|
+
env
|
|
1911
|
+
});
|
|
1912
|
+
return this.execInherit(executable, args, {
|
|
1913
|
+
cwd,
|
|
1914
|
+
env
|
|
1915
|
+
});
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Execute command with inherited stdio (streams to terminal).
|
|
1919
|
+
*/
|
|
1920
|
+
async execInherit(executable, args, options) {
|
|
1921
|
+
const proc = spawn(executable, args, {
|
|
1922
|
+
stdio: "inherit",
|
|
1923
|
+
cwd: options.cwd,
|
|
1924
|
+
env: {
|
|
1925
|
+
...process.env,
|
|
1926
|
+
...options.env
|
|
1927
|
+
}
|
|
1928
|
+
});
|
|
1929
|
+
return new Promise((resolve, reject) => {
|
|
1930
|
+
proc.on("exit", (code) => {
|
|
1931
|
+
if (code === 0 || code === null) resolve("");
|
|
1932
|
+
else reject(new AlephaError(`Command exited with code ${code}`));
|
|
1933
|
+
});
|
|
1934
|
+
proc.on("error", reject);
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
/**
|
|
1938
|
+
* Execute command and capture stdout.
|
|
1939
|
+
*/
|
|
1940
|
+
execCapture(command, options) {
|
|
1941
|
+
return new Promise((resolve, reject) => {
|
|
1942
|
+
exec(command, {
|
|
1943
|
+
cwd: options.cwd,
|
|
1944
|
+
env: {
|
|
1945
|
+
...process.env,
|
|
1946
|
+
LOG_FORMAT: "pretty",
|
|
1947
|
+
...options.env
|
|
1948
|
+
}
|
|
1949
|
+
}, (err, stdout) => {
|
|
1950
|
+
if (err) {
|
|
1951
|
+
err.stdout = stdout;
|
|
1952
|
+
reject(err);
|
|
1953
|
+
} else resolve(stdout);
|
|
1954
|
+
});
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Resolve executable path from node_modules/.bin.
|
|
1959
|
+
*
|
|
1960
|
+
* Search order:
|
|
1961
|
+
* 1. Local: node_modules/.bin/
|
|
1962
|
+
* 2. Pnpm nested: node_modules/alepha/node_modules/.bin/
|
|
1963
|
+
* 3. Monorepo: Walk up to 3 parent directories
|
|
1964
|
+
*/
|
|
1965
|
+
async resolveExecutable(name, root) {
|
|
1966
|
+
const suffix = process.platform === "win32" ? ".cmd" : "";
|
|
1967
|
+
let execPath = await this.findExecutable(root, `node_modules/.bin/${name}${suffix}`);
|
|
1968
|
+
if (!execPath) execPath = await this.findExecutable(root, `node_modules/alepha/node_modules/.bin/${name}${suffix}`);
|
|
1969
|
+
if (!execPath) {
|
|
1970
|
+
let parentDir = this.fs.join(root, "..");
|
|
1971
|
+
for (let i = 0; i < 3; i++) {
|
|
1972
|
+
execPath = await this.findExecutable(parentDir, `node_modules/.bin/${name}${suffix}`);
|
|
1973
|
+
if (execPath) break;
|
|
1974
|
+
parentDir = this.fs.join(parentDir, "..");
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
if (!execPath) throw new AlephaError(`Could not find executable for '${name}'. Make sure the package is installed.`);
|
|
1978
|
+
return execPath;
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* Check if executable exists at path.
|
|
1982
|
+
*/
|
|
1983
|
+
async findExecutable(root, relativePath) {
|
|
1984
|
+
const fullPath = this.fs.join(root, relativePath);
|
|
1985
|
+
if (await this.fs.exists(fullPath)) return fullPath;
|
|
1986
|
+
}
|
|
1987
|
+
/**
|
|
1988
|
+
* Check if a command is installed and available in the system PATH.
|
|
1989
|
+
*/
|
|
1990
|
+
isInstalled(command) {
|
|
1991
|
+
return new Promise((resolve) => {
|
|
1992
|
+
exec(process.platform === "win32" ? `where ${command}` : `command -v ${command}`, (error) => resolve(!error));
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1995
|
+
};
|
|
1996
|
+
|
|
1997
|
+
//#endregion
|
|
1998
|
+
//#region ../../src/system/providers/ShellProvider.ts
|
|
1999
|
+
/**
|
|
2000
|
+
* Abstract provider for executing shell commands and binaries.
|
|
2001
|
+
*
|
|
2002
|
+
* Implementations:
|
|
2003
|
+
* - `NodeShellProvider` - Real shell execution using Node.js child_process
|
|
2004
|
+
* - `MemoryShellProvider` - In-memory mock for testing
|
|
2005
|
+
*
|
|
2006
|
+
* @example
|
|
2007
|
+
* ```typescript
|
|
2008
|
+
* class MyService {
|
|
2009
|
+
* protected readonly shell = $inject(ShellProvider);
|
|
2010
|
+
*
|
|
2011
|
+
* async build() {
|
|
2012
|
+
* // Run shell command directly
|
|
2013
|
+
* await this.shell.run("yarn install");
|
|
2014
|
+
*
|
|
2015
|
+
* // Run local binary with resolution
|
|
2016
|
+
* await this.shell.run("vite build", { resolve: true });
|
|
2017
|
+
*
|
|
2018
|
+
* // Capture output
|
|
2019
|
+
* const output = await this.shell.run("echo hello", { capture: true });
|
|
2020
|
+
* }
|
|
2021
|
+
* }
|
|
2022
|
+
* ```
|
|
2023
|
+
*/
|
|
2024
|
+
var ShellProvider = class {};
|
|
2025
|
+
|
|
2026
|
+
//#endregion
|
|
2027
|
+
//#region ../../src/system/index.ts
|
|
2028
|
+
/**
|
|
2029
|
+
* | type | quality | stability |
|
|
2030
|
+
* |------|---------|-----------|
|
|
2031
|
+
* | tooling | standard | stable |
|
|
2032
|
+
*
|
|
2033
|
+
* System-level abstractions for portable code across runtimes.
|
|
2034
|
+
*
|
|
2035
|
+
* **Features:**
|
|
2036
|
+
* - File system operations (read, write, exists, etc.)
|
|
2037
|
+
* - Shell command execution
|
|
2038
|
+
* - File type detection and MIME utilities
|
|
2039
|
+
* - Memory implementations for testing
|
|
2040
|
+
*
|
|
2041
|
+
* @module alepha.system
|
|
2042
|
+
*/
|
|
2043
|
+
const AlephaSystem = $module({
|
|
2044
|
+
name: "alepha.system",
|
|
2045
|
+
primitives: [],
|
|
2046
|
+
services: [
|
|
2047
|
+
FileDetector,
|
|
2048
|
+
FileSystemProvider,
|
|
2049
|
+
MemoryFileSystemProvider,
|
|
2050
|
+
NodeFileSystemProvider,
|
|
2051
|
+
ShellProvider,
|
|
2052
|
+
MemoryShellProvider,
|
|
2053
|
+
NodeShellProvider
|
|
2054
|
+
],
|
|
2055
|
+
register: (alepha) => alepha.with({
|
|
2056
|
+
optional: true,
|
|
2057
|
+
provide: FileSystemProvider,
|
|
2058
|
+
use: NodeFileSystemProvider
|
|
2059
|
+
}).with({
|
|
2060
|
+
optional: true,
|
|
2061
|
+
provide: ShellProvider,
|
|
2062
|
+
use: alepha.isTest() ? MemoryShellProvider : NodeShellProvider
|
|
2063
|
+
})
|
|
2064
|
+
});
|
|
2065
|
+
|
|
228
2066
|
//#endregion
|
|
229
2067
|
//#region ../../src/command/errors/CommandError.ts
|
|
230
2068
|
var CommandError = class extends AlephaError {
|
|
@@ -239,6 +2077,7 @@ var Runner = class {
|
|
|
239
2077
|
startTime = Date.now();
|
|
240
2078
|
prettyPrint = $inject(PrettyPrint);
|
|
241
2079
|
alepha = $inject(Alepha);
|
|
2080
|
+
shell = $inject(ShellProvider);
|
|
242
2081
|
run;
|
|
243
2082
|
cliName = "";
|
|
244
2083
|
commandName = "";
|
|
@@ -305,19 +2144,9 @@ var Runner = class {
|
|
|
305
2144
|
return runFn;
|
|
306
2145
|
}
|
|
307
2146
|
async exec(cmd, opts = {}) {
|
|
308
|
-
return
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
env: {
|
|
312
|
-
...process.env,
|
|
313
|
-
LOG_FORMAT: "pretty"
|
|
314
|
-
}
|
|
315
|
-
}, (err, stdout$1) => {
|
|
316
|
-
if (err) {
|
|
317
|
-
err.stdout = stdout$1;
|
|
318
|
-
reject(err);
|
|
319
|
-
} else resolve(stdout$1);
|
|
320
|
-
});
|
|
2147
|
+
return this.shell.run(cmd, {
|
|
2148
|
+
root: opts.root,
|
|
2149
|
+
capture: true
|
|
321
2150
|
});
|
|
322
2151
|
}
|
|
323
2152
|
/**
|
|
@@ -327,7 +2156,7 @@ var Runner = class {
|
|
|
327
2156
|
*/
|
|
328
2157
|
async execute(task) {
|
|
329
2158
|
if (Array.isArray(task)) {
|
|
330
|
-
await Promise.all(task.map((t
|
|
2159
|
+
await Promise.all(task.map((t) => this.executeTask(t)));
|
|
331
2160
|
return "";
|
|
332
2161
|
} else return await this.executeTask(task);
|
|
333
2162
|
}
|
|
@@ -350,15 +2179,15 @@ var Runner = class {
|
|
|
350
2179
|
const taskId = task.name;
|
|
351
2180
|
if (this.useDynamicLogger) this.prettyPrint.startSpinner(taskId, task.name);
|
|
352
2181
|
else this.log.info(`Starting '${task.name}' ...`);
|
|
353
|
-
let stdout
|
|
2182
|
+
let stdout = "";
|
|
354
2183
|
try {
|
|
355
|
-
stdout
|
|
2184
|
+
stdout = String(await task.handler() ?? "");
|
|
356
2185
|
} catch (error) {
|
|
357
2186
|
if (this.useDynamicLogger) this.prettyPrint.error(taskId, task.name);
|
|
358
2187
|
if (error instanceof Error && "stdout" in error) this.log.info(`\n\n${error.stdout}`);
|
|
359
2188
|
throw new CommandError(`Task '${task.name}' failed`, { cause: error });
|
|
360
2189
|
}
|
|
361
|
-
if (stdout
|
|
2190
|
+
if (stdout) this.log.trace(stdout);
|
|
362
2191
|
const duration = ((Date.now() - now) / 1e3).toFixed(1);
|
|
363
2192
|
if (this.useDynamicLogger) this.prettyPrint.success(taskId, task.name, `${duration}s`);
|
|
364
2193
|
else this.log.info(`Finished '${task.name}' after ${duration}s`);
|
|
@@ -366,7 +2195,7 @@ var Runner = class {
|
|
|
366
2195
|
name: task.name,
|
|
367
2196
|
duration: `${duration}s`
|
|
368
2197
|
});
|
|
369
|
-
return stdout
|
|
2198
|
+
return stdout;
|
|
370
2199
|
}
|
|
371
2200
|
renderTable(data) {
|
|
372
2201
|
if (data.length === 0) return;
|
|
@@ -494,6 +2323,32 @@ const cliOptions = $atom({
|
|
|
494
2323
|
}),
|
|
495
2324
|
default: {}
|
|
496
2325
|
});
|
|
2326
|
+
/**
|
|
2327
|
+
* CLI provider for parsing and executing commands.
|
|
2328
|
+
*
|
|
2329
|
+
* Handles:
|
|
2330
|
+
* - Command resolution (simple, nested, colon-notation)
|
|
2331
|
+
* - Flag and argument parsing
|
|
2332
|
+
* - Environment variable validation
|
|
2333
|
+
* - Help generation
|
|
2334
|
+
* - Pre/post command hooks
|
|
2335
|
+
*
|
|
2336
|
+
* @example
|
|
2337
|
+
* ```typescript
|
|
2338
|
+
* // Define a command
|
|
2339
|
+
* class MyCommands {
|
|
2340
|
+
* build = $command({
|
|
2341
|
+
* name: "build",
|
|
2342
|
+
* description: "Build the project",
|
|
2343
|
+
* flags: t.object({ watch: t.optional(t.boolean()) }),
|
|
2344
|
+
* handler: async ({ flags }) => { ... }
|
|
2345
|
+
* });
|
|
2346
|
+
* }
|
|
2347
|
+
*
|
|
2348
|
+
* // CLI automatically discovers and executes commands
|
|
2349
|
+
* const alepha = Alepha.create().with(MyCommands);
|
|
2350
|
+
* ```
|
|
2351
|
+
*/
|
|
497
2352
|
var CliProvider = class {
|
|
498
2353
|
env = $env(envSchema);
|
|
499
2354
|
alepha = $inject(Alepha);
|
|
@@ -512,11 +2367,18 @@ var CliProvider = class {
|
|
|
512
2367
|
get argv() {
|
|
513
2368
|
return this.options.argv || (typeof process !== "undefined" ? process.argv.slice(2) : []);
|
|
514
2369
|
}
|
|
2370
|
+
/**
|
|
2371
|
+
* Global flags available to all commands.
|
|
2372
|
+
*/
|
|
515
2373
|
globalFlags = { help: {
|
|
516
2374
|
aliases: ["h", "help"],
|
|
517
2375
|
description: "Show this help message",
|
|
518
2376
|
schema: t.boolean()
|
|
519
2377
|
} };
|
|
2378
|
+
/**
|
|
2379
|
+
* Main entry point - resolves and executes the command from process.argv.
|
|
2380
|
+
* This is the production execution path with full lifecycle support.
|
|
2381
|
+
*/
|
|
520
2382
|
onReady = $hook({
|
|
521
2383
|
on: "ready",
|
|
522
2384
|
handler: async () => {
|
|
@@ -526,7 +2388,7 @@ var CliProvider = class {
|
|
|
526
2388
|
if (this.parseFlags(argv, Object.entries(this.getAllGlobalFlags()).map(([key, value]) => ({
|
|
527
2389
|
key,
|
|
528
2390
|
...value
|
|
529
|
-
}))).help) {
|
|
2391
|
+
})), { strict: false }).help) {
|
|
530
2392
|
this.printHelp(command);
|
|
531
2393
|
return;
|
|
532
2394
|
}
|
|
@@ -549,7 +2411,15 @@ var CliProvider = class {
|
|
|
549
2411
|
}
|
|
550
2412
|
});
|
|
551
2413
|
/**
|
|
552
|
-
* Execute a command with
|
|
2414
|
+
* Execute a command with full lifecycle support.
|
|
2415
|
+
*
|
|
2416
|
+
* This is the production execution path that includes:
|
|
2417
|
+
* - Mode-based .env file loading
|
|
2418
|
+
* - Pre/post command hooks
|
|
2419
|
+
* - Runner session for pretty CLI output
|
|
2420
|
+
* - Alepha context wrapper for proper scoping
|
|
2421
|
+
*
|
|
2422
|
+
* @see run() for a lightweight test-only alternative
|
|
553
2423
|
*/
|
|
554
2424
|
async executeCommand(command, argv, isRootCommand) {
|
|
555
2425
|
const root = process.cwd();
|
|
@@ -559,7 +2429,7 @@ var CliProvider = class {
|
|
|
559
2429
|
if (modeValue === void 0 && typeof command.options.mode === "string") modeValue = command.options.mode;
|
|
560
2430
|
await this.loadModeEnv(root, modeValue);
|
|
561
2431
|
}
|
|
562
|
-
const commandFlags = this.parseCommandFlags(argv, command.flags);
|
|
2432
|
+
const commandFlags = this.parseCommandFlags(argv, command.flags, { modeEnabled: !!command.options.mode });
|
|
563
2433
|
const commandArgs = this.parseCommandArgs(argv, command.options.args, isRootCommand, command.flags);
|
|
564
2434
|
const commandEnv = this.parseCommandEnv(command.env, command.name);
|
|
565
2435
|
await this.alepha.context.run(async () => {
|
|
@@ -598,7 +2468,7 @@ var CliProvider = class {
|
|
|
598
2468
|
});
|
|
599
2469
|
}
|
|
600
2470
|
/**
|
|
601
|
-
* Remove consumed command path arguments from argv.
|
|
2471
|
+
* Remove consumed command path arguments from argv (keeps flags and remaining args).
|
|
602
2472
|
*/
|
|
603
2473
|
removeConsumedArgs(argv, consumedArgs) {
|
|
604
2474
|
const result = [];
|
|
@@ -650,38 +2520,94 @@ var CliProvider = class {
|
|
|
650
2520
|
consumedArgs
|
|
651
2521
|
};
|
|
652
2522
|
}
|
|
2523
|
+
/**
|
|
2524
|
+
* Get all registered commands in the application.
|
|
2525
|
+
*/
|
|
653
2526
|
get commands() {
|
|
654
2527
|
return this.alepha.primitives($command);
|
|
655
2528
|
}
|
|
2529
|
+
/**
|
|
2530
|
+
* Execute a command handler with given arguments.
|
|
2531
|
+
*
|
|
2532
|
+
* This is a **lightweight test helper** that directly invokes the command handler
|
|
2533
|
+
* without the full production lifecycle. It intentionally skips:
|
|
2534
|
+
* - Pre/post command hooks
|
|
2535
|
+
* - Runner session (pretty CLI output)
|
|
2536
|
+
* - Alepha context wrapper
|
|
2537
|
+
* - .env.{mode} file loading
|
|
2538
|
+
*
|
|
2539
|
+
* For production execution, the `onReady` hook uses `executeCommand()` which
|
|
2540
|
+
* provides the full lifecycle. Merging them would either make this method too
|
|
2541
|
+
* heavy for simple testing or require many optional parameters to toggle behaviors.
|
|
2542
|
+
*
|
|
2543
|
+
* @example
|
|
2544
|
+
* ```typescript
|
|
2545
|
+
* // In tests
|
|
2546
|
+
* const cli = alepha.inject(CliProvider);
|
|
2547
|
+
* const cmd = alepha.inject(InitCommand);
|
|
2548
|
+
*
|
|
2549
|
+
* await cli.run(cmd.init, "--agent --pm=yarn");
|
|
2550
|
+
* await cli.run(cmd.init, { argv: "--agent", root: "/project" });
|
|
2551
|
+
* ```
|
|
2552
|
+
*/
|
|
2553
|
+
async run(command, options = {}) {
|
|
2554
|
+
const opts = typeof options === "string" || Array.isArray(options) ? { argv: options } : options;
|
|
2555
|
+
const args = typeof opts.argv === "string" ? opts.argv.split(" ").filter(Boolean) : opts.argv ?? [];
|
|
2556
|
+
const root = opts.root ?? process.cwd();
|
|
2557
|
+
const commandFlags = this.parseCommandFlags(args, command.flags, { modeEnabled: !!command.options.mode });
|
|
2558
|
+
const commandArgs = this.parseCommandArgs(args, command.options.args, true, command.flags);
|
|
2559
|
+
const commandEnv = this.parseCommandEnv(command.env, command.name);
|
|
2560
|
+
let modeValue;
|
|
2561
|
+
if (command.options.mode) {
|
|
2562
|
+
modeValue = this.parseModeFlag(args);
|
|
2563
|
+
if (modeValue === void 0 && typeof command.options.mode === "string") modeValue = command.options.mode;
|
|
2564
|
+
}
|
|
2565
|
+
await command.options.handler({
|
|
2566
|
+
flags: commandFlags,
|
|
2567
|
+
args: commandArgs,
|
|
2568
|
+
env: commandEnv,
|
|
2569
|
+
run: this.runner.run,
|
|
2570
|
+
ask: this.asker.ask,
|
|
2571
|
+
fs,
|
|
2572
|
+
glob,
|
|
2573
|
+
root,
|
|
2574
|
+
help: () => this.printHelp(command),
|
|
2575
|
+
mode: modeValue
|
|
2576
|
+
});
|
|
2577
|
+
}
|
|
2578
|
+
/** Find a command by name or alias */
|
|
656
2579
|
findCommand(name) {
|
|
657
2580
|
return this.commands.findLast((command) => command.name === name || command.aliases.includes(name));
|
|
658
2581
|
}
|
|
659
|
-
/**
|
|
660
|
-
* Find all pre-hooks for a command.
|
|
661
|
-
*/
|
|
2582
|
+
/** Find all pre-hooks for a command (commands named `pre{commandName}`) */
|
|
662
2583
|
findPreHooks(commandName) {
|
|
663
2584
|
return this.commands.filter((cmd) => cmd.name === `pre${commandName}`);
|
|
664
2585
|
}
|
|
665
|
-
/**
|
|
666
|
-
* Find all post-hooks for a command.
|
|
667
|
-
*/
|
|
2586
|
+
/** Find all post-hooks for a command (commands named `post{commandName}`) */
|
|
668
2587
|
findPostHooks(commandName) {
|
|
669
2588
|
return this.commands.filter((cmd) => cmd.name === `post${commandName}`);
|
|
670
2589
|
}
|
|
671
|
-
/**
|
|
672
|
-
* Get global flags (help only, root command flags are NOT global).
|
|
673
|
-
*/
|
|
2590
|
+
/** Get global flags (help only, root command flags are NOT global) */
|
|
674
2591
|
getAllGlobalFlags() {
|
|
675
2592
|
return { ...this.globalFlags };
|
|
676
2593
|
}
|
|
677
|
-
|
|
2594
|
+
/** Parse command flags from argv using the command's flag schema */
|
|
2595
|
+
parseCommandFlags(argv, schema, options = {}) {
|
|
2596
|
+
const { modeEnabled = false } = options;
|
|
678
2597
|
const flagDefs = Object.entries(schema.properties).map(([key, value]) => ({
|
|
679
2598
|
key,
|
|
680
2599
|
aliases: [key, ...value.aliases ?? (value.alias ? [value.alias] : void 0) ?? []],
|
|
681
2600
|
description: value.description,
|
|
682
2601
|
schema: value
|
|
683
2602
|
}));
|
|
2603
|
+
if (modeEnabled) flagDefs.push({
|
|
2604
|
+
key: "__mode__",
|
|
2605
|
+
aliases: ["mode", "m"],
|
|
2606
|
+
description: void 0,
|
|
2607
|
+
schema: t.string()
|
|
2608
|
+
});
|
|
684
2609
|
const parsed = this.parseFlags(argv, flagDefs);
|
|
2610
|
+
delete parsed.__mode__;
|
|
685
2611
|
for (const [key, value] of Object.entries(schema.properties)) if (!(key in parsed) && t.schema.isOptional(value)) {
|
|
686
2612
|
const innerSchema = value;
|
|
687
2613
|
if (innerSchema && "default" in innerSchema) parsed[key] = innerSchema.default;
|
|
@@ -693,6 +2619,7 @@ var CliProvider = class {
|
|
|
693
2619
|
throw error;
|
|
694
2620
|
}
|
|
695
2621
|
}
|
|
2622
|
+
/** Parse and validate environment variables using the command's env schema */
|
|
696
2623
|
parseCommandEnv(schema, commandName) {
|
|
697
2624
|
const result = {};
|
|
698
2625
|
const missing = [];
|
|
@@ -714,9 +2641,7 @@ var CliProvider = class {
|
|
|
714
2641
|
throw error;
|
|
715
2642
|
}
|
|
716
2643
|
}
|
|
717
|
-
/**
|
|
718
|
-
* Parse --mode or -m flag from argv.
|
|
719
|
-
*/
|
|
2644
|
+
/** Parse --mode or -m flag from argv for environment file loading */
|
|
720
2645
|
parseModeFlag(argv) {
|
|
721
2646
|
for (let i = 0; i < argv.length; i++) {
|
|
722
2647
|
const arg = argv[i];
|
|
@@ -728,16 +2653,16 @@ var CliProvider = class {
|
|
|
728
2653
|
}
|
|
729
2654
|
}
|
|
730
2655
|
}
|
|
731
|
-
/**
|
|
732
|
-
* Load environment files based on mode.
|
|
733
|
-
*/
|
|
2656
|
+
/** Load .env and .env.{mode} files into process.env */
|
|
734
2657
|
async loadModeEnv(root, mode) {
|
|
735
2658
|
const envFiles = [".env"];
|
|
736
2659
|
if (mode) envFiles.push(`.env.${mode}`);
|
|
737
2660
|
this.log.debug(`Loading env files: ${envFiles.join(", ")}`);
|
|
738
2661
|
await this.envUtils.loadEnv(root, envFiles);
|
|
739
2662
|
}
|
|
740
|
-
|
|
2663
|
+
/** Low-level flag parser - extracts flag values from argv based on definitions */
|
|
2664
|
+
parseFlags(argv, flagDefs, options = {}) {
|
|
2665
|
+
const { strict = true } = options;
|
|
741
2666
|
const result = {};
|
|
742
2667
|
for (let i = 0; i < argv.length; i++) {
|
|
743
2668
|
const arg = argv[i];
|
|
@@ -745,7 +2670,10 @@ var CliProvider = class {
|
|
|
745
2670
|
const [rawKey, ...valueParts] = arg.replace(/^-{1,2}/, "").split("=");
|
|
746
2671
|
let value = valueParts.join("=");
|
|
747
2672
|
const def = flagDefs.find((d) => d.aliases.includes(rawKey));
|
|
748
|
-
if (!def)
|
|
2673
|
+
if (!def) {
|
|
2674
|
+
if (strict) throw new CommandError(`Unknown flag: --${rawKey}`);
|
|
2675
|
+
continue;
|
|
2676
|
+
}
|
|
749
2677
|
if (t.schema.isBoolean(def.schema)) result[def.key] = true;
|
|
750
2678
|
else if (value) try {
|
|
751
2679
|
if (t.schema.isObject(def.schema) || t.schema.isArray(def.schema)) result[def.key] = JSON.parse(value);
|
|
@@ -768,9 +2696,7 @@ var CliProvider = class {
|
|
|
768
2696
|
}
|
|
769
2697
|
return result;
|
|
770
2698
|
}
|
|
771
|
-
/**
|
|
772
|
-
* Get indices of argv elements that are consumed by flags (including space-separated values).
|
|
773
|
-
*/
|
|
2699
|
+
/** Get indices of argv elements consumed by flags (for separating args from flags) */
|
|
774
2700
|
getFlagConsumedIndices(argv, flagDefs) {
|
|
775
2701
|
const consumed = /* @__PURE__ */ new Set();
|
|
776
2702
|
for (let i = 0; i < argv.length; i++) {
|
|
@@ -821,6 +2747,7 @@ var CliProvider = class {
|
|
|
821
2747
|
throw error;
|
|
822
2748
|
}
|
|
823
2749
|
}
|
|
2750
|
+
/** Convert a string argument value to the appropriate type based on schema */
|
|
824
2751
|
parseArgumentValue(value, schema) {
|
|
825
2752
|
if (t.schema.isString(schema)) return value;
|
|
826
2753
|
if (t.schema.isNumber(schema) || t.schema.isInteger(schema)) {
|
|
@@ -837,21 +2764,23 @@ var CliProvider = class {
|
|
|
837
2764
|
}
|
|
838
2765
|
return value;
|
|
839
2766
|
}
|
|
2767
|
+
/** Generate usage string for command arguments (e.g., "<path>" or "[path]") */
|
|
840
2768
|
generateArgsUsage(schema) {
|
|
841
2769
|
if (!schema) return "";
|
|
842
2770
|
if (t.schema.isOptional(schema)) {
|
|
843
|
-
const typeName
|
|
844
|
-
return ` [${"title" in schema ? schema.title : "arg1"}${typeName
|
|
2771
|
+
const typeName = this.getTypeName(schema);
|
|
2772
|
+
return ` [${"title" in schema ? schema.title : "arg1"}${typeName}]`;
|
|
845
2773
|
}
|
|
846
2774
|
if (t.schema.isTuple(schema) && schema.items) return ` ${schema.items.map((item, index) => {
|
|
847
2775
|
const argName = `arg${index + 1}`;
|
|
848
|
-
const typeName
|
|
849
|
-
if (t.schema.isOptional(item)) return `[${argName}${typeName
|
|
850
|
-
return `<${argName}${typeName
|
|
2776
|
+
const typeName = this.getTypeName(item);
|
|
2777
|
+
if (t.schema.isOptional(item)) return `[${argName}${typeName}]`;
|
|
2778
|
+
return `<${argName}${typeName}>`;
|
|
851
2779
|
}).join(" ")}`;
|
|
852
2780
|
const typeName = this.getTypeName(schema);
|
|
853
2781
|
return ` <${"title" in schema ? schema.title : "arg1"}${typeName}>`;
|
|
854
2782
|
}
|
|
2783
|
+
/** Get display type name for a schema (e.g., ": number", ": boolean") */
|
|
855
2784
|
getTypeName(schema) {
|
|
856
2785
|
if (!schema) return "";
|
|
857
2786
|
if (t.schema.isString(schema)) return "";
|
|
@@ -860,6 +2789,12 @@ var CliProvider = class {
|
|
|
860
2789
|
if (t.schema.isBoolean(schema)) return ": boolean";
|
|
861
2790
|
return "";
|
|
862
2791
|
}
|
|
2792
|
+
/**
|
|
2793
|
+
* Print help for a specific command or general CLI help.
|
|
2794
|
+
*
|
|
2795
|
+
* @param command - If provided, shows help for this specific command.
|
|
2796
|
+
* If omitted, shows general CLI help with all commands.
|
|
2797
|
+
*/
|
|
863
2798
|
printHelp(command) {
|
|
864
2799
|
const cliName = this.name || "cli";
|
|
865
2800
|
const c = this.color;
|
|
@@ -958,30 +2893,26 @@ var CliProvider = class {
|
|
|
958
2893
|
}
|
|
959
2894
|
this.log.info("");
|
|
960
2895
|
}
|
|
961
|
-
/**
|
|
962
|
-
* Generate colored args usage string for help display.
|
|
963
|
-
*/
|
|
2896
|
+
/** Generate colored usage string for command arguments (for help display) */
|
|
964
2897
|
generateColoredArgsUsage(schema) {
|
|
965
2898
|
if (!schema) return "";
|
|
966
2899
|
const c = this.color;
|
|
967
2900
|
if (t.schema.isOptional(schema)) {
|
|
968
|
-
const typeName
|
|
969
|
-
const key
|
|
970
|
-
return ` ${c.set("GREY_DARK", `[${key
|
|
2901
|
+
const typeName = this.getTypeName(schema);
|
|
2902
|
+
const key = "title" in schema ? schema.title : "arg1";
|
|
2903
|
+
return ` ${c.set("GREY_DARK", `[${key}${typeName}]`)}`;
|
|
971
2904
|
}
|
|
972
2905
|
if (t.schema.isTuple(schema) && schema.items) return ` ${schema.items.map((item, index) => {
|
|
973
2906
|
const argName = `arg${index + 1}`;
|
|
974
|
-
const typeName
|
|
975
|
-
if (t.schema.isOptional(item)) return c.set("GREY_DARK", `[${argName}${typeName
|
|
976
|
-
return c.set("CYAN", `<${argName}${typeName
|
|
2907
|
+
const typeName = this.getTypeName(item);
|
|
2908
|
+
if (t.schema.isOptional(item)) return c.set("GREY_DARK", `[${argName}${typeName}]`);
|
|
2909
|
+
return c.set("CYAN", `<${argName}${typeName}>`);
|
|
977
2910
|
}).join(" ")}`;
|
|
978
2911
|
const typeName = this.getTypeName(schema);
|
|
979
2912
|
const key = "title" in schema ? schema.title : "arg1";
|
|
980
2913
|
return ` ${c.set("CYAN", `<${key}${typeName}>`)}`;
|
|
981
2914
|
}
|
|
982
|
-
/**
|
|
983
|
-
* Get the full command path (e.g., "deploy vercel" for a child command).
|
|
984
|
-
*/
|
|
2915
|
+
/** Get the full command path (e.g., "deploy vercel" for a nested command) */
|
|
985
2916
|
getCommandPath(command) {
|
|
986
2917
|
const path = [command.name];
|
|
987
2918
|
let current = command;
|
|
@@ -993,33 +2924,29 @@ var CliProvider = class {
|
|
|
993
2924
|
}
|
|
994
2925
|
return path.join(" ");
|
|
995
2926
|
}
|
|
996
|
-
/**
|
|
997
|
-
* Find the parent command of a given command.
|
|
998
|
-
*/
|
|
2927
|
+
/** Find the parent command of a nested command */
|
|
999
2928
|
findParentCommand(command) {
|
|
1000
2929
|
for (const cmd of this.commands) if (cmd.children.includes(command)) return cmd;
|
|
1001
2930
|
}
|
|
1002
|
-
/**
|
|
1003
|
-
* Get top-level commands (commands that are not children of other commands).
|
|
1004
|
-
*/
|
|
2931
|
+
/** Get top-level commands (commands that are not children of other commands) */
|
|
1005
2932
|
getTopLevelCommands() {
|
|
1006
2933
|
const allChildren = /* @__PURE__ */ new Set();
|
|
1007
2934
|
for (const command of this.commands) for (const child of command.children) allChildren.add(child);
|
|
1008
2935
|
return this.commands.filter((cmd) => !allChildren.has(cmd));
|
|
1009
2936
|
}
|
|
1010
|
-
/**
|
|
1011
|
-
* Get max length for child command display.
|
|
1012
|
-
*/
|
|
2937
|
+
/** Calculate max display length for child commands (for help alignment) */
|
|
1013
2938
|
getMaxChildCmdLength(children) {
|
|
1014
2939
|
return Math.max(...children.filter((c) => !c.options.hide).map((c) => {
|
|
1015
2940
|
return `${[c.name, ...c.aliases].join(", ")}${this.generateArgsUsage(c.options.args)}`.length;
|
|
1016
2941
|
}), 0);
|
|
1017
2942
|
}
|
|
2943
|
+
/** Calculate max display length for commands (for help alignment) */
|
|
1018
2944
|
getMaxCmdLength(commands) {
|
|
1019
2945
|
return Math.max(...commands.filter((c) => !c.options.hide && c.name !== "").map((c) => {
|
|
1020
2946
|
return `${[c.name, ...c.aliases].join(", ")}${c.hasChildren ? " <command>" : this.generateArgsUsage(c.options.args)}`.length;
|
|
1021
2947
|
}));
|
|
1022
2948
|
}
|
|
2949
|
+
/** Calculate max display length for flags (for help alignment) */
|
|
1023
2950
|
getMaxFlagLength(flags) {
|
|
1024
2951
|
return Math.max(...flags.map((f) => {
|
|
1025
2952
|
return (Array.isArray(f.aliases) ? f.aliases : [f.aliases]).map((a) => a.length === 1 ? `-${a}` : `--${a}`).join(", ").length;
|
|
@@ -1030,12 +2957,20 @@ var CliProvider = class {
|
|
|
1030
2957
|
//#endregion
|
|
1031
2958
|
//#region ../../src/command/index.ts
|
|
1032
2959
|
/**
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
2960
|
+
* | type | quality | stability |
|
|
2961
|
+
* |------|---------|-----------|
|
|
2962
|
+
* | tooling | rare | stable |
|
|
2963
|
+
*
|
|
2964
|
+
* Declarative CLI command framework.
|
|
1035
2965
|
*
|
|
1036
|
-
*
|
|
2966
|
+
* **Features:**
|
|
2967
|
+
* - CLI command definitions
|
|
2968
|
+
* - Interactive CLI prompts
|
|
2969
|
+
* - Command execution
|
|
2970
|
+
* - Formatted colored output
|
|
2971
|
+
* - Environment variable utilities
|
|
2972
|
+
* - Schema validation for CLI arguments
|
|
1037
2973
|
*
|
|
1038
|
-
* @see {@link $command}
|
|
1039
2974
|
* @module alepha.command
|
|
1040
2975
|
*/
|
|
1041
2976
|
const AlephaCommand = $module({
|