This repository was archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (61 loc) · 2.14 KB
/
index.js
File metadata and controls
85 lines (61 loc) · 2.14 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
//--------------------------------------------------------
//-- Git archive
//--------------------------------------------------------
'use strict';
const os = require('os');
const fs = require('fs');
const { execSync } = require('child_process');
const slash = require('slash');
const tmp = require('tmp');
const slugify = require('@sindresorhus/slugify');
const isWindowsShell = () => {
return os.type().toLowerCase().includes('windows') && !process.env.SHELL; // eslint-disable-line no-process-env
};
const getPath = ({ url, treeish, format, extract, path }) => {
return new Promise((resolve) => {
if (path) {
resolve(path);
} else {
tmp.setGracefulCleanup();
const prefix = slugify(`absolunetgitarchive-${url}-${treeish}`);
if (extract) {
tmp.dir({ prefix: `${prefix}-`, unsafeCleanup: true }, (error, temporaryPath) => {
if (error) {
throw new Error(error);
}
resolve(temporaryPath);
});
} else {
tmp.file({ prefix: `${prefix}-`, postfix: `.${format}`, unsafeCleanup: true }, (error, temporaryFile) => {
if (error) {
throw new Error(error);
}
resolve(temporaryFile);
});
}
}
});
};
class GitArchive {
// The tree or commit to produce an archive for.
download(url, { treeish = 'master', format = 'zip', extract = true, path } = {}) {
return new Promise((resolve) => {
getPath({ url, treeish, format, extract, path }).then((_finalPath) => {
const finalPath = slash(_finalPath);
const method = `ssh-add && git archive --remote=${url} ${treeish}`;
const options = { stdio: 'inherit' };
if (extract) {
const fileName = `${finalPath}/archive`;
execSync(`${method} --format tar.gz --output ${fileName}`, options);
const tarFileName = `${isWindowsShell() ? fileName : fileName.replace(/(?<drive>[A-Z]):\//u, '/$<drive>/')}`;
execSync(`tar -xf ${tarFileName} -C ${finalPath}`, { stdio: 'ignore' });
fs.unlinkSync(fileName);
} else {
execSync(`${method} --format ${format} --output ${finalPath}`, options);
}
resolve(finalPath);
});
});
}
}
module.exports = new GitArchive();