Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/deno_ral/tar.ts
6449 views
1
/*
2
* tar.ts
3
*
4
* Copyright (C) 2025 Posit Software, PBC
5
*
6
* Abstraction layer over Deno's `tar` utilities.
7
*/
8
9
import { TarStream, type TarStreamInput } from "tar/tar-stream";
10
import { join } from "../deno_ral/path.ts";
11
import { pathWithForwardSlashes } from "../core/path.ts";
12
13
/**
14
* Creates a tar archive from the specified files and directories.
15
* @param outputPath The path where the tar archive will be created.
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.
19
* @returns A promise that resolves when the tar archive is created.
20
*/
21
export async function createTarFromFiles(
22
outputPath: string,
23
filePaths: string[],
24
options?: { baseDir?: string },
25
) {
26
const baseDir = options?.baseDir;
27
28
// Create array of TarStreamInput objects from file paths
29
const inputs: TarStreamInput[] = await Promise.all(
30
filePaths.map(async (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);
36
37
if (stat.isDirectory) {
38
// Handle directory
39
return {
40
type: "directory",
41
path: archivePath + (archivePath.endsWith("/") ? "" : "/"),
42
};
43
} else {
44
// Handle file
45
return {
46
type: "file",
47
path: archivePath,
48
size: stat.size,
49
readable: (await Deno.open(fullPath)).readable,
50
};
51
}
52
}),
53
);
54
55
// Create tar file using streaming API
56
await ReadableStream.from(inputs)
57
.pipeThrough(new TarStream())
58
.pipeTo((await Deno.create(outputPath)).writable);
59
}
60
61