diff --git a/package.json b/package.json index 3e5d1f8..8667ad5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/use-config", - "version": "1.0.15", + "version": "1.0.17", "types": "dist/config.d.ts", "scripts": { "build": "npm run clean && tsup", diff --git a/src/config.ts b/src/config.ts index e8d8075..0bc0676 100644 --- a/src/config.ts +++ b/src/config.ts @@ -46,7 +46,7 @@ export const fileIsExist = (path: string) => { return false; } }; -export const getDirname = () => { +export const getCwdDirname = () => { return path.resolve(); }; /** @@ -55,7 +55,7 @@ export const getDirname = () => { * @returns */ export const getConfigFile = (fileName = 'app.config.json5') => { - const dirname = getDirname(); + const dirname = getCwdDirname(); // 本级 const benPath = dirname + '/' + fileName; const ben = fileIsExist(benPath); @@ -72,7 +72,7 @@ export const getConfigFile = (fileName = 'app.config.json5') => { }; // 初始化读取配置文件 export const init = (initConfigBase?: any): Config => { - const dirname = getDirname(); + const dirname = getCwdDirname(); try { // 配置读取路径,3级判断 const filePath = getConfigFile(); diff --git a/src/env.ts b/src/env.ts index d23ceae..af0c352 100644 --- a/src/env.ts +++ b/src/env.ts @@ -41,7 +41,7 @@ export const fileIsExist = (path: string) => { return false; } }; -export const getDirname = () => { +export const getCwdDirname = () => { return process.cwd(); }; /** @@ -68,13 +68,13 @@ export type ConfigOpts = { */ export const getConfigFile = (opts?: ConfigOpts) => { if (opts?.envConfigFile) { - const filePath = path.join(opts.cwd || getDirname(), opts.envConfigFile); + const filePath = path.join(opts.cwd || getCwdDirname(), opts.envConfigFile); if (fileIsExist(filePath)) { return filePath; } } const fileName = opts?.fileName || '.env'; - const dirname = opts?.cwd || getDirname(); + const dirname = opts?.cwd || getCwdDirname(); // 本级 const benPath = dirname + '/' + fileName; const ben = fileIsExist(benPath); @@ -224,6 +224,43 @@ export const resolvePath = (releactivePath: string = '', opts?: ResolvePathOptio } else if (opts?.cwd) { return path.resolve(opts.cwd, releactivePath); } else { - return path.resolve(getDirname(), releactivePath); + return path.resolve(getCwdDirname(), releactivePath); } }; + +/** + * 获取当前文件的路径 + * @param opts + * @returns + */ +export const getDirname = (opts?: Pick) => { + if (opts?.meta) { + const __filename = fileURLToPath(opts.meta.url); + const __dirname = path.dirname(__filename); + return { + __filename, + __dirname, + }; + } else { + return { + __dirname: getCwdDirname(), + __filename: path.join(getCwdDirname(), 'index.ts'), + }; + } +}; + +export const getDevInputs = (files: string[] = []) => { + return files.map((item) => { + const filename = path.basename(item); + return { + // 相对路径 + path: item, + // 文件名 + filename: filename, + // 文件后缀 + extname: path.extname(filename), + // 文件名不带后缀 + naming: filename.replace(path.extname(filename), ''), + }; + }); +};