CLIUtils.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import * as path from 'path';
  2. import { TDictionary } from '../../types/TDictionary';
  3. export class CLIUtils {
  4. /**
  5. * @type {string[]}
  6. */
  7. public static readonly allowedConfigFileExtensions: string[] = [
  8. '.js',
  9. '.json'
  10. ];
  11. /**
  12. * @param {string} configPath
  13. * @returns {TDictionary}
  14. */
  15. public static getUserConfig (configPath: string): TDictionary {
  16. let config: TDictionary;
  17. const configFileExtension: string = path.extname(configPath);
  18. const isValidExtension: boolean = CLIUtils.allowedConfigFileExtensions.includes(configFileExtension);
  19. if (!isValidExtension) {
  20. throw new ReferenceError('Given config path must be a valid `.js` or `.json` file path');
  21. }
  22. try {
  23. config = require(configPath);
  24. } catch {
  25. try {
  26. config = __non_webpack_require__(configPath);
  27. } catch {
  28. throw new ReferenceError(`Cannot open config file with path: ${configPath}`);
  29. }
  30. }
  31. return config;
  32. }
  33. }