-
Notifications
You must be signed in to change notification settings - Fork 2
/
recomposeFileHandler.ts
111 lines (99 loc) · 3.62 KB
/
recomposeFileHandler.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
101
102
103
104
105
106
107
108
109
110
111
'use strict';
/* eslint-disable no-await-in-loop */
import { readdir, stat, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { ReassembleXMLFileHandler, setLogLevel } from 'xml-disassembler';
import { YamlToXmlReassembler } from 'xml2yaml-disassembler';
import { JsonToXmlReassembler } from 'xml2json-disassembler';
import { CUSTOM_LABELS_FILE } from '../helpers/constants.js';
import { renameBotVersionFile } from './renameBotVersionFiles.js';
import { moveFiles } from './moveFiles.js';
export async function recomposeFileHandler(
metaAttributes: {
metaSuffix: string;
strictDirectoryName: boolean;
folderType: string;
metadataPaths: string[];
},
postpurge: boolean,
debug: boolean,
format: string
): Promise<void> {
const { metaSuffix, strictDirectoryName, folderType, metadataPaths } = metaAttributes;
if (debug) setLogLevel('debug');
for (const metadataPath of metadataPaths) {
if (metaSuffix === 'labels') {
await reassembleLabels(metadataPath, metaSuffix, postpurge, format);
} else {
let recurse: boolean = false;
if (strictDirectoryName || folderType) recurse = true;
await reassembleDirectories(metadataPath, metaSuffix, recurse, postpurge, format);
}
if (metaSuffix === 'bot') await renameBotVersionFile(metadataPath);
}
}
async function reassembleHandler(
filePath: string,
fileExtension: string,
postPurge: boolean,
format: string
): Promise<void> {
let handler: ReassembleXMLFileHandler | JsonToXmlReassembler | YamlToXmlReassembler;
if (format === 'yaml') {
handler = new YamlToXmlReassembler();
} else if (format === 'json') {
handler = new JsonToXmlReassembler();
} else {
handler = new ReassembleXMLFileHandler();
}
await handler.reassemble({
filePath,
fileExtension,
postPurge,
});
}
async function reassembleLabels(
metadataPath: string,
metaSuffix: string,
postpurge: boolean,
format: string
): Promise<void> {
let sourceDirectory = metadataPath;
let destinationDirectory = join(metadataPath, 'CustomLabels', 'labels');
await moveFiles(sourceDirectory, destinationDirectory, (fileName) => fileName !== CUSTOM_LABELS_FILE);
// do not use postpurge flag due to file moving
await reassembleHandler(join(metadataPath, 'CustomLabels'), `${metaSuffix}-meta.xml`, false, format);
sourceDirectory = join(metadataPath, 'CustomLabels', 'labels');
destinationDirectory = metadataPath;
await moveFiles(sourceDirectory, destinationDirectory, () => true);
await rm(join(metadataPath, 'CustomLabels'), { recursive: true });
if (postpurge) await deleteFilesInDirectory(destinationDirectory);
}
async function deleteFilesInDirectory(directory: string): Promise<void> {
const files = await readdir(directory);
for (const file of files) {
const filePath = join(directory, file);
const fileStat = await stat(filePath);
if (fileStat.isFile() && file !== CUSTOM_LABELS_FILE) {
await rm(filePath);
}
}
}
async function reassembleDirectories(
metadataPath: string,
metaSuffix: string,
recurse: boolean,
postpurge: boolean,
format: string
): Promise<void> {
const subdirectories = (await readdir(metadataPath)).map((file) => join(metadataPath, file));
for (const subdirectory of subdirectories) {
const subDirStat = await stat(subdirectory);
if (subDirStat.isDirectory() && recurse) {
// recursively call this function and set recurse to false
await reassembleDirectories(subdirectory, metaSuffix, false, postpurge, format);
} else if (subDirStat.isDirectory()) {
await reassembleHandler(subdirectory, `${metaSuffix}-meta.xml`, postpurge, format);
}
}
}