-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathLegacyFileConverter.ts
106 lines (90 loc) · 3.14 KB
/
LegacyFileConverter.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
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import { app } from 'electron';
import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import { legacyOutputFileEnding } from '../../Frontend/shared-constants';
import { writeOpossumFile } from '../../shared/write-file';
import { getFilePathWithAppendix } from '../utils/getFilePathWithAppendix';
import { FileConverter } from './FileConverter';
export class LegacyFileConverter extends FileConverter {
readonly fileTypeName: string = 'Legacy Opossum';
readonly fileTypeSwitch: string = '--opossum';
private tryToGetLegacyInputJsonFromLegacyOutputJson(
filePath: string,
): string {
const outputFilePattern = `(${legacyOutputFileEnding})$`;
const outputFileRegex = new RegExp(outputFilePattern);
const jsonInputFilePath = filePath.replace(outputFileRegex, '.json');
if (fs.existsSync(jsonInputFilePath)) {
return jsonInputFilePath;
}
const jsonGzipInputFilePath = filePath.replace(outputFileRegex, '.json.gz');
if (fs.existsSync(jsonGzipInputFilePath)) {
return jsonGzipInputFilePath;
}
return filePath;
}
private readInputJson(filePath: string): string {
let inputJson: string;
if (filePath.endsWith('.json.gz')) {
const file = fs.readFileSync(filePath);
inputJson = zlib.gunzipSync(file).toString();
} else {
inputJson = fs.readFileSync(filePath, {
encoding: 'utf-8',
});
}
return inputJson;
}
private readOutputJson(filePath: string): string | undefined {
const expectedAssociatedAttributionFilePath = getFilePathWithAppendix(
filePath,
legacyOutputFileEnding,
);
if (fs.existsSync(expectedAssociatedAttributionFilePath)) {
return fs.readFileSync(expectedAssociatedAttributionFilePath, {
encoding: 'utf-8',
});
}
return undefined;
}
override async convertToOpossum(
toBeConvertedFilePath: string,
opossumSaveLocation: string,
): Promise<void> {
try {
let pathToInputJson = toBeConvertedFilePath;
if (toBeConvertedFilePath.endsWith(legacyOutputFileEnding)) {
pathToInputJson = this.tryToGetLegacyInputJsonFromLegacyOutputJson(
toBeConvertedFilePath,
);
}
await writeOpossumFile({
path: opossumSaveLocation,
input: this.readInputJson(pathToInputJson),
output: this.readOutputJson(pathToInputJson),
});
} catch (error) {
throw new Error(
'Conversion of Legacy Opossum file to .opossum file failed',
);
}
}
protected override async preConvertFile(
toBeConvertedFilePath: string,
): Promise<string | null> {
let tempFilePath;
try {
tempFilePath = path.join(app.getPath('temp'), 'temp.opossum');
} catch (error) {
// When executing as part of unit tests, app.getPath('temp') throws an error
tempFilePath = path.join(__dirname, 'temp.opossum');
}
await this.convertToOpossum(toBeConvertedFilePath, tempFilePath);
return tempFilePath;
}
}