-
Notifications
You must be signed in to change notification settings - Fork 2
/
maven-content-source.js
131 lines (117 loc) · 3.72 KB
/
maven-content-source.js
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
"use strict";
const sha1 = require('sha1');
const fs = require('fs');
class MavenContentSource {
// fields
#mavenArtifact;
#repository;
#cacheFolder;
#mavenClient;
#git;
#hashKey;
#startPath;
#startPaths;
#logger;
toString() {
return this.#mavenArtifact
+ ' from '
+ this.#repository
+ ' in ' + this.#getCacheFolderName();
}
// methods
constructor({
mavenClient,
git,
repository,
mavenArtifact,
cacheFolder,
logger,
startPath,
startPaths
}) {
this.#mavenArtifact = mavenArtifact;
this.#mavenClient = mavenClient;
this.#git = git;
this.#repository = repository;
this.#cacheFolder = cacheFolder;
this.#logger = logger;
this.#startPath = startPath;
this.#startPaths = startPaths;
if (!["zip", "jar", "tgz"].includes(this.#mavenArtifact.extension)) {
throw new MavenContentSourceError('Unsupported extension "' + this.#mavenArtifact.extension + '", use one of zip, jar, tgz');
}
this.#hashKey = this.#computeHashKey();
}
async #ensureCacheParentFolderExists() {
await fs.promises.mkdir(this.#cacheFolder, {recursive: true})
}
#computeHashKey() {
return sha1(
this.#repository.baseUrl
+ this.#mavenArtifact.groupId
+ this.#mavenArtifact.artifactId
+ this.#mavenArtifact.version
+ this.#mavenArtifact.classifier
+ this.#mavenArtifact.extension)
}
async #materializeToDisk() {
await this.#ensureCacheParentFolderExists();
const cacheFolderName = this.#getCacheFolderName();
const git = this.#git
if (fs.existsSync(cacheFolderName)) {
// already cached.
return cacheFolderName;
}
await fs.promises.mkdir(cacheFolderName);
await this.#mavenClient.downloadAndExtract(this.#repository, cacheFolderName, this.#mavenArtifact);
await git.init({
fs: fs,
dir: cacheFolderName,
bare: false,
defaultBranch: this.#mavenArtifact.version
})
const FILE = 0
const status = await git.statusMatrix({fs, dir: cacheFolderName});
await Promise.all(status.map(async (row) => {
const filepath = row[FILE];
await git.add({
fs,
dir: cacheFolderName,
filepath
});
}));
await git.commit({
fs,
dir: cacheFolderName,
author: {name: 'Antora', email: 'root@example.com'},
message: 'Initial commit.'
});
return cacheFolderName;
}
async addAsSourceToPlaybook(playbook) {
const folder = await this.#materializeToDisk();
if (!playbook.content) playbook.content = {};
if (!playbook.content.sources) playbook.content.sources = [];
let antoraContentSource = {
url: folder,
branches: 'HEAD'
};
if (this.#startPath) {
antoraContentSource.start_path = this.#startPath;
}
if (this.#startPaths) {
antoraContentSource.start_paths = this.#startPaths;
}
playbook.content.sources.push(antoraContentSource);
}
#getCacheFolderName() {
return this.#cacheFolder + '/' + this.#hashKey
}
}
class MavenContentSourceError extends Error {
constructor(message) {
super(message);
this.name = 'MavenContentSourceError';
}
}
module.exports = MavenContentSource