Created
July 6, 2020 11:37
-
-
Save buhichan/d4c6c3b8d6021d17ca3edb2e614b44e3 to your computer and use it in GitHub Desktop.
获取最大依赖深度, 来创造一个读代码的顺序列表...?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
createProgram, | |
SourceFile, | |
isImportDeclaration, | |
createCompilerHost, | |
CompilerOptions, | |
ModuleKind, | |
ModuleResolutionKind, | |
ScriptTarget, | |
resolveModuleNameFromCache, | |
resolveModuleName, | |
} from "typescript" | |
import * as path from "path" | |
import { writeFileSync } from "fs" | |
const compilerOptions: CompilerOptions = { | |
module: ModuleKind.AMD, | |
moduleResolution: ModuleResolutionKind.NodeJs, | |
experimentalDecorators: true, | |
noImplicitReturns: true, | |
noUnusedLocals: true, | |
strict: true, | |
forceConsistentCasingInFileNames: true, | |
baseUrl: ".", | |
paths: { | |
"vs/*": ["./vs/*"], | |
}, | |
lib: ["ES2015", "ES2017.String", "ES2018.Promise", "DOM", "DOM.Iterable", "WebWorker.ImportScripts"], | |
allowJs: true, | |
removeComments: false, | |
preserveConstEnums: true, | |
sourceMap: false, | |
outDir: "../out/vs", | |
target: ScriptTarget.ES2017, | |
types: ["keytar", "mocha", "semver", "sinon", "winreg"], | |
} | |
const host = createCompilerHost(compilerOptions) | |
const cwd = process.cwd() | |
process.chdir("./vscode/src") | |
const entry = "vs/code/electron-main/main.ts" | |
const tscCwd = process.cwd() | |
const program = createProgram({ | |
rootNames: [entry], | |
options: compilerOptions, | |
host, | |
}) | |
const map: Record< | |
string, | |
{ | |
references: string[] | |
visited: boolean | |
distance: number | |
} | |
> = {} | |
function checkFile(sourceFile: SourceFile) { | |
const references: string[] = [] | |
for (let statement of sourceFile.statements) { | |
if (isImportDeclaration(statement)) { | |
// console.log(statement.moduleSpecifier) | |
const moduleSpecifier = statement.moduleSpecifier.getText(sourceFile).slice(1, -1) | |
if (moduleSpecifier.startsWith("vs")) { | |
const depModule = resolveModuleName(moduleSpecifier, sourceFile.fileName, compilerOptions, host) | |
if (depModule.resolvedModule) { | |
let depModuleFileName = depModule.resolvedModule.resolvedFileName | |
references.push(depModuleFileName) | |
} | |
} | |
} | |
} | |
let fileName = sourceFile.fileName | |
if(fileName.startsWith(tscCwd)){ | |
fileName = fileName.slice(tscCwd.length + 1) | |
} | |
map[fileName] = { | |
references, | |
visited: false, | |
distance: references.length === 0 ? 0 : -Infinity, | |
} | |
} | |
program | |
.getSourceFiles() | |
.filter(sourceFile => { | |
return !sourceFile.fileName.includes("/node_modules/") | |
}) | |
.forEach(checkFile) | |
function markDistance(file: string) { | |
if(!map[file]){ | |
return 0 | |
} | |
if(map[file].visited){ | |
return -Infinity | |
} | |
if (map[file].distance === -Infinity) { | |
map[file].visited = true | |
let maxDistance = Math.max(...map[file].references.map(markDistance)) | |
if(maxDistance === -Infinity){ | |
maxDistance = -1 | |
} | |
map[file].distance = maxDistance + 1 | |
} | |
return map[file].distance | |
} | |
markDistance(entry) | |
const readList = {} | |
for (let filename in map) { | |
const distance = map[filename].distance | |
readList[distance] = readList[distance] || [] | |
readList[distance].push(filename) | |
} | |
writeFileSync(path.join(cwd, "readlist.json"), JSON.stringify(readList, null, "\t")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment