add dev input module

This commit is contained in:
熊潇 2025-05-16 23:55:53 +08:00
parent 956114e891
commit 248e045c63
3 changed files with 45 additions and 8 deletions

View File

@ -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",

View File

@ -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();

View File

@ -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<ResolvePathOptions, 'meta'>) => {
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), ''),
};
});
};