CLIUtils.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import * as path from 'path';
  4. import { IPackageConfig } from '../interfaces/IPackageConfig';
  5. export class CLIUtils {
  6. /**
  7. * @type {string[]}
  8. */
  9. private static readonly availableInputExtensions: string[] = [
  10. '.js'
  11. ];
  12. /**
  13. * @type {BufferEncoding}
  14. */
  15. private static readonly encoding: BufferEncoding = 'utf8';
  16. /**
  17. * @param outputPath
  18. * @param inputPath
  19. * @returns {string}
  20. */
  21. public static getOutputCodePath (outputPath: string, inputPath: string): string {
  22. if (outputPath) {
  23. return outputPath;
  24. }
  25. return inputPath
  26. .split('.')
  27. .map<string>((value: string, index: number) => {
  28. return index === 0 ? `${value}-obfuscated` : value;
  29. })
  30. .join('.');
  31. }
  32. /**
  33. * @param outputCodePath
  34. * @param sourceMapFileName
  35. * @returns {string}
  36. */
  37. public static getOutputSourceMapPath (outputCodePath: string, sourceMapFileName: string = ''): string {
  38. if (sourceMapFileName) {
  39. outputCodePath = `${outputCodePath.substr(
  40. 0, outputCodePath.lastIndexOf('/')
  41. )}/${sourceMapFileName}`;
  42. }
  43. if (!/\.js\.map$/.test(outputCodePath)) {
  44. outputCodePath = `${outputCodePath.split('.')[0]}.js.map`;
  45. } else if (/\.js$/.test(outputCodePath)) {
  46. outputCodePath += '.map';
  47. }
  48. return outputCodePath;
  49. }
  50. /**
  51. * @returns {IPackageConfig}
  52. */
  53. public static getPackageConfig (): IPackageConfig {
  54. return <IPackageConfig>JSON.parse(
  55. fs.readFileSync(
  56. path.join(
  57. path.dirname(
  58. fs.realpathSync(process.argv[1])
  59. ),
  60. '../package.json'
  61. ),
  62. CLIUtils.encoding
  63. )
  64. );
  65. }
  66. /**
  67. * @param filePath
  68. */
  69. public static isFilePath (filePath: string): boolean {
  70. try {
  71. return fs.statSync(filePath).isFile();
  72. } catch (e) {
  73. return false;
  74. }
  75. }
  76. /**
  77. * @param inputPath
  78. * @returns {string}
  79. */
  80. public static readFile (inputPath: string): string {
  81. return fs.readFileSync(inputPath, CLIUtils.encoding);
  82. }
  83. /**
  84. * @param inputPath
  85. */
  86. public static validateInputPath (inputPath: string): void {
  87. if (!CLIUtils.isFilePath(inputPath)) {
  88. throw new ReferenceError(`Given input path must be a valid file path`);
  89. }
  90. if (!CLIUtils.availableInputExtensions.includes(path.extname(inputPath))) {
  91. throw new ReferenceError(`Input file must have .js extension`);
  92. }
  93. }
  94. /**
  95. * @param outputPath
  96. * @param data
  97. */
  98. public static writeFile (outputPath: string, data: any): void {
  99. mkdirp.sync(path.dirname(outputPath));
  100. fs.writeFileSync(outputPath, data, {
  101. encoding: CLIUtils.encoding
  102. });
  103. }
  104. }