@powerlines/core 0.14.5 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/api.cjs +6 -5
- package/dist/constants/api.d.cts +3 -3
- package/dist/constants/api.d.cts.map +1 -1
- package/dist/constants/api.d.mts +3 -3
- package/dist/constants/api.d.mts.map +1 -1
- package/dist/constants/api.mjs +4 -4
- package/dist/constants/api.mjs.map +1 -1
- package/dist/constants/commands.cjs +3 -2
- package/dist/constants/commands.d.cts +1 -1
- package/dist/constants/commands.d.mts +1 -1
- package/dist/constants/commands.mjs +2 -2
- package/dist/constants/commands.mjs.map +1 -1
- package/dist/constants/devtools.cjs +23 -0
- package/dist/constants/devtools.d.cts +13 -0
- package/dist/constants/devtools.d.cts.map +1 -0
- package/dist/constants/devtools.d.mts +13 -0
- package/dist/constants/devtools.d.mts.map +1 -0
- package/dist/constants/devtools.mjs +14 -0
- package/dist/constants/devtools.mjs.map +1 -0
- package/dist/constants/environments.cjs +1 -0
- package/dist/constants/extensions.cjs +21 -0
- package/dist/constants/extensions.d.cts +5 -0
- package/dist/constants/extensions.d.cts.map +1 -0
- package/dist/constants/extensions.d.mts +5 -0
- package/dist/constants/extensions.d.mts.map +1 -0
- package/dist/constants/extensions.mjs +20 -0
- package/dist/constants/extensions.mjs.map +1 -0
- package/dist/constants/fs.cjs +1 -0
- package/dist/constants/hooks.cjs +1 -0
- package/dist/constants/index.cjs +48 -30
- package/dist/constants/index.d.cts +5 -3
- package/dist/constants/index.d.mts +5 -3
- package/dist/constants/index.mjs +5 -3
- package/dist/constants/log-level.cjs +90 -0
- package/dist/constants/log-level.d.cts +63 -0
- package/dist/constants/log-level.d.cts.map +1 -0
- package/dist/constants/log-level.d.mts +63 -0
- package/dist/constants/log-level.d.mts.map +1 -0
- package/dist/constants/log-level.mjs +82 -0
- package/dist/constants/log-level.mjs.map +1 -0
- package/dist/constants/meta.cjs +1 -0
- package/dist/constants/plugin.cjs +3 -2
- package/dist/constants/plugin.d.cts +2 -2
- package/dist/constants/plugin.d.mts +2 -2
- package/dist/context/base-context.cjs +127 -0
- package/dist/context/base-context.d.cts +81 -0
- package/dist/context/base-context.d.cts.map +1 -0
- package/dist/context/base-context.d.mts +81 -0
- package/dist/context/base-context.d.mts.map +1 -0
- package/dist/context/base-context.mjs +125 -0
- package/dist/context/base-context.mjs.map +1 -0
- package/dist/context/context.cjs +934 -0
- package/dist/context/context.d.cts +369 -0
- package/dist/context/context.d.cts.map +1 -0
- package/dist/context/context.d.mts +369 -0
- package/dist/context/context.d.mts.map +1 -0
- package/dist/context/context.mjs +932 -0
- package/dist/context/context.mjs.map +1 -0
- package/dist/context/environment-context.cjs +219 -0
- package/dist/context/environment-context.d.cts +100 -0
- package/dist/context/environment-context.d.cts.map +1 -0
- package/dist/context/environment-context.d.mts +100 -0
- package/dist/context/environment-context.d.mts.map +1 -0
- package/dist/context/environment-context.mjs +218 -0
- package/dist/context/environment-context.mjs.map +1 -0
- package/dist/context/execution-context.cjs +230 -0
- package/dist/context/execution-context.d.cts +101 -0
- package/dist/context/execution-context.d.cts.map +1 -0
- package/dist/context/execution-context.d.mts +101 -0
- package/dist/context/execution-context.d.mts.map +1 -0
- package/dist/context/execution-context.mjs +228 -0
- package/dist/context/execution-context.mjs.map +1 -0
- package/dist/context/index.cjs +12 -0
- package/dist/context/index.d.cts +6 -0
- package/dist/context/index.d.mts +6 -0
- package/dist/context/index.mjs +7 -0
- package/dist/context/plugin-context.cjs +83 -0
- package/dist/context/plugin-context.d.cts +18 -0
- package/dist/context/plugin-context.d.cts.map +1 -0
- package/dist/context/plugin-context.d.mts +18 -0
- package/dist/context/plugin-context.d.mts.map +1 -0
- package/dist/context/plugin-context.mjs +82 -0
- package/dist/context/plugin-context.mjs.map +1 -0
- package/dist/index.cjs +62 -9
- package/dist/index.d.cts +20 -9
- package/dist/index.d.mts +20 -9
- package/dist/index.mjs +17 -5
- package/dist/lib/config.cjs +160 -15
- package/dist/lib/config.d.cts +92 -6
- package/dist/lib/config.d.cts.map +1 -1
- package/dist/lib/config.d.mts +92 -6
- package/dist/lib/config.d.mts.map +1 -1
- package/dist/lib/config.mjs +154 -16
- package/dist/lib/config.mjs.map +1 -1
- package/dist/lib/context-helpers.cjs +43 -0
- package/dist/lib/context-helpers.d.cts +19 -0
- package/dist/lib/context-helpers.d.cts.map +1 -0
- package/dist/lib/context-helpers.d.mts +19 -0
- package/dist/lib/context-helpers.d.mts.map +1 -0
- package/dist/lib/context-helpers.mjs +41 -0
- package/dist/lib/context-helpers.mjs.map +1 -0
- package/dist/lib/entry.cjs +5 -5
- package/dist/lib/entry.d.cts.map +1 -1
- package/dist/lib/entry.d.mts.map +1 -1
- package/dist/lib/entry.mjs +5 -5
- package/dist/lib/entry.mjs.map +1 -1
- package/dist/lib/environment.cjs +72 -0
- package/dist/lib/environment.d.cts +12 -0
- package/dist/lib/environment.d.cts.map +1 -0
- package/dist/lib/environment.d.mts +12 -0
- package/dist/lib/environment.d.mts.map +1 -0
- package/dist/lib/environment.mjs +67 -0
- package/dist/lib/environment.mjs.map +1 -0
- package/dist/lib/events.cjs +43 -0
- package/dist/lib/events.d.cts +10 -0
- package/dist/lib/events.d.cts.map +1 -0
- package/dist/lib/events.d.mts +10 -0
- package/dist/lib/events.d.mts.map +1 -0
- package/dist/lib/events.mjs +42 -0
- package/dist/lib/events.mjs.map +1 -0
- package/dist/lib/generate-types.cjs +371 -0
- package/dist/lib/generate-types.d.cts +67 -0
- package/dist/lib/generate-types.d.cts.map +1 -0
- package/dist/lib/generate-types.d.mts +67 -0
- package/dist/lib/generate-types.d.mts.map +1 -0
- package/dist/lib/generate-types.mjs +368 -0
- package/dist/lib/generate-types.mjs.map +1 -0
- package/dist/lib/hooks.cjs +152 -0
- package/dist/lib/hooks.d.cts +28 -0
- package/dist/lib/hooks.d.cts.map +1 -0
- package/dist/lib/hooks.d.mts +28 -0
- package/dist/lib/hooks.d.mts.map +1 -0
- package/dist/lib/hooks.mjs +147 -0
- package/dist/lib/hooks.mjs.map +1 -0
- package/dist/lib/index.cjs +64 -9
- package/dist/lib/index.d.cts +15 -3
- package/dist/lib/index.d.mts +15 -3
- package/dist/lib/index.mjs +19 -5
- package/dist/lib/install-dependencies.cjs +25 -0
- package/dist/lib/install-dependencies.d.cts +12 -0
- package/dist/lib/install-dependencies.d.cts.map +1 -0
- package/dist/lib/install-dependencies.d.mts +12 -0
- package/dist/lib/install-dependencies.d.mts.map +1 -0
- package/dist/lib/install-dependencies.mjs +24 -0
- package/dist/lib/install-dependencies.mjs.map +1 -0
- package/dist/lib/meta.cjs +57 -0
- package/dist/lib/meta.d.cts +34 -0
- package/dist/lib/meta.d.cts.map +1 -0
- package/dist/lib/meta.d.mts +34 -0
- package/dist/lib/meta.d.mts.map +1 -0
- package/dist/lib/meta.mjs +54 -0
- package/dist/lib/meta.mjs.map +1 -0
- package/dist/lib/plugins.cjs +150 -0
- package/dist/lib/plugins.d.cts +40 -0
- package/dist/lib/plugins.d.cts.map +1 -0
- package/dist/lib/plugins.d.mts +40 -0
- package/dist/lib/plugins.d.mts.map +1 -0
- package/dist/lib/plugins.mjs +146 -0
- package/dist/lib/plugins.mjs.map +1 -0
- package/dist/lib/resolver.cjs +35 -0
- package/dist/lib/resolver.d.cts +21 -0
- package/dist/lib/resolver.d.cts.map +1 -0
- package/dist/lib/resolver.d.mts +21 -0
- package/dist/lib/resolver.d.mts.map +1 -0
- package/dist/lib/resolver.mjs +33 -0
- package/dist/lib/resolver.mjs.map +1 -0
- package/dist/lib/schemas.cjs +9 -0
- package/dist/lib/schemas.d.cts +2 -0
- package/dist/lib/schemas.d.mts +2 -0
- package/dist/lib/schemas.mjs +3 -0
- package/dist/lib/streaming-channel.cjs +260 -0
- package/dist/lib/streaming-channel.d.cts +133 -0
- package/dist/lib/streaming-channel.d.cts.map +1 -0
- package/dist/lib/streaming-channel.d.mts +133 -0
- package/dist/lib/streaming-channel.d.mts.map +1 -0
- package/dist/lib/streaming-channel.mjs +258 -0
- package/dist/lib/streaming-channel.mjs.map +1 -0
- package/dist/lib/typescript/index.cjs +16 -0
- package/dist/lib/typescript/index.d.cts +3 -0
- package/dist/lib/typescript/index.d.mts +3 -0
- package/dist/lib/typescript/index.mjs +4 -0
- package/dist/lib/typescript/ts-morph.cjs +105 -0
- package/dist/lib/typescript/ts-morph.d.cts +38 -0
- package/dist/lib/typescript/ts-morph.d.cts.map +1 -0
- package/dist/lib/typescript/ts-morph.d.mts +38 -0
- package/dist/lib/typescript/ts-morph.d.mts.map +1 -0
- package/dist/lib/typescript/ts-morph.mjs +102 -0
- package/dist/lib/typescript/ts-morph.mjs.map +1 -0
- package/dist/lib/typescript/tsconfig.cjs +253 -0
- package/dist/lib/typescript/tsconfig.d.cts +77 -0
- package/dist/lib/typescript/tsconfig.d.cts.map +1 -0
- package/dist/lib/typescript/tsconfig.d.mts +77 -0
- package/dist/lib/typescript/tsconfig.d.mts.map +1 -0
- package/dist/lib/typescript/tsconfig.mjs +240 -0
- package/dist/lib/typescript/tsconfig.mjs.map +1 -0
- package/dist/lib/unplugin/helpers.cjs +2 -2
- package/dist/lib/unplugin/module-resolution.cjs +18 -18
- package/dist/lib/unplugin/module-resolution.mjs +18 -18
- package/dist/lib/unplugin/module-resolution.mjs.map +1 -1
- package/dist/lib/unplugin/plugin.cjs +55 -62
- package/dist/lib/unplugin/plugin.d.cts +10 -11
- package/dist/lib/unplugin/plugin.d.cts.map +1 -1
- package/dist/lib/unplugin/plugin.d.mts +10 -11
- package/dist/lib/unplugin/plugin.d.mts.map +1 -1
- package/dist/lib/unplugin/plugin.mjs +55 -62
- package/dist/lib/unplugin/plugin.mjs.map +1 -1
- package/dist/lib/utilities/file-header.cjs +2 -2
- package/dist/lib/utilities/file-header.mjs +2 -2
- package/dist/lib/utilities/file-header.mjs.map +1 -1
- package/dist/lib/utilities/format.cjs +1 -1
- package/dist/lib/utilities/format.mjs +1 -1
- package/dist/lib/utilities/index.cjs +2 -2
- package/dist/lib/utilities/index.mjs +2 -2
- package/dist/lib/utilities/write-file.cjs +1 -2
- package/dist/lib/utilities/write-file.d.cts +1 -1
- package/dist/lib/utilities/write-file.d.cts.map +1 -1
- package/dist/lib/utilities/write-file.d.mts +1 -1
- package/dist/lib/utilities/write-file.d.mts.map +1 -1
- package/dist/lib/utilities/write-file.mjs +1 -2
- package/dist/lib/utilities/write-file.mjs.map +1 -1
- package/dist/lib/vfs.cjs +1104 -0
- package/dist/lib/vfs.d.cts +321 -0
- package/dist/lib/vfs.d.cts.map +1 -0
- package/dist/lib/vfs.d.mts +321 -0
- package/dist/lib/vfs.d.mts.map +1 -0
- package/dist/lib/vfs.mjs +1102 -0
- package/dist/lib/vfs.mjs.map +1 -0
- package/dist/plugin-base.cjs +1 -1
- package/dist/plugin-base.mjs +1 -1
- package/dist/plugin-base.mjs.map +1 -1
- package/dist/plugin-utils/build-helpers.cjs +2 -2
- package/dist/plugin-utils/build-helpers.mjs +2 -2
- package/dist/plugin-utils/build-helpers.mjs.map +1 -1
- package/dist/plugin-utils/context-helpers.cjs +22 -12
- package/dist/plugin-utils/context-helpers.d.cts +9 -1
- package/dist/plugin-utils/context-helpers.d.cts.map +1 -1
- package/dist/plugin-utils/context-helpers.d.mts +9 -1
- package/dist/plugin-utils/context-helpers.d.mts.map +1 -1
- package/dist/plugin-utils/context-helpers.mjs +22 -13
- package/dist/plugin-utils/context-helpers.mjs.map +1 -1
- package/dist/plugin-utils/filter.cjs +13 -5
- package/dist/plugin-utils/filter.d.cts +2 -1
- package/dist/plugin-utils/filter.d.cts.map +1 -1
- package/dist/plugin-utils/filter.d.mts +2 -1
- package/dist/plugin-utils/filter.d.mts.map +1 -1
- package/dist/plugin-utils/filter.mjs +13 -6
- package/dist/plugin-utils/filter.mjs.map +1 -1
- package/dist/plugin-utils/format.cjs +44 -0
- package/dist/plugin-utils/format.d.cts +11 -0
- package/dist/plugin-utils/format.d.cts.map +1 -0
- package/dist/plugin-utils/format.d.mts +11 -0
- package/dist/plugin-utils/format.d.mts.map +1 -0
- package/dist/plugin-utils/format.mjs +43 -0
- package/dist/plugin-utils/format.mjs.map +1 -0
- package/dist/plugin-utils/helpers.cjs +6 -6
- package/dist/plugin-utils/helpers.d.cts +15 -15
- package/dist/plugin-utils/helpers.d.cts.map +1 -1
- package/dist/plugin-utils/helpers.d.mts +15 -15
- package/dist/plugin-utils/helpers.d.mts.map +1 -1
- package/dist/plugin-utils/helpers.mjs +1 -1
- package/dist/plugin-utils/helpers.mjs.map +1 -1
- package/dist/plugin-utils/index.cjs +34 -4
- package/dist/plugin-utils/index.d.cts +7 -4
- package/dist/plugin-utils/index.d.mts +7 -4
- package/dist/plugin-utils/index.mjs +9 -6
- package/dist/plugin-utils/install.cjs +47 -0
- package/dist/plugin-utils/install.d.cts +23 -0
- package/dist/plugin-utils/install.d.cts.map +1 -0
- package/dist/plugin-utils/install.d.mts +23 -0
- package/dist/plugin-utils/install.d.mts.map +1 -0
- package/dist/plugin-utils/install.mjs +45 -0
- package/dist/plugin-utils/install.mjs.map +1 -0
- package/dist/plugin-utils/logging.cjs +588 -1
- package/dist/plugin-utils/logging.d.cts +93 -1
- package/dist/plugin-utils/logging.d.cts.map +1 -1
- package/dist/plugin-utils/logging.d.mts +93 -1
- package/dist/plugin-utils/logging.d.mts.map +1 -1
- package/dist/plugin-utils/logging.mjs +572 -1
- package/dist/plugin-utils/logging.mjs.map +1 -1
- package/dist/plugin-utils/merge.cjs +1 -1
- package/dist/plugin-utils/merge.mjs +1 -1
- package/dist/plugin-utils/modules.cjs +1 -1
- package/dist/plugin-utils/modules.mjs +1 -1
- package/dist/plugin-utils/modules.mjs.map +1 -1
- package/dist/plugin-utils/paths.cjs +6 -2
- package/dist/plugin-utils/paths.mjs +6 -2
- package/dist/plugin-utils/paths.mjs.map +1 -1
- package/dist/plugin-utils/virtual.cjs +80 -0
- package/dist/plugin-utils/virtual.d.cts +69 -0
- package/dist/plugin-utils/virtual.d.cts.map +1 -0
- package/dist/plugin-utils/virtual.d.mts +69 -0
- package/dist/plugin-utils/virtual.d.mts.map +1 -0
- package/dist/plugin-utils/virtual.mjs +73 -0
- package/dist/plugin-utils/virtual.mjs.map +1 -0
- package/dist/schemas/fs.cjs +232 -0
- package/dist/schemas/fs.d.cts +127 -0
- package/dist/schemas/fs.d.cts.map +1 -0
- package/dist/schemas/fs.d.mts +127 -0
- package/dist/schemas/fs.d.mts.map +1 -0
- package/dist/schemas/fs.mjs +226 -0
- package/dist/schemas/fs.mjs.map +1 -0
- package/dist/storage/base.cjs +216 -0
- package/dist/storage/base.d.cts +201 -0
- package/dist/storage/base.d.cts.map +1 -0
- package/dist/storage/base.d.mts +201 -0
- package/dist/storage/base.d.mts.map +1 -0
- package/dist/storage/base.mjs +215 -0
- package/dist/storage/base.mjs.map +1 -0
- package/dist/storage/file-system.cjs +180 -0
- package/dist/storage/file-system.d.cts +129 -0
- package/dist/storage/file-system.d.cts.map +1 -0
- package/dist/storage/file-system.d.mts +129 -0
- package/dist/storage/file-system.d.mts.map +1 -0
- package/dist/storage/file-system.mjs +179 -0
- package/dist/storage/file-system.mjs.map +1 -0
- package/dist/storage/helpers.cjs +37 -0
- package/dist/storage/helpers.d.cts +25 -0
- package/dist/storage/helpers.d.cts.map +1 -0
- package/dist/storage/helpers.d.mts +25 -0
- package/dist/storage/helpers.d.mts.map +1 -0
- package/dist/storage/helpers.mjs +34 -0
- package/dist/storage/helpers.mjs.map +1 -0
- package/dist/storage/index.cjs +12 -0
- package/dist/storage/index.d.cts +5 -0
- package/dist/storage/index.d.mts +5 -0
- package/dist/storage/index.mjs +6 -0
- package/dist/storage/virtual.cjs +98 -0
- package/dist/storage/virtual.d.cts +80 -0
- package/dist/storage/virtual.d.cts.map +1 -0
- package/dist/storage/virtual.d.mts +80 -0
- package/dist/storage/virtual.d.mts.map +1 -0
- package/dist/storage/virtual.mjs +97 -0
- package/dist/storage/virtual.mjs.map +1 -0
- package/dist/types/api.d.cts +18 -152
- package/dist/types/api.d.cts.map +1 -1
- package/dist/types/api.d.mts +18 -152
- package/dist/types/api.d.mts.map +1 -1
- package/dist/types/config.d.cts +194 -135
- package/dist/types/config.d.cts.map +1 -1
- package/dist/types/config.d.mts +195 -136
- package/dist/types/config.d.mts.map +1 -1
- package/dist/types/context.d.cts +152 -128
- package/dist/types/context.d.cts.map +1 -1
- package/dist/types/context.d.mts +155 -131
- package/dist/types/context.d.mts.map +1 -1
- package/dist/types/fs.d.mts +1 -1
- package/dist/types/index.d.cts +5 -6
- package/dist/types/index.d.mts +5 -6
- package/dist/types/logging.d.cts +161 -0
- package/dist/types/logging.d.cts.map +1 -0
- package/dist/types/logging.d.mts +161 -0
- package/dist/types/logging.d.mts.map +1 -0
- package/dist/types/plugin.d.cts +6 -9
- package/dist/types/plugin.d.cts.map +1 -1
- package/dist/types/plugin.d.mts +6 -9
- package/dist/types/plugin.d.mts.map +1 -1
- package/dist/types/tsconfig.d.mts +1 -1
- package/dist/types/unplugin.d.cts +5 -26
- package/dist/types/unplugin.d.cts.map +1 -1
- package/dist/types/unplugin.d.mts +5 -26
- package/dist/types/unplugin.d.mts.map +1 -1
- package/package.json +259 -457
- package/schemas/fs.capnp +42 -0
- package/dist/constants/virtual-modules.cjs +0 -8
- package/dist/constants/virtual-modules.d.cts +0 -6
- package/dist/constants/virtual-modules.d.cts.map +0 -1
- package/dist/constants/virtual-modules.d.mts +0 -6
- package/dist/constants/virtual-modules.d.mts.map +0 -1
- package/dist/constants/virtual-modules.mjs +0 -7
- package/dist/constants/virtual-modules.mjs.map +0 -1
- package/dist/lib/logger.cjs +0 -99
- package/dist/lib/logger.d.cts +0 -53
- package/dist/lib/logger.d.cts.map +0 -1
- package/dist/lib/logger.d.mts +0 -53
- package/dist/lib/logger.d.mts.map +0 -1
- package/dist/lib/logger.mjs +0 -93
- package/dist/lib/logger.mjs.map +0 -1
- package/dist/types/_internal.cjs +0 -0
- package/dist/types/_internal.d.cts +0 -106
- package/dist/types/_internal.d.cts.map +0 -1
- package/dist/types/_internal.d.mts +0 -106
- package/dist/types/_internal.d.mts.map +0 -1
- package/dist/types/_internal.mjs +0 -1
- package/dist/types/commands.d.cts +0 -16
- package/dist/types/commands.d.cts.map +0 -1
- package/dist/types/commands.d.mts +0 -16
- package/dist/types/commands.d.mts.map +0 -1
- package/dist/types/utils.d.cts +0 -9
- package/dist/types/utils.d.cts.map +0 -1
- package/dist/types/utils.d.mts +0 -9
- package/dist/types/utils.d.mts.map +0 -1
package/dist/lib/vfs.mjs
ADDED
|
@@ -0,0 +1,1102 @@
|
|
|
1
|
+
import { format } from "./utilities/format.mjs";
|
|
2
|
+
import { replacePathTokens } from "../plugin-utils/paths.mjs";
|
|
3
|
+
import "../plugin-utils/index.mjs";
|
|
4
|
+
import { DEFAULT_EXTENSIONS } from "../constants/extensions.mjs";
|
|
5
|
+
import { FileSystem } from "../schemas/fs.mjs";
|
|
6
|
+
import { FileSystemStorageAdapter } from "../storage/file-system.mjs";
|
|
7
|
+
import { VirtualStorageAdapter } from "../storage/virtual.mjs";
|
|
8
|
+
import { existsSync } from "@stryke/fs/exists";
|
|
9
|
+
import { appendPath } from "@stryke/path/append";
|
|
10
|
+
import { findFileExtensionSafe, findFileName, findFilePath, hasFileExtension } from "@stryke/path/file-path-fns";
|
|
11
|
+
import { replaceExtension, replacePath } from "@stryke/path/replace";
|
|
12
|
+
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
13
|
+
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
14
|
+
import { isString } from "@stryke/type-checks/is-string";
|
|
15
|
+
import { toArray } from "@stryke/convert/to-array";
|
|
16
|
+
import { isSet } from "@stryke/type-checks/is-set";
|
|
17
|
+
import { getUnique } from "@stryke/helpers/get-unique";
|
|
18
|
+
import { isRegExp } from "@stryke/type-checks/is-regexp";
|
|
19
|
+
import { joinPaths } from "@stryke/path/join";
|
|
20
|
+
import { isAbsolutePath } from "@stryke/path/is-type";
|
|
21
|
+
import { isParentPath } from "@stryke/path/is-parent-path";
|
|
22
|
+
import { murmurhash } from "@stryke/hash";
|
|
23
|
+
import { getResolutionCombinations, resolve, resolveSync } from "@stryke/fs/resolve";
|
|
24
|
+
import { prettyBytes } from "@stryke/string-format/pretty-bytes";
|
|
25
|
+
import * as capnp from "@stryke/capnp";
|
|
26
|
+
import { readFileBuffer, readFileBufferSync, writeFileBuffer } from "@stryke/fs/buffer";
|
|
27
|
+
import { correctPath, stripStars } from "@stryke/path/correct-path";
|
|
28
|
+
import { globToRegex } from "@stryke/path/glob-to-regex";
|
|
29
|
+
import { slash } from "@stryke/path/slash";
|
|
30
|
+
import { match } from "bundle-require";
|
|
31
|
+
import { create } from "flat-cache";
|
|
32
|
+
import { Blob } from "node:buffer";
|
|
33
|
+
import { fileURLToPath } from "node:url";
|
|
34
|
+
|
|
35
|
+
//#region src/lib/vfs.ts
|
|
36
|
+
function toFilePath(path) {
|
|
37
|
+
return correctPath(slash(path?.toString() || ".").replace(/^file:\/\//, ""));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a given file id is valid based on the specified prefix.
|
|
41
|
+
*
|
|
42
|
+
* @param id - The file ID to check.
|
|
43
|
+
* @param prefix - The prefix to use for built-in files. Default is "powerlines".
|
|
44
|
+
* @returns `true` if the file ID is valid, otherwise `false`.
|
|
45
|
+
*/
|
|
46
|
+
function isValidId(id, prefix = "powerlines") {
|
|
47
|
+
return id.replace(/^\\0/, "").startsWith(`${prefix.replace(/:$/, "")}`);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Formats a file id by removing the file extension and prepended runtime prefix.
|
|
51
|
+
*
|
|
52
|
+
* @param id - The file ID to format.
|
|
53
|
+
* @param prefix - The prefix to use for built-in files. Default is "powerlines".
|
|
54
|
+
* @returns The formatted file ID.
|
|
55
|
+
*/
|
|
56
|
+
function normalizeId(id, prefix = "powerlines") {
|
|
57
|
+
return replaceExtension(toFilePath(id)).replace(/^\\0/, "").replace(/^powerlines:/, "").replace(new RegExp(`^${prefix.replace(/:$/, "")}:`), "");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Normalizes a given path by resolving it against the project root, workspace root, and built-ins path.
|
|
61
|
+
*
|
|
62
|
+
* @param path - The path to normalize.
|
|
63
|
+
* @param builtinsPath - The path to built-in files.
|
|
64
|
+
* @param prefix - The prefix to use for built-in files. Default is "powerlines".
|
|
65
|
+
* @returns The normalized path.
|
|
66
|
+
*/
|
|
67
|
+
function normalizePath(path, builtinsPath, prefix = "powerlines") {
|
|
68
|
+
if (!isSetString(path)) {
|
|
69
|
+
if (!isString(path)) throw new Error("Path type must be a string or a file descriptor");
|
|
70
|
+
throw new Error("Path cannot be empty");
|
|
71
|
+
}
|
|
72
|
+
return isAbsolutePath(path) ? path : isValidId(toFilePath(path), prefix) ? normalizeId(toFilePath(path), prefix).replace(new RegExp(`^${prefix.replace(/:$/, "")}:`), builtinsPath) : toFilePath(path);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Normalizes glob patterns by resolving them against the workspace root.
|
|
76
|
+
*
|
|
77
|
+
* @param workspaceRoot - The root directory of the workspace.
|
|
78
|
+
* @param patterns - The glob patterns to normalize.
|
|
79
|
+
* @returns An array of normalized glob patterns.
|
|
80
|
+
*/
|
|
81
|
+
function normalizeGlobPatterns(workspaceRoot, patterns) {
|
|
82
|
+
return getUnique(toArray(patterns).map((pattern) => {
|
|
83
|
+
if (isSetObject(pattern) && (isSetString(pattern.input) || isSetString(pattern.glob))) return joinPaths(pattern.input || workspaceRoot, pattern.glob || "**/*");
|
|
84
|
+
else if (!isSetString(pattern)) return;
|
|
85
|
+
return pattern;
|
|
86
|
+
}).filter(isSetString));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Represents a virtual file system (VFS) that stores files and their associated metadata in virtual memory.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* This class provides methods to manage virtual files, check their existence, retrieve their content, and manipulate the virtual file system. It allows for efficient file management and retrieval without relying on the actual file system.
|
|
93
|
+
*/
|
|
94
|
+
var VirtualFileSystem = class VirtualFileSystem {
|
|
95
|
+
/**
|
|
96
|
+
* A map of virtual file IDs to their associated metadata.
|
|
97
|
+
*/
|
|
98
|
+
#metadata;
|
|
99
|
+
/**
|
|
100
|
+
* A map of underlying file paths to their virtual file IDs.
|
|
101
|
+
*/
|
|
102
|
+
#ids;
|
|
103
|
+
/**
|
|
104
|
+
* A map of virtual file IDs to their underlying file paths.
|
|
105
|
+
*/
|
|
106
|
+
#paths;
|
|
107
|
+
/**
|
|
108
|
+
* The unified volume that combines the virtual file system with the real file system.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* This volume allows for seamless access to both virtual and real files.
|
|
112
|
+
*/
|
|
113
|
+
#storage;
|
|
114
|
+
/**
|
|
115
|
+
* A cache for module resolution results.
|
|
116
|
+
*/
|
|
117
|
+
#resolverCache;
|
|
118
|
+
/**
|
|
119
|
+
* Indicator specifying if the virtual file system (VFS) is disposed
|
|
120
|
+
*/
|
|
121
|
+
#isDisposed = false;
|
|
122
|
+
/**
|
|
123
|
+
* The context of the virtual file system.
|
|
124
|
+
*/
|
|
125
|
+
#context;
|
|
126
|
+
/**
|
|
127
|
+
* The file system's logger client utility.
|
|
128
|
+
*/
|
|
129
|
+
#logger;
|
|
130
|
+
/**
|
|
131
|
+
* Normalizes a given module id by resolving it against the built-ins path.
|
|
132
|
+
*
|
|
133
|
+
* @param id - The module id to normalize.
|
|
134
|
+
* @returns The normalized module id.
|
|
135
|
+
*/
|
|
136
|
+
#normalizeId(id) {
|
|
137
|
+
let normalized = id;
|
|
138
|
+
if (isParentPath(normalized, this.#context.builtinsPath)) normalized = replacePath(normalized, this.#context.builtinsPath);
|
|
139
|
+
return normalizeId(normalized, this.#context.config.framework?.name ?? "powerlines");
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Normalizes a given path by resolving it against the project root, workspace root, and built-ins path.
|
|
143
|
+
*
|
|
144
|
+
* @param path - The path to normalize.
|
|
145
|
+
* @returns The normalized path.
|
|
146
|
+
*/
|
|
147
|
+
#normalizePath(path) {
|
|
148
|
+
if (!isSetString(path)) {
|
|
149
|
+
if (!isString(path)) throw new Error("Path type must be a string or a file descriptor");
|
|
150
|
+
throw new Error("Path cannot be empty");
|
|
151
|
+
}
|
|
152
|
+
return normalizePath(path.includes("{") || path.includes("}") ? replacePathTokens(this.#context, path) : path, this.#context.builtinsPath, this.#context.config.framework?.name ?? "powerlines");
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Gets the storage adapter and relative key for a given key.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* The `key` can be either a path or a storage adapter name.
|
|
159
|
+
*
|
|
160
|
+
* @param key - The key to get the storage adapter for.
|
|
161
|
+
* @returns The storage adapter and relative key for the given key.
|
|
162
|
+
*/
|
|
163
|
+
#getStorage(key, preset) {
|
|
164
|
+
const path = this.resolveSync(this.#normalizePath(key)) || key;
|
|
165
|
+
for (const base of Object.keys(this.#storage).filter(Boolean).sort().reverse()) if ((path === base || isParentPath(path, base)) && (!preset || this.#storage[base]?.preset?.toLowerCase() === preset.toLowerCase())) return {
|
|
166
|
+
base,
|
|
167
|
+
relativeKey: replacePath(path, base),
|
|
168
|
+
adapter: this.#storage[base]
|
|
169
|
+
};
|
|
170
|
+
if (!preset || this.#storage[""]?.preset?.toLowerCase() === preset.toLowerCase()) return {
|
|
171
|
+
base: "",
|
|
172
|
+
relativeKey: path,
|
|
173
|
+
adapter: this.#storage[""]
|
|
174
|
+
};
|
|
175
|
+
this.#storage[path] = preset === "virtual" ? new VirtualStorageAdapter(this.#context, { base: path }) : new FileSystemStorageAdapter(this.#context, { base: path });
|
|
176
|
+
return {
|
|
177
|
+
base: path,
|
|
178
|
+
relativeKey: "",
|
|
179
|
+
adapter: this.#storage[path]
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Gets all storage adapters that match a given base key.
|
|
184
|
+
*
|
|
185
|
+
* @param base - The base key to match storage adapters against.
|
|
186
|
+
* @param includeParent - Whether to include parent storage adapters.
|
|
187
|
+
* @returns An array of storage adapters that match the given base key.
|
|
188
|
+
*/
|
|
189
|
+
#getStorages(base = "", includeParent = false) {
|
|
190
|
+
const baseKey = this.resolveSync(base) || base;
|
|
191
|
+
return Object.keys(this.#storage).sort().reverse().filter((key) => isParentPath(key, baseKey) || includeParent && isParentPath(baseKey, key) || baseKey.includes("*") && (isParentPath(stripStars(baseKey), key) || globToRegex(replaceExtension(baseKey)).test(key))).map((key) => ({
|
|
192
|
+
relativeBase: baseKey.length > key.length ? baseKey.slice(key.length) : void 0,
|
|
193
|
+
base: key,
|
|
194
|
+
adapter: this.#storage[key]
|
|
195
|
+
}));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A helper function to resolve modules in the virtual file system (VFS).
|
|
199
|
+
*
|
|
200
|
+
* @remarks
|
|
201
|
+
* This function can be used to resolve modules relative to the project root directory.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* const resolved = await context.resolvePath("some-module", "/path/to/importer");
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @param id - The module to resolve.
|
|
209
|
+
* @param importer - An optional path to the importer module.
|
|
210
|
+
* @param options - Additional resolution options.
|
|
211
|
+
* @returns A promise that resolves to the resolved module path.
|
|
212
|
+
*/
|
|
213
|
+
#innerResolve = async (id, importer, options = {}) => {
|
|
214
|
+
let path = id;
|
|
215
|
+
if (path.includes("{") || path.includes("}")) path = replacePathTokens(this.#context, path);
|
|
216
|
+
if (options.skipAlias !== true) path = this.resolveAlias(path);
|
|
217
|
+
if (isAbsolutePath(path) && (!options.isFile || !await this.isDirectory(path))) return path;
|
|
218
|
+
const resolverCacheKey = murmurhash({
|
|
219
|
+
path: this.#normalizeId(path),
|
|
220
|
+
importer,
|
|
221
|
+
options
|
|
222
|
+
});
|
|
223
|
+
let result;
|
|
224
|
+
if (!this.#context.config.skipCache) {
|
|
225
|
+
result = this.resolverCache.get(resolverCacheKey);
|
|
226
|
+
if (result) return result;
|
|
227
|
+
}
|
|
228
|
+
result = this.paths[this.#normalizeId(path)];
|
|
229
|
+
if (!isSetString(result)) {
|
|
230
|
+
const paths = options.paths ?? [];
|
|
231
|
+
if (importer && !paths.includes(importer)) paths.push(importer);
|
|
232
|
+
if (!importer) {
|
|
233
|
+
paths.push(this.#context.config.cwd);
|
|
234
|
+
paths.push(appendPath(this.#context.config.root, this.#context.config.cwd));
|
|
235
|
+
paths.push(appendPath(joinPaths(this.#context.config.root, "src"), this.#context.config.cwd));
|
|
236
|
+
}
|
|
237
|
+
paths.push(...Object.keys(this.#context.tsconfig?.options?.paths ?? {}).filter((tsconfigPath) => path.startsWith(tsconfigPath.replace(/\*$/, ""))).map((tsconfigPath) => this.#context.tsconfig?.options?.paths?.[tsconfigPath]).flat().filter(Boolean).map((tsconfigPath) => appendPath(tsconfigPath, this.#context.config.cwd)));
|
|
238
|
+
for (const combination of getResolutionCombinations(path, { paths: getUnique(paths) })) {
|
|
239
|
+
const { relativeKey, adapter } = this.#getStorage(combination);
|
|
240
|
+
if (await adapter.exists(relativeKey)) {
|
|
241
|
+
result = combination;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (!isSetString(result)) try {
|
|
246
|
+
result = await resolve(path, {
|
|
247
|
+
...options,
|
|
248
|
+
paths
|
|
249
|
+
});
|
|
250
|
+
} catch {}
|
|
251
|
+
}
|
|
252
|
+
if (isSetString(result)) {
|
|
253
|
+
if (!this.#context.config.skipCache) this.resolverCache.set(this.#normalizeId(path), result);
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* A synchronous helper function to resolve modules using the Jiti resolver
|
|
259
|
+
*
|
|
260
|
+
* @remarks
|
|
261
|
+
* This function can be used to resolve modules relative to the project root directory.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* const resolvedPath = context.resolveSync("some-module", "/path/to/importer");
|
|
266
|
+
* ```
|
|
267
|
+
*
|
|
268
|
+
* @param id - The module to resolve.
|
|
269
|
+
* @param importer - An optional path to the importer module.
|
|
270
|
+
* @param options - Additional resolution options.
|
|
271
|
+
* @returns The resolved module path.
|
|
272
|
+
*/
|
|
273
|
+
#innerResolveSync = (id, importer, options = {}) => {
|
|
274
|
+
let path = id;
|
|
275
|
+
if (path.includes("{") || path.includes("}")) path = replacePathTokens(this.#context, path);
|
|
276
|
+
if (options.skipAlias !== true) path = this.resolveAlias(path);
|
|
277
|
+
if (isAbsolutePath(path) && (!options.isFile || !this.isDirectorySync(path))) return path;
|
|
278
|
+
let result;
|
|
279
|
+
if (!this.#context.config.skipCache) {
|
|
280
|
+
result = this.resolverCache.get(this.#normalizeId(path));
|
|
281
|
+
if (isSetString(result)) return result;
|
|
282
|
+
}
|
|
283
|
+
result = this.paths[this.#normalizeId(path)];
|
|
284
|
+
if (!isSetString(result)) {
|
|
285
|
+
const paths = options.paths ?? [];
|
|
286
|
+
if (importer && !paths.includes(importer)) paths.push(importer);
|
|
287
|
+
if (!importer) {
|
|
288
|
+
paths.push(this.#context.config.cwd);
|
|
289
|
+
paths.push(appendPath(this.#context.config.root, this.#context.config.cwd));
|
|
290
|
+
paths.push(appendPath(joinPaths(this.#context.config.root, "src"), this.#context.config.cwd));
|
|
291
|
+
}
|
|
292
|
+
paths.push(...Object.keys(this.#context.tsconfig?.options?.paths ?? {}).filter((tsconfigPath) => path.startsWith(tsconfigPath.replace(/\*$/, ""))).map((tsconfigPath) => this.#context.tsconfig?.options?.paths?.[tsconfigPath]).flat().filter(Boolean).map((tsconfigPath) => appendPath(tsconfigPath, this.#context.config.cwd)));
|
|
293
|
+
for (const combination of getResolutionCombinations(path, { paths })) {
|
|
294
|
+
const { relativeKey, adapter } = this.#getStorage(combination);
|
|
295
|
+
if (adapter.existsSync(relativeKey)) {
|
|
296
|
+
result = combination;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (!isSetString(result)) try {
|
|
301
|
+
result = resolveSync(path, {
|
|
302
|
+
...options,
|
|
303
|
+
paths
|
|
304
|
+
});
|
|
305
|
+
} catch {}
|
|
306
|
+
}
|
|
307
|
+
if (isSetString(result)) {
|
|
308
|
+
if (!this.#context.config.skipCache) this.resolverCache.set(this.#normalizeId(path), result);
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Creates a virtual file system (VFS) that is backed up to a Cap'n Proto message buffer.
|
|
314
|
+
*
|
|
315
|
+
* @param context - The context of the virtual file system, typically containing options and logging functions.
|
|
316
|
+
* @returns A promise that resolves to a new virtual file system instance.
|
|
317
|
+
*/
|
|
318
|
+
static async create(context) {
|
|
319
|
+
context.debug("Starting virtual file system (VFS) initialization processes...");
|
|
320
|
+
let result;
|
|
321
|
+
if (!context.config.skipCache && existsSync(joinPaths(context.dataPath, "fs.bin"))) {
|
|
322
|
+
const buffer = await readFileBuffer(joinPaths(context.dataPath, "fs.bin"));
|
|
323
|
+
const fs = new capnp.Message(buffer, false).getRoot(FileSystem);
|
|
324
|
+
result = new VirtualFileSystem(context, fs);
|
|
325
|
+
if (fs._hasStorage() && fs.storage.length > 0) await Promise.all(fs.storage.values().map(async (file) => {
|
|
326
|
+
if (file.path && file.code) {
|
|
327
|
+
let id;
|
|
328
|
+
if (fs._hasIds()) id = fs.ids.find((fileId) => fileId.path === file.path);
|
|
329
|
+
let metadata;
|
|
330
|
+
if (fs._hasMetadata()) metadata = fs.metadata.find((meta) => meta.id === result.#normalizeId(id?.id ?? file.path));
|
|
331
|
+
await result.write(file.path, file.code, { meta: {
|
|
332
|
+
id: result.#normalizeId(id?.id ?? metadata?.id ?? file.path),
|
|
333
|
+
type: metadata?.type || "normal",
|
|
334
|
+
properties: metadata?._hasProperties() ? metadata?.properties.values().reduce((ret, kvp) => {
|
|
335
|
+
ret[kvp.key] = kvp.value;
|
|
336
|
+
return ret;
|
|
337
|
+
}, {}) : void 0,
|
|
338
|
+
timestamp: metadata?.timestamp
|
|
339
|
+
} });
|
|
340
|
+
}
|
|
341
|
+
}));
|
|
342
|
+
} else result = new VirtualFileSystem(context, new capnp.Message().initRoot(FileSystem));
|
|
343
|
+
result.#logger.debug("Successfully completed virtual file system (VFS) initialization.");
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Synchronously creates a virtual file system (VFS) that is backed up to a Cap'n Proto message buffer.
|
|
348
|
+
*
|
|
349
|
+
* @param context - The context of the virtual file system, typically containing options and logging functions.
|
|
350
|
+
* @returns A new virtual file system instance.
|
|
351
|
+
*/
|
|
352
|
+
static createSync(context) {
|
|
353
|
+
context.debug("Starting virtual file system (VFS) initialization processes...");
|
|
354
|
+
let result;
|
|
355
|
+
if (!context.config.skipCache && existsSync(joinPaths(context.dataPath, "fs.bin"))) {
|
|
356
|
+
const buffer = readFileBufferSync(joinPaths(context.dataPath, "fs.bin"));
|
|
357
|
+
const fs = new capnp.Message(buffer, false).getRoot(FileSystem);
|
|
358
|
+
result = new VirtualFileSystem(context, fs);
|
|
359
|
+
if (fs._hasStorage() && fs.storage.length > 0) fs.storage.values().forEach((file) => {
|
|
360
|
+
if (file.path && file.code) {
|
|
361
|
+
let id;
|
|
362
|
+
if (fs._hasIds()) id = fs.ids.find((fileId) => fileId.path === file.path);
|
|
363
|
+
let metadata;
|
|
364
|
+
if (fs._hasMetadata()) metadata = fs.metadata.find((meta) => meta.id === result.#normalizeId(id?.id ?? file.path));
|
|
365
|
+
result.writeSync(file.path, file.code, { meta: {
|
|
366
|
+
id: result.#normalizeId(id?.id ?? metadata?.id ?? file.path),
|
|
367
|
+
type: metadata?.type,
|
|
368
|
+
properties: metadata?._hasProperties() ? metadata?.properties.values().reduce((ret, kvp) => {
|
|
369
|
+
ret[kvp.key] = kvp.value;
|
|
370
|
+
return ret;
|
|
371
|
+
}, {}) : void 0,
|
|
372
|
+
timestamp: metadata?.timestamp
|
|
373
|
+
} });
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
} else result = new VirtualFileSystem(context, new capnp.Message().initRoot(FileSystem));
|
|
377
|
+
result.#logger.debug("Successfully completed virtual file system (VFS) initialization.");
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* A map of file ids to their metadata.
|
|
382
|
+
*/
|
|
383
|
+
get metadata() {
|
|
384
|
+
return new Proxy(this.#metadata, {
|
|
385
|
+
get: (target, prop) => {
|
|
386
|
+
return target[this.#normalizeId(prop)];
|
|
387
|
+
},
|
|
388
|
+
set: (target, prop, value) => {
|
|
389
|
+
target[this.#normalizeId(prop)] = value;
|
|
390
|
+
return true;
|
|
391
|
+
},
|
|
392
|
+
deleteProperty: (target, prop) => {
|
|
393
|
+
delete target[this.#normalizeId(prop)];
|
|
394
|
+
return true;
|
|
395
|
+
},
|
|
396
|
+
has: (target, prop) => {
|
|
397
|
+
return this.#normalizeId(prop) in target;
|
|
398
|
+
},
|
|
399
|
+
ownKeys: (target) => {
|
|
400
|
+
return getUnique(Reflect.ownKeys(target).map((key) => this.#normalizeId(key)));
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* A map of file paths to their module ids.
|
|
406
|
+
*/
|
|
407
|
+
get ids() {
|
|
408
|
+
return new Proxy(this.#ids, {
|
|
409
|
+
get: (target, prop) => {
|
|
410
|
+
return target[this.#normalizePath(prop)];
|
|
411
|
+
},
|
|
412
|
+
set: (target, prop, value) => {
|
|
413
|
+
target[this.#normalizePath(prop)] = value;
|
|
414
|
+
return true;
|
|
415
|
+
},
|
|
416
|
+
deleteProperty: (target, prop) => {
|
|
417
|
+
delete target[this.#normalizePath(prop)];
|
|
418
|
+
return true;
|
|
419
|
+
},
|
|
420
|
+
has: (target, prop) => {
|
|
421
|
+
return this.#normalizePath(prop) in target;
|
|
422
|
+
},
|
|
423
|
+
ownKeys: (target) => {
|
|
424
|
+
return getUnique(Reflect.ownKeys(target).map((key) => this.#normalizePath(key)));
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* A map of module ids to their file paths.
|
|
430
|
+
*/
|
|
431
|
+
get paths() {
|
|
432
|
+
return new Proxy(this.#paths, {
|
|
433
|
+
get: (target, prop) => {
|
|
434
|
+
return this.#normalizeId(prop) in target ? target[this.#normalizeId(prop)] : void 0;
|
|
435
|
+
},
|
|
436
|
+
set: (target, prop, value) => {
|
|
437
|
+
target[this.#normalizeId(prop)] = value;
|
|
438
|
+
return true;
|
|
439
|
+
},
|
|
440
|
+
deleteProperty: (target, prop) => {
|
|
441
|
+
delete target[this.#normalizeId(prop)];
|
|
442
|
+
return true;
|
|
443
|
+
},
|
|
444
|
+
has: (target, prop) => {
|
|
445
|
+
return this.#normalizeId(prop) in target;
|
|
446
|
+
},
|
|
447
|
+
ownKeys: (target) => {
|
|
448
|
+
return getUnique(Reflect.ownKeys(target).map((key) => this.#normalizeId(key)));
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Gets the resolver cache.
|
|
454
|
+
*/
|
|
455
|
+
get resolverCache() {
|
|
456
|
+
if (!this.#resolverCache) this.#resolverCache = create({
|
|
457
|
+
cacheId: "module-resolution",
|
|
458
|
+
cacheDir: this.#context.cachePath,
|
|
459
|
+
ttl: 2.5 * 60 * 1e3,
|
|
460
|
+
lruSize: 8e3,
|
|
461
|
+
persistInterval: 100
|
|
462
|
+
});
|
|
463
|
+
return this.#resolverCache;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Creates a new instance of the {@link VirtualFileSystem}.
|
|
467
|
+
*
|
|
468
|
+
* @param context - The context of the virtual file system, typically containing options and logging functions.
|
|
469
|
+
* @param fs - A buffer containing the serialized virtual file system data.
|
|
470
|
+
*/
|
|
471
|
+
constructor(context, fs) {
|
|
472
|
+
this.#context = context;
|
|
473
|
+
this.#storage = { "": new FileSystemStorageAdapter(context) };
|
|
474
|
+
if (isSetObject(this.#context.config.output.storage)) this.#storage = {
|
|
475
|
+
...this.#storage,
|
|
476
|
+
...this.#context.config.output.storage
|
|
477
|
+
};
|
|
478
|
+
this.#storage.virtual ??= new VirtualStorageAdapter(context, { base: "/_virtual" });
|
|
479
|
+
this.#storage[this.#context.config.output.path] ??= new FileSystemStorageAdapter(context, { base: this.#context.config.output.path });
|
|
480
|
+
if (this.#context.config.output.copy && this.#context.config.output.copy.path) this.#storage[this.#context.config.output.copy.path] ??= new FileSystemStorageAdapter(context, { base: this.#context.config.output.copy.path });
|
|
481
|
+
if (this.#context.config.output.storage !== "fs") {
|
|
482
|
+
this.#storage[this.#context.artifactsPath] ??= new VirtualStorageAdapter(context, { base: this.#context.artifactsPath });
|
|
483
|
+
this.#storage[this.#context.builtinsPath] ??= new VirtualStorageAdapter(context, { base: this.#context.builtinsPath });
|
|
484
|
+
this.#storage[this.#context.entryPath] ??= new VirtualStorageAdapter(context, { base: this.#context.entryPath });
|
|
485
|
+
}
|
|
486
|
+
this.#metadata = {};
|
|
487
|
+
if (fs._hasMetadata()) this.#metadata = fs.metadata.values().reduce((ret, metadata) => {
|
|
488
|
+
ret[metadata.id] = {
|
|
489
|
+
id: metadata.id,
|
|
490
|
+
type: metadata.type,
|
|
491
|
+
timestamp: metadata.timestamp ?? Date.now(),
|
|
492
|
+
properties: metadata._hasProperties() ? metadata.properties.values().reduce((ret, item) => {
|
|
493
|
+
ret[item.key] = item.value;
|
|
494
|
+
return ret;
|
|
495
|
+
}, {}) : {}
|
|
496
|
+
};
|
|
497
|
+
return ret;
|
|
498
|
+
}, {});
|
|
499
|
+
this.#ids = {};
|
|
500
|
+
this.#paths = {};
|
|
501
|
+
if (fs._hasIds()) {
|
|
502
|
+
this.#ids = fs.ids.values().reduce((ret, identifier) => {
|
|
503
|
+
ret[identifier.path] ??= identifier.id;
|
|
504
|
+
return ret;
|
|
505
|
+
}, {});
|
|
506
|
+
this.#paths = fs.ids.values().reduce((ret, identifier) => {
|
|
507
|
+
ret[identifier.id] ??= identifier.path;
|
|
508
|
+
return ret;
|
|
509
|
+
}, {});
|
|
510
|
+
}
|
|
511
|
+
this.#logger = context.extendLogger({ category: "fs" });
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Asynchronously checks if a file exists in the virtual file system (VFS).
|
|
515
|
+
*
|
|
516
|
+
* @param path - The path to the file.
|
|
517
|
+
* @returns A promise that resolves to `true` if the file exists, otherwise `false`.
|
|
518
|
+
*/
|
|
519
|
+
async exists(path) {
|
|
520
|
+
const { relativeKey, adapter } = this.#getStorage(path);
|
|
521
|
+
return adapter.exists(relativeKey);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Synchronously checks if a file exists in the virtual file system (VFS).
|
|
525
|
+
*
|
|
526
|
+
* @param path - The path to the file.
|
|
527
|
+
* @returns `true` if the file exists, otherwise `false`.
|
|
528
|
+
*/
|
|
529
|
+
existsSync(path) {
|
|
530
|
+
const { relativeKey, adapter } = this.#getStorage(path);
|
|
531
|
+
return adapter.existsSync(relativeKey);
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Checks if a file is virtual in the virtual file system (VFS).
|
|
535
|
+
*
|
|
536
|
+
* @param path - The path to the file.
|
|
537
|
+
* @param importer - An optional path to the importer module.
|
|
538
|
+
* @param options - Additional resolution options.
|
|
539
|
+
* @returns `true` if the file is virtual, otherwise `false`.
|
|
540
|
+
*/
|
|
541
|
+
isVirtual(path, importer, options) {
|
|
542
|
+
const resolved = this.resolveSync(path, importer, options);
|
|
543
|
+
if (!isSetString(resolved)) {
|
|
544
|
+
if (isSet(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
return this.#getStorage(resolved)?.adapter?.preset === "virtual" || resolved.startsWith(`${this.#context.config.framework?.name ?? "powerlines"}:`) || path.startsWith(`${this.#context.config.framework?.name ?? "powerlines"}:`);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Checks if a path is a directory in the virtual file system (VFS).
|
|
551
|
+
*
|
|
552
|
+
* @param path - The path to check.
|
|
553
|
+
* @param importer - An optional path to the importer module.
|
|
554
|
+
* @param options - Additional resolution options.
|
|
555
|
+
* @returns `true` if the path is a directory, otherwise `false`.
|
|
556
|
+
*/
|
|
557
|
+
isDirectorySync(path, importer, options) {
|
|
558
|
+
const resolved = this.resolveSync(path, importer, options);
|
|
559
|
+
if (!isSetString(resolved)) {
|
|
560
|
+
if (isSet(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
return !!(this.existsSync(resolved) && this.#getStorage(resolved)?.adapter?.isDirectorySync(resolved));
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Checks if a path is a directory in the virtual file system (VFS).
|
|
567
|
+
*
|
|
568
|
+
* @param path - The path to check.
|
|
569
|
+
* @param importer - An optional path to the importer module.
|
|
570
|
+
* @param options - Additional resolution options.
|
|
571
|
+
* @returns `true` if the path is a directory, otherwise `false`.
|
|
572
|
+
*/
|
|
573
|
+
async isDirectory(path, importer, options) {
|
|
574
|
+
const resolved = await this.resolve(path, importer, options);
|
|
575
|
+
if (!isSetString(resolved)) {
|
|
576
|
+
if (isSet(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
return !!(await this.exists(resolved) && await this.#getStorage(resolved)?.adapter?.isDirectory(resolved));
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Checks if a path is a file in the virtual file system (VFS).
|
|
583
|
+
*
|
|
584
|
+
* @param path - The path to check.
|
|
585
|
+
* @param importer - An optional path to the importer module.
|
|
586
|
+
* @param options - Additional resolution options.
|
|
587
|
+
* @returns `true` if the path is a file, otherwise `false`.
|
|
588
|
+
*/
|
|
589
|
+
isFileSync(path, importer, options) {
|
|
590
|
+
const resolved = this.resolveSync(path, importer, options);
|
|
591
|
+
if (!isSetString(resolved)) {
|
|
592
|
+
if (isSet(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
595
|
+
return this.#getStorage(resolved)?.adapter?.isFileSync(resolved) ?? false;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Checks if a path is a file in the virtual file system (VFS).
|
|
599
|
+
*
|
|
600
|
+
* @param path - The path to check.
|
|
601
|
+
* @param importer - An optional path to the importer module.
|
|
602
|
+
* @param options - Additional resolution options.
|
|
603
|
+
* @returns `true` if the path is a file, otherwise `false`.
|
|
604
|
+
*/
|
|
605
|
+
async isFile(path, importer, options) {
|
|
606
|
+
const resolved = await this.resolve(path, importer, options);
|
|
607
|
+
if (!isSetString(resolved)) {
|
|
608
|
+
if (isSet(resolved)) throw new Error(`Resolved path is not a string: ${String(resolved)}`);
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
611
|
+
return await this.#getStorage(resolved)?.adapter?.isFile(resolved) ?? false;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Checks if a file Id must be resolved by the virtual file system (VFS).
|
|
615
|
+
*
|
|
616
|
+
* @remarks
|
|
617
|
+
* Examples of file Ids that would be considered virtual include:
|
|
618
|
+
* - Ids that start with the framework specific prefix (e.g. `powerlines:`)
|
|
619
|
+
* - Ids that match a configured alias for virtual modules (returned true from {@link VirtualFileSystemInterface.isVirtual})
|
|
620
|
+
*
|
|
621
|
+
* @param path - The path or id of the file.
|
|
622
|
+
* @param importer - An optional path to the importer module, used for resolving the file path.
|
|
623
|
+
* @param options - Additional options for resolving the file path.
|
|
624
|
+
* @returns `true` if the file is virtual, otherwise `false`.
|
|
625
|
+
*/
|
|
626
|
+
isResolvableId(path, importer, options) {
|
|
627
|
+
return path.startsWith(`${this.#context.config.framework?.name ?? "powerlines"}:`) || this.isVirtual(path, importer, options) || this.isAlias(path) || this.isTsconfigPath(path);
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Lists files in a given path.
|
|
631
|
+
*
|
|
632
|
+
* @param path - The path to list files from.
|
|
633
|
+
* @returns An array of file names in the specified path.
|
|
634
|
+
*/
|
|
635
|
+
listSync(path) {
|
|
636
|
+
let resolvedPath = path;
|
|
637
|
+
if (resolvedPath.includes("*")) {
|
|
638
|
+
this.#logger.warn(`Invoking "listSync" with a glob pattern is not supported. It is likely you meant to use "globSync". Path: ${path}`);
|
|
639
|
+
resolvedPath = stripStars(resolvedPath);
|
|
640
|
+
}
|
|
641
|
+
return getUnique(this.#getStorages(resolvedPath, true).map((storage) => storage.adapter.listSync(storage.relativeBase ? storage.base ? appendPath(storage.relativeBase, storage.base) : storage.relativeBase : storage.base)).flat().filter(Boolean));
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Lists files in a given path.
|
|
645
|
+
*
|
|
646
|
+
* @param path - The path to list files from.
|
|
647
|
+
* @returns An array of file names in the specified path.
|
|
648
|
+
*/
|
|
649
|
+
async list(path) {
|
|
650
|
+
let resolvedPath = path;
|
|
651
|
+
if (resolvedPath.includes("*")) {
|
|
652
|
+
this.#logger.warn(`Invoking "list" with a glob pattern is not supported. It is likely you meant to use "glob". Path: ${path}`);
|
|
653
|
+
resolvedPath = stripStars(resolvedPath);
|
|
654
|
+
}
|
|
655
|
+
return getUnique((await Promise.all(this.#getStorages(resolvedPath, true).map(async (storage) => storage.adapter.list(storage.relativeBase ? storage.base ? appendPath(storage.relativeBase, storage.base) : storage.relativeBase : storage.base)))).flat().filter(Boolean));
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Removes a file in the virtual file system (VFS).
|
|
659
|
+
*
|
|
660
|
+
* @param path - The path to create the directory at.
|
|
661
|
+
*/
|
|
662
|
+
async remove(path) {
|
|
663
|
+
const normalizedPath = this.#normalizePath(path);
|
|
664
|
+
this.#logger.trace(`Removing file: ${normalizedPath}`);
|
|
665
|
+
const { relativeKey, adapter } = this.#getStorage(normalizedPath);
|
|
666
|
+
if (hasFileExtension(normalizedPath)) await adapter.remove(relativeKey);
|
|
667
|
+
else await adapter.clear(relativeKey);
|
|
668
|
+
const id = this.#ids[normalizedPath];
|
|
669
|
+
if (id && this.#metadata[id]) {
|
|
670
|
+
delete this.#metadata[id];
|
|
671
|
+
delete this.#ids[normalizedPath];
|
|
672
|
+
delete this.#paths[id];
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Removes a file in the virtual file system (VFS).
|
|
677
|
+
*
|
|
678
|
+
* @param path - The path to create the directory at.
|
|
679
|
+
*/
|
|
680
|
+
removeSync(path) {
|
|
681
|
+
const normalizedPath = this.#normalizePath(path);
|
|
682
|
+
this.#logger.trace(`Removing file: ${normalizedPath}`);
|
|
683
|
+
const { relativeKey, adapter } = this.#getStorage(normalizedPath);
|
|
684
|
+
if (hasFileExtension(normalizedPath)) adapter.removeSync(relativeKey);
|
|
685
|
+
else adapter.clearSync(relativeKey);
|
|
686
|
+
const id = this.#ids[normalizedPath];
|
|
687
|
+
if (id && this.#metadata[id]) {
|
|
688
|
+
delete this.#metadata[id];
|
|
689
|
+
delete this.#ids[normalizedPath];
|
|
690
|
+
delete this.#paths[id];
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Glob files in the virtual file system (VFS) based on the provided pattern(s).
|
|
695
|
+
*
|
|
696
|
+
* @param patterns - A pattern (or multiple patterns) to use to determine the file paths to return
|
|
697
|
+
* @returns An array of file paths matching the provided pattern(s)
|
|
698
|
+
*/
|
|
699
|
+
async glob(patterns) {
|
|
700
|
+
const results = [];
|
|
701
|
+
for (const pattern of normalizeGlobPatterns(this.#context.config.cwd, patterns)) {
|
|
702
|
+
const normalized = this.#normalizePath(pattern);
|
|
703
|
+
if (!/[*?[\]{}]/.test(normalized) && !normalized.includes("*")) if (this.isDirectorySync(normalized)) results.push(...await this.list(normalized));
|
|
704
|
+
else {
|
|
705
|
+
const resolved = await this.resolve(normalized);
|
|
706
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
const absPattern = isAbsolutePath(normalized) ? normalized : this.#normalizePath(appendPath(normalized, this.#context.config.cwd));
|
|
710
|
+
await Promise.all((await this.list(stripStars(absPattern))).map(async (file) => {
|
|
711
|
+
if (globToRegex(absPattern).test(file)) {
|
|
712
|
+
const resolved = await this.resolve(file);
|
|
713
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
714
|
+
}
|
|
715
|
+
}));
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return results;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Synchronously glob files in the virtual file system (VFS) based on the provided pattern(s).
|
|
722
|
+
*
|
|
723
|
+
* @param patterns - A pattern (or multiple patterns) to use to determine the file paths to return
|
|
724
|
+
* @returns An array of file paths matching the provided pattern(s)
|
|
725
|
+
*/
|
|
726
|
+
globSync(patterns) {
|
|
727
|
+
const results = [];
|
|
728
|
+
for (const pattern of normalizeGlobPatterns(this.#context.config.cwd, patterns)) {
|
|
729
|
+
const normalized = this.#normalizePath(pattern);
|
|
730
|
+
if (!/[*?[\]{}]/.test(normalized) && !normalized.includes("*")) if (this.isDirectorySync(normalized)) results.push(...this.listSync(normalized));
|
|
731
|
+
else {
|
|
732
|
+
const resolved = this.resolveSync(normalized);
|
|
733
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
734
|
+
}
|
|
735
|
+
else {
|
|
736
|
+
const absPattern = isAbsolutePath(normalized) ? normalized : this.#normalizePath(appendPath(normalized, this.#context.config.cwd));
|
|
737
|
+
const files = this.listSync(stripStars(absPattern));
|
|
738
|
+
for (const file of files) if (globToRegex(absPattern).test(file)) {
|
|
739
|
+
const resolved = this.resolveSync(file);
|
|
740
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
return results;
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Copies a file from one path to another in the virtual file system (VFS).
|
|
748
|
+
*
|
|
749
|
+
* @param srcPath - The source path to copy
|
|
750
|
+
* @param destPath - The destination path to copy to
|
|
751
|
+
*/
|
|
752
|
+
async copy(srcPath, destPath) {
|
|
753
|
+
const src = srcPath instanceof URL ? fileURLToPath(srcPath) : srcPath;
|
|
754
|
+
const dest = destPath instanceof URL ? fileURLToPath(destPath) : destPath;
|
|
755
|
+
if (!isSetString(src) && (!isSetObject(src) || !isSetString(src.input)) || !isSetString(dest)) return;
|
|
756
|
+
const sourceStr = isString(src) ? src : src.input ? src.input : this.#context.config.cwd;
|
|
757
|
+
const source = await this.resolve(sourceStr);
|
|
758
|
+
if (!source) return;
|
|
759
|
+
if (this.isDirectorySync(source) || isSetString(src) && src.includes("*") || isSetObject(src) && isSetString(src.glob)) await Promise.all((await this.glob(src)).map(async (file) => {
|
|
760
|
+
return this.copy(file, appendPath(replacePath(file, sourceStr), dest));
|
|
761
|
+
}));
|
|
762
|
+
else {
|
|
763
|
+
const content = await this.read(source);
|
|
764
|
+
if (content !== void 0) await this.write(this.#normalizePath(dest), content, { skipFormat: true });
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Synchronously copies a file from one path to another in the virtual file system (VFS).
|
|
769
|
+
*
|
|
770
|
+
* @param srcPath - The source path to copy
|
|
771
|
+
* @param destPath - The destination path to copy to
|
|
772
|
+
*/
|
|
773
|
+
copySync(srcPath, destPath) {
|
|
774
|
+
const src = srcPath instanceof URL ? fileURLToPath(srcPath) : srcPath;
|
|
775
|
+
const dest = destPath instanceof URL ? fileURLToPath(destPath) : destPath;
|
|
776
|
+
if (!isSetString(src) && (!isSetObject(src) || !isSetString(src.input)) || !isSetString(dest)) return;
|
|
777
|
+
const sourceStr = isString(src) ? src : src.input ? src.input : this.#context.config.cwd;
|
|
778
|
+
const source = this.resolveSync(sourceStr);
|
|
779
|
+
if (!source) return;
|
|
780
|
+
if (this.isDirectorySync(source) || isSetString(src) && src.includes("*") || isSetObject(src) && isSetString(src.glob)) this.globSync(src).map((file) => {
|
|
781
|
+
return this.copySync(file, appendPath(findFilePath(replacePath(file, sourceStr)), dest));
|
|
782
|
+
});
|
|
783
|
+
else {
|
|
784
|
+
const content = this.readSync(source);
|
|
785
|
+
if (content !== void 0) this.writeSync(this.#normalizePath(hasFileExtension(dest) ? dest : appendPath(findFileName(source), dest)), content, { skipFormat: true });
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Moves a file (or files) from one path to another in the virtual file system (VFS).
|
|
790
|
+
*
|
|
791
|
+
* @param srcPath - The source path to move
|
|
792
|
+
* @param destPath - The destination path to move to
|
|
793
|
+
*/
|
|
794
|
+
async move(srcPath, destPath) {
|
|
795
|
+
if (hasFileExtension(srcPath)) {
|
|
796
|
+
await this.copy(srcPath, destPath);
|
|
797
|
+
await this.remove(srcPath);
|
|
798
|
+
} else await Promise.all((await this.list(srcPath)).map(async (file) => {
|
|
799
|
+
await this.copy(file, destPath);
|
|
800
|
+
await this.remove(file);
|
|
801
|
+
}));
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Synchronously moves a file (or files) from one path to another in the virtual file system (VFS).
|
|
805
|
+
*
|
|
806
|
+
* @param srcPath - The source path to move
|
|
807
|
+
* @param destPath - The destination path to move to
|
|
808
|
+
*/
|
|
809
|
+
moveSync(srcPath, destPath) {
|
|
810
|
+
if (hasFileExtension(srcPath)) {
|
|
811
|
+
this.copySync(srcPath, destPath);
|
|
812
|
+
this.removeSync(srcPath);
|
|
813
|
+
} else this.listSync(srcPath).forEach((file) => {
|
|
814
|
+
this.copySync(file, destPath);
|
|
815
|
+
this.removeSync(file);
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Asynchronously reads a file from the virtual file system (VFS).
|
|
820
|
+
*
|
|
821
|
+
* @param path - The path or ID of the file to read.
|
|
822
|
+
* @returns A promise that resolves to the contents of the file as a string, or undefined if the file does not exist.
|
|
823
|
+
*/
|
|
824
|
+
async read(path) {
|
|
825
|
+
const filePath = await this.resolve(path, void 0, { isFile: true });
|
|
826
|
+
if (!filePath || !this.existsSync(filePath)) return;
|
|
827
|
+
const { adapter } = this.#getStorage(filePath);
|
|
828
|
+
this.#logger.trace(`Reading ${adapter.name} file: ${filePath}`);
|
|
829
|
+
return await adapter.get(filePath) ?? void 0;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Synchronously reads a file from the virtual file system (VFS).
|
|
833
|
+
*
|
|
834
|
+
* @param path - The path or ID of the file to read.
|
|
835
|
+
* @returns The contents of the file as a string, or undefined if the file does not exist.
|
|
836
|
+
*/
|
|
837
|
+
readSync(path) {
|
|
838
|
+
const filePath = this.resolveSync(path, void 0, { isFile: true });
|
|
839
|
+
if (!filePath || !this.existsSync(filePath)) return;
|
|
840
|
+
const { adapter } = this.#getStorage(filePath);
|
|
841
|
+
this.#logger.trace(`Reading ${adapter.name} file: ${filePath}`);
|
|
842
|
+
return adapter.getSync(filePath) ?? void 0;
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Writes a file to the virtual file system (VFS).
|
|
846
|
+
*
|
|
847
|
+
* @param path - The path to the file.
|
|
848
|
+
* @param data - The contents of the file.
|
|
849
|
+
* @param options - Optional parameters for writing the file.
|
|
850
|
+
* @returns A promise that resolves when the file is written.
|
|
851
|
+
*/
|
|
852
|
+
async write(path, data = "", options = {}) {
|
|
853
|
+
const meta = options.meta ?? {};
|
|
854
|
+
const resolvedPath = await this.resolve(this.#normalizePath(path)) || path;
|
|
855
|
+
const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
|
|
856
|
+
this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(new Blob(toArray(data)).size)})`);
|
|
857
|
+
let code = data;
|
|
858
|
+
try {
|
|
859
|
+
if (!options.skipFormat) code = await format(this.#context, resolvedPath, data);
|
|
860
|
+
} catch (err) {
|
|
861
|
+
if (DEFAULT_EXTENSIONS.includes(findFileExtensionSafe(resolvedPath, { fullExtension: true }))) this.#logger.warn(`Failed to format file ${resolvedPath} before writing: ${err.message}`);
|
|
862
|
+
code = data;
|
|
863
|
+
}
|
|
864
|
+
this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(new Blob(toArray(code)).size)})`);
|
|
865
|
+
const id = this.#normalizeId(meta.id || resolvedPath);
|
|
866
|
+
this.metadata[id] = {
|
|
867
|
+
type: "normal",
|
|
868
|
+
timestamp: Date.now(),
|
|
869
|
+
...this.metadata[id] ?? {},
|
|
870
|
+
...meta
|
|
871
|
+
};
|
|
872
|
+
this.paths[id] = resolvedPath;
|
|
873
|
+
this.ids[resolvedPath] = id;
|
|
874
|
+
return adapter.set(relativeKey, code);
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Synchronously writes a file to the virtual file system (VFS).
|
|
878
|
+
*
|
|
879
|
+
* @param path - The file to write.
|
|
880
|
+
* @param data - The contents of the file.
|
|
881
|
+
* @param options - Optional parameters for writing the file.
|
|
882
|
+
*/
|
|
883
|
+
writeSync(path, data = "", options = {}) {
|
|
884
|
+
const meta = options.meta ?? {};
|
|
885
|
+
const resolvedPath = this.resolveSync(this.#normalizePath(path)) || path;
|
|
886
|
+
const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
|
|
887
|
+
this.#logger.trace(`Writing ${resolvedPath} file to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(new Blob(toArray(data)).size)})`);
|
|
888
|
+
const id = this.#normalizeId(meta.id || resolvedPath);
|
|
889
|
+
this.metadata[id] = {
|
|
890
|
+
type: "normal",
|
|
891
|
+
timestamp: Date.now(),
|
|
892
|
+
...this.metadata[id] ?? {},
|
|
893
|
+
...meta
|
|
894
|
+
};
|
|
895
|
+
this.paths[id] = resolvedPath;
|
|
896
|
+
this.ids[resolvedPath] = id;
|
|
897
|
+
return adapter.setSync(relativeKey, data);
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* Synchronously creates a directory at the specified path.
|
|
901
|
+
*
|
|
902
|
+
* @param dirPath - The path of the directory to create.
|
|
903
|
+
*/
|
|
904
|
+
mkdirSync(dirPath) {
|
|
905
|
+
return this.#getStorage(dirPath)?.adapter?.mkdirSync(dirPath);
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Creates a directory at the specified path.
|
|
909
|
+
*
|
|
910
|
+
* @param path - The path of the directory to create.
|
|
911
|
+
*/
|
|
912
|
+
async mkdir(path) {
|
|
913
|
+
return this.#getStorage(path)?.adapter?.mkdir(path);
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Retrieves the metadata of a file in the virtual file system (VFS).
|
|
917
|
+
*
|
|
918
|
+
* @param pathOrId - The path or ID of the file to retrieve metadata for.
|
|
919
|
+
* @returns The metadata of the file, or undefined if the file does not exist.
|
|
920
|
+
*/
|
|
921
|
+
getMetadata(pathOrId) {
|
|
922
|
+
const resolved = this.resolveSync(pathOrId);
|
|
923
|
+
if (resolved && this.metadata[resolved]) return this.metadata[resolved];
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Resolves a given module ID using the configured aliases.
|
|
927
|
+
*
|
|
928
|
+
* @remarks
|
|
929
|
+
* This function can be used to map module IDs to different paths based on the alias configuration.
|
|
930
|
+
*
|
|
931
|
+
* @param id - The module ID to resolve.
|
|
932
|
+
* @returns The resolved module ID - after applying any configured aliases (this will be the same as the input ID if no aliases match).
|
|
933
|
+
*/
|
|
934
|
+
resolveAlias(id) {
|
|
935
|
+
let path = id;
|
|
936
|
+
if (this.#context.config.resolve.alias) {
|
|
937
|
+
if (Array.isArray(this.#context.config.resolve.alias) && this.#context.config.resolve.alias.length > 0) {
|
|
938
|
+
const found = this.#context.config.resolve.alias.filter((alias) => isSetString(alias.find) && (alias.find === path || path.startsWith(`${alias.find}/`)) || isRegExp(alias.find) && alias.find.test(path));
|
|
939
|
+
if (found.length > 0) {
|
|
940
|
+
const alias = found.reduce((ret, current) => {
|
|
941
|
+
return (isSetString(ret.find) ? ret.find.length : isRegExp(ret.find) ? ret.find.source.length : 0) > (isSetString(current.find) ? current.find.length : isRegExp(current.find) ? current.find.source.length : 0) ? ret : current;
|
|
942
|
+
});
|
|
943
|
+
if (isSetString(alias.find)) path = path.replace(new RegExp(`^${alias.find}`), alias.replacement);
|
|
944
|
+
else if (isRegExp(alias.find)) path = path.replace(alias.find, alias.replacement);
|
|
945
|
+
}
|
|
946
|
+
} else if (isSetObject(this.#context.config.resolve.alias)) {
|
|
947
|
+
const found = Object.keys(this.#context.config.resolve.alias).filter((key) => key === path || path.startsWith(`${key}/`));
|
|
948
|
+
if (found.length > 0) {
|
|
949
|
+
const alias = found.reduce((ret, current) => {
|
|
950
|
+
return ret.length > current.length ? ret : current;
|
|
951
|
+
});
|
|
952
|
+
path = path.replace(new RegExp(`^${alias}`), this.#context.config.resolve.alias[alias]);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return path;
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Checks if a given module ID is an alias.
|
|
960
|
+
*
|
|
961
|
+
* @remarks
|
|
962
|
+
* This function can be used to determine if a module ID matches any configured aliases.
|
|
963
|
+
*
|
|
964
|
+
* @param id - The module ID to check.
|
|
965
|
+
* @returns A boolean indicating whether the module ID is an alias.
|
|
966
|
+
*/
|
|
967
|
+
isAlias(id) {
|
|
968
|
+
const path = id;
|
|
969
|
+
if (this.#context.config.resolve.alias) {
|
|
970
|
+
if (Array.isArray(this.#context.config.resolve.alias) && this.#context.config.resolve.alias.length > 0) return this.#context.config.resolve.alias.filter((alias) => isSetString(alias.find) && (alias.find === path || path.startsWith(`${alias.find}/`)) || isRegExp(alias.find) && alias.find.test(path)).length > 0;
|
|
971
|
+
else if (isSetObject(this.#context.config.resolve.alias)) return Object.keys(this.#context.config.resolve.alias).filter((key) => key === path || path.startsWith(`${key}/`)).length > 0;
|
|
972
|
+
}
|
|
973
|
+
return false;
|
|
974
|
+
}
|
|
975
|
+
/**
|
|
976
|
+
* Checks if a given module ID is a tsconfig path.
|
|
977
|
+
*
|
|
978
|
+
* @remarks
|
|
979
|
+
* This function can be used to determine if a module ID matches any configured tsconfig paths.
|
|
980
|
+
*
|
|
981
|
+
* @param id - The module ID to check.
|
|
982
|
+
* @returns A boolean indicating whether the module ID is a tsconfig path.
|
|
983
|
+
*/
|
|
984
|
+
isTsconfigPath(id) {
|
|
985
|
+
return !!(this.#context.tsconfig.options.paths && Object.keys(this.#context.tsconfig.options.paths).length > 0 && match(id, this.#context.resolvePatterns));
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* A helper function to resolve modules in the virtual file system (VFS).
|
|
989
|
+
*
|
|
990
|
+
* @remarks
|
|
991
|
+
* This function can be used to resolve modules relative to the project root directory.
|
|
992
|
+
*
|
|
993
|
+
* @example
|
|
994
|
+
* ```ts
|
|
995
|
+
* const resolved = await context.resolvePath("some-module", "/path/to/importer");
|
|
996
|
+
* ```
|
|
997
|
+
*
|
|
998
|
+
* @param id - The module to resolve.
|
|
999
|
+
* @param importer - An optional path to the importer module.
|
|
1000
|
+
* @param options - Additional resolution options.
|
|
1001
|
+
* @returns A promise that resolves to the resolved module path.
|
|
1002
|
+
*/
|
|
1003
|
+
async resolve(id, importer, options = {}) {
|
|
1004
|
+
const origResult = await this.#innerResolve(id, importer, options);
|
|
1005
|
+
if (origResult && options.isFile && await this.isDirectory(origResult)) {
|
|
1006
|
+
const indexResult = await this.resolve(joinPaths(origResult, "index"), importer, options);
|
|
1007
|
+
if (indexResult) return indexResult;
|
|
1008
|
+
if (!hasFileExtension(origResult)) for (const ext of DEFAULT_EXTENSIONS) {
|
|
1009
|
+
const extResult = await this.resolve(`${origResult}.${ext}`, importer, options);
|
|
1010
|
+
if (extResult) return extResult;
|
|
1011
|
+
}
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
return origResult;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* A synchronous helper function to resolve modules using the Jiti resolver
|
|
1018
|
+
*
|
|
1019
|
+
* @remarks
|
|
1020
|
+
* This function can be used to resolve modules relative to the project root directory.
|
|
1021
|
+
*
|
|
1022
|
+
* @example
|
|
1023
|
+
* ```ts
|
|
1024
|
+
* const resolvedPath = context.resolveSync("some-module", "/path/to/importer");
|
|
1025
|
+
* ```
|
|
1026
|
+
*
|
|
1027
|
+
* @param id - The module to resolve.
|
|
1028
|
+
* @param importer - An optional path to the importer module.
|
|
1029
|
+
* @param options - Additional resolution options.
|
|
1030
|
+
* @returns The resolved module path.
|
|
1031
|
+
*/
|
|
1032
|
+
resolveSync(id, importer, options = {}) {
|
|
1033
|
+
const origResult = this.#innerResolveSync(id, importer, options);
|
|
1034
|
+
if (origResult && options.isFile && this.isDirectorySync(origResult)) {
|
|
1035
|
+
const indexResult = this.resolveSync(joinPaths(origResult, "index"), importer, options);
|
|
1036
|
+
if (indexResult) return indexResult;
|
|
1037
|
+
if (!hasFileExtension(origResult)) for (const ext of DEFAULT_EXTENSIONS) {
|
|
1038
|
+
const extResult = this.resolveSync(`${origResult}.${ext}`, importer, options);
|
|
1039
|
+
if (extResult) return extResult;
|
|
1040
|
+
}
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
return origResult;
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Disposes of the virtual file system (VFS) by saving its state to disk.
|
|
1047
|
+
*/
|
|
1048
|
+
async dispose() {
|
|
1049
|
+
if (!this.#isDisposed) {
|
|
1050
|
+
this.#isDisposed = true;
|
|
1051
|
+
this.#logger.debug("Disposing virtual file system...");
|
|
1052
|
+
await this.remove(joinPaths(this.#context.dataPath, "fs.bin"));
|
|
1053
|
+
const message = new capnp.Message();
|
|
1054
|
+
const fs = message.initRoot(FileSystem);
|
|
1055
|
+
const storage = fs._initStorage(Object.keys(this.#paths).length);
|
|
1056
|
+
await Promise.all(Object.values(this.#paths).map(async (path, index) => {
|
|
1057
|
+
const code = await this.read(path);
|
|
1058
|
+
const fd = storage.get(index);
|
|
1059
|
+
fd.path = path;
|
|
1060
|
+
fd.code = code || "";
|
|
1061
|
+
}));
|
|
1062
|
+
const ids = fs._initIds(Object.keys(this.#ids).length);
|
|
1063
|
+
Object.entries(this.#ids).filter(([, id]) => id).forEach(([path, id], index) => {
|
|
1064
|
+
const fileId = ids.get(index);
|
|
1065
|
+
fileId.id = id;
|
|
1066
|
+
fileId.path = path;
|
|
1067
|
+
});
|
|
1068
|
+
const metadata = fs._initMetadata(Object.keys(this.#metadata).length);
|
|
1069
|
+
Object.entries(this.#metadata).filter(([, value]) => value).forEach(([id, value], index) => {
|
|
1070
|
+
const fileMetadata = metadata.get(index);
|
|
1071
|
+
fileMetadata.id = id;
|
|
1072
|
+
fileMetadata.type = value.type;
|
|
1073
|
+
fileMetadata.timestamp = value.timestamp ?? Date.now();
|
|
1074
|
+
if (value.properties) {
|
|
1075
|
+
const props = fileMetadata._initProperties(Object.keys(value.properties).length);
|
|
1076
|
+
Object.entries(value.properties).filter(([, val]) => isSetString(val)).forEach(([key, val], index) => {
|
|
1077
|
+
const prop = props.get(index);
|
|
1078
|
+
prop.key = key;
|
|
1079
|
+
prop.value = val;
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
});
|
|
1083
|
+
await writeFileBuffer(joinPaths(this.#context.dataPath, "fs.bin"), message.toArrayBuffer());
|
|
1084
|
+
if (!this.#context.config.skipCache) this.resolverCache.save(true);
|
|
1085
|
+
await Promise.all(this.#getStorages().map(async (storage) => storage.adapter.dispose()));
|
|
1086
|
+
this.#logger.trace("Virtual file system has been disposed.");
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Asynchronously disposes of the virtual file system (VFS) by saving its state to disk.
|
|
1091
|
+
*
|
|
1092
|
+
* @remarks
|
|
1093
|
+
* This method is automatically called when the VFS instance is used within a `using` block, or can be manually invoked to ensure that the VFS state is saved and resources are cleaned up properly.
|
|
1094
|
+
*/
|
|
1095
|
+
async [Symbol.asyncDispose]() {
|
|
1096
|
+
return this.dispose();
|
|
1097
|
+
}
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
//#endregion
|
|
1101
|
+
export { VirtualFileSystem };
|
|
1102
|
+
//# sourceMappingURL=vfs.mjs.map
|