IdentifierNamesCacheUtils.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
  4. import { IFileData } from '../../interfaces/cli/IFileData';
  5. import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
  6. /**
  7. * Utils to work with identifier names cache file
  8. */
  9. export class IdentifierNamesCacheUtils {
  10. /**
  11. * @type {string}
  12. */
  13. private static readonly identifierNamesCacheExtension: string = '.json';
  14. /**
  15. * @type {string}
  16. */
  17. private readonly identifierNamesCachePath: string | undefined;
  18. /**
  19. * @param {string} identifierNamesCachePath
  20. */
  21. public constructor (identifierNamesCachePath: string | undefined) {
  22. this.identifierNamesCachePath = identifierNamesCachePath;
  23. }
  24. /**
  25. * @param {string} filePath
  26. * @returns {boolean}
  27. */
  28. private static isValidFilePath (filePath: string): boolean {
  29. try {
  30. return fs.statSync(filePath).isFile()
  31. && path.extname(filePath) === IdentifierNamesCacheUtils.identifierNamesCacheExtension;
  32. } catch {
  33. return false;
  34. }
  35. }
  36. /**
  37. * @param {string} filePath
  38. * @returns {IFileData}
  39. */
  40. private static readFile (filePath: string): IFileData {
  41. return {
  42. filePath: path.normalize(filePath),
  43. content: fs.readFileSync(filePath, JavaScriptObfuscatorCLI.encoding)
  44. };
  45. }
  46. /**
  47. * @returns {TIdentifierNamesCache | null}
  48. */
  49. public read (): TIdentifierNamesCache | null {
  50. if (!this.identifierNamesCachePath) {
  51. return null;
  52. }
  53. if (!IdentifierNamesCacheUtils.isValidFilePath(this.identifierNamesCachePath)) {
  54. throw new ReferenceError(`Given identifier names cache path must be a valid ${
  55. IdentifierNamesCacheUtils.identifierNamesCacheExtension
  56. } file path`);
  57. }
  58. const fileData: IFileData = IdentifierNamesCacheUtils.readFile(this.identifierNamesCachePath);
  59. if (!fileData.content) {
  60. // Initial state of identifier names cache file
  61. return {};
  62. }
  63. try {
  64. // Already written identifier names cache file
  65. return JSON.parse(fileData.content);
  66. } catch {
  67. throw new ReferenceError('Identifier names cache file must contains a json dictionary with identifier names');
  68. }
  69. }
  70. /**
  71. * @param {TIdentifierNamesCache} identifierNamesCache
  72. */
  73. public write (identifierNamesCache: TIdentifierNamesCache): void {
  74. if (!this.identifierNamesCachePath) {
  75. return;
  76. }
  77. const identifierNamesCacheJson: string = JSON.stringify(identifierNamesCache);
  78. fs.writeFileSync(this.identifierNamesCachePath, identifierNamesCacheJson, {
  79. encoding: JavaScriptObfuscatorCLI.encoding
  80. });
  81. }
  82. }