-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
100 lines (87 loc) · 2.78 KB
/
index.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
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
import { ANTORA_DEFAULTS } from "./lib/constants/Enum";
import { ConfluenceClientV1 } from "./lib/client/ConfluenceClientV1";
import { ConfluenceClient } from "./lib/client/ConfluenceClient";
import {
buildPageStructure,
deletePages,
getPagesToBeRemoved,
getRenamedPages,
publish,
} from "./lib/service/PageService";
import { BufferFile } from "vinyl";
import { getLogger } from "./lib/Logger";
import {
createState,
initializeState,
updateState,
} from "./lib/service/StateService";
import { AntoraPlaybook, CaptainConfig, PageRepresentation } from "./lib/types";
const LOGGER = getLogger();
const publishToConfluence = async (
destConfig: CaptainConfig,
files: BufferFile[],
playbook: AntoraPlaybook,
) => {
if (process.env.SKIP_CONFLUENCE) {
LOGGER.info(
"Skip publishing to Confluence, because SKIP_CONFLUENCE was set",
);
return;
}
LOGGER.info(`Publishing ${playbook.site.title} to Confluence`);
const outPutDir = playbook.output.dir || ANTORA_DEFAULTS.OUTPUT_DIR;
const confluenceClient: ConfluenceClient = new ConfluenceClientV1({
editorVersion: destConfig.editorVersion || "v1",
baseUrl: new URL(destConfig.confluenceApi),
spaceKey: destConfig.confluenceSpace,
ancestorId: destConfig.ancestorId,
captainName: destConfig.captainName,
});
await confluenceClient.init();
const pageStructure = new Map();
pageStructure.set("inventory", new Map());
pageStructure.set("flat", []);
const state = await initializeState(confluenceClient);
if (state) {
const stateValues: PageRepresentation[] = Object.values(
JSON.parse(state.value),
);
await buildPageStructure(files, pageStructure, destConfig);
const removals = getPagesToBeRemoved(stateValues, pageStructure);
if (removals.length > 0) {
LOGGER.info("Removing untracked pages");
await deletePages(confluenceClient, removals);
}
const renames = getRenamedPages(stateValues, pageStructure);
LOGGER.info("Publishing pages");
await publish(
confluenceClient,
outPutDir,
pageStructure,
destConfig.showBanner || false,
pageStructure.get("flat"),
renames,
);
LOGGER.info("Writing state to Confluence");
await updateState(confluenceClient, {
...state,
value: JSON.stringify(Object.fromEntries(pageStructure.get("inventory"))),
});
} else {
await buildPageStructure(files, pageStructure, destConfig);
LOGGER.info("Publishing pages");
await publish(
confluenceClient,
outPutDir,
pageStructure,
destConfig.showBanner || false,
pageStructure.get("flat"),
);
await createState(
confluenceClient,
JSON.stringify(Object.fromEntries(pageStructure.get("inventory"))),
);
}
return {};
};
module.exports = publishToConfluence;