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

Add forceReload to reload all imported modules #23

Merged
merged 7 commits into from
Dec 26, 2024
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ directoryImport('./sample-directory', (moduleName, modulePath, moduleData) => {
| importPattern | RegExp | RegExp pattern to filter files |
| importMode | String | The import mode. Can be 'sync' or 'async' |
| limit | Number | Limit the number of imported modules |
| forceReload | Boolean | If true, reload modules disabling require cache |

[back to top](#top)

Expand Down Expand Up @@ -335,6 +336,11 @@ directoryImport(options, (moduleName, modulePath, moduleData) => {

## Change Log

### [3.3.2] - 2024-12-25

#### Added
- Add forceReload option.

### [3.3.1] - 2024-03-27

#### Fixed
Expand Down
20 changes: 2 additions & 18 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "directory-import",
"version": "3.3.1",
"version": "3.3.2",
"description": "Module will allow you to synchronously or asynchronously import (requires) all modules from the folder you specify",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -10,7 +10,8 @@
"scripts": {
"build": "rimraf ./dist && tsup",
"start": "node dist/index.js",
"publish": "npm run build && npm publish",
"prepare": "npm run build",
"publish": "npm run prepare && npm publish",
"jest": "jest --silent test/index.test.ts",
"jest:watcher": "jest --watchAll",
"jest:windows": "jest --silent test-windows/index.test.ts"
Expand Down Expand Up @@ -59,11 +60,10 @@
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-sonarjs": "^0.23.0",
"eslint-plugin-unicorn": "^49.0.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"prettier": "^3.1.0",
"ts-jest": "^29.1.1",
"tsup": "^8.0.2",
"typescript": "^5.2.2"
}
}
}
5 changes: 5 additions & 0 deletions src/import-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ function importModule(

const relativeModulePath = filePath.slice(options.targetDirectoryPath.length);

if (options.forceReload) {
// eslint-disable-next-line security/detect-non-literal-require, @typescript-eslint/no-var-requires, unicorn/prefer-module
delete require.cache[filePath];
}

// eslint-disable-next-line security/detect-non-literal-require, @typescript-eslint/no-var-requires, unicorn/prefer-module
const importedModule = require(filePath) as unknown;

Expand Down
1 change: 1 addition & 0 deletions src/prepare-private-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getDefaultOptions = (): ImportedModulesPrivateOptions => {
callerFilePath: path.resolve('/'),
callerDirectoryPath: path.resolve('/'),
targetDirectoryPath: path.resolve('/'),
forceReload: false,
};

options.callerFilePath =
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ImportedModulesPublicOptions {
importPattern?: RegExp;
importMode?: ImportModulesMode;
limit?: number;
forceReload?: boolean;
}

export interface ImportedModulesPrivateOptions extends Required<ImportedModulesPublicOptions> {
Expand Down
42 changes: 42 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY,
DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY,
} from './constants';
import fs from 'fs';

test('Import modules from the default (current) directory synchronously', () => {
const result = directoryImport();
Expand Down Expand Up @@ -221,3 +222,44 @@ test('Import modules with specified options and call the provided callback for e
expect(result).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);
expect(callbackResults).toEqual(DEFAULT_EXPECTED_CALLBACK_RESULTS_FROM_SAMPLE_DIRECTORY);
});

test('Import modules with cache', () => {
const result = directoryImport(DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY)

expect(result).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);

// change the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World Changed!' };\n");

// re-import the modules
const result2 = directoryImport({
targetDirectoryPath: DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY,
forceReload: false,
});

expect(result2).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);
// revert the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World!' };\n");
});

test('Import modules without cache', () => {
const result = directoryImport(DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY)

expect(result).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);

// change the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World Changed!' };\n");

jest.resetModules();

// re-import the modules
const result2 = directoryImport({
targetDirectoryPath: DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY,
forceReload: true,
});

expect(result2['/sample-file-2.js']).toEqual({ testData: 'Hello World Changed!' });

// revert the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World!' };\n");
});
Loading