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

Disable require cache while loading other files than JSON #142

Merged
merged 1 commit into from Jan 5, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/package.json
Expand Up @@ -53,6 +53,7 @@
"bson": "^4.2.2",
"debug": "^4.3.1",
"extend": "^3.0.0",
"import-fresh": "^3.3.0",
"mongodb": "3.6.3"
},
"devDependencies": {
Expand Down
20 changes: 11 additions & 9 deletions core/src/populator/filesystem.ts
@@ -1,6 +1,7 @@
import { readdirSync, lstatSync, readFileSync } from 'fs';
import { EJSON } from 'bson';
import { extname } from 'path';
const importFresh = require('import-fresh');

/**
* Provides functionality for manipulating files and directories.
Expand All @@ -24,7 +25,7 @@ export class FileSystem {
*/
listValidDirectories(path: string) {
const filesAndDirectories = this.listFileNames(path);
const directories = filesAndDirectories.filter(fileOrDirectory => {
const directories = filesAndDirectories.filter((fileOrDirectory) => {
const itemPath = `${path}/${fileOrDirectory}`;
return (
this.isDirectory(itemPath) &&
Expand Down Expand Up @@ -73,7 +74,7 @@ export class FileSystem {
fileNames: string[],
supportedExtensions: string[],
) {
const documentFileNames = fileNames.filter(fileName => {
const documentFileNames = fileNames.filter((fileName) => {
const fileNameSplit = fileName.split(
FileSystem.FILE_NAME_SPLIT_CHARACTER,
);
Expand All @@ -83,7 +84,7 @@ export class FileSystem {

const fileExtension = fileNameSplit.pop()!;
return supportedExtensions.some(
extension => extension.toLowerCase() === fileExtension.toLowerCase(),
(extension) => extension.toLowerCase() === fileExtension.toLowerCase(),
);
});

Expand Down Expand Up @@ -120,14 +121,15 @@ export class FileSystem {
*/
readFile(path: string): any {
const fileExtension = extname(path);
if (fileExtension === '.json') {
const content = readFileSync(path, 'utf-8');
return EJSON.parse(content, {
relaxed: true,
});

if (fileExtension !== '.json') {
return importFresh(path);
}

return require(path);
const content = readFileSync(path, 'utf-8');
return EJSON.parse(content, {
relaxed: true,
});
}
}

Expand Down
6 changes: 2 additions & 4 deletions core/test/unit/FileSystem.ts
Expand Up @@ -45,14 +45,12 @@ jest.mock('fs', () => ({
}),
}));

jest.mock(
'mockFiles/test2.js',
() => ({
jest.mock('import-fresh', () =>
jest.fn().mockReturnValue({
value: {
second: true,
},
}),
{ virtual: true },
);

describe('FileSystem', () => {
Expand Down