1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import path from 'path';
- const fs: {
- __createMockFiles: typeof createMockFiles;
- readFileSync: typeof readFileSync;
- existsSync: typeof existsSync;
- writeFileSync: typeof writeFileSync;
- mkdirSync: typeof mkdirSync;
- rmSync: typeof rmSync;
- readdirSync: typeof readdirSync;
- copyFileSync: typeof copyFileSync;
- copySync: typeof copyFileSync;
- } = jest.genMockFromModule('fs-extra');
- let mockFiles = Object.create(null);
- const createMockFiles = (newMockFiles: Record<string, string>) => {
- mockFiles = Object.create(null);
- // Create folder tree
- for (const file in newMockFiles) {
- const dir = path.dirname(file);
- if (!mockFiles[dir]) {
- mockFiles[dir] = [];
- }
- mockFiles[dir].push(path.basename(file));
- mockFiles[file] = newMockFiles[file];
- }
- };
- const readFileSync = (p: string) => {
- return mockFiles[p];
- };
- const existsSync = (p: string) => {
- return mockFiles[p] !== undefined;
- };
- const writeFileSync = (p: string, data: any) => {
- mockFiles[p] = data;
- };
- const mkdirSync = (p: string) => {
- mockFiles[p] = Object.create(null);
- };
- const rmSync = (p: string, options: { recursive: boolean }) => {
- if (options.recursive) {
- delete mockFiles[p];
- } else {
- delete mockFiles[p][Object.keys(mockFiles[p])[0]];
- }
- };
- const readdirSync = (p: string) => {
- const files: string[] = [];
- const depth = p.split('/').length;
- Object.keys(mockFiles).forEach((file) => {
- if (file.startsWith(p)) {
- const fileDepth = file.split('/').length;
- if (fileDepth === depth + 1) {
- files.push(file.split('/').pop() || '');
- }
- }
- });
- return files;
- };
- const copyFileSync = (source: string, destination: string) => {
- mockFiles[destination] = mockFiles[source];
- };
- const copySync = (source: string, destination: string) => {
- mockFiles[destination] = mockFiles[source];
- if (mockFiles[source] instanceof Array) {
- mockFiles[source].forEach((file: string) => {
- mockFiles[destination + '/' + file] = mockFiles[source + '/' + file];
- });
- }
- };
- fs.readdirSync = readdirSync;
- fs.existsSync = existsSync;
- fs.readFileSync = readFileSync;
- fs.writeFileSync = writeFileSync;
- fs.mkdirSync = mkdirSync;
- fs.rmSync = rmSync;
- fs.copyFileSync = copyFileSync;
- fs.copySync = copySync;
- fs.__createMockFiles = createMockFiles;
- module.exports = fs;
|