mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added config file extension validation
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
import * as path from 'path';
|
||||
|
||||
import { TDictionary } from '../../types/TDictionary';
|
||||
|
||||
export class CLIUtils {
|
||||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
public static readonly allowedConfigFileExtensions: string[] = [
|
||||
'.js',
|
||||
'.json'
|
||||
];
|
||||
|
||||
/**
|
||||
* @param {string} configPath
|
||||
* @returns {TDictionary}
|
||||
@@ -8,13 +18,20 @@ export class CLIUtils {
|
||||
public static getUserConfig (configPath: string): TDictionary {
|
||||
let config: TDictionary;
|
||||
|
||||
const configFileExtension: string = path.extname(configPath);
|
||||
const isValidExtension: boolean = CLIUtils.allowedConfigFileExtensions.includes(configFileExtension);
|
||||
|
||||
if (!isValidExtension) {
|
||||
throw new ReferenceError('Given config path must be a valid `.js` or `.json` file path');
|
||||
}
|
||||
|
||||
try {
|
||||
config = require(configPath);
|
||||
} catch {
|
||||
try {
|
||||
config = __non_webpack_require__(configPath);
|
||||
} catch {
|
||||
throw new ReferenceError('Given config path must be a valid `.js` or `.json` file path');
|
||||
throw new ReferenceError(`Cannot open config file with path: ${configPath}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user