-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathartifacts.ts
56 lines (51 loc) · 1.68 KB
/
artifacts.ts
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
import path from 'path';
import { bunyan } from '@expo/logger';
import fs from 'fs-extra';
import * as tar from 'tar';
import config from './config';
export async function prepareArtifacts(artifactPaths: string[], logger?: bunyan): Promise<string> {
const l = logger?.child({ phase: 'PREPARE_ARTIFACTS' });
l?.info('Preparing artifacts');
let suffix;
let localPath;
if (artifactPaths.length === 1 && !(await fs.lstat(artifactPaths[0])).isDirectory()) {
[localPath] = artifactPaths;
suffix = path.extname(artifactPaths[0]);
} else {
const parentDir = artifactPaths.reduce(
(acc, item) => getCommonParentDir(acc, item),
artifactPaths[0]
);
const relativePathsToArchive = artifactPaths.map((absolute) =>
path.relative(parentDir, absolute)
);
const archivePath = path.join(config.workingdir, 'artifacts.tar.gz');
await tar.c(
{
gzip: true,
file: archivePath,
cwd: parentDir,
},
relativePathsToArchive
);
suffix = '.tar.gz';
localPath = archivePath;
}
const artifactName = `build-${Date.now()}${suffix}`;
const destPath = config.artifactPath ?? path.join(config.artifactsDir, artifactName);
await fs.copy(localPath, destPath);
l?.info({ phase: 'PREPARE_ARTIFACTS' }, `Writing artifacts to ${destPath}`);
return destPath;
}
function getCommonParentDir(path1: string, path2: string): string {
const normalizedPath1 = path.normalize(path1);
const normalizedPath2 = path.normalize(path2);
let current = path.dirname(normalizedPath1);
while (current !== '/') {
if (normalizedPath2.startsWith(current)) {
return current;
}
current = path.dirname(current);
}
return '/';
}