perf: Use Imports Instead of Aliases

This commit is contained in:
vben
2024-06-16 23:40:52 +08:00
parent 07d1f85ff7
commit 6ffec31ce8
19 changed files with 41 additions and 140 deletions

View File

@@ -32,6 +32,7 @@
"@manypkg/get-packages": "^2.2.1",
"consola": "^3.2.3",
"find-up": "^7.0.0",
"nanoid": "^5.0.7",
"pkg-types": "^1.1.1",
"prettier": "^3.3.2",
"rimraf": "^5.0.7",

View File

@@ -1,48 +0,0 @@
import fs from 'node:fs';
import { dirname, extname, resolve } from 'node:path';
import { findUpSync } from 'find-up';
/**
* 查找 package.json 文件所在的目录
* @param pathname
*/
function findUpPackageDir(pathname: string = '') {
const file = findUpSync('package.json', {
cwd: dirname(pathname),
type: 'file',
});
return dirname(file || '');
}
/**
* 根据给定的扩展名数组来查找文件是否存在,并返回对应文件路径
* @param pathname 文件路径
* @param extensions 扩展名数组
* @returns 对应文件路径,如果未找到则返回 null
*/
function findFileByExtension(
pathname: string = '',
extensions: string[] = ['.ts'],
): string {
if (extname(pathname)) {
return pathname;
}
for (const ext of extensions) {
const fullpath = resolve(pathname);
if (fs.existsSync(fullpath + ext) && fs.statSync(fullpath + ext).isFile()) {
return fullpath + ext;
}
}
for (const ext of extensions) {
const resultPath = resolve(pathname, `index${ext}`);
if (fs.existsSync(resultPath)) {
return resultPath;
}
}
return pathname;
}
export { findFileByExtension, findUpPackageDir };

View File

@@ -1,5 +1,4 @@
export { UNICODE } from './constants';
export { findFileByExtension, findUpPackageDir } from './find';
export * from './git';
export { add as gitAdd, getStagedFiles } from './git';
export { generatorContentHash } from './hash';