-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive-bundle.service.ts
More file actions
149 lines (130 loc) · 4.24 KB
/
archive-bundle.service.ts
File metadata and controls
149 lines (130 loc) · 4.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
import { basename, dirname, join, resolve } from 'node:path';
import { tmpdir } from 'node:os';
import type { LogScope } from '../types/logging.types.js';
import { printInfo } from '../ui/logger.js';
import { runCommand } from '../utils/process.js';
export const ARCHIVE_BUNDLE_HELPER_IMAGE = 'busybox:1.36.1';
/**
* Creates a temporary workspace used to assemble or extract a bundle archive.
*/
export function createArchiveWorkspace(prefix: string): string {
return mkdtempSync(join(tmpdir(), `atlas-lab-${prefix}-`));
}
/**
* Removes a temporary workspace without failing if it has already gone away.
*/
export function cleanupArchiveWorkspace(workspacePath: string): void {
rmSync(workspacePath, { force: true, recursive: true });
}
/**
* Makes sure the helper image used for tar packaging is locally available.
*/
export async function ensureArchiveHelperImage(projectRoot: string, scope: LogScope): Promise<void> {
const inspectResult = await runCommand('docker', ['image', 'inspect', ARCHIVE_BUNDLE_HELPER_IMAGE], {
allowFailure: true,
captureOutput: true,
cwd: projectRoot,
scope
});
if (inspectResult.exitCode === 0) {
printInfo(`Archive helper image is available: ${ARCHIVE_BUNDLE_HELPER_IMAGE}`, scope);
return;
}
printInfo(`Pulling archive helper image ${ARCHIVE_BUNDLE_HELPER_IMAGE}...`, scope);
await runCommand('docker', ['pull', ARCHIVE_BUNDLE_HELPER_IMAGE], {
cwd: projectRoot,
scope
});
}
/**
* Packs a directory into a single gzip-compressed tar bundle.
*/
export async function packDirectoryToArchiveBundle(
sourceDirectory: string,
outputArchivePath: string,
projectRoot: string,
scope: LogScope
): Promise<void> {
const normalizedSourceDirectory = resolve(sourceDirectory);
const normalizedOutputArchivePath = resolve(outputArchivePath);
const outputDirectory = dirname(normalizedOutputArchivePath);
const outputFileName = basename(normalizedOutputArchivePath);
mkdirSync(outputDirectory, { recursive: true });
printInfo(`Packing archive bundle ${normalizedOutputArchivePath}`, scope);
await runCommand(
'docker',
[
'run',
'--rm',
'--mount',
`type=bind,source=${normalizedSourceDirectory},target=/source,readonly`,
'--mount',
`type=bind,source=${outputDirectory},target=/output`,
ARCHIVE_BUNDLE_HELPER_IMAGE,
'sh',
'-c',
`tar -czf /output/${quotePosixShellArgument(outputFileName)} -C /source .`
],
{
cwd: projectRoot,
scope
}
);
}
/**
* Extracts a gzip-compressed tar bundle into a directory.
*/
export async function extractArchiveBundleToDirectory(
inputArchivePath: string,
outputDirectory: string,
projectRoot: string,
scope: LogScope
): Promise<void> {
const normalizedInputArchivePath = resolve(inputArchivePath);
const normalizedOutputDirectory = resolve(outputDirectory);
const inputDirectory = dirname(normalizedInputArchivePath);
const inputFileName = basename(normalizedInputArchivePath);
if (!existsSync(normalizedOutputDirectory)) {
mkdirSync(normalizedOutputDirectory, { recursive: true });
}
printInfo(`Extracting archive bundle ${normalizedInputArchivePath}`, scope);
await runCommand(
'docker',
[
'run',
'--rm',
'--mount',
`type=bind,source=${inputDirectory},target=/input,readonly`,
'--mount',
`type=bind,source=${normalizedOutputDirectory},target=/output`,
ARCHIVE_BUNDLE_HELPER_IMAGE,
'sh',
'-c',
`tar -xzf /input/${quotePosixShellArgument(inputFileName)} -C /output`
],
{
cwd: projectRoot,
scope
}
);
}
/**
* Appends the canonical bundle extension without duplicating `.tar`.
*/
export function normalizeArchiveBundleOutputPath(filePath: string): string {
const normalizedPath = filePath.toLowerCase();
if (normalizedPath.endsWith('.tar.gz') || normalizedPath.endsWith('.tgz')) {
return filePath;
}
if (normalizedPath.endsWith('.tar')) {
return `${filePath}.gz`;
}
return `${filePath}.tar.gz`;
}
/**
* Escapes a single shell argument for the POSIX shell used inside BusyBox.
*/
function quotePosixShellArgument(value: string): string {
return `'${value.replace(/'/gu, `'\"'\"'`)}'`;
}