This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+54
View File
@@ -0,0 +1,54 @@
'use strict';
const fs = require('fs');
const path = require('path');
// Prevent caching of this module so module.parent is always accurate.
delete require.cache[__filename];
const parentFile = module.parent && module.parent.filename;
const parentDirectory = path.dirname(parentFile || '.');
// The default file extensions used by `require()`.
const fileExtensions = new Set(['.js', '.json', '.node']);
module.exports = (directory, options) => {
directory = path.resolve(parentDirectory, directory || '');
options = {
camelize: true,
fileExtensions,
...options
};
let files;
try {
files = fs.readdirSync(directory);
} catch (_) {
return {};
}
const done = new Set();
const returnValue = {};
for (const fileExtension of options.fileExtensions) {
for (const file of files) {
const filenameStem = path.basename(file).replace(/\.\w+$/, '');
const fullPath = path.join(directory, file);
if (done.has(filenameStem) ||
fullPath === parentFile ||
path.extname(file) !== fileExtension ||
filenameStem[0] === '_' ||
filenameStem[0] === '.') {
continue;
}
const exportKey = options.camelize ? filenameStem.replace(/-(\w)/g, (m, p1) => p1.toUpperCase()) : filenameStem;
returnValue[exportKey] = require(fullPath);
done.add(filenameStem);
}
}
return returnValue;
};
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+74
View File
@@ -0,0 +1,74 @@
{
"_args": [
[
"import-modules@2.0.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_development": true,
"_from": "import-modules@2.0.0",
"_id": "import-modules@2.0.0",
"_inBundle": false,
"_integrity": "sha512-iczM/v9drffdNnABOKwj0f9G3cFDon99VcG1mxeBsdqnbd+vnQ5c2uAiCHNQITqFTOPaEvwg3VjoWCur0uHLEw==",
"_location": "/import-modules",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "import-modules@2.0.0",
"name": "import-modules",
"escapedName": "import-modules",
"rawSpec": "2.0.0",
"saveSpec": null,
"fetchSpec": "2.0.0"
},
"_requiredBy": [
"/eslint-plugin-unicorn"
],
"_resolved": "https://registry.npmjs.org/import-modules/-/import-modules-2.0.0.tgz",
"_spec": "2.0.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/import-files/issues"
},
"description": "Import all modules in a directory",
"devDependencies": {
"ava": "^2.4.0",
"xo": "^0.25.3"
},
"engines": {
"node": ">=8"
},
"files": [
"index.js"
],
"homepage": "https://github.com/sindresorhus/import-files#readme",
"keywords": [
"import",
"require",
"files",
"modules",
"all",
"directory",
"folder",
"js",
"paths",
"multiple",
"index"
],
"license": "MIT",
"name": "import-modules",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/import-files.git"
},
"scripts": {
"test": "xo && ava"
},
"version": "2.0.0"
}
+68
View File
@@ -0,0 +1,68 @@
# import-modules [![Build Status](https://travis-ci.org/sindresorhus/import-modules.svg?branch=master)](https://travis-ci.org/sindresorhus/import-modules)
> Import all modules in a directory
*This module is intentionally simple. Not interested in more features.*
## Install
```
$ npm install import-modules
```
## Usage
```
.
└── dir
├── foo-bar.js
└── baz-faz.js
```
```js
const importModules = require('import-modules');
const modules = importModules('directory');
console.log(modules);
//=> {fooBar: [Function], bazFaz: [Function]}
```
## API
### importModules(directory?, options?)
#### directory
Type: `string`<br>
Default: `__dirname`
Directory to import modules from. Unless you've set the `fileExtensions` option, that means any `.js`, `.json`, `.node` files, in that order. Does not recurse. Ignores the caller file and files starting with `.` or `_`.
#### options
Type: `object`
##### camelize
Type: `boolean`<br>
Default: `true`
Convert dash-style names (`foo-bar`) to camel-case (`fooBar`).
##### fileExtensions
Type: `string[]`<br>
Default: `['.js', '.json', '.node']`
File extensions to look for. Order matters.
## Related
- [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
- [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
- [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily