idlize

Форк
0
/
idlize.ts 
61 строка · 2.2 Кб
1
/*
2
 * Copyright (c) 2024 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import * as ts from "typescript"
17
import * as fs from "fs"
18
import * as path from "path"
19
import { GenerateOptions, GenericVisitor } from "./options"
20

21
export function generate<T>(
22
    inputDir: string,
23
    inputFile: string | undefined,
24
    outputDir: string,
25
    visitorFactory: (sourceFile: ts.SourceFile, typeChecker: ts.TypeChecker) => GenericVisitor<T>,
26
    options: GenerateOptions<T>
27
): void {
28
    inputDir = path.resolve(inputDir)
29
    // Build a program using the set of root file names in fileNames
30
    let program = ts.createProgram(inputFile ? [
31
        path.join(inputDir, inputFile)
32
    ] : fs.readdirSync(inputDir)
33
        .map(elem => path.join(inputDir, elem)).concat([path.join(__dirname, "../stdlib.d.ts")]), options.compilerOptions)
34

35
    // Get the checker, we will use it to find more about classes
36
    if (outputDir && !fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true })
37

38
    const typeChecker = program.getTypeChecker()
39
    options.onBegin?.(outputDir,typeChecker)
40

41
    // Visit every sourceFile in the program
42
    for (const sourceFile of program.getSourceFiles()) {
43
        if (path.resolve(sourceFile.fileName).indexOf(inputDir) == -1) {
44
            // console.log("Ignore ", path.resolve(sourceFile.fileName) , "wrt", inputDir)
45
            continue
46
        }
47
        if (inputFile && path.basename(sourceFile.fileName) != inputFile) {
48
            continue
49
        }
50

51
        // Walk the tree to search for classes
52
        const visitor = visitorFactory(sourceFile, typeChecker)
53
        const output = visitor.visitWholeFile()
54

55
        options.onSingleFile?.(output, outputDir, sourceFile)
56
    }
57

58
    options.onEnd?.(outputDir)
59

60
    return
61
}
62

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.