Merge branch 'dev' of gitea.51mars.com:Oak-Team/oak-domain into dev

This commit is contained in:
Xu Chang 2024-11-08 14:49:38 +08:00
commit c8847db748
2 changed files with 119 additions and 107 deletions

View File

@ -573,54 +573,54 @@ function analyzeImportDeclaration(node, referencedSchemas, additionalImports, fi
* @param filename
* @returns
*/
// function getImportedFilePath(path: string, fileSpecifierPath: string, filename: string) {
// let importedFilepath = '';
// const getExistedFileName = () => {
// if (existsSync(`${importedFilepath}.ts`)) {
// return `${importedFilepath}.ts`;
// }
// else if (existsSync(`${importedFilepath}.d.ts`)) {
// return `${importedFilepath}.d.ts`;
// }
// return '';
// };
// if (fileSpecifierPath.startsWith('.')) {
// importedFilepath = PathLib.join(path, fileSpecifierPath);
// const importedFilename = getExistedFileName();
// assert(importedFilename, `「${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
// return importedFilename;
// }
// else {
// const cwd = process.cwd();
// const fileSpecifierPaths = fileSpecifierPath.split('/');
// const moduleName = fileSpecifierPaths[0] || fileSpecifierPaths[1];
// assert(moduleName);
// // 从path向外找package.json -> node_modules直至找到fileSpecifier
// const paths = path.split('/');
// for (let iter = paths.length; iter >= 0; iter--) {
// const paths2 = paths.slice(0, iter);
// const pkgJsonPath = PathLib.join(cwd, ...paths2, 'package.json');
// if (existsSync(pkgJsonPath)) {
// const pkgJson = require(pkgJsonPath);
// if (pkgJson.dependencies?.hasOwnProperty(moduleName)) {
// const dependentPath = pkgJson.dependencies[moduleName] as string;
// if (dependentPath.trimStart().startsWith('file:')) {
// const dependentFilePath = dependentPath.trimStart().slice(5);
// importedFilepath = PathLib.join(pkgJsonPath, '..', dependentFilePath, ...(fileSpecifierPaths[0] ? fileSpecifierPaths.slice(1) : (fileSpecifierPaths.slice(2))));
// }
// else {
// importedFilepath = PathLib.join(pkgJsonPath, '..', 'node_modules', fileSpecifierPath);
// }
// const importedFilename = getExistedFileName();
// if (importedFilename) {
// return importedFilename;
// }
// }
// }
// }
// }
// assert(false, `「${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
// }
function getImportedFilePath(path, fileSpecifierPath, filename) {
let importedFilepath = '';
const getExistedFileName = () => {
if ((0, fs_1.existsSync)(`${importedFilepath}.ts`)) {
return `${importedFilepath}.ts`;
}
else if ((0, fs_1.existsSync)(`${importedFilepath}.d.ts`)) {
return `${importedFilepath}.d.ts`;
}
return '';
};
if (fileSpecifierPath.startsWith('.')) {
importedFilepath = path_1.default.join(path, fileSpecifierPath);
const importedFilename = getExistedFileName();
(0, assert_1.default)(importedFilename, `${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
return importedFilename;
}
else {
const cwd = process.cwd();
const fileSpecifierPaths = fileSpecifierPath.split('/');
const moduleName = fileSpecifierPaths[0] || fileSpecifierPaths[1];
(0, assert_1.default)(moduleName);
// 从path向外找package.json -> node_modules直至找到fileSpecifier
const paths = path.split('/');
for (let iter = paths.length; iter >= 0; iter--) {
const paths2 = paths.slice(0, iter);
const pkgJsonPath = path_1.default.join(cwd, ...paths2, 'package.json');
if ((0, fs_1.existsSync)(pkgJsonPath)) {
const pkgJson = require(pkgJsonPath);
if (pkgJson.dependencies?.hasOwnProperty(moduleName)) {
const dependentPath = pkgJson.dependencies[moduleName];
if (dependentPath.trimStart().startsWith('file:')) {
const dependentFilePath = dependentPath.trimStart().slice(5);
importedFilepath = path_1.default.join(pkgJsonPath, '..', dependentFilePath, ...(fileSpecifierPaths[0] ? fileSpecifierPaths.slice(1) : (fileSpecifierPaths.slice(2))));
}
else {
importedFilepath = path_1.default.join(pkgJsonPath, '..', 'node_modules', fileSpecifierPath);
}
const importedFilename = getExistedFileName();
if (importedFilename) {
return importedFilename;
}
}
}
}
}
(0, assert_1.default)(false, `${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
}
function analyzeSchemaDefinition(node, moduleName, filename, path, program, referencedSchemas, schemaAttrs, enumAttributes, importAttrFrom, relativePath) {
let hasEntityAttr = false;
let hasEntityIdAttr = false;
@ -652,13 +652,20 @@ function analyzeSchemaDefinition(node, moduleName, filename, path, program, refe
const compilerHost = ts.createCompilerHost(program.getCompilerOptions());
// 解析模块
const resolvedModule = ts.resolveModuleName(from, filename, program.getCompilerOptions(), compilerHost);
(0, assert_1.default)(resolvedModule.resolvedModule, "找不到module定义");
const resolvedFileName = resolvedModule.resolvedModule.resolvedFileName;
const sourceFile = program.getSourceFile(resolvedFileName);
// assert(resolvedModule.resolvedModule, "找不到module定义")
let pathName = "";
if (!resolvedModule.resolvedModule) {
console.warn(`${filename}」找不到相应的module定义${from}`);
pathName = getImportedFilePath(path, from, filename);
}
else {
pathName = resolvedModule.resolvedModule.resolvedFileName;
}
const sourceFile = program.getSourceFile(pathName);
// const importedFilename = getImportedFilePath(path, from, filename);
// const sourceFile = program.getSourceFile(importedFilename);
(0, assert_1.default)(sourceFile, `${filename}」找不到相应的sourceFile${resolvedFileName}`);
const relativeFilename = path_1.default.relative(process.cwd(), resolvedFileName);
(0, assert_1.default)(sourceFile, `${filename}」找不到相应的sourceFile${pathName}`);
const relativeFilename = path_1.default.relative(process.cwd(), pathName);
const result = analyzeReferenceSchemaFile(moduleName, path_1.default.basename(relativeFilename), path_1.default.dirname(relativeFilename), sourceFile, program, referencedSchemas, schemaAttrs, enumAttributes, importAttrFrom, path_1.default.join(from, '..'));
return result;
}).filter(ele => !!ele);

View File

@ -795,58 +795,58 @@ function analyzeImportDeclaration(
* @param filename
* @returns
*/
// function getImportedFilePath(path: string, fileSpecifierPath: string, filename: string) {
// let importedFilepath = '';
function getImportedFilePath(path: string, fileSpecifierPath: string, filename: string) {
let importedFilepath = '';
// const getExistedFileName = () => {
// if (existsSync(`${importedFilepath}.ts`)) {
// return `${importedFilepath}.ts`;
// }
// else if (existsSync(`${importedFilepath}.d.ts`)) {
// return `${importedFilepath}.d.ts`;
// }
// return '';
// };
const getExistedFileName = () => {
if (existsSync(`${importedFilepath}.ts`)) {
return `${importedFilepath}.ts`;
}
else if (existsSync(`${importedFilepath}.d.ts`)) {
return `${importedFilepath}.d.ts`;
}
return '';
};
// if (fileSpecifierPath.startsWith('.')) {
// importedFilepath = PathLib.join(path, fileSpecifierPath);
// const importedFilename = getExistedFileName();
// assert(importedFilename, `「${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
// return importedFilename;
// }
// else {
// const cwd = process.cwd();
if (fileSpecifierPath.startsWith('.')) {
importedFilepath = PathLib.join(path, fileSpecifierPath);
const importedFilename = getExistedFileName();
assert(importedFilename, `${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
return importedFilename;
}
else {
const cwd = process.cwd();
// const fileSpecifierPaths = fileSpecifierPath.split('/');
// const moduleName = fileSpecifierPaths[0] || fileSpecifierPaths[1];
// assert(moduleName);
// // 从path向外找package.json -> node_modules直至找到fileSpecifier
// const paths = path.split('/');
// for (let iter = paths.length; iter >= 0; iter--) {
// const paths2 = paths.slice(0, iter);
// const pkgJsonPath = PathLib.join(cwd, ...paths2, 'package.json');
// if (existsSync(pkgJsonPath)) {
// const pkgJson = require(pkgJsonPath);
// if (pkgJson.dependencies?.hasOwnProperty(moduleName)) {
// const dependentPath = pkgJson.dependencies[moduleName] as string;
// if (dependentPath.trimStart().startsWith('file:')) {
// const dependentFilePath = dependentPath.trimStart().slice(5);
// importedFilepath = PathLib.join(pkgJsonPath, '..', dependentFilePath, ...(fileSpecifierPaths[0] ? fileSpecifierPaths.slice(1) : (fileSpecifierPaths.slice(2))));
// }
// else {
// importedFilepath = PathLib.join(pkgJsonPath, '..', 'node_modules', fileSpecifierPath);
// }
// const importedFilename = getExistedFileName();
// if (importedFilename) {
// return importedFilename;
// }
// }
// }
// }
const fileSpecifierPaths = fileSpecifierPath.split('/');
const moduleName = fileSpecifierPaths[0] || fileSpecifierPaths[1];
assert(moduleName);
// 从path向外找package.json -> node_modules直至找到fileSpecifier
const paths = path.split('/');
for (let iter = paths.length; iter >= 0; iter--) {
const paths2 = paths.slice(0, iter);
const pkgJsonPath = PathLib.join(cwd, ...paths2, 'package.json');
if (existsSync(pkgJsonPath)) {
const pkgJson = require(pkgJsonPath);
if (pkgJson.dependencies?.hasOwnProperty(moduleName)) {
const dependentPath = pkgJson.dependencies[moduleName] as string;
if (dependentPath.trimStart().startsWith('file:')) {
const dependentFilePath = dependentPath.trimStart().slice(5);
importedFilepath = PathLib.join(pkgJsonPath, '..', dependentFilePath, ...(fileSpecifierPaths[0] ? fileSpecifierPaths.slice(1) : (fileSpecifierPaths.slice(2))));
}
else {
importedFilepath = PathLib.join(pkgJsonPath, '..', 'node_modules', fileSpecifierPath);
}
const importedFilename = getExistedFileName();
if (importedFilename) {
return importedFilename;
}
}
}
}
// }
// assert(false, `「${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
// }
}
assert(false, `${filename}」中import路径${fileSpecifierPath}找不到对应的声明`);
}
function analyzeSchemaDefinition(
node: ts.InterfaceDeclaration,
@ -902,15 +902,20 @@ function analyzeSchemaDefinition(
compilerHost
);
assert(resolvedModule.resolvedModule, "找不到module定义")
const resolvedFileName = resolvedModule.resolvedModule.resolvedFileName;
const sourceFile = program.getSourceFile(resolvedFileName);
// assert(resolvedModule.resolvedModule, "找不到module定义")
let pathName = ""
if (!resolvedModule.resolvedModule) {
console.warn(`${filename}」找不到相应的module定义${from}`);
pathName = getImportedFilePath(path, from, filename);
} else {
pathName = resolvedModule.resolvedModule.resolvedFileName;
}
const sourceFile = program.getSourceFile(pathName);
// const importedFilename = getImportedFilePath(path, from, filename);
// const sourceFile = program.getSourceFile(importedFilename);
assert(sourceFile, `${filename}」找不到相应的sourceFile${resolvedFileName}`);
const relativeFilename = PathLib.relative(process.cwd(), resolvedFileName);
assert(sourceFile, `${filename}」找不到相应的sourceFile${pathName}`);
const relativeFilename = PathLib.relative(process.cwd(), pathName);
const result = analyzeReferenceSchemaFile(
moduleName,