Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only diff json files #1323

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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