• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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
16import * as fs from 'node:fs';
17
18import type { OptionValues } from 'commander';
19import { Command } from 'commander';
20
21import { CLI } from './CLI';
22
23export interface DeclgenCLIOptions {
24  outDir?: string;
25  inputFiles: string[];
26  tsconfig?: string;
27  inputDirs: string[];
28}
29
30export class DeclgenCLI extends CLI<DeclgenCLIOptions> {
31  constructor() {
32    super();
33  }
34
35  doRead(): string[] {
36    void this;
37
38    return process.argv;
39  }
40
41  doInit(): Command {
42    void this;
43
44    const cliParser = new Command();
45
46    cliParser.name('declgen').description('Declarations generator for ArkTS.');
47    cliParser.option('-o, --out <outDir>', 'Directory where to put generated declarations.');
48    cliParser.option('-p, --project <tsconfigPath>', 'path to TS project config file');
49    cliParser.option(
50      '-d, --dir <projectDir>',
51      'Directory with TS files to generate declrartions from.',
52      (val, prev) => {
53        return prev.concat(val);
54      },
55      [] as string[]
56    );
57    cliParser.option(
58      '-f, --file <fileName>',
59      'TS file to generate declarations from.',
60      (val, prev) => {
61        return prev.concat(val);
62      },
63      [] as string[]
64    );
65
66    return cliParser;
67  }
68
69  doOptions(opts: OptionValues): DeclgenCLIOptions {
70    void this;
71    return {
72      outDir: opts.out,
73      inputFiles: opts.file,
74      tsconfig: opts.project,
75      inputDirs: opts.dir
76    };
77  }
78
79  doValidate(opts: DeclgenCLIOptions): Error | undefined {
80    void this;
81
82    for (const entity of [...opts.inputDirs, ...opts.inputFiles]) {
83      if (!fs.existsSync(entity)) {
84        return new Error(`No entity <${entity}> exists!`);
85      }
86    }
87
88    if (opts.tsconfig && !fs.existsSync(opts.tsconfig)) {
89      return new Error(`Invalid tsconfig path! No file <${opts.tsconfig}> exists!`);
90    }
91
92    return undefined;
93  }
94}
95