• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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
16import fs from 'fs';
17import ts from 'typescript';
18
19import { type BasicApiInfo } from './typedef';
20import { NodeProcessorHelper } from './coreImpl';
21
22export function parse(inputFilePath: string, outputFilePath: string): void {
23  const fileContent: string = fs.readFileSync(inputFilePath, 'utf-8').toString();
24  const sourceFile: ts.SourceFile = ts.createSourceFile('', fileContent, ts.ScriptTarget.ES2017, true);
25  const parseResult: BasicApiInfo[] = [];
26  sourceFile.forEachChild((cNode: ts.Node) => {
27    const apiInfos: BasicApiInfo[] = NodeProcessorHelper.processNode(cNode, sourceFile);
28    parseResult.push(...apiInfos);
29  });
30  const indentSpaces: number = 2;
31  const jsonText: string = JSON.stringify(parseResult, null, indentSpaces);
32  fs.writeFileSync(outputFilePath, jsonText, 'utf-8');
33}