-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathtar.ts
More file actions
59 lines (53 loc) · 1.8 KB
/
tar.ts
File metadata and controls
59 lines (53 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* tar.ts
*
* Copyright (C) 2025 Posit Software, PBC
*
* Abstraction layer over Deno's `tar` utilities.
*/
import { TarStream, type TarStreamInput } from "tar/tar-stream";
import { join } from "../deno_ral/path.ts";
import { pathWithForwardSlashes } from "../core/path.ts";
/**
* Creates a tar archive from the specified files and directories.
* @param outputPath The path where the tar archive will be created.
* @param filePaths An array of file and directory paths to include in the tar archive.
* @param options Optional configuration for tar creation.
* @param options.baseDir Base directory for resolving relative paths in the archive.
* @returns A promise that resolves when the tar archive is created.
*/
export async function createTarFromFiles(
outputPath: string,
filePaths: string[],
options?: { baseDir?: string },
) {
const baseDir = options?.baseDir;
// Create array of TarStreamInput objects from file paths
const inputs: TarStreamInput[] = await Promise.all(
filePaths.map(async (path) => {
const fullPath = baseDir ? join(baseDir, path) : path;
const stat = await Deno.stat(fullPath);
// Use original path for archive, full path for reading
const archivePath = pathWithForwardSlashes(path);
if (stat.isDirectory) {
// Handle directory
return {
type: "directory",
path: archivePath + (archivePath.endsWith("/") ? "" : "/"),
};
} else {
// Handle file
return {
type: "file",
path: archivePath,
size: stat.size,
readable: (await Deno.open(fullPath)).readable,
};
}
}),
);
// Create tar file using streaming API
await ReadableStream.from(inputs)
.pipeThrough(new TarStream())
.pipeTo((await Deno.create(outputPath)).writable);
}