fix: 修复在node_modules下的引用处理时出现的部分问题
This commit is contained in:
parent
32675a9080
commit
2114f959e6
|
|
@ -13,7 +13,7 @@ declare const Schema: Record<string, {
|
||||||
inModi: boolean;
|
inModi: boolean;
|
||||||
relations: false | string[];
|
relations: false | string[];
|
||||||
extendsFrom: string[];
|
extendsFrom: string[];
|
||||||
importAttrFrom: Record<string, [string, string | undefined, string]>;
|
importAttrFrom: Record<string, [string, string | undefined, string, string]>;
|
||||||
}>;
|
}>;
|
||||||
export declare function constructAttributes(entity: string): ts.PropertyAssignment[];
|
export declare function constructAttributes(entity: string): ts.PropertyAssignment[];
|
||||||
export declare function translateLocaleObject(locale: ts.ObjectLiteralExpression): Record<string, any>;
|
export declare function translateLocaleObject(locale: ts.ObjectLiteralExpression): Record<string, any>;
|
||||||
|
|
|
||||||
|
|
@ -238,11 +238,16 @@ function addImportedFrom(moduleName, name, node) {
|
||||||
* @param projectRoot 项目根目录,默认为当前目录
|
* @param projectRoot 项目根目录,默认为当前目录
|
||||||
* @returns 新的相对导入路径
|
* @returns 新的相对导入路径
|
||||||
*/
|
*/
|
||||||
function resolveCompiledImportPath(sourceFilePath, outputFilePath, importPath, projectRoot = '.') {
|
function resolveCompiledImportPath(sourceFilePath, outputFilePath, importPath, projectRoot = '.', relative) {
|
||||||
// 如果不是相对路径(例如 node_modules 的包或别名路径),直接返回
|
// 如果不是相对路径(例如 node_modules 的包或别名路径),直接返回
|
||||||
if (!importPath.startsWith('.')) {
|
if (!importPath.startsWith('.')) {
|
||||||
return importPath;
|
return importPath;
|
||||||
}
|
}
|
||||||
|
if (sourceFilePath.startsWith("node_modules")) {
|
||||||
|
return importPath.startsWith('.') ? (relative
|
||||||
|
? path_1.default.join(relative, importPath).replace(/\\/g, '/')
|
||||||
|
: path_1.default.join('..', importPath).replace(/\\/g, '/')) : importPath;
|
||||||
|
}
|
||||||
// 1. 获取源文件所在目录
|
// 1. 获取源文件所在目录
|
||||||
const sourceDir = path_1.default.dirname(sourceFilePath);
|
const sourceDir = path_1.default.dirname(sourceFilePath);
|
||||||
// 2. 解析原始导入路径,得到目标文件的绝对路径(相对于项目根目录)
|
// 2. 解析原始导入路径,得到目标文件的绝对路径(相对于项目根目录)
|
||||||
|
|
@ -294,7 +299,7 @@ function analyzeExternalAttrImport(node, program, importAttrFrom, relativePath)
|
||||||
const sourceFilePath = declaration.getSourceFile().fileName;
|
const sourceFilePath = declaration.getSourceFile().fileName;
|
||||||
(0, lodash_1.assign)(importAttrFrom, {
|
(0, lodash_1.assign)(importAttrFrom, {
|
||||||
// [name]: [importFromRelatively, propertyName],
|
// [name]: [importFromRelatively, propertyName],
|
||||||
[name]: [importFrom, propertyName, sourceFilePath],
|
[name]: [importFrom, propertyName, sourceFilePath, relativePath],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (ts.isTypeAliasDeclaration(declaration)) {
|
else if (ts.isTypeAliasDeclaration(declaration)) {
|
||||||
|
|
@ -3816,12 +3821,12 @@ function _outputBaseSchema(outputDir, printer) {
|
||||||
// else {
|
// else {
|
||||||
// fromExternalImportAttrs[from] = [[attr, propertyName]];
|
// fromExternalImportAttrs[from] = [[attr, propertyName]];
|
||||||
// }
|
// }
|
||||||
const [from, propertyName, sourceFilePath] = importAttrFrom[attr];
|
const [from, propertyName, sourceFilePath, relative] = importAttrFrom[attr];
|
||||||
// ===== 在这里进行路径转换 =====
|
// ===== 在这里进行路径转换 =====
|
||||||
const outputFilePath = path_1.default.join(outputDir, entity, '_baseSchema.ts'); // 输出文件路径
|
const outputFilePath = path_1.default.join(outputDir, entity, '_baseSchema.ts'); // 输出文件路径
|
||||||
// 使用路径解析算法计算新的导入路径
|
// 使用路径解析算法计算新的导入路径
|
||||||
const newImportPath = resolveCompiledImportPath(path_1.default.relative(process.cwd(), sourceFilePath), path_1.default.relative(process.cwd(), outputFilePath), from, // 原始导入路径
|
const newImportPath = resolveCompiledImportPath(path_1.default.relative(process.cwd(), sourceFilePath), path_1.default.relative(process.cwd(), outputFilePath), from, // 原始导入路径
|
||||||
".");
|
".", relative);
|
||||||
// console.log('resolve import path:', {
|
// console.log('resolve import path:', {
|
||||||
// sourceFilePath: PathLib.relative(process.cwd(), sourceFilePath),
|
// sourceFilePath: PathLib.relative(process.cwd(), sourceFilePath),
|
||||||
// outputFilePath: PathLib.relative(process.cwd(), outputFilePath),
|
// outputFilePath: PathLib.relative(process.cwd(), outputFilePath),
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ const Schema: Record<string, {
|
||||||
inModi: boolean;
|
inModi: boolean;
|
||||||
relations: false | string[];
|
relations: false | string[];
|
||||||
extendsFrom: string[];
|
extendsFrom: string[];
|
||||||
importAttrFrom: Record<string, [string, string | undefined, string]>;
|
importAttrFrom: Record<string, [string, string | undefined, string, string]>;
|
||||||
}> = {};
|
}> = {};
|
||||||
const OneToMany: Record<string, Array<[string, string, boolean]>> = {};
|
const OneToMany: Record<string, Array<[string, string, boolean]>> = {};
|
||||||
const ManyToOne: Record<string, Array<[string, string, boolean]>> = {};
|
const ManyToOne: Record<string, Array<[string, string, boolean]>> = {};
|
||||||
|
|
@ -361,13 +361,27 @@ function resolveCompiledImportPath(
|
||||||
sourceFilePath: string,
|
sourceFilePath: string,
|
||||||
outputFilePath: string,
|
outputFilePath: string,
|
||||||
importPath: string,
|
importPath: string,
|
||||||
projectRoot: string = '.'
|
projectRoot: string = '.',
|
||||||
|
relative?: string,
|
||||||
): string {
|
): string {
|
||||||
// 如果不是相对路径(例如 node_modules 的包或别名路径),直接返回
|
// 如果不是相对路径(例如 node_modules 的包或别名路径),直接返回
|
||||||
if (!importPath.startsWith('.')) {
|
if (!importPath.startsWith('.')) {
|
||||||
return importPath;
|
return importPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sourceFilePath.startsWith("node_modules")) {
|
||||||
|
// 如果是node_modules下的文件,保持原有的处理逻辑
|
||||||
|
return importPath.startsWith('.') ? (relative
|
||||||
|
? PathLib.join(
|
||||||
|
relative,
|
||||||
|
importPath
|
||||||
|
).replace(/\\/g, '/')
|
||||||
|
: PathLib.join(
|
||||||
|
'..',
|
||||||
|
importPath
|
||||||
|
).replace(/\\/g, '/')) : importPath;
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 获取源文件所在目录
|
// 1. 获取源文件所在目录
|
||||||
const sourceDir = PathLib.dirname(sourceFilePath);
|
const sourceDir = PathLib.dirname(sourceFilePath);
|
||||||
|
|
||||||
|
|
@ -396,7 +410,7 @@ function resolveCompiledImportPath(
|
||||||
function analyzeExternalAttrImport(
|
function analyzeExternalAttrImport(
|
||||||
node: ts.TypeReferenceNode,
|
node: ts.TypeReferenceNode,
|
||||||
program: ts.Program,
|
program: ts.Program,
|
||||||
importAttrFrom: Record<string, [string, string | undefined, string]>,
|
importAttrFrom: Record<string, [string, string | undefined, string, string]>,
|
||||||
relativePath?: string,
|
relativePath?: string,
|
||||||
) {
|
) {
|
||||||
const checker = program.getTypeChecker();
|
const checker = program.getTypeChecker();
|
||||||
|
|
@ -436,7 +450,7 @@ function analyzeExternalAttrImport(
|
||||||
|
|
||||||
assign(importAttrFrom, {
|
assign(importAttrFrom, {
|
||||||
// [name]: [importFromRelatively, propertyName],
|
// [name]: [importFromRelatively, propertyName],
|
||||||
[name]: [importFrom, propertyName, sourceFilePath],
|
[name]: [importFrom, propertyName, sourceFilePath, relativePath],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (ts.isTypeAliasDeclaration(declaration)) {
|
else if (ts.isTypeAliasDeclaration(declaration)) {
|
||||||
|
|
@ -961,7 +975,7 @@ function analyzeSchemaDefinition(
|
||||||
referencedSchemas: string[],
|
referencedSchemas: string[],
|
||||||
schemaAttrs: ts.TypeElement[],
|
schemaAttrs: ts.TypeElement[],
|
||||||
enumAttributes: Record<string, string[]>,
|
enumAttributes: Record<string, string[]>,
|
||||||
importAttrFrom: Record<string, [string, string | undefined, string]>,
|
importAttrFrom: Record<string, [string, string | undefined, string, string]>,
|
||||||
relativePath?: string
|
relativePath?: string
|
||||||
) {
|
) {
|
||||||
let hasEntityAttr = false;
|
let hasEntityAttr = false;
|
||||||
|
|
@ -1191,7 +1205,7 @@ function analyzeReferenceSchemaFile(
|
||||||
referencedSchemas: string[],
|
referencedSchemas: string[],
|
||||||
schemaAttrs: ts.TypeElement[],
|
schemaAttrs: ts.TypeElement[],
|
||||||
enumAttributes: Record<string, string[]>,
|
enumAttributes: Record<string, string[]>,
|
||||||
importAttrFrom: Record<string, [string, string | undefined, string]>,
|
importAttrFrom: Record<string, [string, string | undefined, string, string]>,
|
||||||
relativePath: string) {
|
relativePath: string) {
|
||||||
let result: ReturnType<typeof analyzeSchemaDefinition> | undefined;
|
let result: ReturnType<typeof analyzeSchemaDefinition> | undefined;
|
||||||
ts.forEachChild(sourceFile!, (node) => {
|
ts.forEachChild(sourceFile!, (node) => {
|
||||||
|
|
@ -1259,7 +1273,7 @@ function analyzeEntity(filename: string, path: string, program: ts.Program, rela
|
||||||
let actionType = 'crud';
|
let actionType = 'crud';
|
||||||
let _static = false;
|
let _static = false;
|
||||||
const enumAttributes: Record<string, string[]> = {};
|
const enumAttributes: Record<string, string[]> = {};
|
||||||
const importAttrFrom: Record<string, [string, string | undefined, string]> = {};
|
const importAttrFrom: Record<string, [string, string | undefined, string, string]> = {};
|
||||||
let localeDef: ts.ObjectLiteralExpression | undefined = undefined;
|
let localeDef: ts.ObjectLiteralExpression | undefined = undefined;
|
||||||
let extendsFrom: string[] | undefined;
|
let extendsFrom: string[] | undefined;
|
||||||
// let relationHierarchy: ts.ObjectLiteralExpression | undefined = undefined;
|
// let relationHierarchy: ts.ObjectLiteralExpression | undefined = undefined;
|
||||||
|
|
@ -6987,7 +7001,7 @@ function _outputBaseSchema(outputDir: string, printer: ts.Printer) {
|
||||||
// else {
|
// else {
|
||||||
// fromExternalImportAttrs[from] = [[attr, propertyName]];
|
// fromExternalImportAttrs[from] = [[attr, propertyName]];
|
||||||
// }
|
// }
|
||||||
const [from, propertyName, sourceFilePath] = importAttrFrom[attr];
|
const [from, propertyName, sourceFilePath, relative] = importAttrFrom[attr];
|
||||||
|
|
||||||
// ===== 在这里进行路径转换 =====
|
// ===== 在这里进行路径转换 =====
|
||||||
const outputFilePath = PathLib.join(outputDir, entity, '_baseSchema.ts'); // 输出文件路径
|
const outputFilePath = PathLib.join(outputDir, entity, '_baseSchema.ts'); // 输出文件路径
|
||||||
|
|
@ -6998,6 +7012,7 @@ function _outputBaseSchema(outputDir: string, printer: ts.Printer) {
|
||||||
PathLib.relative(process.cwd(), outputFilePath),
|
PathLib.relative(process.cwd(), outputFilePath),
|
||||||
from, // 原始导入路径
|
from, // 原始导入路径
|
||||||
".",
|
".",
|
||||||
|
relative
|
||||||
);
|
);
|
||||||
|
|
||||||
// console.log('resolve import path:', {
|
// console.log('resolve import path:', {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue