@efebia/fastify-auto-import 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{globber.d.ts → cjs/globber.d.ts} +0 -0
- package/lib/{globber.js → cjs/globber.js} +8 -3
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.js +18 -0
- package/lib/{serial.d.ts → cjs/serial.d.ts} +0 -0
- package/lib/{serial.js → cjs/serial.js} +0 -0
- package/lib/esm/globber.d.ts +11 -0
- package/lib/esm/globber.js +40 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.js +13 -0
- package/lib/esm/serial.d.ts +1 -0
- package/lib/esm/serial.js +4 -0
- package/package.json +14 -4
- package/lib/index.d.ts +0 -3
- package/lib/index.js +0 -16
|
File without changes
|
|
@@ -31,11 +31,15 @@ const promises_1 = require("node:fs/promises");
|
|
|
31
31
|
const node_path_1 = __importDefault(require("node:path"));
|
|
32
32
|
const node_url_1 = __importStar(require("node:url"));
|
|
33
33
|
const pkg_up_1 = __importDefault(require("pkg-up"));
|
|
34
|
-
const
|
|
34
|
+
const serial_js_1 = require("./serial.js");
|
|
35
35
|
const getPackageType = async (directory) => {
|
|
36
36
|
const nearestPackage = await (0, pkg_up_1.default)({ cwd: directory });
|
|
37
37
|
if (nearestPackage) {
|
|
38
|
-
|
|
38
|
+
if ('require' in global) {
|
|
39
|
+
return require(nearestPackage).type;
|
|
40
|
+
}
|
|
41
|
+
const file = await (0, promises_1.readFile)(nearestPackage, { encoding: 'utf-8' });
|
|
42
|
+
return JSON.parse(file).type;
|
|
39
43
|
}
|
|
40
44
|
};
|
|
41
45
|
const globFiles = async (opts) => {
|
|
@@ -44,7 +48,7 @@ const globFiles = async (opts) => {
|
|
|
44
48
|
const pluginsDirectory = node_path_1.default.join(startingDirectory, opts.directory);
|
|
45
49
|
const routesDirectories = await (0, promises_1.readdir)(pluginsDirectory);
|
|
46
50
|
const filteredDirectories = routesDirectories.filter(dir => !opts.excludedDirectories.includes(dir));
|
|
47
|
-
const importedFiles = await (0,
|
|
51
|
+
const importedFiles = await (0, serial_js_1.serial)(filteredDirectories.map((dir) => async () => {
|
|
48
52
|
var _a;
|
|
49
53
|
var _b;
|
|
50
54
|
const osPath = node_path_1.default.join(pluginsDirectory, dir, packageType === 'module' ? `${opts.routeFile.replace(".js", "")}.js` : opts.routeFile.replace(".js", ""));
|
|
@@ -57,6 +61,7 @@ const globFiles = async (opts) => {
|
|
|
57
61
|
return require(osPath);
|
|
58
62
|
}
|
|
59
63
|
catch (e) {
|
|
64
|
+
console.log(e);
|
|
60
65
|
(_b = opts.log) === null || _b === void 0 ? void 0 : _b.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
|
|
61
66
|
return null;
|
|
62
67
|
}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
7
|
+
const globber_js_1 = require("./globber.js");
|
|
8
|
+
const plugin = (0, fastify_plugin_1.default)(async (fastify, opts) => {
|
|
9
|
+
var _a, _b, _c;
|
|
10
|
+
const globbedFiles = await (0, globber_js_1.globFiles)(Object.assign(Object.assign({}, opts), { routeFile: (_a = opts.routeFile) !== null && _a !== void 0 ? _a : 'routes', directory: (_b = opts.directory) !== null && _b !== void 0 ? _b : 'src/plugins', log: opts.log || fastify.log, excludedDirectories: (_c = opts.excludedDirectories) !== null && _c !== void 0 ? _c : [] }));
|
|
11
|
+
await Promise.all(globbedFiles.map(async (globbedFile) => {
|
|
12
|
+
await fastify.register(globbedFile);
|
|
13
|
+
}));
|
|
14
|
+
}, {
|
|
15
|
+
name: '@efebia/fastify-auto-import',
|
|
16
|
+
fastify: '4.x'
|
|
17
|
+
});
|
|
18
|
+
exports.default = plugin;
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import pino from "pino";
|
|
2
|
+
export type Globber = {
|
|
3
|
+
directory?: string;
|
|
4
|
+
routeFile?: string;
|
|
5
|
+
startingDirectory: string;
|
|
6
|
+
log?: pino.BaseLogger;
|
|
7
|
+
excludedDirectories?: string[];
|
|
8
|
+
};
|
|
9
|
+
export declare const globFiles: (opts: Required<Globber> & {
|
|
10
|
+
log?: pino.BaseLogger;
|
|
11
|
+
}) => Promise<never[]>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import url, { fileURLToPath } from "node:url";
|
|
4
|
+
import pkgUp from "pkg-up";
|
|
5
|
+
import { serial } from "./serial.js";
|
|
6
|
+
const getPackageType = async (directory) => {
|
|
7
|
+
const nearestPackage = await pkgUp({ cwd: directory });
|
|
8
|
+
if (nearestPackage) {
|
|
9
|
+
if ('require' in global) {
|
|
10
|
+
return require(nearestPackage).type;
|
|
11
|
+
}
|
|
12
|
+
const file = await readFile(nearestPackage, { encoding: 'utf-8' });
|
|
13
|
+
return JSON.parse(file).type;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export const globFiles = async (opts) => {
|
|
17
|
+
const packageType = await getPackageType(opts.directory);
|
|
18
|
+
const startingDirectory = packageType === 'module' ? path.dirname(fileURLToPath(opts.startingDirectory)) : opts.startingDirectory;
|
|
19
|
+
const pluginsDirectory = path.join(startingDirectory, opts.directory);
|
|
20
|
+
const routesDirectories = await readdir(pluginsDirectory);
|
|
21
|
+
const filteredDirectories = routesDirectories.filter(dir => !opts.excludedDirectories.includes(dir));
|
|
22
|
+
const importedFiles = await serial(filteredDirectories.map((dir) => async () => {
|
|
23
|
+
var _a;
|
|
24
|
+
const osPath = path.join(pluginsDirectory, dir, packageType === 'module' ? `${opts.routeFile.replace(".js", "")}.js` : opts.routeFile.replace(".js", ""));
|
|
25
|
+
const href = url.pathToFileURL(osPath).href;
|
|
26
|
+
try {
|
|
27
|
+
if (packageType === "module") {
|
|
28
|
+
const importedPlugin = await import(href);
|
|
29
|
+
return importedPlugin.default;
|
|
30
|
+
}
|
|
31
|
+
return require(osPath);
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.log(e);
|
|
35
|
+
(_a = opts.log) === null || _a === void 0 ? void 0 : _a.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}));
|
|
39
|
+
return importedFiles.filter((e) => e);
|
|
40
|
+
};
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fp from 'fastify-plugin';
|
|
2
|
+
import { globFiles } from './globber.js';
|
|
3
|
+
const plugin = fp(async (fastify, opts) => {
|
|
4
|
+
var _a, _b, _c;
|
|
5
|
+
const globbedFiles = await globFiles(Object.assign(Object.assign({}, opts), { routeFile: (_a = opts.routeFile) !== null && _a !== void 0 ? _a : 'routes', directory: (_b = opts.directory) !== null && _b !== void 0 ? _b : 'src/plugins', log: opts.log || fastify.log, excludedDirectories: (_c = opts.excludedDirectories) !== null && _c !== void 0 ? _c : [] }));
|
|
6
|
+
await Promise.all(globbedFiles.map(async (globbedFile) => {
|
|
7
|
+
await fastify.register(globbedFile);
|
|
8
|
+
}));
|
|
9
|
+
}, {
|
|
10
|
+
name: '@efebia/fastify-auto-import',
|
|
11
|
+
fastify: '4.x'
|
|
12
|
+
});
|
|
13
|
+
export default plugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const serial: <TResult>(funcs: (() => Promise<TResult>)[]) => Promise<never[]>;
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@efebia/fastify-auto-import",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"license": "MIT",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./lib/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
5
8
|
"dependencies": {
|
|
9
|
+
"fastify": "^4.11.0",
|
|
6
10
|
"fastify-plugin": "^4.4.0",
|
|
7
11
|
"pino": "^8.8.0",
|
|
8
12
|
"pkg-up": "^3.1.0"
|
|
@@ -10,7 +14,6 @@
|
|
|
10
14
|
"devDependencies": {
|
|
11
15
|
"@types/jest": "^29.2.5",
|
|
12
16
|
"@types/node": "^18.11.18",
|
|
13
|
-
"fastify": "^4.11.0",
|
|
14
17
|
"jest": "^29.3.1",
|
|
15
18
|
"ts-jest": "^29.0.3",
|
|
16
19
|
"typescript": "^4.9.4"
|
|
@@ -20,7 +23,14 @@
|
|
|
20
23
|
],
|
|
21
24
|
"scripts": {
|
|
22
25
|
"test": "yarn node $(yarn bin jest)",
|
|
23
|
-
"build": "tsc"
|
|
26
|
+
"build:esm": "tsc --module nodenext --moduleResolution nodenext --outDir lib/esm",
|
|
27
|
+
"build:cjs": "tsc --module commonjs --moduleResolution node --outDir lib/cjs",
|
|
28
|
+
"build": "yarn build:esm && yarn build:cjs"
|
|
24
29
|
},
|
|
25
|
-
"
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"require": "./lib/cjs/index.js",
|
|
33
|
+
"import": "./lib/esm/index.js"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
26
36
|
}
|
package/lib/index.d.ts
DELETED
package/lib/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
6
|
-
const globber_1 = require("./globber");
|
|
7
|
-
module.exports = (0, fastify_plugin_1.default)(async (fastify, opts) => {
|
|
8
|
-
var _a, _b, _c;
|
|
9
|
-
const globbedFiles = await (0, globber_1.globFiles)(Object.assign(Object.assign({}, opts), { routeFile: (_a = opts.routeFile) !== null && _a !== void 0 ? _a : 'routes', directory: (_b = opts.directory) !== null && _b !== void 0 ? _b : 'src/plugins', log: opts.log || fastify.log, excludedDirectories: (_c = opts.excludedDirectories) !== null && _c !== void 0 ? _c : [] }));
|
|
10
|
-
await Promise.all(globbedFiles.map(async (globbedFile) => {
|
|
11
|
-
await fastify.register(globbedFile);
|
|
12
|
-
}));
|
|
13
|
-
}, {
|
|
14
|
-
name: '@efebia/fastify-auto-import',
|
|
15
|
-
fastify: '4.x'
|
|
16
|
-
});
|