@powerlines/plugin-webpack 0.5.132 → 0.5.134
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/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/helpers/index.cjs +2 -3
- package/dist/helpers/index.d.cts +1 -3
- package/dist/helpers/index.d.mts +1 -3
- package/dist/helpers/index.mjs +1 -2
- package/dist/helpers/unplugin.cjs +11 -2
- package/dist/helpers/unplugin.d.cts +6 -2
- package/dist/helpers/unplugin.d.mts +6 -2
- package/dist/helpers/unplugin.mjs +9 -1
- package/dist/index.cjs +8 -50
- package/dist/index.d.cts +3 -4
- package/dist/index.d.mts +5 -5
- package/dist/index.mjs +3 -46
- package/dist/powerlines/schemas/fs.cjs +226 -0
- package/dist/powerlines/schemas/fs.mjs +224 -0
- package/dist/powerlines/src/api.cjs +580 -0
- package/dist/powerlines/src/api.mjs +578 -0
- package/dist/powerlines/src/internal/babel/module-resolver-plugin.cjs +86 -0
- package/dist/powerlines/src/internal/babel/module-resolver-plugin.mjs +84 -0
- package/dist/powerlines/src/internal/helpers/environment.cjs +52 -0
- package/dist/powerlines/src/internal/helpers/environment.mjs +48 -0
- package/dist/powerlines/src/internal/helpers/generate-types.cjs +51 -0
- package/dist/powerlines/src/internal/helpers/generate-types.mjs +49 -0
- package/dist/powerlines/src/internal/helpers/hooks.cjs +63 -0
- package/dist/powerlines/src/internal/helpers/hooks.mjs +61 -0
- package/dist/powerlines/src/internal/helpers/install-dependencies.cjs +25 -0
- package/dist/powerlines/src/internal/helpers/install-dependencies.mjs +24 -0
- package/dist/powerlines/src/internal/helpers/install.cjs +37 -0
- package/dist/powerlines/src/internal/helpers/install.mjs +36 -0
- package/dist/powerlines/src/internal/helpers/resolve-tsconfig.cjs +98 -0
- package/dist/powerlines/src/internal/helpers/resolve-tsconfig.mjs +94 -0
- package/dist/powerlines/src/internal/helpers/resolver.cjs +43 -0
- package/dist/powerlines/src/internal/helpers/resolver.mjs +41 -0
- package/dist/powerlines/src/lib/build/webpack.cjs +47 -0
- package/dist/powerlines/src/lib/build/webpack.mjs +45 -0
- package/dist/powerlines/src/lib/config-file.cjs +79 -0
- package/dist/powerlines/src/lib/config-file.mjs +76 -0
- package/dist/powerlines/src/lib/contexts/api-context.cjs +184 -0
- package/dist/powerlines/src/lib/contexts/api-context.mjs +182 -0
- package/dist/powerlines/src/lib/contexts/context.cjs +933 -0
- package/dist/powerlines/src/lib/contexts/context.mjs +931 -0
- package/dist/powerlines/src/lib/contexts/environment-context.cjs +160 -0
- package/dist/powerlines/src/lib/contexts/environment-context.mjs +159 -0
- package/dist/powerlines/src/lib/contexts/plugin-context.cjs +75 -0
- package/dist/powerlines/src/lib/contexts/plugin-context.mjs +74 -0
- package/dist/powerlines/src/lib/entry.cjs +69 -0
- package/dist/powerlines/src/lib/entry.mjs +67 -0
- package/dist/powerlines/src/lib/fs/helpers.cjs +85 -0
- package/dist/powerlines/src/lib/fs/helpers.mjs +81 -0
- package/dist/powerlines/src/lib/fs/storage/base.cjs +196 -0
- package/dist/powerlines/src/lib/fs/storage/base.mjs +195 -0
- package/dist/powerlines/src/lib/fs/storage/file-system.cjs +170 -0
- package/dist/powerlines/src/lib/fs/storage/file-system.mjs +169 -0
- package/dist/powerlines/src/lib/fs/storage/virtual.cjs +88 -0
- package/dist/powerlines/src/lib/fs/storage/virtual.mjs +87 -0
- package/dist/powerlines/src/lib/fs/vfs.cjs +830 -0
- package/dist/powerlines/src/lib/fs/vfs.mjs +828 -0
- package/dist/powerlines/src/lib/logger.cjs +58 -0
- package/dist/powerlines/src/lib/logger.mjs +55 -0
- package/dist/powerlines/src/lib/typescript/ts-morph.cjs +104 -0
- package/dist/powerlines/src/lib/typescript/ts-morph.mjs +102 -0
- package/dist/powerlines/src/lib/typescript/tsconfig.cjs +149 -0
- package/dist/powerlines/src/lib/typescript/tsconfig.mjs +144 -0
- package/dist/powerlines/src/lib/unplugin/factory.cjs +6 -0
- package/dist/powerlines/src/lib/unplugin/factory.mjs +8 -0
- package/dist/powerlines/src/lib/unplugin/helpers.cjs +17 -0
- package/dist/powerlines/src/lib/unplugin/helpers.mjs +16 -0
- package/dist/powerlines/src/lib/unplugin/index.cjs +3 -0
- package/dist/powerlines/src/lib/unplugin/index.mjs +5 -0
- package/dist/powerlines/src/lib/unplugin/plugin.cjs +128 -0
- package/dist/powerlines/src/lib/unplugin/plugin.mjs +127 -0
- package/dist/powerlines/src/lib/utilities/file-header.cjs +22 -0
- package/dist/powerlines/src/lib/utilities/file-header.mjs +21 -0
- package/dist/powerlines/src/lib/utilities/meta.cjs +45 -0
- package/dist/powerlines/src/lib/utilities/meta.mjs +41 -0
- package/dist/powerlines/src/lib/utilities/source-file.cjs +22 -0
- package/dist/powerlines/src/lib/utilities/source-file.mjs +21 -0
- package/dist/powerlines/src/plugin-utils/helpers.cjs +148 -0
- package/dist/powerlines/src/plugin-utils/helpers.mjs +138 -0
- package/dist/powerlines/src/plugin-utils/paths.cjs +36 -0
- package/dist/powerlines/src/plugin-utils/paths.mjs +35 -0
- package/dist/powerlines/src/types/babel.d.mts +4 -0
- package/dist/powerlines/src/types/build.cjs +15 -0
- package/dist/powerlines/src/types/build.d.cts +149 -0
- package/dist/powerlines/src/types/build.d.mts +149 -0
- package/dist/powerlines/src/types/build.mjs +14 -0
- package/dist/powerlines/src/types/commands.cjs +16 -0
- package/dist/powerlines/src/types/commands.d.cts +8 -0
- package/dist/powerlines/src/types/commands.d.mts +9 -0
- package/dist/powerlines/src/types/commands.mjs +15 -0
- package/dist/powerlines/src/types/config.d.cts +377 -0
- package/dist/powerlines/src/types/config.d.mts +379 -0
- package/dist/powerlines/src/types/context.d.cts +403 -0
- package/dist/powerlines/src/types/context.d.mts +405 -0
- package/dist/powerlines/src/types/fs.d.cts +486 -0
- package/dist/powerlines/src/types/fs.d.mts +486 -0
- package/dist/powerlines/src/types/hooks.d.mts +2 -0
- package/dist/powerlines/src/types/plugin.cjs +32 -0
- package/dist/powerlines/src/types/plugin.d.cts +231 -0
- package/dist/powerlines/src/types/plugin.d.mts +231 -0
- package/dist/powerlines/src/types/plugin.mjs +31 -0
- package/dist/powerlines/src/types/resolved.d.cts +82 -0
- package/dist/powerlines/src/types/resolved.d.mts +83 -0
- package/dist/powerlines/src/types/tsconfig.d.cts +69 -0
- package/dist/powerlines/src/types/tsconfig.d.mts +69 -0
- package/dist/types/index.cjs +0 -2
- package/dist/types/index.d.cts +1 -2
- package/dist/types/index.d.mts +1 -2
- package/dist/types/index.mjs +0 -3
- package/dist/types/plugin.cjs +0 -1
- package/dist/types/plugin.d.cts +12 -1
- package/dist/types/plugin.d.mts +12 -1
- package/dist/types/plugin.mjs +0 -2
- package/package.json +5 -5
- package/dist/helpers-B15z10jN.mjs +0 -1
- package/dist/helpers-LF26RHol.cjs +0 -0
- package/dist/index-9iG2qHLe.d.mts +0 -1
- package/dist/index-D4ELpJXS.d.cts +0 -1
- package/dist/index-D6CnpA_r.d.cts +0 -1
- package/dist/index-DL0uimUT.d.mts +0 -1
- package/dist/plugin-C5bG6Zu0.d.mts +0 -1772
- package/dist/plugin-Cl1J-dKa.d.cts +0 -1769
- package/dist/plugin-ifZVa20V.mjs +0 -1
- package/dist/plugin-pBKbb5K9.cjs +0 -0
- package/dist/types-U3zd8PTP.mjs +0 -1
- package/dist/types-o3zWarRp.cjs +0 -0
- package/dist/unplugin-Bo9KoKSB.d.cts +0 -7
- package/dist/unplugin-Ct4wZxE5.d.mts +0 -7
- package/dist/unplugin-GJmHohOE.mjs +0 -4617
- package/dist/unplugin-bKYETUU8.cjs +0 -4661
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
import { replacePathTokens } from "../../plugin-utils/paths.mjs";
|
|
2
|
+
import { FileSystem } from "../../../schemas/fs.mjs";
|
|
3
|
+
import { extendLog } from "../logger.mjs";
|
|
4
|
+
import { normalizeGlobPatterns, normalizeId, normalizePath } from "./helpers.mjs";
|
|
5
|
+
import { FileSystemStorageAdapter } from "./storage/file-system.mjs";
|
|
6
|
+
import { VirtualStorageAdapter } from "./storage/virtual.mjs";
|
|
7
|
+
import { LogLevelLabel } from "@storm-software/config-tools/types";
|
|
8
|
+
import { joinPaths } from "@stryke/path/join-paths";
|
|
9
|
+
import { toArray } from "@stryke/convert/to-array";
|
|
10
|
+
import { getResolutionCombinations, resolve, resolveSync } from "@stryke/fs/resolve";
|
|
11
|
+
import { appendPath } from "@stryke/path/append";
|
|
12
|
+
import { replacePath } from "@stryke/path/replace";
|
|
13
|
+
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
14
|
+
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
15
|
+
import { isString } from "@stryke/type-checks/is-string";
|
|
16
|
+
import { findFileName, findFilePath, hasFileExtension } from "@stryke/path/file-path-fns";
|
|
17
|
+
import { existsSync } from "@stryke/fs/exists";
|
|
18
|
+
import { murmurhash } from "@stryke/hash/murmurhash";
|
|
19
|
+
import { getUnique } from "@stryke/helpers/get-unique";
|
|
20
|
+
import { isParentPath } from "@stryke/path/is-parent-path";
|
|
21
|
+
import { isAbsolutePath } from "@stryke/path/is-type";
|
|
22
|
+
import { create } from "flat-cache";
|
|
23
|
+
import * as capnp from "@stryke/capnp";
|
|
24
|
+
import { readFileBuffer, readFileBufferSync, writeFileBuffer } from "@stryke/fs/buffer";
|
|
25
|
+
import { prettyBytes } from "@stryke/string-format/pretty-bytes";
|
|
26
|
+
import { isRegExp } from "@stryke/type-checks/is-regexp";
|
|
27
|
+
import { Blob } from "node:buffer";
|
|
28
|
+
import { fileURLToPath } from "node:url";
|
|
29
|
+
import { format, resolveConfig } from "prettier";
|
|
30
|
+
|
|
31
|
+
//#region ../powerlines/src/lib/fs/vfs.ts
|
|
32
|
+
/**
|
|
33
|
+
* Represents a virtual file system (VFS) that stores files and their associated metadata in virtual memory.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* 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.
|
|
37
|
+
*/
|
|
38
|
+
var VirtualFileSystem = class VirtualFileSystem {
|
|
39
|
+
/**
|
|
40
|
+
* A map of virtual file IDs to their associated metadata.
|
|
41
|
+
*/
|
|
42
|
+
#metadata;
|
|
43
|
+
/**
|
|
44
|
+
* A map of virtual file IDs to their underlying file paths.
|
|
45
|
+
*/
|
|
46
|
+
#ids;
|
|
47
|
+
/**
|
|
48
|
+
* A map of underlying file paths to their virtual file IDs.
|
|
49
|
+
*/
|
|
50
|
+
#paths;
|
|
51
|
+
/**
|
|
52
|
+
* The unified volume that combines the virtual file system with the real file system.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* This volume allows for seamless access to both virtual and real files.
|
|
56
|
+
*/
|
|
57
|
+
#storage = { "": new FileSystemStorageAdapter() };
|
|
58
|
+
/**
|
|
59
|
+
* A cache for module resolution results.
|
|
60
|
+
*/
|
|
61
|
+
#resolverCache;
|
|
62
|
+
/**
|
|
63
|
+
* Indicator specifying if the virtual file system (VFS) is disposed
|
|
64
|
+
*/
|
|
65
|
+
#isDisposed = false;
|
|
66
|
+
/**
|
|
67
|
+
* The context of the virtual file system.
|
|
68
|
+
*/
|
|
69
|
+
#context;
|
|
70
|
+
/**
|
|
71
|
+
* The file system's logging function.
|
|
72
|
+
*/
|
|
73
|
+
#log;
|
|
74
|
+
/**
|
|
75
|
+
* Normalizes a given module id by resolving it against the built-ins path.
|
|
76
|
+
*
|
|
77
|
+
* @param id - The module id to normalize.
|
|
78
|
+
* @returns The normalized module id.
|
|
79
|
+
*/
|
|
80
|
+
#normalizeId(id) {
|
|
81
|
+
let normalized = id;
|
|
82
|
+
if (isParentPath(normalized, this.#context.builtinsPath)) normalized = replacePath(normalized, this.#context.builtinsPath);
|
|
83
|
+
return normalizeId(normalized, this.#context.config.output.builtinPrefix);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Normalizes a given path by resolving it against the project root, workspace root, and built-ins path.
|
|
87
|
+
*
|
|
88
|
+
* @param path - The path to normalize.
|
|
89
|
+
* @returns The normalized path.
|
|
90
|
+
*/
|
|
91
|
+
#normalizePath(path) {
|
|
92
|
+
return normalizePath(path.includes("{") || path.includes("}") ? replacePathTokens(this.#context, path) : path, this.#context.builtinsPath, this.#context.config.output.builtinPrefix);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Builds a regular expression from a string pattern for path matching.
|
|
96
|
+
*
|
|
97
|
+
* @param path - The string pattern to convert.
|
|
98
|
+
* @returns A regular expression for matching paths.
|
|
99
|
+
*/
|
|
100
|
+
#buildRegex(path) {
|
|
101
|
+
const token = "::GLOBSTAR::";
|
|
102
|
+
return /* @__PURE__ */ new RegExp(`^${this.#normalizePath(path).replace(/\*\*/g, token).replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*").replace(/\?/g, "[^/]").replace(new RegExp(token, "g"), ".*")}$`);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Gets the storage adapter and relative key for a given key.
|
|
106
|
+
*
|
|
107
|
+
* @remarks
|
|
108
|
+
* The `key` can be either a path or a storage adapter name.
|
|
109
|
+
*
|
|
110
|
+
* @param key - The key to get the storage adapter for.
|
|
111
|
+
* @returns The storage adapter and relative key for the given key.
|
|
112
|
+
*/
|
|
113
|
+
#getStorage(key) {
|
|
114
|
+
const found = Object.entries(this.#storage).find(([, adapter]) => adapter.name === key || adapter.preset && adapter.preset.toLowerCase() === key?.toLowerCase());
|
|
115
|
+
if (found) return {
|
|
116
|
+
base: found[0],
|
|
117
|
+
relativeKey: "",
|
|
118
|
+
adapter: found[1]
|
|
119
|
+
};
|
|
120
|
+
const path = this.resolveSync(this.#normalizePath(key)) || key;
|
|
121
|
+
for (const base of Object.keys(this.#storage).filter(Boolean).sort().reverse()) if (isParentPath(path, base)) return {
|
|
122
|
+
base,
|
|
123
|
+
relativeKey: replacePath(path, base),
|
|
124
|
+
adapter: this.#storage[base]
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
base: "",
|
|
128
|
+
relativeKey: path,
|
|
129
|
+
adapter: this.#storage[""]
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Gets all storage adapters that match a given base key.
|
|
134
|
+
*
|
|
135
|
+
* @param base - The base key to match storage adapters against.
|
|
136
|
+
* @param includeParent - Whether to include parent storage adapters.
|
|
137
|
+
* @returns An array of storage adapters that match the given base key.
|
|
138
|
+
*/
|
|
139
|
+
#getStorages(base = "", includeParent = false) {
|
|
140
|
+
return Object.keys(this.#storage).sort().reverse().filter((key) => isParentPath(key, base) || includeParent && isParentPath(base, key)).map((key) => ({
|
|
141
|
+
relativeBase: base.length > key.length ? base.slice(key.length) : void 0,
|
|
142
|
+
base: key,
|
|
143
|
+
adapter: this.#storage[key]
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Creates a virtual file system (VFS) that is backed up to a Cap'n Proto message buffer.
|
|
148
|
+
*
|
|
149
|
+
* @param context - The context of the virtual file system, typically containing options and logging functions.
|
|
150
|
+
* @returns A promise that resolves to a new virtual file system instance.
|
|
151
|
+
*/
|
|
152
|
+
static async create(context) {
|
|
153
|
+
if (!context.config.skipCache && existsSync(joinPaths(context.dataPath, "fs.bin"))) {
|
|
154
|
+
const buffer = await readFileBuffer(joinPaths(context.dataPath, "fs.bin"));
|
|
155
|
+
const fs = new capnp.Message(buffer, false).getRoot(FileSystem);
|
|
156
|
+
const result = new VirtualFileSystem(context, fs);
|
|
157
|
+
if (fs._hasStorage() && fs.storage.length > 0) await Promise.all(fs.storage.values().map(async (file) => {
|
|
158
|
+
await result.write(file.path, file.code);
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
return new VirtualFileSystem(context, new capnp.Message().initRoot(FileSystem));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Synchronously creates a virtual file system (VFS) that is backed up to a Cap'n Proto message buffer.
|
|
165
|
+
*
|
|
166
|
+
* @param context - The context of the virtual file system, typically containing options and logging functions.
|
|
167
|
+
* @returns A new virtual file system instance.
|
|
168
|
+
*/
|
|
169
|
+
static createSync(context) {
|
|
170
|
+
if (!context.config.skipCache && existsSync(joinPaths(context.dataPath, "fs.bin"))) {
|
|
171
|
+
const buffer = readFileBufferSync(joinPaths(context.dataPath, "fs.bin"));
|
|
172
|
+
const fs = new capnp.Message(buffer, false).getRoot(FileSystem);
|
|
173
|
+
const result = new VirtualFileSystem(context, fs);
|
|
174
|
+
if (fs._hasStorage() && fs.storage.length > 0) fs.storage.values().map((file) => {
|
|
175
|
+
result.writeSync(file.path, file.code);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return new VirtualFileSystem(context, new capnp.Message().initRoot(FileSystem));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* A map of file ids to their metadata.
|
|
182
|
+
*/
|
|
183
|
+
get metadata() {
|
|
184
|
+
return new Proxy(this.#metadata, { get: (target, prop) => {
|
|
185
|
+
return target[this.#normalizeId(prop)];
|
|
186
|
+
} });
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* A map of file paths to their module ids.
|
|
190
|
+
*/
|
|
191
|
+
get ids() {
|
|
192
|
+
return new Proxy(this.#paths, { get: (target, prop) => {
|
|
193
|
+
return target[this.#normalizePath(prop)];
|
|
194
|
+
} });
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* A map of module ids to their file paths.
|
|
198
|
+
*/
|
|
199
|
+
get paths() {
|
|
200
|
+
return new Proxy(this.#paths, { get: (target, prop) => {
|
|
201
|
+
return target[this.#normalizeId(prop)];
|
|
202
|
+
} });
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Gets the resolver cache.
|
|
206
|
+
*/
|
|
207
|
+
get resolverCache() {
|
|
208
|
+
if (!this.#resolverCache) this.#resolverCache = create({
|
|
209
|
+
cacheId: "module-resolution",
|
|
210
|
+
cacheDir: this.#context.cachePath,
|
|
211
|
+
ttl: 3600 * 1e3,
|
|
212
|
+
lruSize: 5e3,
|
|
213
|
+
persistInterval: 100
|
|
214
|
+
});
|
|
215
|
+
return this.#resolverCache;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Creates a new instance of the {@link VirtualFileSystem}.
|
|
219
|
+
*
|
|
220
|
+
* @param context - The context of the virtual file system, typically containing options and logging functions.
|
|
221
|
+
* @param fs - A buffer containing the serialized virtual file system data.
|
|
222
|
+
*/
|
|
223
|
+
constructor(context, fs) {
|
|
224
|
+
this.#context = context;
|
|
225
|
+
if (isSetObject(this.#context.config.output.storage)) this.#storage = {
|
|
226
|
+
...this.#storage,
|
|
227
|
+
...this.#context.config.output.storage
|
|
228
|
+
};
|
|
229
|
+
this.#storage.virtual ??= new VirtualStorageAdapter({ base: "/_virtual" });
|
|
230
|
+
if (this.#context.config.output.storage !== "fs") {
|
|
231
|
+
this.#storage[this.#context.artifactsPath] ??= new VirtualStorageAdapter({ base: this.#context.artifactsPath });
|
|
232
|
+
this.#storage[this.#context.builtinsPath] ??= new VirtualStorageAdapter({ base: this.#context.builtinsPath });
|
|
233
|
+
this.#storage[this.#context.entryPath] ??= new VirtualStorageAdapter({ base: this.#context.entryPath });
|
|
234
|
+
}
|
|
235
|
+
this.#metadata = {};
|
|
236
|
+
if (fs._hasMetadata()) {
|
|
237
|
+
this.#metadata = fs.metadata.values().reduce((ret, metadata) => {
|
|
238
|
+
ret[metadata.id] = {
|
|
239
|
+
id: metadata.id,
|
|
240
|
+
type: metadata.type,
|
|
241
|
+
timestamp: metadata.timestamp || Date.now(),
|
|
242
|
+
properties: metadata._hasProperties() ? metadata.properties.values().reduce((ret$1, item) => {
|
|
243
|
+
ret$1[item.key] = item.value;
|
|
244
|
+
return ret$1;
|
|
245
|
+
}, {}) : {}
|
|
246
|
+
};
|
|
247
|
+
return ret;
|
|
248
|
+
}, {});
|
|
249
|
+
if (context.config.skipCache !== true) Object.entries(this.#metadata).filter(([, value]) => value.type === "entry").forEach(([id, value]) => {
|
|
250
|
+
this.#context.entry ??= [];
|
|
251
|
+
this.#context.entry.push({
|
|
252
|
+
file: id,
|
|
253
|
+
name: value.properties.name,
|
|
254
|
+
output: value.properties.output,
|
|
255
|
+
input: value.properties["input.file"] ? {
|
|
256
|
+
file: value.properties["input.file"],
|
|
257
|
+
name: value.properties["input.name"]
|
|
258
|
+
} : void 0
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
this.#ids = {};
|
|
263
|
+
this.#paths = {};
|
|
264
|
+
if (fs._hasIds()) {
|
|
265
|
+
this.#ids = fs.ids.values().reduce((ret, identifier) => {
|
|
266
|
+
ret[identifier.path] ??= identifier.id;
|
|
267
|
+
return ret;
|
|
268
|
+
}, {});
|
|
269
|
+
this.#paths = fs.ids.values().reduce((ret, identifier) => {
|
|
270
|
+
ret[identifier.id] ??= identifier.path;
|
|
271
|
+
return ret;
|
|
272
|
+
}, {});
|
|
273
|
+
}
|
|
274
|
+
this.#log = extendLog(this.#context.log, "file-system");
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Asynchronously checks if a file exists in the virtual file system (VFS).
|
|
278
|
+
*
|
|
279
|
+
* @param path - The path to the file.
|
|
280
|
+
* @returns A promise that resolves to `true` if the file exists, otherwise `false`.
|
|
281
|
+
*/
|
|
282
|
+
async exists(path) {
|
|
283
|
+
const { relativeKey, adapter } = this.#getStorage(path);
|
|
284
|
+
return adapter.exists(relativeKey);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Synchronously checks if a file exists in the virtual file system (VFS).
|
|
288
|
+
*
|
|
289
|
+
* @param path - The path to the file.
|
|
290
|
+
* @returns `true` if the file exists, otherwise `false`.
|
|
291
|
+
*/
|
|
292
|
+
existsSync(path) {
|
|
293
|
+
const { relativeKey, adapter } = this.#getStorage(path);
|
|
294
|
+
return adapter.existsSync(relativeKey);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Checks if a file is virtual in the virtual file system (VFS).
|
|
298
|
+
*
|
|
299
|
+
* @param path - The path to the file.
|
|
300
|
+
* @returns `true` if the file is virtual, otherwise `false`.
|
|
301
|
+
*/
|
|
302
|
+
isVirtual(path) {
|
|
303
|
+
const resolved = this.resolveSync(path);
|
|
304
|
+
if (!resolved) return false;
|
|
305
|
+
return this.#getStorage(resolved)?.adapter?.name === "virtual";
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Checks if a path is a directory in the virtual file system (VFS).
|
|
309
|
+
*
|
|
310
|
+
* @param path - The path to check.
|
|
311
|
+
* @returns `true` if the path is a directory, otherwise `false`.
|
|
312
|
+
*/
|
|
313
|
+
isDirectorySync(path) {
|
|
314
|
+
const resolved = this.resolveSync(path);
|
|
315
|
+
if (!resolved) return false;
|
|
316
|
+
return !!(this.existsSync(resolved) && this.#getStorage(resolved)?.adapter?.isDirectorySync(resolved));
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Checks if a path is a directory in the virtual file system (VFS).
|
|
320
|
+
*
|
|
321
|
+
* @param path - The path to check.
|
|
322
|
+
* @returns `true` if the path is a directory, otherwise `false`.
|
|
323
|
+
*/
|
|
324
|
+
async isDirectory(path) {
|
|
325
|
+
const resolved = await this.resolve(path);
|
|
326
|
+
if (!resolved) return false;
|
|
327
|
+
return !!(await this.exists(resolved) && await this.#getStorage(resolved)?.adapter?.isDirectory(resolved));
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Checks if a path is a file in the virtual file system (VFS).
|
|
331
|
+
*
|
|
332
|
+
* @param path - The path to check.
|
|
333
|
+
* @returns `true` if the path is a file, otherwise `false`.
|
|
334
|
+
*/
|
|
335
|
+
isFileSync(path) {
|
|
336
|
+
const resolved = this.resolveSync(path);
|
|
337
|
+
if (!resolved) return false;
|
|
338
|
+
return this.#getStorage(resolved)?.adapter?.isFileSync(resolved) ?? false;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Checks if a path is a file in the virtual file system (VFS).
|
|
342
|
+
*
|
|
343
|
+
* @param path - The path to check.
|
|
344
|
+
* @returns `true` if the path is a file, otherwise `false`.
|
|
345
|
+
*/
|
|
346
|
+
async isFile(path) {
|
|
347
|
+
const resolved = await this.resolve(path);
|
|
348
|
+
if (!resolved) return false;
|
|
349
|
+
return await this.#getStorage(resolved)?.adapter?.isFile(resolved) ?? false;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Lists files in a given path.
|
|
353
|
+
*
|
|
354
|
+
* @param path - The path to list files from.
|
|
355
|
+
* @returns An array of file names in the specified path.
|
|
356
|
+
*/
|
|
357
|
+
listSync(path) {
|
|
358
|
+
return getUnique(this.#getStorages(path, true).map((storage) => storage.adapter.listSync(storage.relativeBase ? storage.base ? appendPath(storage.relativeBase, storage.base) : storage.relativeBase : storage.base)).flat());
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Lists files in a given path.
|
|
362
|
+
*
|
|
363
|
+
* @param path - The path to list files from.
|
|
364
|
+
* @returns An array of file names in the specified path.
|
|
365
|
+
*/
|
|
366
|
+
async list(path) {
|
|
367
|
+
return getUnique((await Promise.all(this.#getStorages(path, true).map(async (storage) => storage.adapter.list(storage.relativeBase ? storage.base ? appendPath(storage.relativeBase, storage.base) : storage.relativeBase : storage.base)))).flat());
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Removes a file in the virtual file system (VFS).
|
|
371
|
+
*
|
|
372
|
+
* @param path - The path to create the directory at.
|
|
373
|
+
*/
|
|
374
|
+
async remove(path) {
|
|
375
|
+
const normalizedPath = this.#normalizePath(path);
|
|
376
|
+
this.#log(LogLevelLabel.TRACE, `Removing file: ${normalizedPath}`);
|
|
377
|
+
const { relativeKey, adapter } = this.#getStorage(normalizedPath);
|
|
378
|
+
if (hasFileExtension(normalizedPath)) await adapter.remove(relativeKey);
|
|
379
|
+
else await adapter.clear(relativeKey);
|
|
380
|
+
const id = this.#ids[normalizedPath];
|
|
381
|
+
if (id && this.#metadata[id]) {
|
|
382
|
+
delete this.#metadata[id];
|
|
383
|
+
delete this.#ids[normalizedPath];
|
|
384
|
+
delete this.#paths[id];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Removes a file in the virtual file system (VFS).
|
|
389
|
+
*
|
|
390
|
+
* @param path - The path to create the directory at.
|
|
391
|
+
*/
|
|
392
|
+
removeSync(path) {
|
|
393
|
+
const normalizedPath = this.#normalizePath(path);
|
|
394
|
+
this.#log(LogLevelLabel.TRACE, `Removing file: ${normalizedPath}`);
|
|
395
|
+
const { relativeKey, adapter } = this.#getStorage(normalizedPath);
|
|
396
|
+
if (hasFileExtension(normalizedPath)) adapter.removeSync(relativeKey);
|
|
397
|
+
else adapter.clearSync(relativeKey);
|
|
398
|
+
const id = this.#ids[normalizedPath];
|
|
399
|
+
if (id && this.#metadata[id]) {
|
|
400
|
+
delete this.#metadata[id];
|
|
401
|
+
delete this.#ids[normalizedPath];
|
|
402
|
+
delete this.#paths[id];
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Glob files in the virtual file system (VFS) based on the provided pattern(s).
|
|
407
|
+
*
|
|
408
|
+
* @param patterns - A pattern (or multiple patterns) to use to determine the file paths to return
|
|
409
|
+
* @returns An array of file paths matching the provided pattern(s)
|
|
410
|
+
*/
|
|
411
|
+
async glob(patterns) {
|
|
412
|
+
const results = [];
|
|
413
|
+
for (const pattern of normalizeGlobPatterns(this.#context.workspaceConfig.workspaceRoot, patterns)) {
|
|
414
|
+
const normalized = this.#normalizePath(pattern);
|
|
415
|
+
if (!/[*?[\]{}]/.test(normalized) && !normalized.includes("**")) {
|
|
416
|
+
if (this.isDirectorySync(normalized)) results.push(...await this.list(normalized));
|
|
417
|
+
else {
|
|
418
|
+
const resolved = await this.resolve(normalized);
|
|
419
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
420
|
+
}
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
const absPattern = isAbsolutePath(normalized) ? normalized : this.#normalizePath(joinPaths(this.#context.workspaceConfig.workspaceRoot, normalized));
|
|
424
|
+
const firstGlobIdx = absPattern.search(/[*?[\]{}]/);
|
|
425
|
+
const baseDir = firstGlobIdx === -1 ? findFilePath(absPattern) : absPattern.slice(0, Math.max(0, absPattern.lastIndexOf("/", firstGlobIdx)));
|
|
426
|
+
await Promise.all((await this.list(baseDir && isAbsolutePath(baseDir) ? baseDir : this.#context.workspaceConfig.workspaceRoot)).map(async (file) => {
|
|
427
|
+
if (this.#buildRegex(absPattern).test(file)) {
|
|
428
|
+
const resolved = this.resolveSync(file);
|
|
429
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
430
|
+
}
|
|
431
|
+
}));
|
|
432
|
+
}
|
|
433
|
+
return results;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Synchronously glob files in the virtual file system (VFS) based on the provided pattern(s).
|
|
437
|
+
*
|
|
438
|
+
* @param patterns - A pattern (or multiple patterns) to use to determine the file paths to return
|
|
439
|
+
* @returns An array of file paths matching the provided pattern(s)
|
|
440
|
+
*/
|
|
441
|
+
globSync(patterns) {
|
|
442
|
+
const results = [];
|
|
443
|
+
for (const pattern of normalizeGlobPatterns(this.#context.workspaceConfig.workspaceRoot, patterns)) {
|
|
444
|
+
const normalized = this.#normalizePath(pattern);
|
|
445
|
+
if (!/[*?[\]{}]/.test(normalized) && !normalized.includes("**")) {
|
|
446
|
+
if (this.isDirectorySync(normalized)) results.push(...this.listSync(normalized));
|
|
447
|
+
else {
|
|
448
|
+
const resolved = this.resolveSync(normalized);
|
|
449
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
450
|
+
}
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const absPattern = isAbsolutePath(normalized) ? normalized : this.#normalizePath(joinPaths(this.#context.workspaceConfig.workspaceRoot, normalized));
|
|
454
|
+
const firstGlobIdx = absPattern.search(/[*?[\]{}]/);
|
|
455
|
+
const baseDir = firstGlobIdx === -1 ? findFilePath(absPattern) : absPattern.slice(0, Math.max(0, absPattern.lastIndexOf("/", firstGlobIdx)));
|
|
456
|
+
const files = this.listSync(baseDir && isAbsolutePath(baseDir) ? baseDir : this.#context.workspaceConfig.workspaceRoot);
|
|
457
|
+
for (const file of files) if (this.#buildRegex(absPattern).test(file)) {
|
|
458
|
+
const resolved = this.resolveSync(file);
|
|
459
|
+
if (resolved && !results.includes(resolved)) results.push(resolved);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return results;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Copies a file from one path to another in the virtual file system (VFS).
|
|
466
|
+
*
|
|
467
|
+
* @param srcPath - The source path to copy
|
|
468
|
+
* @param destPath - The destination path to copy to
|
|
469
|
+
*/
|
|
470
|
+
async copy(srcPath, destPath) {
|
|
471
|
+
const src = srcPath instanceof URL ? fileURLToPath(srcPath) : srcPath;
|
|
472
|
+
const dest = destPath instanceof URL ? fileURLToPath(destPath) : destPath;
|
|
473
|
+
if (!isSetString(src) && (!isSetObject(src) || !isSetString(src.input)) || !isSetString(dest)) return;
|
|
474
|
+
const sourceStr = isString(src) ? src : src.input ? src.input : this.#context.workspaceConfig.workspaceRoot;
|
|
475
|
+
const source = await this.resolve(sourceStr);
|
|
476
|
+
if (!source) return;
|
|
477
|
+
if (this.isDirectorySync(source) || isSetString(src) && src.includes("*") || isSetObject(src) && isSetString(src.glob)) await Promise.all((await this.glob(src)).map(async (file) => {
|
|
478
|
+
return this.copy(file, appendPath(replacePath(file, sourceStr), dest));
|
|
479
|
+
}));
|
|
480
|
+
else {
|
|
481
|
+
const content = await this.read(source);
|
|
482
|
+
if (content !== void 0) await this.write(this.#normalizePath(dest), content, { skipFormat: true });
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Synchronously copies a file from one path to another in the virtual file system (VFS).
|
|
487
|
+
*
|
|
488
|
+
* @param srcPath - The source path to copy
|
|
489
|
+
* @param destPath - The destination path to copy to
|
|
490
|
+
*/
|
|
491
|
+
copySync(srcPath, destPath) {
|
|
492
|
+
const src = srcPath instanceof URL ? fileURLToPath(srcPath) : srcPath;
|
|
493
|
+
const dest = destPath instanceof URL ? fileURLToPath(destPath) : destPath;
|
|
494
|
+
if (!isSetString(src) && (!isSetObject(src) || !isSetString(src.input)) || !isSetString(dest)) return;
|
|
495
|
+
const sourceStr = isString(src) ? src : src.input ? src.input : this.#context.workspaceConfig.workspaceRoot;
|
|
496
|
+
const source = this.resolveSync(sourceStr);
|
|
497
|
+
if (!source) return;
|
|
498
|
+
if (this.isDirectorySync(source) || isSetString(src) && src.includes("*") || isSetObject(src) && isSetString(src.glob)) this.globSync(src).map((file) => {
|
|
499
|
+
return this.copySync(file, appendPath(findFilePath(replacePath(file, sourceStr)), dest));
|
|
500
|
+
});
|
|
501
|
+
else {
|
|
502
|
+
const content = this.readSync(source);
|
|
503
|
+
if (content !== void 0) this.writeSync(this.#normalizePath(hasFileExtension(dest) ? dest : appendPath(findFileName(source), dest)), content, { skipFormat: true });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Moves a file (or files) from one path to another in the virtual file system (VFS).
|
|
508
|
+
*
|
|
509
|
+
* @param srcPath - The source path to move
|
|
510
|
+
* @param destPath - The destination path to move to
|
|
511
|
+
*/
|
|
512
|
+
async move(srcPath, destPath) {
|
|
513
|
+
if (hasFileExtension(srcPath)) {
|
|
514
|
+
await this.copy(srcPath, destPath);
|
|
515
|
+
await this.remove(srcPath);
|
|
516
|
+
} else await Promise.all((await this.list(srcPath)).map(async (file) => {
|
|
517
|
+
await this.copy(file, destPath);
|
|
518
|
+
await this.remove(file);
|
|
519
|
+
}));
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Synchronously moves a file (or files) from one path to another in the virtual file system (VFS).
|
|
523
|
+
*
|
|
524
|
+
* @param srcPath - The source path to move
|
|
525
|
+
* @param destPath - The destination path to move to
|
|
526
|
+
*/
|
|
527
|
+
moveSync(srcPath, destPath) {
|
|
528
|
+
if (hasFileExtension(srcPath)) {
|
|
529
|
+
this.copySync(srcPath, destPath);
|
|
530
|
+
this.removeSync(srcPath);
|
|
531
|
+
} else this.listSync(srcPath).forEach((file) => {
|
|
532
|
+
this.copySync(file, destPath);
|
|
533
|
+
this.removeSync(file);
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Asynchronously reads a file from the virtual file system (VFS).
|
|
538
|
+
*
|
|
539
|
+
* @param path - The path or ID of the file to read.
|
|
540
|
+
* @returns A promise that resolves to the contents of the file as a string, or undefined if the file does not exist.
|
|
541
|
+
*/
|
|
542
|
+
async read(path) {
|
|
543
|
+
const filePath = await this.resolve(path);
|
|
544
|
+
if (!filePath) return;
|
|
545
|
+
const { relativeKey, adapter } = this.#getStorage(filePath);
|
|
546
|
+
this.#log(LogLevelLabel.TRACE, `Reading ${adapter.name} file: ${filePath}`);
|
|
547
|
+
return await adapter.get(relativeKey) ?? void 0;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Synchronously reads a file from the virtual file system (VFS).
|
|
551
|
+
*
|
|
552
|
+
* @param path - The path or ID of the file to read.
|
|
553
|
+
* @returns The contents of the file as a string, or undefined if the file does not exist.
|
|
554
|
+
*/
|
|
555
|
+
readSync(path) {
|
|
556
|
+
const filePath = this.resolveSync(path);
|
|
557
|
+
if (!filePath) return;
|
|
558
|
+
const { relativeKey, adapter } = this.#getStorage(filePath);
|
|
559
|
+
this.#log(LogLevelLabel.TRACE, `Reading ${adapter.name} file: ${filePath}`);
|
|
560
|
+
return adapter.getSync(relativeKey) ?? void 0;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Writes a file to the virtual file system (VFS).
|
|
564
|
+
*
|
|
565
|
+
* @param path - The path to the file.
|
|
566
|
+
* @param data - The contents of the file.
|
|
567
|
+
* @param options - Optional parameters for writing the file.
|
|
568
|
+
* @returns A promise that resolves when the file is written.
|
|
569
|
+
*/
|
|
570
|
+
async write(path, data = "", options = {}) {
|
|
571
|
+
let code = data;
|
|
572
|
+
if (!options.skipFormat) {
|
|
573
|
+
const resolvedConfig = await resolveConfig(this.#normalizePath(path));
|
|
574
|
+
if (resolvedConfig) code = await format(data, {
|
|
575
|
+
absolutePath: this.#normalizePath(path),
|
|
576
|
+
...resolvedConfig
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
const { relativeKey, adapter } = this.#getStorage(options.storage || path);
|
|
580
|
+
this.#log(LogLevelLabel.TRACE, `Writing ${this.#normalizePath(relativeKey)} 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)})`);
|
|
581
|
+
const id = options?.meta?.id || this.#normalizeId(relativeKey);
|
|
582
|
+
this.#metadata[id] = {
|
|
583
|
+
variant: "normal",
|
|
584
|
+
timestamp: Date.now(),
|
|
585
|
+
...options.meta ?? {}
|
|
586
|
+
};
|
|
587
|
+
this.#paths[id] = this.#normalizePath(relativeKey);
|
|
588
|
+
this.#ids[this.#normalizePath(relativeKey)] = id;
|
|
589
|
+
return adapter.set(relativeKey, code);
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Synchronously writes a file to the virtual file system (VFS).
|
|
593
|
+
*
|
|
594
|
+
* @param path - The file to write.
|
|
595
|
+
* @param data - The contents of the file.
|
|
596
|
+
* @param options - Optional parameters for writing the file.
|
|
597
|
+
*/
|
|
598
|
+
writeSync(path, data = "", options = {}) {
|
|
599
|
+
const { relativeKey, adapter } = this.#getStorage(options.storage || path);
|
|
600
|
+
this.#log(LogLevelLabel.TRACE, `Writing ${this.#normalizePath(relativeKey)} 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)})`);
|
|
601
|
+
const id = options?.meta?.id || this.#normalizeId(relativeKey);
|
|
602
|
+
this.#metadata[id] = {
|
|
603
|
+
variant: "normal",
|
|
604
|
+
timestamp: Date.now(),
|
|
605
|
+
...options.meta ?? {}
|
|
606
|
+
};
|
|
607
|
+
this.#paths[id] = this.#normalizePath(relativeKey);
|
|
608
|
+
this.#ids[this.#normalizePath(relativeKey)] = id;
|
|
609
|
+
return adapter.setSync(relativeKey, data);
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Synchronously creates a directory at the specified path.
|
|
613
|
+
*
|
|
614
|
+
* @param dirPath - The path of the directory to create.
|
|
615
|
+
*/
|
|
616
|
+
mkdirSync(dirPath) {
|
|
617
|
+
return this.#getStorage(dirPath)?.adapter?.mkdirSync(dirPath);
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Creates a directory at the specified path.
|
|
621
|
+
*
|
|
622
|
+
* @param path - The path of the directory to create.
|
|
623
|
+
*/
|
|
624
|
+
async mkdir(path) {
|
|
625
|
+
return this.#getStorage(path)?.adapter?.mkdir(path);
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Retrieves the metadata of a file in the virtual file system (VFS).
|
|
629
|
+
*
|
|
630
|
+
* @param pathOrId - The path or ID of the file to retrieve metadata for.
|
|
631
|
+
* @returns The metadata of the file, or undefined if the file does not exist.
|
|
632
|
+
*/
|
|
633
|
+
getMetadata(pathOrId) {
|
|
634
|
+
const resolved = this.resolveSync(pathOrId);
|
|
635
|
+
if (resolved && this.metadata[resolved]) return this.metadata[resolved];
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Resolves a given module ID using the configured aliases.
|
|
639
|
+
*
|
|
640
|
+
* @remarks
|
|
641
|
+
* This function can be used to map module IDs to different paths based on the alias configuration.
|
|
642
|
+
*
|
|
643
|
+
* @param id - The module ID to resolve.
|
|
644
|
+
* @returns The resolved module ID - after applying any configured aliases (this will be the same as the input ID if no aliases match).
|
|
645
|
+
*/
|
|
646
|
+
resolveAlias(id) {
|
|
647
|
+
let path = id;
|
|
648
|
+
if (this.#context.config.build.alias) {
|
|
649
|
+
if (Array.isArray(this.#context.config.build.alias) && this.#context.config.build.alias.length > 0) {
|
|
650
|
+
const found = this.#context.config.build.alias.filter((alias) => isSetString(alias.find) && (alias.find === path || path.startsWith(`${alias.find}/`)) || isRegExp(alias.find) && alias.find.test(path));
|
|
651
|
+
if (found.length > 0) {
|
|
652
|
+
const alias = found.reduce((ret, current) => {
|
|
653
|
+
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;
|
|
654
|
+
});
|
|
655
|
+
if (isSetString(alias.find)) path = path.replace(/* @__PURE__ */ new RegExp(`^${alias.find}`), alias.replacement);
|
|
656
|
+
else if (isRegExp(alias.find)) path = path.replace(alias.find, alias.replacement);
|
|
657
|
+
}
|
|
658
|
+
} else if (isSetObject(this.#context.config.build.alias)) {
|
|
659
|
+
const found = Object.keys(this.#context.config.build.alias).filter((key) => key === path || path.startsWith(`${key}/`));
|
|
660
|
+
if (found.length > 0) {
|
|
661
|
+
const alias = found.reduce((ret, current) => {
|
|
662
|
+
return ret.length > current.length ? ret : current;
|
|
663
|
+
});
|
|
664
|
+
path = path.replace(/* @__PURE__ */ new RegExp(`^${alias}`), this.#context.config.build.alias[alias]);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return path;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* A helper function to resolve modules in the virtual file system (VFS).
|
|
672
|
+
*
|
|
673
|
+
* @remarks
|
|
674
|
+
* This function can be used to resolve modules relative to the project root directory.
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```ts
|
|
678
|
+
* const resolved = await context.resolvePath("some-module", "/path/to/importer");
|
|
679
|
+
* ```
|
|
680
|
+
*
|
|
681
|
+
* @param id - The module to resolve.
|
|
682
|
+
* @param importer - An optional path to the importer module.
|
|
683
|
+
* @param options - Additional resolution options.
|
|
684
|
+
* @returns A promise that resolves to the resolved module path.
|
|
685
|
+
*/
|
|
686
|
+
async resolve(id, importer, options = {}) {
|
|
687
|
+
let path = id;
|
|
688
|
+
if (path.includes("{") || path.includes("}")) path = replacePathTokens(this.#context, path);
|
|
689
|
+
if (options.skipAlias !== true) path = this.resolveAlias(path);
|
|
690
|
+
if (isAbsolutePath(path)) return path;
|
|
691
|
+
const resolverCacheKey = murmurhash({
|
|
692
|
+
path: this.#normalizeId(path),
|
|
693
|
+
importer,
|
|
694
|
+
options
|
|
695
|
+
});
|
|
696
|
+
let result;
|
|
697
|
+
if (!this.#context.config.skipCache) {
|
|
698
|
+
result = this.resolverCache.get(resolverCacheKey);
|
|
699
|
+
if (result) return result;
|
|
700
|
+
}
|
|
701
|
+
result = this.paths[this.#normalizeId(path)];
|
|
702
|
+
if (!result) {
|
|
703
|
+
const paths = options.paths ?? [];
|
|
704
|
+
if (importer && !paths.includes(importer)) paths.push(importer);
|
|
705
|
+
paths.push(this.#context.workspaceConfig.workspaceRoot);
|
|
706
|
+
paths.push(appendPath(this.#context.config.projectRoot, this.#context.workspaceConfig.workspaceRoot));
|
|
707
|
+
paths.push(appendPath(this.#context.config.sourceRoot, this.#context.workspaceConfig.workspaceRoot));
|
|
708
|
+
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.workspaceConfig.workspaceRoot)));
|
|
709
|
+
for (const combination of getResolutionCombinations(path, { paths })) {
|
|
710
|
+
const { relativeKey, adapter } = this.#getStorage(combination);
|
|
711
|
+
if (await adapter.exists(relativeKey)) {
|
|
712
|
+
result = combination;
|
|
713
|
+
break;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (!result) try {
|
|
717
|
+
result = await resolve(path, {
|
|
718
|
+
...options,
|
|
719
|
+
paths
|
|
720
|
+
});
|
|
721
|
+
} catch {}
|
|
722
|
+
}
|
|
723
|
+
if (result && !this.#context.config.skipCache) this.resolverCache.set(resolverCacheKey, result);
|
|
724
|
+
return result;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* A synchronous helper function to resolve modules using the Jiti resolver
|
|
728
|
+
*
|
|
729
|
+
* @remarks
|
|
730
|
+
* This function can be used to resolve modules relative to the project root directory.
|
|
731
|
+
*
|
|
732
|
+
* @example
|
|
733
|
+
* ```ts
|
|
734
|
+
* const resolvedPath = context.resolveSync("some-module", "/path/to/importer");
|
|
735
|
+
* ```
|
|
736
|
+
*
|
|
737
|
+
* @param id - The module to resolve.
|
|
738
|
+
* @param importer - An optional path to the importer module.
|
|
739
|
+
* @param options - Additional resolution options.
|
|
740
|
+
* @returns The resolved module path.
|
|
741
|
+
*/
|
|
742
|
+
resolveSync(id, importer, options = {}) {
|
|
743
|
+
let path = id;
|
|
744
|
+
if (path.includes("{") || path.includes("}")) path = replacePathTokens(this.#context, path);
|
|
745
|
+
if (options.skipAlias !== true) path = this.resolveAlias(path);
|
|
746
|
+
if (isAbsolutePath(path)) return path;
|
|
747
|
+
let result;
|
|
748
|
+
if (!this.#context.config.skipCache) {
|
|
749
|
+
result = this.resolverCache.get(this.#normalizeId(path));
|
|
750
|
+
if (result) return result;
|
|
751
|
+
}
|
|
752
|
+
result = this.paths[this.#normalizeId(path)];
|
|
753
|
+
if (!result) {
|
|
754
|
+
const paths = options.paths ?? [];
|
|
755
|
+
if (importer && !paths.includes(importer)) paths.push(importer);
|
|
756
|
+
paths.push(this.#context.workspaceConfig.workspaceRoot);
|
|
757
|
+
paths.push(appendPath(this.#context.config.projectRoot, this.#context.workspaceConfig.workspaceRoot));
|
|
758
|
+
paths.push(appendPath(this.#context.config.sourceRoot, this.#context.workspaceConfig.workspaceRoot));
|
|
759
|
+
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.workspaceConfig.workspaceRoot)));
|
|
760
|
+
for (const combination of getResolutionCombinations(path, { paths })) {
|
|
761
|
+
const { relativeKey, adapter } = this.#getStorage(combination);
|
|
762
|
+
if (adapter.existsSync(relativeKey)) {
|
|
763
|
+
result = combination;
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (!result) try {
|
|
768
|
+
result = resolveSync(path, {
|
|
769
|
+
...options,
|
|
770
|
+
paths
|
|
771
|
+
});
|
|
772
|
+
} catch {}
|
|
773
|
+
}
|
|
774
|
+
if (result && !this.#context.config.skipCache) this.resolverCache.set(this.#normalizeId(path), result);
|
|
775
|
+
return result;
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Disposes of the virtual file system (VFS) by saving its state to disk.
|
|
779
|
+
*/
|
|
780
|
+
async dispose() {
|
|
781
|
+
if (!this.#isDisposed) {
|
|
782
|
+
this.#isDisposed = true;
|
|
783
|
+
this.#log(LogLevelLabel.DEBUG, "Disposing virtual file system...");
|
|
784
|
+
await this.remove(joinPaths(this.#context.dataPath, "fs.bin"));
|
|
785
|
+
const message = new capnp.Message();
|
|
786
|
+
const fs = message.initRoot(FileSystem);
|
|
787
|
+
const paths = await this.list();
|
|
788
|
+
const storage = fs._initStorage(paths.length);
|
|
789
|
+
await Promise.all(paths.map(async (path, index) => {
|
|
790
|
+
const code = await this.read(path);
|
|
791
|
+
const fd = storage.get(index);
|
|
792
|
+
fd.path = path;
|
|
793
|
+
fd.code = code || "";
|
|
794
|
+
}));
|
|
795
|
+
const ids = fs._initIds(Object.keys(this.ids).length);
|
|
796
|
+
Object.entries(this.ids).filter(([, path]) => path).forEach(([id, path], index) => {
|
|
797
|
+
const fileId = ids.get(index);
|
|
798
|
+
fileId.id = id;
|
|
799
|
+
fileId.path = path;
|
|
800
|
+
});
|
|
801
|
+
const metadata = fs._initMetadata(Object.keys(this.metadata).length);
|
|
802
|
+
Object.entries(this.metadata).filter(([, value]) => value).forEach(([id, value], index) => {
|
|
803
|
+
const fileMetadata = metadata.get(index);
|
|
804
|
+
fileMetadata.id = id;
|
|
805
|
+
fileMetadata.type = value.type;
|
|
806
|
+
fileMetadata.timestamp = value.timestamp ?? BigInt(Date.now());
|
|
807
|
+
if (value.properties) {
|
|
808
|
+
const props = fileMetadata._initProperties(Object.keys(value.properties).length);
|
|
809
|
+
Object.entries(value.properties).filter(([, val]) => isSetString(val)).forEach(([key, val], index$1) => {
|
|
810
|
+
const prop = props.get(index$1);
|
|
811
|
+
prop.key = key;
|
|
812
|
+
prop.value = val;
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
await writeFileBuffer(joinPaths(this.#context.dataPath, "fs.bin"), message.toArrayBuffer());
|
|
817
|
+
if (!this.#context.config.skipCache) this.#resolverCache.save(true);
|
|
818
|
+
await Promise.all(this.#getStorages().map(async (storage$1) => storage$1.adapter.dispose()));
|
|
819
|
+
this.#log(LogLevelLabel.TRACE, "Virtual file system has been disposed.");
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
async [Symbol.asyncDispose]() {
|
|
823
|
+
return this.dispose();
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
//#endregion
|
|
828
|
+
export { VirtualFileSystem };
|