• 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

97.44
/src/cli/utils/ObfuscatedCodeFileUtils.ts
1
import * as fs from 'fs';
6✔
2
import * as mkdirp from 'mkdirp';
6✔
3
import * as path from 'path';
6✔
4

5
import { TInputCLIOptions } from '../../types/options/TInputCLIOptions';
6

7
import { StringSeparator } from '../../enums/StringSeparator';
6✔
8

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

11
export class ObfuscatedCodeFileUtils {
6✔
12
    /**
13
     * @type {string}
14
     */
15
    private readonly inputPath: string;
16

17
    /**
18
     * @type {TInputCLIOptions}
19
     */
20
    private readonly options: TInputCLIOptions;
21

22
    /**
23
     * @param {string} inputPath
24
     * @param {TInputCLIOptions} options
25
     */
26
    public constructor(inputPath: string, options: TInputCLIOptions) {
27
        this.inputPath = path.normalize(inputPath);
342✔
28
        this.options = options;
342✔
29
    }
30

31
    /**
32
     * @param {string} filePath
33
     * @returns {string}
34
     */
35
    public getOutputCodePath(filePath: string): string {
36
        const normalizedFilePath: string = path.normalize(filePath);
228✔
37
        const normalizedRawOutputPath: string | null = this.options.output ? path.normalize(this.options.output) : null;
228✔
38

39
        if (!normalizedRawOutputPath) {
228✔
40
            return normalizedFilePath
72✔
41
                .split(StringSeparator.Dot)
42
                .map((value: string, index: number) => {
43
                    return index === 0 ? `${value}${JavaScriptObfuscatorCLI.obfuscatedFilePrefix}` : value;
144✔
44
                })
45
                .join(StringSeparator.Dot);
46
        }
47

48
        const rawInputPathStats: fs.Stats = fs.lstatSync(this.inputPath);
156✔
49
        const outputPathExtName: string = path.extname(normalizedRawOutputPath);
156✔
50

51
        const isDirectoryRawInputPath: boolean = rawInputPathStats.isDirectory();
156✔
52
        const isDirectoryRawOutputPath: boolean =
53
            !JavaScriptObfuscatorCLI.availableInputExtensions.includes(outputPathExtName);
156✔
54

55
        if (isDirectoryRawInputPath) {
156✔
56
            if (isDirectoryRawOutputPath) {
66✔
57
                const parsedNormalizedFilePath: path.ParsedPath = path.parse(normalizedFilePath);
60✔
58

59
                // Make ending with "/" consistent
60
                if (!parsedNormalizedFilePath.dir.endsWith('/') && this.inputPath.endsWith('/')) {
60✔
61
                    parsedNormalizedFilePath.dir += '/';
12✔
62
                }
63

64
                const baseOutputPath: string = path.join(
60✔
65
                    parsedNormalizedFilePath.dir.replace(this.inputPath, ''),
66
                    parsedNormalizedFilePath.base
67
                );
68

69
                return path.join(normalizedRawOutputPath, baseOutputPath);
60✔
70
            } else {
71
                throw new Error('Output path for directory obfuscation should be a directory path');
6✔
72
            }
73
        } else {
74
            if (isDirectoryRawOutputPath) {
90✔
75
                return path.join(normalizedRawOutputPath, path.basename(filePath));
6✔
76
            } else {
77
                return normalizedRawOutputPath;
84✔
78
            }
79
        }
80
    }
81

82
    /**
83
     * @param {string} outputCodePath
84
     * @param {string} sourceMapFileName
85
     * @returns {string}
86
     */
87
    public getOutputSourceMapPath(outputCodePath: string, sourceMapFileName: string = ''): string {
24✔
88
        if (!outputCodePath) {
162✔
89
            throw new Error('Output code path is empty');
6✔
90
        }
91

92
        let normalizedOutputCodePath: string = path.normalize(outputCodePath);
156✔
93
        let parsedOutputCodePath: path.ParsedPath = path.parse(normalizedOutputCodePath);
156✔
94

95
        if (!parsedOutputCodePath.ext && !sourceMapFileName) {
156✔
96
            throw new Error('Source map file name should be set when output code path is a directory path');
6✔
97
        }
98

99
        if (sourceMapFileName) {
150✔
100
            const indexOfLastSeparator: number = normalizedOutputCodePath.lastIndexOf(path.sep);
78✔
101
            let sourceMapPath: string;
102

103
            if (parsedOutputCodePath.ext) {
78✔
104
                // File path with directory, like: `foo/bar.js`, or without, like: `bar.js`
105
                const isFilePathWithDirectory: boolean = indexOfLastSeparator > 0;
60✔
106

107
                sourceMapPath = isFilePathWithDirectory ? normalizedOutputCodePath.slice(0, indexOfLastSeparator) : '';
60✔
108
            } else {
109
                sourceMapPath = normalizedOutputCodePath;
18✔
110
            }
111

112
            // remove possible drive letter for win32 environment
113
            const normalizedSourceMapFilePath: string = sourceMapFileName.replace(/^[a-zA-Z]:\\*/, '');
78✔
114

115
            normalizedOutputCodePath = path.join(sourceMapPath, normalizedSourceMapFilePath);
78✔
116
        }
117

118
        if (!/\.js\.map$/.test(normalizedOutputCodePath)) {
150✔
119
            parsedOutputCodePath = path.parse(normalizedOutputCodePath);
114✔
120
            const outputCodePathWithoutExtension: string = path.join(
114✔
121
                parsedOutputCodePath.dir,
122
                parsedOutputCodePath.name
123
            );
124

125
            normalizedOutputCodePath = `${outputCodePathWithoutExtension}.js.map`;
114✔
126
        } else if (/\.js$/.test(normalizedOutputCodePath)) {
36!
127
            normalizedOutputCodePath += '.map';
×
128
        }
129

130
        return normalizedOutputCodePath;
150✔
131
    }
132

133
    /**
134
     * @param {string} outputPath
135
     * @param {string} data
136
     */
137
    public writeFile(outputPath: string, data: string): void {
138
        mkdirp.sync(path.dirname(outputPath));
198✔
139

140
        fs.writeFileSync(outputPath, data, {
198✔
141
            encoding: JavaScriptObfuscatorCLI.encoding
142
        });
143
    }
144
}
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