{"id":26531796,"url":"https://github.com/aniname/directory-import","last_synced_at":"2025-04-09T22:17:41.363Z","repository":{"id":46523135,"uuid":"199633093","full_name":"ANIname/directory-import","owner":"ANIname","description":"Module for automatic import of files from a directory and subdirectories (sync and async). You can use imported modules either from the returned object or in the callback function","archived":false,"fork":false,"pushed_at":"2024-12-26T09:40:55.000Z","size":22853,"stargazers_count":21,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T22:17:34.717Z","etag":null,"topics":["directory","import","module","modules","recursive","require","subdirectories"],"latest_commit_sha":null,"homepage":"https://npmjs.com/directory-import","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ANIname.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-30T10:50:30.000Z","updated_at":"2025-01-06T10:32:24.000Z","dependencies_parsed_at":"2024-12-26T10:33:19.509Z","dependency_job_id":null,"html_url":"https://github.com/ANIname/directory-import","commit_stats":{"total_commits":41,"total_committers":2,"mean_commits":20.5,"dds":"0.024390243902439046","last_synced_commit":"51230379b83d3a5ed289e78a015a8ab48f354576"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANIname%2Fdirectory-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANIname%2Fdirectory-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANIname%2Fdirectory-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ANIname%2Fdirectory-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ANIname","download_url":"https://codeload.github.com/ANIname/directory-import/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119286,"owners_count":21050755,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directory","import","module","modules","recursive","require","subdirectories"],"created_at":"2025-03-21T18:30:08.918Z","updated_at":"2025-04-09T22:17:41.340Z","avatar_url":"https://github.com/ANIname.png","language":"TypeScript","readme":"\u003c!--suppress HtmlDeprecatedAttribute --\u003e\n\u003cdiv align=\"center\"\u003e\n  \u003cp\u003e\n    \u003cimg src=\"https://img.shields.io/node/v/directory-import?logo=nodedotjs\u0026label=Node.JS version\" alt=\"Node version required\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/package-json/v/KiiDii/directory-import?logo=vitess\u0026logoColor=white\u0026label=Package%20version\" alt=\"Package version\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/languages/code-size/KiiDii/directory-import?logo=typescript\u0026label=Builded code size\" alt=\"GitHub code size\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/license/KiiDii/directory-import?logo=mdbook\u0026label=License\" alt=\"License\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/actions/workflow/status/KiiDii/directory-import/test.yml?logo=jest\u0026label=Tests\" alt=\"Github tests\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/issues/KiiDii/directory-import?logo=github\u0026label=Issues\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/dt/directory-import?logo=npm\u0026label=Downloads\" alt=\"Downloads\"\u003e\n    \u003ca href=\"https://discord.gg/ADFYZtJ\"\u003e\n      \u003cimg src=\"https://img.shields.io/discord/219557939466338304?logo=discord\u0026label=Discord%20chat\" alt=\"Discord server\"\u003e\n    \u003c/a\u003e\n  \u003c/p\u003e\n  \u003cp\u003e\n    \u003ca href=\"https://nodei.co/npm/directory-import\"\u003e\n      \u003cimg src=\"https://nodei.co/npm/directory-import.png?compact=true\" alt=\"Install directory-import from npm\"\u003e\n    \u003c/a\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\n## About\n\nA module for the automatic import of files from a directory and its subdirectories (sync and async).\nThe imported modules can be used either from the returned object or in the callback function.\n\n---\n\n## Installation\n\n```bash\nnpm install directory-import\n```\n\nAfter installation, you can use the module in your project:\n\n```js\nconst { directoryImport } = require('directory-import');\n\nconst importedModules = directoryImport('./path/to/directory');\n\n// Outputs an object with imported modules\n// For example: { modulePath1: module1, modulePath2: module2, ... }\nconsole.log(importedModules);\n```\n\nor:\n\n```ts\nimport { directoryImport } from 'directory-import';\n\nconst importedModules = directoryImport('./path/to/directory');\n\n// Outputs an object with imported modules\n// For example: { modulePath1: module1, modulePath2: module2, ... }\nconsole.log(importedModules);\n```\n\n---\n\n## Simple usage\n\nHere's a simple example of how to use the library and how it works under the hood:\n\n```javascript\nconst { directoryImport } = require('directory-import');\n\nconst importedModules = directoryImport('./sample-directory');\n\nconsole.info(importedModules);\n```\n\n\u003ca href=\"https://github.com/KiiDii/directory-import/blob/master/media/directory-import-example.gif\"\u003e\n  \u003cimg src=\"https://github.com/KiiDii/directory-import/blob/master/media/directory-import-example.gif?raw=true\" alt=\"GIF demonstrating how it works\"\u003e\n\u003c/a\u003e\n\n### [Path to directory from the GIF above][pathToDirectoryFromGif]\n\n### Invoking a callback on each file\n\nThis can be handy when, for instance, you need to perform a specific action based on the imported file.\n\n```javascript\nconst { directoryImport } = require('directory-import');\n\ndirectoryImport('./sample-directory', (moduleName, modulePath, moduleData) =\u003e {\n  console.info({ moduleName, modulePath, moduleData });\n});\n```\n\n\u003ca href=\"https://github.com/KiiDii/directory-import/blob/master/media/directory-import-example-with-callback.gif\"\u003e\n  \u003cimg src=\"https://github.com/KiiDii/directory-import/blob/master/media/directory-import-example-with-callback.gif?raw=true\" alt=\"GIF demonstrating the callback feature\"\u003e\n\u003c/a\u003e\n\n### {Function} Callback properties:\n\n| Property | Type   | Description                                                    |\n| -------- | ------ | -------------------------------------------------------------- |\n| name     | String | Module name based on the filename                              |\n| path     | String | Relative module path                                           |\n| data     | String | Exported data from the module. (Ex: \"module.exports = 'test'\") |\n| index    | Number | Index of the imported module                                   |\n\n### {Object} Options properties:\n\n| Property              | Type    | Description                                                     |\n| --------------------- | ------- | --------------------------------------------------------------- |\n| includeSubdirectories | Boolean | If true, the module will import files from subdirectories       |\n| targetDirectoryPath   | String  | The path to the directory from which modules are to be imported |\n| importPattern         | RegExp  | RegExp pattern to filter files                                  |\n| importMode            | String  | The import mode. Can be 'sync' or 'async'                       |\n| limit                 | Number  | Limit the number of imported modules                            |\n| forceReload           | Boolean | If true, reload modules disabling require cache                 |\n\n[back to top](#top)\n\n---\n\n### More examples:\n\nMinimum code needed for use:\n\n```js\nconst { directoryImport } = require('directory-import');\n\n// Synchronously imports all modules in the same directory from which the code was called\ndirectoryImport();\n```\n\nAsynchronously import files from the specified directory:\n\n```js\nconst { directoryImport } = require('directory-import');\n\nconst result = directoryImport('./path/to/directory', 'async');\n\n// Promise { \u003cpending\u003e }\nconsole.log(result);\n```\n\nPut the result in a variable and invoke a callback on each file:\n\n```js\nconst { directoryImport } = require('directory-import');\n\nconst importedModules = directoryImport('./path/to/directory', (moduleName, modulePath, moduleData) =\u003e {\n  // {\n  //   moduleName: 'sample-file-1',\n  //   modulePath: '/sample-file-1.js',\n  //   moduleData: 'This is first sampleFile'\n  // }\n  // ...\n  console.info({ moduleName, modulePath, moduleData });\n});\n\n// {\n//   '/sample-file-1.js': 'This is first sampleFile',\n//   ...\n// }\nconsole.info(importedModules);\n```\n\n[back to top](#top)\n\n---\n\n### Overloads:\n\n```js\nconst { directoryImport } = require('directory-import');\n\n/**\n * Import modules from the current directory synchronously\n * @returns {Object} An object containing all imported modules.\n */\nconst importedModules = directoryImport();\n\n// {\n//   '/sample-file-1.js': 'This is first sampleFile',\n//   ...\n// }\nconsole.log(importedModules);\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\n/**\n * Import modules from the current directory synchronously and call the provided callback for each imported module.\n * @param {Function} callback - The callback function to call for each imported module.\n * @returns {Object} An object containing all imported modules.\n */\ndirectoryImport((moduleName, modulePath, moduleData) =\u003e {\n  // {\n  //   moduleName: 'sample-file-1',\n  //   modulePath: '/sample-file-1.js',\n  //   moduleData: 'This is first sampleFile'\n  // }\n  // ...\n  console.info({ moduleName, modulePath, moduleData });\n});\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\n/**\n * Import modules from the specified directory synchronously\n * @param {String} directoryPath - The path to the directory from which you want to import modules.\n * @returns {Object} An object containing all imported modules.\n */\nconst importedModules = directoryImport('./path/to/directory');\n\n// {\n//   '/sample-file-1.js': 'This is first sampleFile',\n//   ...\n// }\nconsole.log(importedModules);\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\n/**\n * Import modules from the specified directory synchronously and call the provided callback for each imported module.\n * @param {String} directoryPath - The path to the directory from which you want to import modules.\n * @param {Function} callback - The callback function to call for each imported module.\n * @returns {Object} An object containing all imported modules.\n */\ndirectoryImport('./path/to/directory', (moduleName, modulePath, moduleData) =\u003e {\n  // {\n  //   moduleName: 'sample-file-1',\n  //   modulePath: '/sample-file-1.js',\n  //   moduleData: 'This is first sampleFile'\n  // }\n  // ...\n  console.info({ moduleName, modulePath, moduleData });\n});\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\n/**\n * Import all modules from the specified directory synchronously or asynchronously.\n * @param {string} targetDirectoryPath - The path to the directory to import modules from.\n * @param {'sync'|'async'} mode - The import mode. Can be 'sync' or 'async'.\n * @returns {Object} An object containing all imported modules.\n */\nconst importModules = directoryImport('./path/to/directory', 'sync');\n\n// {\n//   '/sample-file-1.js': 'This is first sampleFile',\n//   ...\n// }\nconsole.log(importedModules);\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\n/**\n * Import all modules from the specified directory synchronously or asynchronously and call the provided callback for each imported module.\n * @param {string} targetDirectoryPath - The path to the directory to import modules from.\n * @param {'sync'|'async'} mode - The import mode. Can be 'sync' or 'async'.\n * @param {Function} callback - The callback function to call for each imported module.\n * @returns {Object} An object containing all imported modules.\n */\ndirectoryImport('./path/to/directory', 'sync', (moduleName, modulePath, moduleData) =\u003e {\n  // {\n  //   moduleName: 'sample-file-1',\n  //   modulePath: '/sample-file-1.js',\n  //   moduleData: 'This is first sampleFile'\n  // }\n  // ...\n  console.info({ moduleName, modulePath, moduleData });\n});\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\nconst options = {\n  includeSubdirectories: true,\n  targetDirectoryPath: './path/to/directory',\n  importPattern: /\\.js/,\n  importMode: 'sync',\n  limit: 2,\n};\n\n/**\n * Import all modules from the specified directory\n * @param {Object} targetDirectoryPath - options - The options object.\n * @returns {Object} An object containing all imported modules.\n */\nconst importModules = directoryImport(options);\n\n// {\n//   '/sample-file-1.js': 'This is first sampleFile',\n//   ...\n// }\nconsole.log(importedModules);\n```\n\n```js\nconst { directoryImport } = require('directory-import');\n\nconst options = {\n  includeSubdirectories: true,\n  targetDirectoryPath: './path/to/directory',\n  importPattern: /\\.js/,\n  importMode: 'sync',\n  limit: 2,\n};\n\n/**\n * Import all modules from the specified directory and call the provided callback for each imported module.\n * @param {Object} targetDirectoryPath - options - The options object.\n * @param {Function} callback - The callback function to call for each imported module.\n * @returns {Object} An object containing all imported modules.\n */\ndirectoryImport(options, (moduleName, modulePath, moduleData) =\u003e {\n  // {\n  //   moduleName: 'sample-file-1',\n  //   modulePath: '/sample-file-1.js',\n  //   moduleData: 'This is first sampleFile'\n  // }\n  // ...\n  console.info({ moduleName, modulePath, moduleData });\n});\n```\n\n[back to top](#top)\n\n---\n\n## Change Log\n\n### [3.3.2] - 2024-12-25\n\n#### Added\n- Add forceReload option.\n\n### [3.3.1] - 2024-03-27\n\n#### Fixed\n- Now module can work with windows operating system!\n- Enhance path handling and regular expressions for robustness\n\n### [3.2.1] - 2024-01-25\n\n#### Fixed\n- Fixed a problem where the callerFilePath option was not working as expected for node.js versions greater than 20.\n\n### [3.1.1] - 2023-11-17\n\n#### Added\n- .mjs files are supported now.\n\n#### Fixed\n- options.callerFilePath now returns the correct path to the file from which the function was called.\n- Declaration files are no longer imported.\n- Other minor fixes.\n\n### [3.0.0] - 2023-07-29\n\n#### Added\n\n- Added tests to increase code reliability and stability.\n- Implemented function overloads for enhanced flexibility and usage\n- Backward compatibility with older versions has been ensured to ease user migration.\n\n#### Changed\n\n- The entire project code has been rewritten in TypeScript for readabilit, reliability, and ease of maintenance.\n\n#### Fixed\n\n- All known bugs identified in the previous version have been fixed.\n\n#### Removed\n\n- All third-party dependencies have been removed to reduce the package size and increase performance.\n\n---\n\n## Contribution\n\nContributions to `directory-import` are always welcome. Here is how you can contribute to the project:\n\n1. **Report issues** - Report about a bug or suggest a new feature [here][gitIssues].\n\n2. **Submit Pull Requests** - If you fixed a bug or developed a new feature, you can submit a PR. Please follow these guidelines while preparing your code:\n\n   - Ensure that your code properly complies with the standard TS conventions.\n   - Make sure to add comments in your code.\n   - Add a description explaining what changes you have made in your PR.\n\nBefore making a PR, please make sure your changes are consistent with the project's coding style and all tests are passing.\n\n3. **Improve the Documentation** - You can enhance the README or the wiki page by adding more explanations, fixing typos, adding examples, etc.\n\nPlease note that your contributions should follow the guidelines described in the [Code of Conduct][CODE_OF_CONDUCT].\n\nThank you for your interest in contributing to the `directory-import`!\n\n[back to top](#top)\n\n---\n\n### Help\n\n- If you have any questions or need help, feel free to join our [Discord server][discordServer].\n- If you find a bug, or you have any suggestions? please create an issue on [GitHub issues][gitIssues].\n- If you want to help with the development of the project, you can create a pull request on [GitHub pull requests][gitPullRequests].\n- If you like the project, you can put a star on [GitHub][gitProject].\n- For more details, please refer to the [Code of Conduct][CODE_OF_CONDUCT].\n\n[back to top](#top)\n\n[pathToDirectoryFromGif]: https://github.com/KiiDii/directory-import/tree/master/sample-directory\n[discordServer]: https://discord.gg/ADFYZtJ\n[gitProject]: https://github.com/ANIname/directory-import\n[gitIssues]: https://github.com/ANIname/directory-import/issues\n[gitPullRequests]: https://github.com/ANIname/directory-import/pulls\n[CODE_OF_CONDUCT]: https://github.com/ANIname/directory-import/blob/master/CODE_OF_CONDUCT.md\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faniname%2Fdirectory-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faniname%2Fdirectory-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faniname%2Fdirectory-import/lists"}