适配了pnpm等其他主流包管理工具
This commit is contained in:
parent
bb80a0d6bc
commit
f6f37ff97b
|
|
@ -1,6 +1,6 @@
|
|||
import PathLib from 'path';
|
||||
import assert from 'assert';
|
||||
import { writeFileSync, readdirSync, mkdirSync, existsSync } from 'fs';
|
||||
import { writeFileSync, readdirSync, mkdirSync, existsSync, realpathSync } from 'fs';
|
||||
import { emptydirSync } from 'fs-extra';
|
||||
import { assign, cloneDeep, difference, identity, intersection, keys, pull, uniq, uniqBy, flatten, result } from 'lodash';
|
||||
import * as ts from 'typescript';
|
||||
|
|
@ -785,7 +785,7 @@ function analyzeImportDeclaration(
|
|||
} */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 在path下的filename文件中import了fileSpecifierPath,要找到其对应的引用路径
|
||||
* 这里关键是要处理形如file:的依赖声明
|
||||
|
|
@ -794,58 +794,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,
|
||||
|
|
@ -889,10 +889,27 @@ function analyzeSchemaDefinition(
|
|||
|
||||
const { text: from } = moduleSpecifier;
|
||||
extendsFrom.push(from);
|
||||
const importedFilename = getImportedFilePath(path, from, filename);
|
||||
const sourceFile = program.getSourceFile(importedFilename);
|
||||
assert(sourceFile, `「${filename}」找不到相应的sourceFile:${importedFilename}`);
|
||||
const relativeFilename = PathLib.relative(process.cwd(), importedFilename);
|
||||
|
||||
// 创建编译器主机
|
||||
const compilerHost = ts.createCompilerHost(program.getCompilerOptions());
|
||||
|
||||
// 解析模块
|
||||
const resolvedModule = ts.resolveModuleName(
|
||||
from,
|
||||
filename,
|
||||
program.getCompilerOptions(),
|
||||
compilerHost
|
||||
);
|
||||
|
||||
assert(resolvedModule.resolvedModule, "找不到module定义")
|
||||
|
||||
const resolvedFileName = resolvedModule.resolvedModule.resolvedFileName;
|
||||
const sourceFile = program.getSourceFile(resolvedFileName);
|
||||
|
||||
// const importedFilename = getImportedFilePath(path, from, filename);
|
||||
// const sourceFile = program.getSourceFile(importedFilename);
|
||||
assert(sourceFile, `「${filename}」找不到相应的sourceFile:${resolvedFileName}`);
|
||||
const relativeFilename = PathLib.relative(process.cwd(), resolvedFileName);
|
||||
|
||||
const result = analyzeReferenceSchemaFile(
|
||||
moduleName,
|
||||
|
|
|
|||
Loading…
Reference in New Issue