CLIUtils.ts 3.0 KB

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