临时修复:在resolvedModule.resolvedModule找不到模块的时候,回归到最初的getImportedFilePath逻辑进行尝试
This commit is contained in:
parent
501f0385d8
commit
e81d82bc2a
|
|
@ -1,6 +1,12 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.buildSchema = exports.analyzeEntities = exports.registerDeducedRelationMap = exports.registerFixedDestinationPathMap = exports.registerIgnoredRelationPathMap = exports.registerFreeEntities = exports.registerIgnoredForeignKeyMap = void 0;
|
||||
exports.registerIgnoredForeignKeyMap = registerIgnoredForeignKeyMap;
|
||||
exports.registerFreeEntities = registerFreeEntities;
|
||||
exports.registerIgnoredRelationPathMap = registerIgnoredRelationPathMap;
|
||||
exports.registerFixedDestinationPathMap = registerFixedDestinationPathMap;
|
||||
exports.registerDeducedRelationMap = registerDeducedRelationMap;
|
||||
exports.analyzeEntities = analyzeEntities;
|
||||
exports.buildSchema = buildSchema;
|
||||
const tslib_1 = require("tslib");
|
||||
const path_1 = tslib_1.__importDefault(require("path"));
|
||||
const assert_1 = tslib_1.__importDefault(require("assert"));
|
||||
|
|
@ -173,7 +179,8 @@ function checkActionDefNameConsistent(filename, actionDefNode) {
|
|||
const adfName = name.text.slice(0, name.text.length - 9);
|
||||
const aName = actionNode.typeName.text.slice(0, actionNode.typeName.text.length - 6);
|
||||
const sName = stateNode.typeName.text.slice(0, stateNode.typeName.text.length - 5);
|
||||
(0, assert_1.default)(adfName === aName && aName === sName, `文件${filename}中的ActionDef${name.text}中ActionDef, Action和State的命名规则不一致`);
|
||||
(0, assert_1.default)(adfName === aName && aName === sName, `文件${filename}中的ActionDef${name.text}中ActionDef, Action和State的命名规则不一致, 需要
|
||||
${adfName}ActionDef, ${adfName}Action, ${adfName}State`);
|
||||
}
|
||||
function checkStringLiteralLegal(filename, obj, text, ele) {
|
||||
(0, assert_1.default)(ts.isLiteralTypeNode(ele) && ts.isStringLiteral(ele.literal), `${filename}中引用的${obj} ${text}中存在不是stringliteral的类型`);
|
||||
|
|
@ -279,7 +286,7 @@ function tryGetStringLiteralValues(moduleName, filename, obj, node, program) {
|
|||
}).filter(ele => !!ele));
|
||||
}
|
||||
if (['state', 'action'].includes(obj)) {
|
||||
(0, assert_1.default)(values.length > 0);
|
||||
(0, assert_1.default)(values.length > 0, `${filename}中的${obj} ${node.typeName.getText()}未定义`);
|
||||
const importDeclartion = declaration.parent.parent.parent;
|
||||
(0, assert_1.default)(ts.isImportDeclaration(importDeclartion));
|
||||
addImportedFrom(moduleName, declaration.name.text, importDeclartion);
|
||||
|
|
@ -647,10 +654,24 @@ function analyzeSchemaDefinition(node, moduleName, filename, path, program, refe
|
|||
(0, assert_1.default)(ts.isStringLiteral(moduleSpecifier));
|
||||
const { text: from } = moduleSpecifier;
|
||||
extendsFrom.push(from);
|
||||
const importedFilename = getImportedFilePath(path, from, filename);
|
||||
const sourceFile = program.getSourceFile(importedFilename);
|
||||
(0, assert_1.default)(sourceFile, `「${filename}」找不到相应的sourceFile:${importedFilename}`);
|
||||
const relativeFilename = path_1.default.relative(process.cwd(), importedFilename);
|
||||
// 创建编译器主机
|
||||
const compilerHost = ts.createCompilerHost(program.getCompilerOptions());
|
||||
// 解析模块
|
||||
const resolvedModule = ts.resolveModuleName(from, filename, program.getCompilerOptions(), compilerHost);
|
||||
// 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:${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);
|
||||
|
|
@ -3458,7 +3479,7 @@ function outputLocale(outputDir, printer) {
|
|||
]), sourceFile);
|
||||
const data = Function(result)();
|
||||
const filename = path_1.default.join(outputDir, entity, 'locales', `${lng}.json`);
|
||||
(0, fs_1.writeFileSync)(filename, JSON.stringify(data), { flag: 'w' });
|
||||
(0, fs_1.writeFileSync)(filename, JSON.stringify(data, null, 2), { flag: 'w' });
|
||||
if (locales[lng]) {
|
||||
locales[lng].push(entity);
|
||||
}
|
||||
|
|
@ -3717,7 +3738,6 @@ let FIXED_FOR_ALL_DESTINATION_PATH_ENTITIES = [];
|
|||
function registerIgnoredForeignKeyMap(map) {
|
||||
IGNORED_FOREIGN_KEY_MAP = map;
|
||||
}
|
||||
exports.registerIgnoredForeignKeyMap = registerIgnoredForeignKeyMap;
|
||||
/**
|
||||
* 此函数不再使用
|
||||
* @param map
|
||||
|
|
@ -3727,7 +3747,6 @@ function registerFreeEntities(selectFreeEntities = [], createFreeEntities = [],
|
|||
CREATE_FREE_ENTITIES = createFreeEntities;
|
||||
UPDATE_FREE_ENTITIES = updateFreeEntities;
|
||||
}
|
||||
exports.registerFreeEntities = registerFreeEntities;
|
||||
/**
|
||||
* 此函数不再使用
|
||||
* @param map
|
||||
|
|
@ -3737,7 +3756,6 @@ function registerIgnoredRelationPathMap(map) {
|
|||
IGNORED_RELATION_PATH_MAP[(0, string_1.firstLetterUpperCase)(k)] = map[k];
|
||||
}
|
||||
}
|
||||
exports.registerIgnoredRelationPathMap = registerIgnoredRelationPathMap;
|
||||
/**
|
||||
* 很多路径虽然最后指向同一对象,但不能封掉,封了会导致查询的时候找不到对应的路径path
|
||||
* @param map
|
||||
|
|
@ -3755,7 +3773,6 @@ function registerFixedDestinationPathMap(map) {
|
|||
}
|
||||
}
|
||||
}
|
||||
exports.registerFixedDestinationPathMap = registerFixedDestinationPathMap;
|
||||
/**
|
||||
* 此函数不再使用
|
||||
* @param map
|
||||
|
|
@ -3774,7 +3791,6 @@ function registerDeducedRelationMap(map) {
|
|||
DEDUCED_RELATION_MAP[entity] = map[k];
|
||||
}
|
||||
}
|
||||
exports.registerDeducedRelationMap = registerDeducedRelationMap;
|
||||
/**
|
||||
* 输出所有和User相关的对象的后继
|
||||
* 此函数不再使用
|
||||
|
|
@ -4257,7 +4273,6 @@ function analyzeEntities(inputDir, relativePath) {
|
|||
analyzeInModi();
|
||||
uniqRelationships();
|
||||
}
|
||||
exports.analyzeEntities = analyzeEntities;
|
||||
function buildSchema(outputDir) {
|
||||
addReverseRelationship();
|
||||
// setRelationEntities();
|
||||
|
|
@ -4276,4 +4291,3 @@ function buildSchema(outputDir) {
|
|||
outputPackageJson(outputDir);
|
||||
}
|
||||
}
|
||||
exports.buildSchema = buildSchema;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue