|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const [REGEXP_JS, READDIR_OPTS] = [/^\.[cme]?js$/, { withFileTypes: true }]; |
4 | | -const [Script, { checkAccess }] = [require('./script'), require('./utils')]; |
5 | 3 | const { basename, extname, join, dirname } = require('node:path'); |
6 | 4 | const { readFile, readdir, stat } = require('node:fs').promises; |
| 5 | +const { prepare, execute } = require('./script'); |
| 6 | +const { checkAccess } = require('./utils'); |
| 7 | +const READ_OPTS = { withFileTypes: true }; |
| 8 | +const JS_EXT = /^\.[cme]?js$/; |
| 9 | +module.exports = from; |
7 | 10 |
|
8 | | -const from = async (src, options = {}) => { |
| 11 | +async function from(src, options = {}) { |
9 | 12 | if (src !== '.' && src !== '..' && basename(src) === src) { |
10 | 13 | const { prepare, ...estimated } = options; |
11 | | - if (prepare) return Script.prepare(src, estimated); |
12 | | - return new Script(src, estimated); |
| 14 | + if (prepare) return prepare(src, estimated); |
| 15 | + return execute(src, estimated); |
13 | 16 | } |
14 | 17 | const reader = (await stat(src)).isDirectory() ? from.dir : from.file; |
15 | | - const result = await reader(src, options); |
16 | | - return result; |
17 | | -}; |
| 18 | + return reader(src, options); |
| 19 | +} |
18 | 20 |
|
19 | 21 | from.file = async (path, options = {}) => { |
20 | 22 | const src = await readFile(path, 'utf8'); |
21 | 23 | if (!src) throw new SyntaxError(`File ${path} is empty`); |
22 | | - const [filename, dir] = [basename(path), dirname(path)]; |
23 | | - const runner = options.prepare ? Script.prepare : Script.execute; |
| 24 | + const { 0: filename, 1: dir } = [basename(path), dirname(path)]; |
| 25 | + const runner = options.prepare ? prepare : execute; |
24 | 26 | return runner(src, { ...options, filename, dir, prepare: undefined }); |
25 | 27 | }; |
26 | 28 |
|
27 | 29 | from.dir = (src, options = {}) => { |
28 | | - const { depth: max = true, ...estimated } = options; |
29 | | - const isAllowed = checkAccess.bind(null, 'reader', estimated.access); |
30 | | - const pull = async (src, depth) => { |
31 | | - const dontGoDeep = typeof max === 'number' && depth + 1 > max; |
32 | | - const files = await readdir(src, READDIR_OPTS); |
| 30 | + const { depth: max = true, access } = options; |
| 31 | + const isAllowed = checkAccess.bind(null, 'reader', access); |
| 32 | + const pull = async (src, depth = 1) => { |
| 33 | + const futher = typeof max === 'number' && depth + 1 > max; |
| 34 | + const files = await readdir(src, READ_OPTS); |
33 | 35 | const scripts = {}; |
34 | 36 | // prettier-ignore |
35 | 37 | await Promise.all(files.map(async file => { |
36 | | - const [path, isDir, ext] = [join(src, file.name), file.isDirectory(), extname(file.name)]; |
37 | | - if (!isAllowed(path) || (isDir && dontGoDeep)) return; |
| 38 | + const isDir = file.isDirectory(); |
| 39 | + const path = join(src, file.name); |
| 40 | + if (!isAllowed(path) || (isDir && futher)) return; |
38 | 41 | const promise = isDir ? pull(path, depth + 1) : from.file(path, options); |
39 | | - const name = basename(file.name, ext.match(REGEXP_JS) ? ext : ''); |
| 42 | + const ext = extname(file.name); |
| 43 | + const name = basename(file.name, ext.match(JS_EXT) ? ext : ''); |
40 | 44 | scripts[name] = await promise; |
41 | 45 | })); |
42 | 46 | return Object.assign({}, scripts); |
43 | 47 | }; |
44 | | - return pull(src, 1); |
| 48 | + return pull(src); |
45 | 49 | }; |
46 | | - |
47 | | -module.exports = from; |
|
0 commit comments