Przeglądaj źródła

Support of output directory paths with a dot symbol

sanex3339 5 lat temu
rodzic
commit
28b2422c7e

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@ v0.25.0
 * Improved `selfDefending` helper logic
 * Fixed a bunch of conflicts between generated identifier names. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/550. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/549
 * Prevented transformation of object keys in sequence expression that has `super` call
+* Support of output directory paths with a dot symbol
 
 v0.24.6
 ---

Plik diff jest za duży
+ 0 - 0
dist/index.cli.js


Plik diff jest za duży
+ 0 - 0
dist/index.js


+ 8 - 1
src/cli/JavaScriptObfuscatorCLI.ts

@@ -28,6 +28,13 @@ import { Utils } from '../utils/Utils';
 import { LoggingPrefix } from '../enums/logger/LoggingPrefix';
 
 export class JavaScriptObfuscatorCLI implements IInitializable {
+    /**
+     * @type {string[]}
+     */
+    public static readonly availableInputExtensions: string[] = [
+        '.js'
+    ];
+
     /**
      * @type {BufferEncoding}
      */
@@ -36,7 +43,7 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
     /**
      * @type {string}
      */
-    public static obfuscatedFilePrefix: string = '-obfuscated';
+    public static readonly obfuscatedFilePrefix: string = '-obfuscated';
 
     /**
      * @type {string}

+ 5 - 1
src/cli/utils/ObfuscatedCodeWriter.ts

@@ -51,8 +51,12 @@ export class ObfuscatedCodeWriter {
         }
 
         const rawInputPathStats: fs.Stats = fs.lstatSync(this.inputPath);
+        const outputPathExtName: string = path.extname(normalizedRawOutputPath);
+
         const isDirectoryRawInputPath: boolean = rawInputPathStats.isDirectory();
-        const isDirectoryRawOutputPath: boolean = path.extname(normalizedRawOutputPath) === '';
+        const isDirectoryRawOutputPath: boolean = !JavaScriptObfuscatorCLI
+            .availableInputExtensions
+            .includes(outputPathExtName);
 
         if (isDirectoryRawInputPath) {
             if (isDirectoryRawOutputPath) {

+ 2 - 9
src/cli/utils/SourceCodeReader.ts

@@ -9,13 +9,6 @@ import { IFileData } from '../../interfaces/cli/IFileData';
 import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
 
 export class SourceCodeReader {
-    /**
-     * @type {string[]}
-     */
-    public static readonly availableInputExtensions: string[] = [
-        '.js'
-    ];
-
     /**
      * @type {string}
      */
@@ -96,7 +89,7 @@ export class SourceCodeReader {
      * @returns {boolean}
      */
     private static isValidFile (filePath: string, excludePatterns: string[] = []): boolean {
-        return SourceCodeReader.availableInputExtensions.includes(path.extname(filePath))
+        return JavaScriptObfuscatorCLI.availableInputExtensions.includes(path.extname(filePath))
             && !filePath.includes(JavaScriptObfuscatorCLI.obfuscatedFilePrefix)
             && !SourceCodeReader.isExcludedPath(filePath, excludePatterns);
     }
@@ -130,7 +123,7 @@ export class SourceCodeReader {
             return this.readDirectoryRecursive(this.inputPath);
         }
 
-        const availableFilePaths: string = SourceCodeReader
+        const availableFilePaths: string = JavaScriptObfuscatorCLI
             .availableInputExtensions
             .map((extension: string) => `\`${extension}\``)
             .join(', ');

+ 42 - 17
test/unit-tests/cli/utils/ObfuscatedCodeWriter.spec.ts

@@ -86,25 +86,50 @@ describe('ObfuscatedCodeWriter', () => {
         });
 
         describe('Variant #4: raw input path is a directory path, raw output path is a directory path', () => {
-            const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
-            const rawInputPath: string = `${tmpDirectoryPath}/input`;
-            const rawOutputPath: string = `${tmpDirectoryPath}/output`;
-            const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/${tmpDirectoryPath}/input/test-input.js`;
-
-            let outputCodePath: string;
-
-            before(() => {
-                const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
-                    rawInputPath,
-                    {
-                        output: rawOutputPath
-                    }
-                );
-                outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
+            describe('Variant #1: base directory name', () => {
+                const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
+                const rawInputPath: string = `${tmpDirectoryPath}/input`;
+                const rawOutputPath: string = `${tmpDirectoryPath}/output`;
+                const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/${tmpDirectoryPath}/input/test-input.js`;
+
+                let outputCodePath: string;
+
+                before(() => {
+                    const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
+                        rawInputPath,
+                        {
+                            output: rawOutputPath
+                        }
+                    );
+                    outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
+                });
+
+                it('should return output path that contains raw output path and actual file input path', () => {
+                    assert.equal(outputCodePath, expectedOutputCodePath);
+                });
             });
 
-            it('should return output path that contains raw output path and actual file input path', () => {
-                assert.equal(outputCodePath, expectedOutputCodePath);
+            describe('Variant #2: directory name with dot', () => {
+                const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
+                const rawInputPath: string = `${tmpDirectoryPath}/input`;
+                const rawOutputPath: string = `${tmpDirectoryPath}/output/foo.bar`;
+                const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/foo.bar/${tmpDirectoryPath}/input/test-input.js`;
+
+                let outputCodePath: string;
+
+                before(() => {
+                    const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
+                        rawInputPath,
+                        {
+                            output: rawOutputPath
+                        }
+                    );
+                    outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
+                });
+
+                it('should return output path that contains raw output path and actual file input path', () => {
+                    assert.equal(outputCodePath, expectedOutputCodePath);
+                });
             });
         });
 

+ 30 - 0
test/unit-tests/cli/utils/SourceCodeReader.spec.ts

@@ -545,6 +545,36 @@ describe('SourceCodeReader', () => {
                     });
                 });
             });
+
+            describe('Variant #5: `inputPath` is a valid path with dot', () => {
+                const tmpDirectoryWithDotPath: string = `${tmpDirectoryPath}.bar`;
+                const tmpFileName: string = 'foo.js';
+                const filePath: string = `${tmpDirectoryWithDotPath}/${tmpFileName}`;
+
+                const expectedResult: IFileData[] = [
+                    {
+                        filePath: filePath,
+                        content: fileContent
+                    }
+                ];
+
+                let result: IFileData[];
+
+                before(() => {
+                    mkdirp.sync(tmpDirectoryWithDotPath);
+                    fs.writeFileSync(filePath, fileContent);
+                    result = new SourceCodeReader(tmpDirectoryWithDotPath, {}).readSourceCode();
+                });
+
+                it('should return files data', () => {
+                    assert.deepEqual(result, expectedResult);
+                });
+
+                after(() => {
+                    fs.unlinkSync(filePath);
+                    rimraf.sync(tmpDirectoryWithDotPath);
+                });
+            });
         });
     });
 

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików