fs-extra.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import path from 'path';
  2. class FsMock {
  3. private static instance: FsMock;
  4. private mockFiles = Object.create(null);
  5. // private constructor() {}
  6. static getInstance(): FsMock {
  7. if (!FsMock.instance) {
  8. FsMock.instance = new FsMock();
  9. }
  10. return FsMock.instance;
  11. }
  12. __createMockFiles = (newMockFiles: Record<string, string>) => {
  13. this.mockFiles = Object.create(null);
  14. // Create folder tree
  15. Object.keys(newMockFiles).forEach((file) => {
  16. const dir = path.dirname(file);
  17. if (!this.mockFiles[dir]) {
  18. this.mockFiles[dir] = [];
  19. }
  20. this.mockFiles[dir].push(path.basename(file));
  21. this.mockFiles[file] = newMockFiles[file];
  22. });
  23. };
  24. __resetAllMocks = () => {
  25. this.mockFiles = Object.create(null);
  26. };
  27. readFileSync = (p: string) => this.mockFiles[p];
  28. existsSync = (p: string) => this.mockFiles[p] !== undefined;
  29. writeFileSync = (p: string, data: string | string[]) => {
  30. this.mockFiles[p] = data;
  31. };
  32. mkdirSync = (p: string) => {
  33. this.mockFiles[p] = Object.create(null);
  34. };
  35. rmSync = (p: string) => {
  36. if (this.mockFiles[p] instanceof Array) {
  37. this.mockFiles[p].forEach((file: string) => {
  38. delete this.mockFiles[path.join(p, file)];
  39. });
  40. }
  41. delete this.mockFiles[p];
  42. };
  43. readdirSync = (p: string) => {
  44. const files: string[] = [];
  45. const depth = p.split('/').length;
  46. Object.keys(this.mockFiles).forEach((file) => {
  47. if (file.startsWith(p)) {
  48. const fileDepth = file.split('/').length;
  49. if (fileDepth === depth + 1) {
  50. files.push(file.split('/').pop() || '');
  51. }
  52. }
  53. });
  54. return files;
  55. };
  56. copyFileSync = (source: string, destination: string) => {
  57. this.mockFiles[destination] = this.mockFiles[source];
  58. };
  59. copySync = (source: string, destination: string) => {
  60. this.mockFiles[destination] = this.mockFiles[source];
  61. if (this.mockFiles[source] instanceof Array) {
  62. this.mockFiles[source].forEach((file: string) => {
  63. this.mockFiles[`${destination}/${file}`] = this.mockFiles[`${source}/${file}`];
  64. });
  65. }
  66. };
  67. createFileSync = (p: string) => {
  68. this.mockFiles[p] = '';
  69. };
  70. unlinkSync = (p: string) => {
  71. if (this.mockFiles[p] instanceof Array) {
  72. this.mockFiles[p].forEach((file: string) => {
  73. delete this.mockFiles[path.join(p, file)];
  74. });
  75. }
  76. delete this.mockFiles[p];
  77. };
  78. getMockFiles = () => this.mockFiles;
  79. promises = {
  80. unlink: async (p: string) => {
  81. if (this.mockFiles[p] instanceof Array) {
  82. this.mockFiles[p].forEach((file: string) => {
  83. delete this.mockFiles[path.join(p, file)];
  84. });
  85. }
  86. delete this.mockFiles[p];
  87. },
  88. writeFile: async (p: string, data: string | string[]) => {
  89. this.mockFiles[p] = data;
  90. },
  91. };
  92. }
  93. export default FsMock.getInstance();