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