@efebia/fastify-auto-import 0.0.2 → 1.0.1

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.
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
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
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
37
  };
@@ -31,11 +41,15 @@ const promises_1 = require("node:fs/promises");
31
41
  const node_path_1 = __importDefault(require("node:path"));
32
42
  const node_url_1 = __importStar(require("node:url"));
33
43
  const pkg_up_1 = __importDefault(require("pkg-up"));
34
- const serial_1 = require("./serial");
44
+ const serial_js_1 = require("./serial.js");
35
45
  const getPackageType = async (directory) => {
36
46
  const nearestPackage = await (0, pkg_up_1.default)({ cwd: directory });
37
47
  if (nearestPackage) {
38
- return require(nearestPackage).type;
48
+ if ('require' in global) {
49
+ return require(nearestPackage).type;
50
+ }
51
+ const file = await (0, promises_1.readFile)(nearestPackage, { encoding: 'utf-8' });
52
+ return JSON.parse(file).type;
39
53
  }
40
54
  };
41
55
  const globFiles = async (opts) => {
@@ -44,20 +58,20 @@ const globFiles = async (opts) => {
44
58
  const pluginsDirectory = node_path_1.default.join(startingDirectory, opts.directory);
45
59
  const routesDirectories = await (0, promises_1.readdir)(pluginsDirectory);
46
60
  const filteredDirectories = routesDirectories.filter(dir => !opts.excludedDirectories.includes(dir));
47
- const importedFiles = await (0, serial_1.serial)(filteredDirectories.map((dir) => async () => {
61
+ const importedFiles = await (0, serial_js_1.serial)(filteredDirectories.map((dir) => async () => {
48
62
  var _a;
49
- var _b;
50
63
  const osPath = node_path_1.default.join(pluginsDirectory, dir, packageType === 'module' ? `${opts.routeFile.replace(".js", "")}.js` : opts.routeFile.replace(".js", ""));
51
64
  const href = node_url_1.default.pathToFileURL(osPath).href;
52
65
  try {
53
66
  if (packageType === "module") {
54
- const importedPlugin = await (_a = href, Promise.resolve().then(() => __importStar(require(_a))));
67
+ const importedPlugin = await Promise.resolve(`${href}`).then(s => __importStar(require(s)));
55
68
  return importedPlugin.default;
56
69
  }
57
70
  return require(osPath);
58
71
  }
59
72
  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");
73
+ console.log(e);
74
+ (_a = opts.log) === null || _a === void 0 ? void 0 : _a.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
61
75
  return null;
62
76
  }
63
77
  }));
@@ -0,0 +1,3 @@
1
+ import { Globber } from './globber.js';
2
+ declare const plugin: import("fastify").FastifyPluginCallback<Globber, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
3
+ export default plugin;
@@ -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: '5.x'
17
+ });
18
+ exports.default = plugin;
@@ -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
+ };
@@ -0,0 +1,3 @@
1
+ import { Globber } from './globber.js';
2
+ declare const plugin: import("fastify").FastifyPluginCallback<Globber, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
3
+ export default plugin;
@@ -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: '5.x'
12
+ });
13
+ export default plugin;
@@ -0,0 +1 @@
1
+ export declare const serial: <TResult>(funcs: (() => Promise<TResult>)[]) => Promise<never[]>;
@@ -0,0 +1,4 @@
1
+ const concat = (list) => Array.prototype.concat.bind(list);
2
+ const promiseConcat = (f) => (x) => f().then(concat(x));
3
+ const promiseReduce = (acc, x) => acc.then(promiseConcat(x));
4
+ export const serial = (funcs) => funcs.reduce(promiseReduce, Promise.resolve([]));
package/package.json CHANGED
@@ -1,26 +1,36 @@
1
1
  {
2
2
  "name": "@efebia/fastify-auto-import",
3
- "version": "0.0.2",
3
+ "version": "1.0.1",
4
4
  "license": "MIT",
5
+ "main": "./lib/index.js",
6
+ "types": "./lib/index.d.ts",
7
+ "type": "module",
5
8
  "dependencies": {
6
- "fastify-plugin": "^4.4.0",
9
+ "fastify": "^5.3.0",
10
+ "fastify-plugin": "^5.0.1",
7
11
  "pino": "^8.8.0",
8
12
  "pkg-up": "^3.1.0"
9
13
  },
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
- "typescript": "^4.9.4"
19
+ "typescript": "~5.8.3"
17
20
  },
18
21
  "files": [
19
22
  "/lib"
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
- "main": "./lib"
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
@@ -1,3 +0,0 @@
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 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
- });
File without changes
File without changes
File without changes