@fgv/ts-res-browser 1.0.0 → 5.0.0-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.rush/temp/{5fc90bc7c2ccf812114ea099111568e5429be2a2.tar.log → 587277bfc395406ad444e989b6c309b9698f534c.tar.log} +2 -2
- package/.rush/temp/chunked-rush-logs/ts-res-browser.build.chunks.jsonl +12 -12
- package/.rush/temp/operation/build/all.log +12 -12
- package/.rush/temp/operation/build/log-chunks.jsonl +12 -12
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +11 -3
- package/README.md +85 -15
- package/cli.js +45 -0
- package/dist/bundle.js +1 -1
- package/package.json +12 -7
- package/rush-logs/ts-res-browser.build.cache.log +1 -1
- package/rush-logs/ts-res-browser.build.log +12 -12
- package/src/App.tsx +122 -1
- package/src/components/common/FileImporter.tsx +10 -5
- package/src/components/layout/Sidebar.tsx +8 -1
- package/src/components/tools/ConfigurationTool.tsx +4 -1
- package/src/components/tools/FilterTool.tsx +106 -19
- package/src/components/tools/ImportTool.tsx +3 -0
- package/src/components/tools/ZipLoader.tsx +302 -0
- package/src/hooks/useAppState.ts +4 -0
- package/src/hooks/useResourceManager.ts +52 -1
- package/src/hooks/useUrlParams.ts +56 -0
- package/src/types/app.ts +9 -1
- package/src/utils/fileImport.ts +7 -5
- package/src/utils/fileTreeConverter.ts +160 -0
- package/src/utils/urlParams.ts +238 -0
- package/src/utils/zip/browserZipFileTreeAccessors.ts +385 -0
- package/webpack.config.js +7 -1
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Configuration options that can be passed via URL parameters
|
|
25
|
+
*/
|
|
26
|
+
export interface IUrlConfigOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Input file path
|
|
29
|
+
*/
|
|
30
|
+
input?: string;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Configuration name or path
|
|
34
|
+
*/
|
|
35
|
+
config?: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Context filter token (pipe-separated)
|
|
39
|
+
*/
|
|
40
|
+
contextFilter?: string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Qualifier defaults token (pipe-separated)
|
|
44
|
+
*/
|
|
45
|
+
qualifierDefaults?: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Resource types filter (comma-separated)
|
|
49
|
+
*/
|
|
50
|
+
resourceTypes?: string;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Maximum distance for language matching
|
|
54
|
+
*/
|
|
55
|
+
maxDistance?: number;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Whether to reduce qualifiers
|
|
59
|
+
*/
|
|
60
|
+
reduceQualifiers?: boolean;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether to launch in interactive mode
|
|
64
|
+
*/
|
|
65
|
+
interactive?: boolean;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Starting directory for input file picker (derived from input path)
|
|
69
|
+
*/
|
|
70
|
+
inputStartDir?: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Starting directory for config file picker (derived from config path)
|
|
74
|
+
*/
|
|
75
|
+
configStartDir?: string;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Whether to use ZIP loading mode
|
|
79
|
+
*/
|
|
80
|
+
loadZip?: boolean;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Path to ZIP file to load (for CLI-generated ZIPs)
|
|
84
|
+
*/
|
|
85
|
+
zipPath?: string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Name of ZIP file to load from Downloads (filename only)
|
|
89
|
+
*/
|
|
90
|
+
zipFile?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Parses URL parameters and extracts configuration options
|
|
95
|
+
*/
|
|
96
|
+
export function parseUrlParameters(): IUrlConfigOptions {
|
|
97
|
+
const params = new URLSearchParams(window.location.search);
|
|
98
|
+
|
|
99
|
+
const options: IUrlConfigOptions = {};
|
|
100
|
+
|
|
101
|
+
// Parse string parameters
|
|
102
|
+
const input = params.get('input');
|
|
103
|
+
if (input) {
|
|
104
|
+
options.input = input;
|
|
105
|
+
// Set starting directory for input file picker
|
|
106
|
+
if (isFilePath(input)) {
|
|
107
|
+
options.inputStartDir = extractDirectoryPath(input);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const config = params.get('config');
|
|
112
|
+
if (config) {
|
|
113
|
+
options.config = config;
|
|
114
|
+
// Set starting directory for config file picker
|
|
115
|
+
if (isFilePath(config)) {
|
|
116
|
+
options.configStartDir = extractDirectoryPath(config);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const contextFilter = params.get('contextFilter');
|
|
121
|
+
if (contextFilter) options.contextFilter = contextFilter;
|
|
122
|
+
|
|
123
|
+
const qualifierDefaults = params.get('qualifierDefaults');
|
|
124
|
+
if (qualifierDefaults) options.qualifierDefaults = qualifierDefaults;
|
|
125
|
+
|
|
126
|
+
const resourceTypes = params.get('resourceTypes');
|
|
127
|
+
if (resourceTypes) options.resourceTypes = resourceTypes;
|
|
128
|
+
|
|
129
|
+
// Parse numeric parameters
|
|
130
|
+
const maxDistance = params.get('maxDistance');
|
|
131
|
+
if (maxDistance) {
|
|
132
|
+
const parsed = parseInt(maxDistance, 10);
|
|
133
|
+
if (!isNaN(parsed)) options.maxDistance = parsed;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Parse boolean parameters
|
|
137
|
+
const reduceQualifiers = params.get('reduceQualifiers');
|
|
138
|
+
if (reduceQualifiers === 'true') options.reduceQualifiers = true;
|
|
139
|
+
|
|
140
|
+
const interactive = params.get('interactive');
|
|
141
|
+
if (interactive === 'true') options.interactive = true;
|
|
142
|
+
|
|
143
|
+
// Parse ZIP loading parameters
|
|
144
|
+
const loadZip = params.get('loadZip');
|
|
145
|
+
if (loadZip === 'true') options.loadZip = true;
|
|
146
|
+
|
|
147
|
+
const zipPath = params.get('zipPath');
|
|
148
|
+
if (zipPath) options.zipPath = zipPath;
|
|
149
|
+
|
|
150
|
+
const zipFile = params.get('zipFile');
|
|
151
|
+
if (zipFile) options.zipFile = zipFile;
|
|
152
|
+
|
|
153
|
+
return options;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Converts context filter token to context object
|
|
158
|
+
* Example: "language=en-US|territory=US" -> { language: "en-US", territory: "US" }
|
|
159
|
+
*/
|
|
160
|
+
export function parseContextFilter(contextFilter: string): Record<string, string> {
|
|
161
|
+
const context: Record<string, string> = {};
|
|
162
|
+
|
|
163
|
+
const tokens = contextFilter.split('|');
|
|
164
|
+
for (const token of tokens) {
|
|
165
|
+
const [key, value] = token.split('=');
|
|
166
|
+
if (key && value) {
|
|
167
|
+
context[key.trim()] = value.trim();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return context;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Converts qualifier defaults token to structured format
|
|
176
|
+
* Example: "language=en-US,en-CA|territory=US" -> { language: ["en-US", "en-CA"], territory: ["US"] }
|
|
177
|
+
*/
|
|
178
|
+
export function parseQualifierDefaults(qualifierDefaults: string): Record<string, string[]> {
|
|
179
|
+
const defaults: Record<string, string[]> = {};
|
|
180
|
+
|
|
181
|
+
const tokens = qualifierDefaults.split('|');
|
|
182
|
+
for (const token of tokens) {
|
|
183
|
+
const [key, values] = token.split('=');
|
|
184
|
+
if (key && values) {
|
|
185
|
+
defaults[key.trim()] = values.split(',').map((v) => v.trim());
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return defaults;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Converts resource types string to array
|
|
194
|
+
* Example: "json,string" -> ["json", "string"]
|
|
195
|
+
*/
|
|
196
|
+
export function parseResourceTypes(resourceTypes: string): string[] {
|
|
197
|
+
return resourceTypes
|
|
198
|
+
.split(',')
|
|
199
|
+
.map((t) => t.trim())
|
|
200
|
+
.filter((t) => t.length > 0);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Extracts the directory path from a file or directory path
|
|
205
|
+
* If the path appears to be a directory (no extension), returns it as-is
|
|
206
|
+
* If the path appears to be a file, returns the parent directory
|
|
207
|
+
*/
|
|
208
|
+
export function extractDirectoryPath(path: string): string {
|
|
209
|
+
if (!path) return '';
|
|
210
|
+
|
|
211
|
+
// Normalize path separators
|
|
212
|
+
const normalizedPath = path.replace(/\\/g, '/');
|
|
213
|
+
|
|
214
|
+
// Check if it looks like a file (has an extension)
|
|
215
|
+
const parts = normalizedPath.split('/');
|
|
216
|
+
const lastPart = parts[parts.length - 1];
|
|
217
|
+
|
|
218
|
+
// If the last part has an extension, treat as file and return directory
|
|
219
|
+
if (lastPart.includes('.') && !lastPart.startsWith('.')) {
|
|
220
|
+
return parts.slice(0, -1).join('/') || '/';
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Otherwise, treat as directory
|
|
224
|
+
return normalizedPath;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Determines if a path appears to be a file (has extension) or directory
|
|
229
|
+
*/
|
|
230
|
+
export function isFilePath(path: string): boolean {
|
|
231
|
+
if (!path) return false;
|
|
232
|
+
|
|
233
|
+
const parts = path.split('/');
|
|
234
|
+
const lastPart = parts[parts.length - 1];
|
|
235
|
+
|
|
236
|
+
// Has extension and doesn't start with dot (hidden files)
|
|
237
|
+
return lastPart.includes('.') && !lastPart.startsWith('.');
|
|
238
|
+
}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import JSZip from 'jszip';
|
|
24
|
+
import { Result, succeed, fail, captureResult, Converter, Validator, FileTree } from '@fgv/ts-utils';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Implementation of {@link FileTree.IFileTreeFileItem} for files in a ZIP archive (browser version).
|
|
28
|
+
*/
|
|
29
|
+
export class BrowserZipFileItem implements FileTree.IFileTreeFileItem {
|
|
30
|
+
/**
|
|
31
|
+
* Indicates that this {@link FileTree.FileTreeItem} is a file.
|
|
32
|
+
*/
|
|
33
|
+
public readonly type: 'file' = 'file';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The absolute path of the file within the ZIP archive.
|
|
37
|
+
*/
|
|
38
|
+
public readonly absolutePath: string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The name of the file
|
|
42
|
+
*/
|
|
43
|
+
public readonly name: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The base name of the file (without extension)
|
|
47
|
+
*/
|
|
48
|
+
public readonly baseName: string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The extension of the file
|
|
52
|
+
*/
|
|
53
|
+
public readonly extension: string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The pre-loaded contents of the file.
|
|
57
|
+
*/
|
|
58
|
+
private readonly _contents: string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The ZIP file tree accessors that created this item.
|
|
62
|
+
*/
|
|
63
|
+
private readonly _accessors: BrowserZipFileTreeAccessors;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Constructor for BrowserZipFileItem.
|
|
67
|
+
* @param zipFilePath - The path of the file within the ZIP.
|
|
68
|
+
* @param contents - The pre-loaded contents of the file.
|
|
69
|
+
* @param accessors - The ZIP file tree accessors.
|
|
70
|
+
*/
|
|
71
|
+
public constructor(zipFilePath: string, contents: string, accessors: BrowserZipFileTreeAccessors) {
|
|
72
|
+
this._contents = contents;
|
|
73
|
+
this._accessors = accessors;
|
|
74
|
+
this.absolutePath = '/' + zipFilePath;
|
|
75
|
+
this.name = accessors.getBaseName(zipFilePath);
|
|
76
|
+
this.extension = accessors.getExtension(zipFilePath);
|
|
77
|
+
this.baseName = accessors.getBaseName(zipFilePath, this.extension);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Gets the contents of the file as parsed JSON.
|
|
82
|
+
*/
|
|
83
|
+
public getContents(): Result<unknown>;
|
|
84
|
+
public getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
|
|
85
|
+
public getContents<T>(converter?: Validator<T> | Converter<T>): Result<T | unknown> {
|
|
86
|
+
return this.getRawContents()
|
|
87
|
+
.onSuccess((contents) => {
|
|
88
|
+
return captureResult(() => {
|
|
89
|
+
const parsed = JSON.parse(contents);
|
|
90
|
+
if (converter) {
|
|
91
|
+
if ('convert' in converter) {
|
|
92
|
+
return converter.convert(parsed);
|
|
93
|
+
} else {
|
|
94
|
+
return (converter as Validator<T>).validate(parsed);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return succeed(parsed);
|
|
98
|
+
}).onFailure(() => {
|
|
99
|
+
return fail(`Failed to parse JSON from file: ${this.absolutePath}`);
|
|
100
|
+
});
|
|
101
|
+
})
|
|
102
|
+
.onFailure((error) => {
|
|
103
|
+
return fail(`Failed to get contents from file: ${error}`);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Gets the raw contents of the file as a string.
|
|
109
|
+
*/
|
|
110
|
+
public getRawContents(): Result<string> {
|
|
111
|
+
return succeed(this._contents);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Implementation of {@link FileTree.IFileTreeDirectoryItem} for directories in a ZIP archive (browser version).
|
|
117
|
+
*/
|
|
118
|
+
export class BrowserZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
|
|
119
|
+
/**
|
|
120
|
+
* Indicates that this {@link FileTree.FileTreeItem} is a directory.
|
|
121
|
+
*/
|
|
122
|
+
public readonly type: 'directory' = 'directory';
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The absolute path of the directory within the ZIP archive.
|
|
126
|
+
*/
|
|
127
|
+
public readonly absolutePath: string;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The name of the directory
|
|
131
|
+
*/
|
|
132
|
+
public readonly name: string;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The ZIP file tree accessors that created this item.
|
|
136
|
+
*/
|
|
137
|
+
private readonly _accessors: BrowserZipFileTreeAccessors;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Constructor for BrowserZipDirectoryItem.
|
|
141
|
+
* @param directoryPath - The path of the directory within the ZIP.
|
|
142
|
+
* @param accessors - The ZIP file tree accessors.
|
|
143
|
+
*/
|
|
144
|
+
public constructor(directoryPath: string, accessors: BrowserZipFileTreeAccessors) {
|
|
145
|
+
this._accessors = accessors;
|
|
146
|
+
this.absolutePath = '/' + directoryPath.replace(/\/$/, ''); // Normalize path
|
|
147
|
+
this.name = accessors.getBaseName(directoryPath);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Gets the children of the directory.
|
|
152
|
+
*/
|
|
153
|
+
public getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem>> {
|
|
154
|
+
return this._accessors.getChildren(this.absolutePath);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* File tree accessors for ZIP archives (browser version using JSZip).
|
|
160
|
+
*/
|
|
161
|
+
export class BrowserZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
|
|
162
|
+
/**
|
|
163
|
+
* The JSZip instance containing the archive.
|
|
164
|
+
*/
|
|
165
|
+
private readonly _zip: JSZip;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Optional prefix to prepend to paths.
|
|
169
|
+
*/
|
|
170
|
+
private readonly _prefix: string;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Cache of all items in the ZIP for efficient lookups.
|
|
174
|
+
*/
|
|
175
|
+
private readonly _itemCache: Map<string, FileTree.FileTreeItem> = new Map();
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Cache of all file contents pre-loaded from the ZIP.
|
|
179
|
+
*/
|
|
180
|
+
private readonly _fileContents: Map<string, string> = new Map();
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Constructor for BrowserZipFileTreeAccessors.
|
|
184
|
+
* @param zip - The JSZip instance.
|
|
185
|
+
* @param fileContents - Pre-loaded file contents.
|
|
186
|
+
* @param prefix - Optional prefix to prepend to paths.
|
|
187
|
+
*/
|
|
188
|
+
private constructor(zip: JSZip, fileContents: Map<string, string>, prefix?: string) {
|
|
189
|
+
this._zip = zip;
|
|
190
|
+
this._fileContents = fileContents;
|
|
191
|
+
this._prefix = prefix || '';
|
|
192
|
+
this._buildItemCache();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Creates a new BrowserZipFileTreeAccessors instance from a ZIP file buffer.
|
|
197
|
+
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
198
|
+
* @param prefix - Optional prefix to prepend to paths.
|
|
199
|
+
* @returns Result containing the BrowserZipFileTreeAccessors instance.
|
|
200
|
+
*/
|
|
201
|
+
public static async fromBuffer(
|
|
202
|
+
zipBuffer: ArrayBuffer | Uint8Array,
|
|
203
|
+
prefix?: string
|
|
204
|
+
): Promise<Result<BrowserZipFileTreeAccessors>> {
|
|
205
|
+
try {
|
|
206
|
+
const zip = new JSZip();
|
|
207
|
+
await zip.loadAsync(zipBuffer);
|
|
208
|
+
const fileContents = new Map<string, string>();
|
|
209
|
+
|
|
210
|
+
// Pre-load all file contents
|
|
211
|
+
const promises: Promise<void>[] = [];
|
|
212
|
+
zip.forEach((relativePath, zipObject) => {
|
|
213
|
+
if (!zipObject.dir) {
|
|
214
|
+
const promise = zipObject.async('string').then((content) => {
|
|
215
|
+
fileContents.set(relativePath, content);
|
|
216
|
+
});
|
|
217
|
+
promises.push(promise);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
await Promise.all(promises);
|
|
222
|
+
return succeed(new BrowserZipFileTreeAccessors(zip, fileContents, prefix));
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return fail(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Creates a new BrowserZipFileTreeAccessors instance from a File object (browser environment).
|
|
230
|
+
* @param file - The File object containing ZIP data.
|
|
231
|
+
* @param prefix - Optional prefix to prepend to paths.
|
|
232
|
+
* @returns Result containing the BrowserZipFileTreeAccessors instance.
|
|
233
|
+
*/
|
|
234
|
+
public static async fromFile(file: File, prefix?: string): Promise<Result<BrowserZipFileTreeAccessors>> {
|
|
235
|
+
try {
|
|
236
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
237
|
+
return await BrowserZipFileTreeAccessors.fromBuffer(arrayBuffer, prefix);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
return fail(`Failed to read file: ${error instanceof Error ? error.message : String(error)}`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Builds the cache of all items in the ZIP archive.
|
|
245
|
+
*/
|
|
246
|
+
private _buildItemCache(): void {
|
|
247
|
+
const directories = new Set<string>();
|
|
248
|
+
|
|
249
|
+
// First pass: collect all directories from file paths
|
|
250
|
+
this._zip.forEach((relativePath, zipObject) => {
|
|
251
|
+
if (!zipObject.dir) {
|
|
252
|
+
// Extract directory path from file path
|
|
253
|
+
const pathParts = relativePath.split('/');
|
|
254
|
+
for (let i = 1; i < pathParts.length; i++) {
|
|
255
|
+
const dirPath = pathParts.slice(0, i).join('/');
|
|
256
|
+
directories.add(dirPath);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Add directory items to cache
|
|
262
|
+
directories.forEach((dirPath) => {
|
|
263
|
+
const absolutePath = this.resolveAbsolutePath(dirPath);
|
|
264
|
+
const item = new BrowserZipDirectoryItem(dirPath, this);
|
|
265
|
+
this._itemCache.set(absolutePath, item);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Add file items to cache
|
|
269
|
+
this._zip.forEach((relativePath, zipObject) => {
|
|
270
|
+
if (!zipObject.dir) {
|
|
271
|
+
const absolutePath = this.resolveAbsolutePath(relativePath);
|
|
272
|
+
const contents = this._fileContents.get(relativePath) || '';
|
|
273
|
+
const item = new BrowserZipFileItem(relativePath, contents, this);
|
|
274
|
+
this._itemCache.set(absolutePath, item);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Resolves paths to an absolute path.
|
|
281
|
+
*/
|
|
282
|
+
public resolveAbsolutePath(...paths: string[]): string {
|
|
283
|
+
const joinedPath = this.joinPaths(...paths);
|
|
284
|
+
const prefixed = this._prefix ? this.joinPaths(this._prefix, joinedPath) : joinedPath;
|
|
285
|
+
return prefixed.startsWith('/') ? prefixed : '/' + prefixed;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Gets the extension of a path.
|
|
290
|
+
*/
|
|
291
|
+
public getExtension(path: string): string {
|
|
292
|
+
const name = this.getBaseName(path);
|
|
293
|
+
const lastDotIndex = name.lastIndexOf('.');
|
|
294
|
+
return lastDotIndex >= 0 ? name.substring(lastDotIndex) : '';
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Gets the base name of a path.
|
|
299
|
+
*/
|
|
300
|
+
public getBaseName(path: string, suffix?: string): string {
|
|
301
|
+
const normalizedPath = path.replace(/\/$/, ''); // Remove trailing slash
|
|
302
|
+
const parts = normalizedPath.split('/');
|
|
303
|
+
let baseName = parts[parts.length - 1] || '';
|
|
304
|
+
|
|
305
|
+
if (suffix && baseName.endsWith(suffix)) {
|
|
306
|
+
baseName = baseName.substring(0, baseName.length - suffix.length);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return baseName;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Joins paths together.
|
|
314
|
+
*/
|
|
315
|
+
public joinPaths(...paths: string[]): string {
|
|
316
|
+
return paths
|
|
317
|
+
.filter((p) => p && p.length > 0)
|
|
318
|
+
.map((p) => p.replace(/^\/+|\/+$/g, '')) // Remove leading/trailing slashes
|
|
319
|
+
.join('/')
|
|
320
|
+
.replace(/\/+/g, '/'); // Normalize multiple slashes
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Gets an item from the file tree.
|
|
325
|
+
*/
|
|
326
|
+
public getItem(path: string): Result<FileTree.FileTreeItem> {
|
|
327
|
+
const absolutePath = this.resolveAbsolutePath(path);
|
|
328
|
+
const item = this._itemCache.get(absolutePath);
|
|
329
|
+
|
|
330
|
+
if (item) {
|
|
331
|
+
return succeed(item);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return fail(`Item not found: ${absolutePath}`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Gets the contents of a file in the file tree.
|
|
339
|
+
*/
|
|
340
|
+
public getFileContents(path: string): Result<string> {
|
|
341
|
+
return this.getItem(path).onSuccess((item) => {
|
|
342
|
+
if (item.type !== 'file') {
|
|
343
|
+
return fail(`Path is not a file: ${path}`);
|
|
344
|
+
}
|
|
345
|
+
return item.getRawContents();
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Gets the children of a directory in the file tree.
|
|
351
|
+
*/
|
|
352
|
+
public getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem>> {
|
|
353
|
+
const absolutePath = this.resolveAbsolutePath(path);
|
|
354
|
+
const children: FileTree.FileTreeItem[] = [];
|
|
355
|
+
|
|
356
|
+
// Find all items that are direct children of this directory
|
|
357
|
+
for (const [itemPath, item] of this._itemCache) {
|
|
358
|
+
if (this._isDirectChild(absolutePath, itemPath)) {
|
|
359
|
+
children.push(item);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return succeed(children);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Checks if childPath is a direct child of parentPath.
|
|
368
|
+
*/
|
|
369
|
+
private _isDirectChild(parentPath: string, childPath: string): boolean {
|
|
370
|
+
// Normalize paths
|
|
371
|
+
const normalizedParent = parentPath.replace(/\/$/, '');
|
|
372
|
+
const normalizedChild = childPath.replace(/\/$/, '');
|
|
373
|
+
|
|
374
|
+
// Child must start with parent path
|
|
375
|
+
if (!normalizedChild.startsWith(normalizedParent + '/')) {
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Get the relative path from parent to child
|
|
380
|
+
const relativePath = normalizedChild.substring(normalizedParent.length + 1);
|
|
381
|
+
|
|
382
|
+
// Direct child means no additional slashes in the relative path
|
|
383
|
+
return !relativePath.includes('/');
|
|
384
|
+
}
|
|
385
|
+
}
|
package/webpack.config.js
CHANGED
|
@@ -16,12 +16,18 @@ module.exports = {
|
|
|
16
16
|
crypto: require.resolve('crypto-browserify'),
|
|
17
17
|
stream: require.resolve('stream-browserify'),
|
|
18
18
|
util: require.resolve('util'),
|
|
19
|
-
buffer: require.resolve('buffer'),
|
|
19
|
+
buffer: require.resolve('buffer/'),
|
|
20
20
|
process: require.resolve('process/browser'),
|
|
21
|
+
zlib: require.resolve('browserify-zlib'),
|
|
22
|
+
assert: require.resolve('assert/'),
|
|
21
23
|
// Remove fs and path since we're using in-memory FileTree
|
|
22
24
|
fs: false,
|
|
23
25
|
path: false,
|
|
24
26
|
vm: false
|
|
27
|
+
},
|
|
28
|
+
alias: {
|
|
29
|
+
// Replace the Node.js ZIP implementation with empty module
|
|
30
|
+
'adm-zip': false
|
|
25
31
|
}
|
|
26
32
|
},
|
|
27
33
|
module: {
|