|
6 | 6 | * Abstraction layer over Deno's `tar` utilities.
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -import { TarStream, type TarStreamInput } from "jsr:@std/tar/tar-stream"; |
| 9 | +import { TarStream, type TarStreamInput } from "tar/tar-stream"; |
| 10 | +import { join } from "../deno_ral/path.ts"; |
| 11 | +import { pathWithForwardSlashes } from "../core/path.ts"; |
10 | 12 |
|
11 | 13 | /**
|
12 | 14 | * Creates a tar archive from the specified files and directories.
|
13 | 15 | * @param outputPath The path where the tar archive will be created.
|
14 |
| - * @param filePaths An array of file and directory paths to include in the tar archive. Paths are relative to outputPath. |
| 16 | + * @param filePaths An array of file and directory paths to include in the tar archive. |
| 17 | + * @param options Optional configuration for tar creation. |
| 18 | + * @param options.baseDir Base directory for resolving relative paths in the archive. |
15 | 19 | * @returns A promise that resolves when the tar archive is created.
|
16 | 20 | */
|
17 | 21 | export async function createTarFromFiles(
|
18 | 22 | outputPath: string,
|
19 | 23 | filePaths: string[],
|
| 24 | + options?: { baseDir?: string }, |
20 | 25 | ) {
|
| 26 | + const baseDir = options?.baseDir; |
| 27 | + |
21 | 28 | // Create array of TarStreamInput objects from file paths
|
22 | 29 | const inputs: TarStreamInput[] = await Promise.all(
|
23 | 30 | filePaths.map(async (path) => {
|
24 |
| - const stat = await Deno.stat(path); |
| 31 | + const fullPath = baseDir ? join(baseDir, path) : path; |
| 32 | + const stat = await Deno.stat(fullPath); |
| 33 | + |
| 34 | + // Use original path for archive, full path for reading |
| 35 | + const archivePath = pathWithForwardSlashes(path); |
25 | 36 |
|
26 | 37 | if (stat.isDirectory) {
|
27 | 38 | // Handle directory
|
28 | 39 | return {
|
29 | 40 | type: "directory",
|
30 |
| - path: path + (path.endsWith("/") ? "" : "/"), |
| 41 | + path: archivePath + (archivePath.endsWith("/") ? "" : "/"), |
31 | 42 | };
|
32 | 43 | } else {
|
33 | 44 | // Handle file
|
34 | 45 | return {
|
35 | 46 | type: "file",
|
36 |
| - path: path, |
| 47 | + path: archivePath, |
37 | 48 | size: stat.size,
|
38 |
| - readable: (await Deno.open(path)).readable, |
| 49 | + readable: (await Deno.open(fullPath)).readable, |
39 | 50 | };
|
40 | 51 | }
|
41 | 52 | }),
|
|
0 commit comments