@fgv/ts-res-browser-cli 5.0.0-9 → 5.0.1-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +23 -0
- package/eslint.config.js +16 -0
- package/lib/options.d.ts +11 -0
- package/lib/simpleBrowserLauncher.js +1 -1
- package/lib/zipArchiver.d.ts +5 -17
- package/lib/zipArchiver.js +11 -72
- package/package.json +13 -14
- package/.rush/temp/2a8cf957acb5079acb09f144aa8702a03195ed17.tar.log +0 -32
- package/.rush/temp/chunked-rush-logs/ts-res-browser-cli.build.chunks.jsonl +0 -5
- package/.rush/temp/operation/build/all.log +0 -5
- package/.rush/temp/operation/build/log-chunks.jsonl +0 -5
- package/.rush/temp/operation/build/state.json +0 -3
- package/.rush/temp/shrinkwrap-deps.json +0 -651
- package/config/rig.json +0 -16
- package/lib/cli.d.ts.map +0 -1
- package/lib/cli.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/options.d.ts.map +0 -1
- package/lib/options.js.map +0 -1
- package/lib/simpleBrowserLauncher.d.ts.map +0 -1
- package/lib/simpleBrowserLauncher.js.map +0 -1
- package/lib/zipArchiver.d.ts.map +0 -1
- package/lib/zipArchiver.js.map +0 -1
- package/rush-logs/ts-res-browser-cli.build.cache.log +0 -3
- package/rush-logs/ts-res-browser-cli.build.log +0 -5
- package/src/cli.ts +0 -420
- package/src/index.ts +0 -26
- package/src/options.ts +0 -139
- package/src/simpleBrowserLauncher.ts +0 -278
- package/src/zipArchiver.ts +0 -153
- package/tsconfig.json +0 -7
package/src/options.ts
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
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
|
-
* Options for launching the ts-res browser
|
|
25
|
-
*/
|
|
26
|
-
export interface IBrowseOptions {
|
|
27
|
-
/**
|
|
28
|
-
* Input file or directory path
|
|
29
|
-
*/
|
|
30
|
-
input?: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* System configuration file path (JSON file in ISystemConfiguration format)
|
|
34
|
-
* or predefined configuration name
|
|
35
|
-
*/
|
|
36
|
-
config?: string;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Context filter token (pipe-separated qualifier=value pairs, e.g., 'language=en-US|territory=US')
|
|
40
|
-
*/
|
|
41
|
-
contextFilter?: string;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Qualifier default values token (pipe-separated qualifier=value pairs, e.g., 'language=en-US,en-CA|territory=US')
|
|
45
|
-
*/
|
|
46
|
-
qualifierDefaults?: string;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Resource types filter (comma-separated, e.g., 'json,string')
|
|
50
|
-
*/
|
|
51
|
-
resourceTypes?: string;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Maximum distance for language matching
|
|
55
|
-
*/
|
|
56
|
-
maxDistance?: number;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Whether to reduce qualifiers when filtering by context
|
|
60
|
-
*/
|
|
61
|
-
reduceQualifiers?: boolean;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Whether to run in verbose mode
|
|
65
|
-
*/
|
|
66
|
-
verbose?: boolean;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Whether to run in quiet mode
|
|
70
|
-
*/
|
|
71
|
-
quiet?: boolean;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Port to run the development server on (for launching locally)
|
|
75
|
-
*/
|
|
76
|
-
port?: number;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Whether to open the browser automatically
|
|
80
|
-
*/
|
|
81
|
-
open?: boolean;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Whether to launch in interactive mode (with sample data)
|
|
85
|
-
*/
|
|
86
|
-
interactive?: boolean;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Browser URL to launch (for remote instances)
|
|
90
|
-
*/
|
|
91
|
-
url?: string;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Whether to automatically start the development server
|
|
95
|
-
*/
|
|
96
|
-
dev?: boolean;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Whether to start server (dev in monorepo, serve in published packages) and connect automatically
|
|
100
|
-
*/
|
|
101
|
-
serve?: boolean;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Whether to use ZIP loading mode (internal flag)
|
|
105
|
-
*/
|
|
106
|
-
loadZip?: boolean;
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Path to ZIP file to load (internal flag)
|
|
110
|
-
*/
|
|
111
|
-
zipPath?: string;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Name of ZIP file to load (internal flag)
|
|
115
|
-
*/
|
|
116
|
-
zipFile?: string;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Command-line options for the browse command
|
|
121
|
-
*/
|
|
122
|
-
export interface IBrowseCommandOptions {
|
|
123
|
-
input?: string;
|
|
124
|
-
config?: string;
|
|
125
|
-
context?: string;
|
|
126
|
-
contextFilter?: string;
|
|
127
|
-
qualifierDefaults?: string;
|
|
128
|
-
resourceTypes?: string;
|
|
129
|
-
maxDistance?: number;
|
|
130
|
-
reduceQualifiers?: boolean;
|
|
131
|
-
verbose?: boolean;
|
|
132
|
-
quiet?: boolean;
|
|
133
|
-
port?: number;
|
|
134
|
-
open?: boolean;
|
|
135
|
-
interactive?: boolean;
|
|
136
|
-
url?: string;
|
|
137
|
-
dev?: boolean;
|
|
138
|
-
serve?: boolean;
|
|
139
|
-
}
|
|
@@ -1,278 +0,0 @@
|
|
|
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 { exec } from 'child_process';
|
|
24
|
-
import * as path from 'path';
|
|
25
|
-
import * as http from 'http';
|
|
26
|
-
import { Result, succeed, fail } from '@fgv/ts-utils';
|
|
27
|
-
import { IBrowseOptions } from './options';
|
|
28
|
-
import { zipArchiver } from './zipArchiver';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Simplified browser launcher using direct dependency on ts-res-browser
|
|
32
|
-
*/
|
|
33
|
-
export class SimpleBrowserLauncher {
|
|
34
|
-
/**
|
|
35
|
-
* Launches the browser with the specified options
|
|
36
|
-
*/
|
|
37
|
-
public async launch(options: IBrowseOptions): Promise<Result<void>> {
|
|
38
|
-
try {
|
|
39
|
-
// Validate options before proceeding
|
|
40
|
-
if (options.interactive && !options.dev && !options.serve && !options.url) {
|
|
41
|
-
return fail(
|
|
42
|
-
'Interactive mode requires either --dev/--serve to start a local server or --url to specify a remote server.\n\n' +
|
|
43
|
-
'Examples:\n' +
|
|
44
|
-
' ts-res-browser-cli --interactive --dev\n' +
|
|
45
|
-
' ts-res-browser-cli --interactive --serve\n' +
|
|
46
|
-
' ts-res-browser-cli --interactive --url https://example.com/ts-res-browser'
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Check if we need to create a ZIP archive for file-based inputs
|
|
51
|
-
let zipCreated = false;
|
|
52
|
-
let finalOptions = { ...options };
|
|
53
|
-
|
|
54
|
-
if (this._shouldCreateZip(options)) {
|
|
55
|
-
if (!options.quiet) {
|
|
56
|
-
console.log('Creating ZIP archive for file-based resources...');
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const zipResult = await this._createZipArchive(options);
|
|
60
|
-
if (zipResult.isFailure()) {
|
|
61
|
-
return fail(`Failed to create ZIP archive: ${zipResult.message}`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (!options.quiet) {
|
|
65
|
-
console.log(`ZIP archive created: ${zipResult.value.zipPath}`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Modify options to use ZIP loading instead of file paths
|
|
69
|
-
const zipFileName = path.basename(zipResult.value.zipPath);
|
|
70
|
-
finalOptions = {
|
|
71
|
-
...options,
|
|
72
|
-
input: undefined,
|
|
73
|
-
config: undefined,
|
|
74
|
-
// Add ZIP loading parameters
|
|
75
|
-
loadZip: true,
|
|
76
|
-
zipFile: zipFileName,
|
|
77
|
-
zipPath: zipResult.value.zipPath
|
|
78
|
-
};
|
|
79
|
-
zipCreated = true;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Start server if requested
|
|
83
|
-
if ((finalOptions.dev || finalOptions.serve) && !finalOptions.url) {
|
|
84
|
-
if (!finalOptions.quiet) {
|
|
85
|
-
console.log('Starting TS-RES Browser server...');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const serverResult = await this._startServer(finalOptions);
|
|
89
|
-
if (serverResult.isFailure()) {
|
|
90
|
-
return serverResult;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Build URL with parameters
|
|
95
|
-
const url = this._buildUrl(finalOptions);
|
|
96
|
-
|
|
97
|
-
if (!finalOptions.quiet) {
|
|
98
|
-
console.log('Opening TS-RES Browser...');
|
|
99
|
-
if (zipCreated) {
|
|
100
|
-
console.log('ZIP archive created - browser will load from Downloads folder');
|
|
101
|
-
} else {
|
|
102
|
-
if (finalOptions.input) {
|
|
103
|
-
console.log(`Resource file: ${finalOptions.input}`);
|
|
104
|
-
}
|
|
105
|
-
if (finalOptions.config) {
|
|
106
|
-
console.log(`Configuration: ${finalOptions.config}`);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (finalOptions.contextFilter) {
|
|
110
|
-
console.log(`Context filter: ${finalOptions.contextFilter}`);
|
|
111
|
-
}
|
|
112
|
-
console.log(`URL: ${url}`);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Open browser if requested (default to true)
|
|
116
|
-
const shouldOpen = finalOptions.open !== false;
|
|
117
|
-
if (shouldOpen) {
|
|
118
|
-
const openResult = await this._openBrowser(url, finalOptions.verbose || false);
|
|
119
|
-
if (openResult.isFailure()) {
|
|
120
|
-
return fail(`Could not open browser: ${openResult.message}`);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (!finalOptions.quiet) {
|
|
124
|
-
console.log('Browser opened successfully');
|
|
125
|
-
}
|
|
126
|
-
} else {
|
|
127
|
-
console.log(`Browser URL: ${url}`);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return succeed(undefined);
|
|
131
|
-
} catch (error) {
|
|
132
|
-
return fail(`Failed to launch browser: ${error}`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Starts the server using ts-res-browser CLI directly
|
|
138
|
-
*/
|
|
139
|
-
private async _startServer(options: IBrowseOptions): Promise<Result<void>> {
|
|
140
|
-
try {
|
|
141
|
-
// Use ts-res-browser directly from node_modules
|
|
142
|
-
const browserPath = require.resolve('@fgv/ts-res-browser/cli.js');
|
|
143
|
-
const command = `node "${browserPath}" ${options.dev ? 'dev' : 'serve'}`;
|
|
144
|
-
|
|
145
|
-
if (options.verbose) {
|
|
146
|
-
console.log(`Running: ${command}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return new Promise((resolve) => {
|
|
150
|
-
const child = exec(command, (error) => {
|
|
151
|
-
if (error) {
|
|
152
|
-
resolve(fail(`Failed to start server: ${error.message}`));
|
|
153
|
-
} else {
|
|
154
|
-
resolve(succeed(undefined));
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
// Give server time to start, then assume success
|
|
159
|
-
setTimeout(() => {
|
|
160
|
-
if (options.verbose) {
|
|
161
|
-
console.log('Server startup initiated');
|
|
162
|
-
}
|
|
163
|
-
resolve(succeed(undefined));
|
|
164
|
-
}, 2000);
|
|
165
|
-
});
|
|
166
|
-
} catch (error) {
|
|
167
|
-
return fail(`Failed to start server: ${error}`);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Opens the browser to the specified URL
|
|
173
|
-
*/
|
|
174
|
-
private async _openBrowser(url: string, verbose: boolean): Promise<Result<void>> {
|
|
175
|
-
return new Promise((resolve) => {
|
|
176
|
-
const command =
|
|
177
|
-
process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start ""' : 'xdg-open';
|
|
178
|
-
|
|
179
|
-
exec(`${command} "${url}"`, (error) => {
|
|
180
|
-
if (error) {
|
|
181
|
-
resolve(fail(`Failed to open browser: ${error.message}`));
|
|
182
|
-
} else {
|
|
183
|
-
if (verbose) {
|
|
184
|
-
console.log(`Opened browser to: ${url}`);
|
|
185
|
-
}
|
|
186
|
-
resolve(succeed(undefined));
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Builds the URL with query parameters based on options
|
|
194
|
-
*/
|
|
195
|
-
private _buildUrl(options: IBrowseOptions): string {
|
|
196
|
-
// Use provided URL or default based on server type
|
|
197
|
-
const defaultPort = options.dev ? 3000 : 8080;
|
|
198
|
-
const baseUrl = options.url || `http://localhost:${options.port || defaultPort}`;
|
|
199
|
-
const params = new URLSearchParams();
|
|
200
|
-
|
|
201
|
-
if (options.input) {
|
|
202
|
-
params.set('input', options.input);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (options.config) {
|
|
206
|
-
params.set('config', options.config);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (options.contextFilter) {
|
|
210
|
-
params.set('contextFilter', options.contextFilter);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (options.qualifierDefaults) {
|
|
214
|
-
params.set('qualifierDefaults', options.qualifierDefaults);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (options.resourceTypes) {
|
|
218
|
-
params.set('resourceTypes', options.resourceTypes);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (options.maxDistance !== undefined) {
|
|
222
|
-
params.set('maxDistance', options.maxDistance.toString());
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (options.reduceQualifiers) {
|
|
226
|
-
params.set('reduceQualifiers', 'true');
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
if (options.interactive) {
|
|
230
|
-
params.set('interactive', 'true');
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (options.loadZip) {
|
|
234
|
-
params.set('loadZip', 'true');
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
if (options.zipFile) {
|
|
238
|
-
params.set('zipFile', options.zipFile);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (options.zipPath) {
|
|
242
|
-
params.set('zipPath', options.zipPath);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const queryString = params.toString();
|
|
246
|
-
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Determines if a ZIP archive should be created based on the options
|
|
251
|
-
*/
|
|
252
|
-
private _shouldCreateZip(options: IBrowseOptions): boolean {
|
|
253
|
-
// Create ZIP if input or config are file paths (not predefined names)
|
|
254
|
-
const hasFilePath = (path: string | undefined): boolean => {
|
|
255
|
-
if (!path) return false;
|
|
256
|
-
// Check if it's a file path (contains directory separators or file extensions)
|
|
257
|
-
return path.includes('/') || path.includes('\\') || (path.includes('.') && !path.startsWith('.'));
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
return hasFilePath(options.input) || hasFilePath(options.config);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Creates a ZIP archive containing the input and config files
|
|
265
|
-
*/
|
|
266
|
-
private async _createZipArchive(
|
|
267
|
-
options: IBrowseOptions
|
|
268
|
-
): Promise<Result<{ zipPath: string; manifest: any }>> {
|
|
269
|
-
try {
|
|
270
|
-
return await zipArchiver.createArchive({
|
|
271
|
-
input: options.input,
|
|
272
|
-
config: options.config
|
|
273
|
-
});
|
|
274
|
-
} catch (error) {
|
|
275
|
-
return fail(`Failed to create ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
package/src/zipArchiver.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import * as archiver from 'archiver';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import * as os from 'os';
|
|
5
|
-
import { Result, succeed, fail } from '@fgv/ts-utils';
|
|
6
|
-
|
|
7
|
-
export interface ZipArchiveOptions {
|
|
8
|
-
input?: string;
|
|
9
|
-
config?: string;
|
|
10
|
-
outputDir?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface ZipArchiveResult {
|
|
14
|
-
zipPath: string;
|
|
15
|
-
manifest: ZipManifest;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface ZipManifest {
|
|
19
|
-
timestamp: string;
|
|
20
|
-
input?: {
|
|
21
|
-
type: 'file' | 'directory';
|
|
22
|
-
originalPath: string;
|
|
23
|
-
archivePath: string;
|
|
24
|
-
};
|
|
25
|
-
config?: {
|
|
26
|
-
type: 'file';
|
|
27
|
-
originalPath: string;
|
|
28
|
-
archivePath: string;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class ZipArchiver {
|
|
33
|
-
/**
|
|
34
|
-
* Create a ZIP archive containing the specified input and config files/directories
|
|
35
|
-
*/
|
|
36
|
-
async createArchive(options: ZipArchiveOptions): Promise<Result<ZipArchiveResult>> {
|
|
37
|
-
try {
|
|
38
|
-
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
39
|
-
const outputDir = options.outputDir || path.join(os.homedir(), 'Downloads');
|
|
40
|
-
const zipFileName = `ts-res-bundle-${timestamp}.zip`;
|
|
41
|
-
const zipPath = path.join(outputDir, zipFileName);
|
|
42
|
-
|
|
43
|
-
// Ensure output directory exists
|
|
44
|
-
if (!fs.existsSync(outputDir)) {
|
|
45
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const output = fs.createWriteStream(zipPath);
|
|
49
|
-
const archive = archiver.create('zip', {
|
|
50
|
-
zlib: { level: 6 } // Good compression level
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Create manifest
|
|
54
|
-
const manifest: ZipManifest = {
|
|
55
|
-
timestamp: new Date().toISOString()
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// Promise to handle async archiving
|
|
59
|
-
const archivePromise = new Promise<void>((resolve, reject) => {
|
|
60
|
-
output.on('close', () => {
|
|
61
|
-
resolve();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
output.on('error', (err: Error) => {
|
|
65
|
-
reject(err);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
archive.on('error', (err: Error) => {
|
|
69
|
-
reject(err);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Pipe archive data to the file
|
|
74
|
-
archive.pipe(output);
|
|
75
|
-
|
|
76
|
-
// Add input files/directory
|
|
77
|
-
if (options.input) {
|
|
78
|
-
const inputPath = path.resolve(options.input);
|
|
79
|
-
|
|
80
|
-
if (!fs.existsSync(inputPath)) {
|
|
81
|
-
return fail(`Input path does not exist: ${inputPath}`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const stats = fs.statSync(inputPath);
|
|
85
|
-
if (stats.isDirectory()) {
|
|
86
|
-
// Add entire directory recursively, preserving structure
|
|
87
|
-
const dirName = path.basename(inputPath);
|
|
88
|
-
archive.directory(inputPath, `input/${dirName}`);
|
|
89
|
-
manifest.input = {
|
|
90
|
-
type: 'directory',
|
|
91
|
-
originalPath: inputPath,
|
|
92
|
-
archivePath: `input/${dirName}`
|
|
93
|
-
};
|
|
94
|
-
} else if (stats.isFile()) {
|
|
95
|
-
// Add single file
|
|
96
|
-
const fileName = path.basename(inputPath);
|
|
97
|
-
archive.file(inputPath, { name: `input/${fileName}` });
|
|
98
|
-
manifest.input = {
|
|
99
|
-
type: 'file',
|
|
100
|
-
originalPath: inputPath,
|
|
101
|
-
archivePath: `input/${fileName}`
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Add config file
|
|
107
|
-
if (options.config) {
|
|
108
|
-
const configPath = path.resolve(options.config);
|
|
109
|
-
|
|
110
|
-
if (!fs.existsSync(configPath)) {
|
|
111
|
-
return fail(`Config file does not exist: ${configPath}`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const stats = fs.statSync(configPath);
|
|
115
|
-
if (!stats.isFile()) {
|
|
116
|
-
return fail(`Config path must be a file: ${configPath}`);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const fileName = path.basename(configPath);
|
|
120
|
-
archive.file(configPath, { name: `config/${fileName}` });
|
|
121
|
-
manifest.config = {
|
|
122
|
-
type: 'file',
|
|
123
|
-
originalPath: configPath,
|
|
124
|
-
archivePath: `config/${fileName}`
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Add manifest
|
|
129
|
-
archive.append(JSON.stringify(manifest, null, 2), { name: 'manifest.json' });
|
|
130
|
-
|
|
131
|
-
// Finalize the archive
|
|
132
|
-
await archive.finalize();
|
|
133
|
-
await archivePromise;
|
|
134
|
-
|
|
135
|
-
return succeed({
|
|
136
|
-
zipPath,
|
|
137
|
-
manifest
|
|
138
|
-
});
|
|
139
|
-
} catch (error) {
|
|
140
|
-
return fail(`Failed to create ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Get the default downloads directory path
|
|
146
|
-
*/
|
|
147
|
-
getDefaultDownloadsDir(): string {
|
|
148
|
-
return path.join(os.homedir(), 'Downloads');
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Export singleton instance
|
|
153
|
-
export const zipArchiver = new ZipArchiver();
|