-
Notifications
You must be signed in to change notification settings - Fork 2
/
decomposeFileHandler.ts
132 lines (122 loc) · 4.63 KB
/
decomposeFileHandler.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
/* eslint-disable no-await-in-loop */
import { resolve, relative, join } from 'node:path';
import { readdir, stat, rm, rename } from 'node:fs/promises';
import { DisassembleXMLFileHandler, setLogLevel } from 'xml-disassembler';
import { XmlToYamlDisassembler } from 'xml2yaml-disassembler';
import { XmlToJsonDisassembler } from 'xml2json-disassembler';
import { CUSTOM_LABELS_FILE, WORKFLOW_SUFFIX_MAPPING } from '../helpers/constants.js';
import { moveFiles } from './moveFiles.js';
export async function decomposeFileHandler(
metaAttributes: {
metadataPaths: string[];
metaSuffix: string;
strictDirectoryName: boolean;
folderType: string;
uniqueIdElements: string;
},
prepurge: boolean,
postpurge: boolean,
debug: boolean,
format: string,
ignorePath: string
): Promise<void> {
const { metadataPaths, metaSuffix, strictDirectoryName, folderType, uniqueIdElements } = metaAttributes;
if (debug) setLogLevel('debug');
for (const metadataPath of metadataPaths) {
if (strictDirectoryName || folderType) {
await subDirectoryHandler(metadataPath, uniqueIdElements, prepurge, postpurge, format, ignorePath);
} else if (metaSuffix === 'labels') {
// do not use the prePurge flag in the xml-disassembler package for labels due to file moving
if (prepurge) await prePurgeLabels(metadataPath);
const absoluteLabelFilePath = resolve(metadataPath, CUSTOM_LABELS_FILE);
const relativeLabelFilePath = relative(process.cwd(), absoluteLabelFilePath);
await disassembleHandler(relativeLabelFilePath, uniqueIdElements, false, postpurge, format, ignorePath);
// move labels from the directory they are created in
await moveAndRenameLabels(metadataPath, format);
} else {
await disassembleHandler(metadataPath, uniqueIdElements, prepurge, postpurge, format, ignorePath);
}
if (metaSuffix === 'workflow') {
await renameWorkflows(metadataPath);
}
}
}
async function disassembleHandler(
filePath: string,
uniqueIdElements: string,
prePurge: boolean,
postPurge: boolean,
format: string,
ignorePath: string
): Promise<void> {
let handler: DisassembleXMLFileHandler | XmlToJsonDisassembler | XmlToYamlDisassembler;
if (format === 'yaml') {
handler = new XmlToYamlDisassembler();
} else if (format === 'json') {
handler = new XmlToJsonDisassembler();
} else {
handler = new DisassembleXMLFileHandler();
}
await handler.disassemble({
filePath,
uniqueIdElements,
prePurge,
postPurge,
ignorePath,
});
}
async function prePurgeLabels(metadataPath: string): Promise<void> {
const subFiles = await readdir(metadataPath);
for (const subFile of subFiles) {
const subfilePath = join(metadataPath, subFile);
if ((await stat(subfilePath)).isFile() && subFile !== CUSTOM_LABELS_FILE) {
await rm(subfilePath, { recursive: true });
}
}
}
async function moveAndRenameLabels(metadataPath: string, format: string): Promise<void> {
const sourceDirectory = join(metadataPath, 'CustomLabels', 'labels');
const destinationDirectory = metadataPath;
const labelFiles = await readdir(sourceDirectory);
for (const file of labelFiles) {
if (file.endsWith(`.labels-meta.${format}`)) {
const oldFilePath = join(sourceDirectory, file);
const newFileName = file.replace(`.labels-meta.${format}`, `.label-meta.${format}`);
const newFilePath = join(destinationDirectory, newFileName);
await rename(oldFilePath, newFilePath);
}
}
await moveFiles(sourceDirectory, destinationDirectory, () => true);
await rm(join(metadataPath, 'CustomLabels'), { recursive: true });
}
async function subDirectoryHandler(
metadataPath: string,
uniqueIdElements: string,
prepurge: boolean,
postpurge: boolean,
format: string,
ignorePath: string
): Promise<void> {
const subFiles = await readdir(metadataPath);
for (const subFile of subFiles) {
const subFilePath = join(metadataPath, subFile);
if ((await stat(subFilePath)).isDirectory()) {
await disassembleHandler(subFilePath, uniqueIdElements, prepurge, postpurge, format, ignorePath);
}
}
}
async function renameWorkflows(directory: string): Promise<void> {
const files = await readdir(directory, { recursive: true });
for (const file of files) {
// Check if the file matches any suffix in WORKFLOW_SUFFIX_MAPPING
for (const [suffix, newSuffix] of Object.entries(WORKFLOW_SUFFIX_MAPPING)) {
if (file.endsWith(suffix)) {
const oldFilePath = join(directory, file);
const newFilePath = join(directory, file.replace(suffix, newSuffix));
await rename(oldFilePath, newFilePath);
break;
}
}
}
}