ObfuscatedCodeWriter.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import * as path from 'path';
  4. import { TInputCLIOptions } from '../../types/options/TInputCLIOptions';
  5. import { StringSeparator } from '../../enums/StringSeparator';
  6. import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
  7. export class ObfuscatedCodeWriter {
  8. /**
  9. * @type {string}
  10. */
  11. private readonly inputPath: string;
  12. /**
  13. * @type {TInputCLIOptions}
  14. */
  15. private readonly options: TInputCLIOptions;
  16. /**
  17. * @param {string} inputPath
  18. * @param {TInputCLIOptions} options
  19. */
  20. public constructor (
  21. inputPath: string,
  22. options: TInputCLIOptions
  23. ) {
  24. this.inputPath = inputPath;
  25. this.options = options;
  26. }
  27. /**
  28. * @param {string} filePath
  29. * @returns {string}
  30. */
  31. public getOutputCodePath (filePath: string): string {
  32. const normalizedRawOutputPath: string | null = this.options.output
  33. ? path.normalize(this.options.output)
  34. : null;
  35. if (!normalizedRawOutputPath) {
  36. return path
  37. .normalize(filePath)
  38. .split(StringSeparator.Dot)
  39. .map((value: string, index: number) => {
  40. return index === 0 ? `${value}${JavaScriptObfuscatorCLI.obfuscatedFilePrefix}` : value;
  41. })
  42. .join(StringSeparator.Dot);
  43. }
  44. const rawInputPathStats: fs.Stats = fs.lstatSync(this.inputPath);
  45. const outputPathExtName: string = path.extname(normalizedRawOutputPath);
  46. const isDirectoryRawInputPath: boolean = rawInputPathStats.isDirectory();
  47. const isDirectoryRawOutputPath: boolean = !JavaScriptObfuscatorCLI
  48. .availableInputExtensions
  49. .includes(outputPathExtName);
  50. if (isDirectoryRawInputPath) {
  51. if (isDirectoryRawOutputPath) {
  52. return path.join(normalizedRawOutputPath, filePath);
  53. } else {
  54. throw new Error('Output path for directory obfuscation should be a directory path');
  55. }
  56. } else {
  57. if (isDirectoryRawOutputPath) {
  58. return path.join(normalizedRawOutputPath, path.basename(filePath));
  59. } else {
  60. return normalizedRawOutputPath;
  61. }
  62. }
  63. }
  64. /**
  65. * @param {string} outputCodePath
  66. * @param {string} sourceMapFileName
  67. * @returns {string}
  68. */
  69. public getOutputSourceMapPath (outputCodePath: string, sourceMapFileName: string = ''): string {
  70. if (sourceMapFileName) {
  71. outputCodePath = `${outputCodePath.substring(
  72. 0, outputCodePath.lastIndexOf('/')
  73. )}/${sourceMapFileName}`;
  74. }
  75. if (!/\.js\.map$/.test(outputCodePath)) {
  76. outputCodePath = `${outputCodePath.split(StringSeparator.Dot)[0]}.js.map`;
  77. } else if (/\.js$/.test(outputCodePath)) {
  78. outputCodePath += '.map';
  79. }
  80. return outputCodePath;
  81. }
  82. /**
  83. * @param {string} outputPath
  84. * @param {string} data
  85. */
  86. public writeFile (outputPath: string, data: string): void {
  87. mkdirp.sync(path.dirname(outputPath));
  88. fs.writeFileSync(outputPath, data, {
  89. encoding: JavaScriptObfuscatorCLI.encoding
  90. });
  91. }
  92. }