CLIUtils.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 {string} outputPath
  18. * @param {string} 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 {string} outputCodePath
  34. * @param {string} sourceMapFileName
  35. * @returns {string}
  36. */
  37. public static getOutputSourceMapPath (outputCodePath: string, sourceMapFileName: string = ''): string {
  38. if (sourceMapFileName) {
  39. outputCodePath = `${outputCodePath.substring(
  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 {string} filePath
  68. * @returns {boolean}
  69. */
  70. public static isFilePath (filePath: string): boolean {
  71. try {
  72. return fs.statSync(filePath).isFile();
  73. } catch (e) {
  74. return false;
  75. }
  76. }
  77. /**
  78. * @param {string} inputPath
  79. * @returns {string}
  80. */
  81. public static readFile (inputPath: string): string {
  82. return fs.readFileSync(inputPath, CLIUtils.encoding);
  83. }
  84. /**
  85. * @param {string} inputPath
  86. */
  87. public static validateInputPath (inputPath: string): void {
  88. if (!CLIUtils.isFilePath(inputPath)) {
  89. throw new ReferenceError(`Given input path must be a valid file path`);
  90. }
  91. if (!CLIUtils.availableInputExtensions.includes(path.extname(inputPath))) {
  92. throw new ReferenceError(`Input file must have .js extension`);
  93. }
  94. }
  95. /**
  96. * @param {string} outputPath
  97. * @param {any} data
  98. */
  99. public static writeFile (outputPath: string, data: any): void {
  100. mkdirp.sync(path.dirname(outputPath));
  101. fs.writeFileSync(outputPath, data, {
  102. encoding: CLIUtils.encoding
  103. });
  104. }
  105. }