@thatcompany/ts-tool
Version:
基于TypeScript编写的工具库
99 lines • 4.18 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scanner = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
require("reflect-metadata");
class Scanner {
static excludeDirs = ['node_modules', 'dist', 'types', 'type', 'lib'];
static services = new Map();
static async scan(callback, dir = '') {
dir = path_1.default.resolve(dir);
const files = await fs_1.promises.readdir(dir);
for (const file of files) {
const filePath = path_1.default.join(dir, file);
const stat = await fs_1.promises.stat(filePath);
// 跳过隐藏文件、node_modules、dist、types、type等目录
if (file.startsWith('.') ||
file.startsWith('_') ||
file.endsWith('.d.ts') ||
this.excludeDirs.includes(file)) {
continue;
}
if (stat.isDirectory()) {
// 递归扫描下一个目录
await this.scan(callback, filePath);
}
else if (filePath.endsWith('.ts') || filePath.endsWith('.js')) {
// 调用回调函数处理文件
await callback(filePath);
}
}
}
/**
* 扫描指定目录下所有符合条件的服务
* @param key 指定的元数据键
* @param dir 指定的目录,默认从 `src` 目录开始扫描
* @param refresh 是否刷新服务缓存
*/
static async getServicesBySymbol(key, dir = '', refresh = false) {
const services = await this.getAllServices(dir, refresh);
if (services.has(key)) {
return services.get(key) || [];
}
return [];
}
/**
* 扫描指定目录下所有服务
* @param dir 指定目录,默认从 `src` 目录开始扫描
* @param refresh 是否刷新服务缓存
*/
static async getAllServices(dir = '', refresh = false) {
if (Scanner.services.size === 0 || refresh) {
Scanner.services.clear();
await this.scan(async (filePath) => {
const module = await Promise.resolve(`${filePath}`).then(s => __importStar(require(s)));
for (const exportName in module) {
if (module.hasOwnProperty(exportName) && typeof module[exportName] === 'function') {
const func = module[exportName];
// 获取函数的所有元数据键(所有符号)
const keys = Reflect.getMetadataKeys(func);
keys.forEach((key) => {
const existingFuncs = Scanner.services.get(key) || [];
Scanner.services.set(key, [...existingFuncs, func]);
});
}
}
}, dir);
}
return Scanner.services;
}
}
exports.Scanner = Scanner;
//# sourceMappingURL=Scanner.js.map