Skip to content

Commit

Permalink
fix: only diff json files
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar committed Sep 13, 2023
1 parent c2a59ff commit 0522225
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,21 @@ describe('#recursiveDirectoryDiff', () => {
);
}
);

fancyIt()('should ignore non json files', () => {
mockedReadDir.mockReturnValueOnce([]);
mockedReadDir.mockReturnValueOnce([
getFile('someFile.txt'),
getFile('someFile.json'),
]);

recursiveDirectoryDiff('currentDir', 'nextDir', false);

expect(mockedReadJson).not.toHaveBeenCalledWith(
join('nextDir', 'someFile.txt')
);
expect(mockedReadJson).toHaveBeenCalledWith(
join('nextDir', 'someFile.json')
);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {ResourceSnapshotType} from '@coveo/platform-client';
import {readdirSync, rmSync} from 'fs';
import {Dirent, readdirSync, rmSync} from 'fs';
import {readJsonSync, writeJsonSync, JsonWriteOptions} from 'fs-extra';
import {join} from 'path';
import {join, extname} from 'path';
import {Project} from '../../project/project';

type ResourcesJSON = Object & {resourceName: string};
Expand All @@ -19,8 +19,8 @@ export function recursiveDirectoryDiff(
nextDir: string,
deleteMissingResources: boolean
) {
const currentFilePaths = getAllFilesPath(currentDir);
const nextFilePaths = getAllFilesPath(nextDir);
const currentFilePaths = getAllJsonFilesPath(currentDir);
const nextFilePaths = getAllJsonFilesPath(nextDir);

nextFilePaths.forEach((filePath) => {
const nextFileJson = readJsonSync(join(nextDir, filePath));
Expand All @@ -44,21 +44,28 @@ export function recursiveDirectoryDiff(
}
}

function getAllFilesPath(
function getAllJsonFilesPath(
currentDir: string,
filePaths: Set<string> = new Set<string>()
) {
const files = readdirSync(currentDir, {withFileTypes: true});
files.forEach((file) => {
if (file.isDirectory()) {
getAllFilesPath(join(currentDir, file.name), filePaths);
getAllJsonFilesPath(join(currentDir, file.name), filePaths);
} else {
filePaths.add(join(currentDir, file.name).replace(firstDirOfPath, ''));
if (isJsonFile(file)) {
filePaths.add(join(currentDir, file.name).replace(firstDirOfPath, ''));
}
}
});
return filePaths;
}

function isJsonFile(file: Dirent) {
const fileExtension = extname(file.name);
return fileExtension.toLowerCase() === '.json';
}

function buildDiffedJson(
currentFile: SnapshotFileJSON,
nextFile: SnapshotFileJSON,
Expand Down

0 comments on commit 0522225

Please sign in to comment.