• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

javascript-obfuscator / javascript-obfuscator / 19907815758

03 Dec 2025 08:27PM UTC coverage: 97.319%. Remained the same
19907815758

push

github

sanex3339
Adjust precommit hook

1770 of 1891 branches covered (93.6%)

Branch coverage included in aggregate %.

5671 of 5755 relevant lines covered (98.54%)

34102965.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/cli/utils/IdentifierNamesCacheFileUtils.ts
1
import * as fs from 'fs';
6✔
2
import * as path from 'path';
6✔
3

4
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
5

6
import { IFileData } from '../../interfaces/cli/IFileData';
7

8
import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
6✔
9

10
/**
11
 * Utils to work with identifier names cache file
12
 */
13
export class IdentifierNamesCacheFileUtils {
6✔
14
    /**
15
     * @type {string}
16
     */
17
    private static readonly identifierNamesCacheExtension: string = '.json';
6✔
18

19
    /**
20
     * @type {string}
21
     */
22
    private readonly identifierNamesCachePath: string | undefined;
23

24
    /**
25
     * @param {string} identifierNamesCachePath
26
     */
27
    public constructor(identifierNamesCachePath: string | undefined) {
28
        this.identifierNamesCachePath = identifierNamesCachePath;
222✔
29
    }
30

31
    /**
32
     * @param {string} filePath
33
     * @returns {boolean}
34
     */
35
    private static isValidFilePath(filePath: string): boolean {
36
        try {
36✔
37
            return (
36✔
38
                fs.statSync(filePath).isFile() &&
60✔
39
                path.extname(filePath) === IdentifierNamesCacheFileUtils.identifierNamesCacheExtension
40
            );
41
        } catch {
42
            return false;
6✔
43
        }
44
    }
45

46
    /**
47
     * @param {string} filePath
48
     * @returns {IFileData}
49
     */
50
    private static readFile(filePath: string): IFileData {
51
        return {
18✔
52
            filePath: path.normalize(filePath),
53
            content: fs.readFileSync(filePath, JavaScriptObfuscatorCLI.encoding)
54
        };
55
    }
56

57
    /**
58
     * @returns {TIdentifierNamesCache | null}
59
     */
60
    public readFile(): TIdentifierNamesCache | null {
61
        if (!this.identifierNamesCachePath) {
198✔
62
            return null;
162✔
63
        }
64

65
        if (!IdentifierNamesCacheFileUtils.isValidFilePath(this.identifierNamesCachePath)) {
36✔
66
            throw new ReferenceError(
18✔
67
                `Given identifier names cache path must be a valid ${
68
                    IdentifierNamesCacheFileUtils.identifierNamesCacheExtension
69
                } file path`
70
            );
71
        }
72

73
        const fileData: IFileData = IdentifierNamesCacheFileUtils.readFile(this.identifierNamesCachePath);
18✔
74

75
        if (!fileData.content) {
18!
76
            // Initial state of identifier names cache file
77
            return {};
×
78
        }
79

80
        try {
18✔
81
            // Already written identifier names cache file
82
            return JSON.parse(fileData.content);
18✔
83
        } catch {
84
            throw new ReferenceError(
12✔
85
                'Identifier names cache file must contains a json dictionary with identifier names'
86
            );
87
        }
88
    }
89

90
    /**
91
     * @param {TIdentifierNamesCache} identifierNamesCache
92
     */
93
    public writeFile(identifierNamesCache: TIdentifierNamesCache): void {
94
        if (!this.identifierNamesCachePath) {
168✔
95
            return;
156✔
96
        }
97

98
        const identifierNamesCacheJson: string = JSON.stringify(identifierNamesCache);
12✔
99

100
        fs.writeFileSync(this.identifierNamesCachePath, identifierNamesCacheJson, {
12✔
101
            encoding: JavaScriptObfuscatorCLI.encoding
102
        });
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc