@efebia/fastify-auto-import 0.0.1 → 0.0.2
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 +11 -11
- package/lib/globber.js +66 -66
- package/lib/index.d.ts +3 -3
- package/lib/index.js +16 -16
- package/lib/serial.d.ts +1 -1
- package/lib/serial.js +8 -8
- package/package.json +1 -1
package/lib/globber.d.ts
CHANGED
|
@@ -1,11 +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[]>;
|
|
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[]>;
|
package/lib/globber.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.globFiles = void 0;
|
|
30
|
-
const promises_1 = require("node:fs/promises");
|
|
31
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
32
|
-
const node_url_1 = __importStar(require("node:url"));
|
|
33
|
-
const pkg_up_1 = __importDefault(require("pkg-up"));
|
|
34
|
-
const serial_1 = require("./serial");
|
|
35
|
-
const getPackageType = async (directory) => {
|
|
36
|
-
const nearestPackage = await (0, pkg_up_1.default)({ cwd: directory });
|
|
37
|
-
if (nearestPackage) {
|
|
38
|
-
return require(nearestPackage).type;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
const globFiles = async (opts) => {
|
|
42
|
-
const packageType = await getPackageType(opts.directory);
|
|
43
|
-
const startingDirectory = packageType === 'module' ? node_path_1.default.dirname((0, node_url_1.fileURLToPath)(opts.startingDirectory)) : opts.startingDirectory;
|
|
44
|
-
const pluginsDirectory = node_path_1.default.join(startingDirectory, opts.directory);
|
|
45
|
-
const routesDirectories = await (0, promises_1.readdir)(pluginsDirectory);
|
|
46
|
-
const filteredDirectories = routesDirectories.filter(dir => !opts.excludedDirectories.includes(dir));
|
|
47
|
-
const importedFiles = await (0, serial_1.serial)(filteredDirectories.map((dir) => async () => {
|
|
48
|
-
var _a;
|
|
49
|
-
var _b;
|
|
50
|
-
const osPath = node_path_1.default.join(pluginsDirectory, dir, packageType === 'module' ? `${opts.routeFile.replace(".js", "")}.js` : opts.routeFile.replace(".js", ""));
|
|
51
|
-
const href = node_url_1.default.pathToFileURL(osPath).href;
|
|
52
|
-
try {
|
|
53
|
-
if (packageType === "module") {
|
|
54
|
-
const importedPlugin = await (_a = href, Promise.resolve().then(() => __importStar(require(_a))));
|
|
55
|
-
return importedPlugin.default;
|
|
56
|
-
}
|
|
57
|
-
return require(osPath);
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
(_b = opts.log) === null || _b === void 0 ? void 0 : _b.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
}));
|
|
64
|
-
return importedFiles.filter((e) => e);
|
|
65
|
-
};
|
|
66
|
-
exports.globFiles = globFiles;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.globFiles = void 0;
|
|
30
|
+
const promises_1 = require("node:fs/promises");
|
|
31
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
32
|
+
const node_url_1 = __importStar(require("node:url"));
|
|
33
|
+
const pkg_up_1 = __importDefault(require("pkg-up"));
|
|
34
|
+
const serial_1 = require("./serial");
|
|
35
|
+
const getPackageType = async (directory) => {
|
|
36
|
+
const nearestPackage = await (0, pkg_up_1.default)({ cwd: directory });
|
|
37
|
+
if (nearestPackage) {
|
|
38
|
+
return require(nearestPackage).type;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const globFiles = async (opts) => {
|
|
42
|
+
const packageType = await getPackageType(opts.directory);
|
|
43
|
+
const startingDirectory = packageType === 'module' ? node_path_1.default.dirname((0, node_url_1.fileURLToPath)(opts.startingDirectory)) : opts.startingDirectory;
|
|
44
|
+
const pluginsDirectory = node_path_1.default.join(startingDirectory, opts.directory);
|
|
45
|
+
const routesDirectories = await (0, promises_1.readdir)(pluginsDirectory);
|
|
46
|
+
const filteredDirectories = routesDirectories.filter(dir => !opts.excludedDirectories.includes(dir));
|
|
47
|
+
const importedFiles = await (0, serial_1.serial)(filteredDirectories.map((dir) => async () => {
|
|
48
|
+
var _a;
|
|
49
|
+
var _b;
|
|
50
|
+
const osPath = node_path_1.default.join(pluginsDirectory, dir, packageType === 'module' ? `${opts.routeFile.replace(".js", "")}.js` : opts.routeFile.replace(".js", ""));
|
|
51
|
+
const href = node_url_1.default.pathToFileURL(osPath).href;
|
|
52
|
+
try {
|
|
53
|
+
if (packageType === "module") {
|
|
54
|
+
const importedPlugin = await (_a = href, Promise.resolve().then(() => __importStar(require(_a))));
|
|
55
|
+
return importedPlugin.default;
|
|
56
|
+
}
|
|
57
|
+
return require(osPath);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
(_b = opts.log) === null || _b === void 0 ? void 0 : _b.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}));
|
|
64
|
+
return importedFiles.filter((e) => e);
|
|
65
|
+
};
|
|
66
|
+
exports.globFiles = globFiles;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Globber } from './globber';
|
|
2
|
-
declare const _default: import("fastify").FastifyPluginAsync<Globber, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
3
|
-
export = _default;
|
|
1
|
+
import { Globber } from './globber';
|
|
2
|
+
declare const _default: import("fastify").FastifyPluginAsync<Globber, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
3
|
+
export = _default;
|
package/lib/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
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
|
-
});
|
|
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
|
+
});
|
package/lib/serial.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const serial: <TResult>(funcs: (() => Promise<TResult>)[]) => Promise<never[]>;
|
|
1
|
+
export declare const serial: <TResult>(funcs: (() => Promise<TResult>)[]) => Promise<never[]>;
|
package/lib/serial.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.serial = void 0;
|
|
4
|
-
const concat = (list) => Array.prototype.concat.bind(list);
|
|
5
|
-
const promiseConcat = (f) => (x) => f().then(concat(x));
|
|
6
|
-
const promiseReduce = (acc, x) => acc.then(promiseConcat(x));
|
|
7
|
-
const serial = (funcs) => funcs.reduce(promiseReduce, Promise.resolve([]));
|
|
8
|
-
exports.serial = serial;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serial = void 0;
|
|
4
|
+
const concat = (list) => Array.prototype.concat.bind(list);
|
|
5
|
+
const promiseConcat = (f) => (x) => f().then(concat(x));
|
|
6
|
+
const promiseReduce = (acc, x) => acc.then(promiseConcat(x));
|
|
7
|
+
const serial = (funcs) => funcs.reduce(promiseReduce, Promise.resolve([]));
|
|
8
|
+
exports.serial = serial;
|