• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gturi / nca / 8318773424

17 Mar 2024 10:35PM UTC coverage: 86.56% (+0.3%) from 86.271%
8318773424

push

github

gturi
add missing fields

236 of 334 branches covered (70.66%)

Branch coverage included in aggregate %.

1168 of 1288 relevant lines covered (90.68%)

1295.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.7
/src/mapper/completion-loader.ts
1
import yargs, { Arguments, CompletionCallback } from "yargs";
2
import { NcaCommand } from "../model/api/nca-command";
3
import { Completion } from "../model/api/completion";
4
import { LoggingUtil } from "../util/logging-utils";
172✔
5
import { ArrayUtils } from "../util/array-utils";
172✔
6
import { PathUtils } from "../util/path-utils";
172✔
7
import { CompletionInput } from "../model/input/completion-input";
8
import { NcaConfig } from "../config/nca-config";
172✔
9
import { iter } from 'iterator-helper';
172✔
10
import { InputBuilder } from "./input-builder";
172✔
11

12
/* eslint-disable @typescript-eslint/no-explicit-any */
13
type CompletionFilter = (onCompleted?: CompletionCallback) => any;
14
type Done = (completions: string[]) => any;
15
/* eslint-enable @typescript-eslint/no-explicit-any */
16
type CurrentNcaCommand = {
17
  ncaCommand: NcaCommand | undefined;
18
  subCommands: NcaCommand[] | undefined;
19
};
20

21
export class CompletionLoader {
172✔
22

23
  private readonly ncaCommands: NcaCommand[];
24
  private readonly current: string;
25
  private readonly argv: Arguments;
26
  private readonly completionFilter: CompletionFilter;
27
  private readonly done: Done;
28

29
  protected constructor(
30
    ncaCommands: NcaCommand[],
31
    current: string,
32
    argv: Arguments,
33
    completionFilter: CompletionFilter,
34
    done: Done
35
  ) {
36
    this.ncaCommands = ncaCommands;
26✔
37
    this.current = current;
26✔
38
    this.argv = argv;
26✔
39
    this.completionFilter = completionFilter;
26✔
40
    this.done = done;
26✔
41
  }
42

43
  static initCompletion<T>(argvBuilder: yargs.Argv<T>, ncaCommands: NcaCommand[]) {
44
    argvBuilder.completion(
172✔
45
      'completion',
46
      (current: string, argv: Arguments,
47
        completionFilter: CompletionFilter, done: Done) => {
48
        return new CompletionLoader(ncaCommands, current, argv, completionFilter, done).load();
26✔
49
      }
50
    );
51
  }
52

53
  protected load() {
54
    this.completionFilter((err, defaultCompletions) => {
26✔
55

56
      LoggingUtil.logToFile(
13✔
57
        () => [
×
58
          '------------------',
59
          `current: "${this.current}"`,
60
          `argv: "${JSON.stringify(this.argv)}"`,
61
          `error: "${JSON.stringify(err)}"`,
62
          '------------------',
63
          '',
64
          ''
65
        ].join('\n')
66
      );
67

68
      const ncaCommand = this.getCurrentNcaCommand(this.argv._);
13✔
69

70
      const modulePath = this.getModulePath(ncaCommand);
13✔
71

72
      const completionInput = InputBuilder.getCompletionInput(
13✔
73
        this.current, this.argv, err, defaultCompletions, modulePath
74
      );
75

76
      const completionArray = this.getCompletionArray(ncaCommand?.completion, completionInput)
13✔
77
      this.done(completionArray);
13✔
78
    });
79
  }
80

81
  private getCurrentNcaCommand(commands: (string | number)[]): NcaCommand | null {
82
    const currentNcaCommand: CurrentNcaCommand = {
13✔
83
      ncaCommand: undefined,
84
      subCommands: this.ncaCommands
85
    };
86

87
    iter(commands)
13✔
88
      .map(command => `${command}`)
23✔
89
      .filter(command => !NcaConfig.getForbiddenNames().includes(command))
23✔
90
      // find last + break operation:
91
      // the first command that is not found will terminate the iteration
92
      .every(command => this.existsNcaCommandForName(currentNcaCommand, command));
20✔
93

94
    return currentNcaCommand.ncaCommand ?? null;
13✔
95
  }
96

97
  private existsNcaCommandForName(currentNcaCommand: CurrentNcaCommand, command: string): boolean {
98
    currentNcaCommand.ncaCommand = currentNcaCommand.subCommands?.find(
20✔
99
      ncaCommand => ncaCommand.name === command
143✔
100
    );
101
    currentNcaCommand.subCommands = currentNcaCommand.ncaCommand?.subCommands;
20✔
102
    return currentNcaCommand.ncaCommand !== undefined;
20✔
103
  }
104

105
  private getModulePath(ncaCommand: NcaCommand | null): string | null {
106
    const completionPath = ncaCommand?.completion?.completionPath
13✔
107
    return completionPath
13✔
108
      ? PathUtils.resolvePath(completionPath, ncaCommand.directory)
109
      : null;
110
  }
111

112
  private getCompletionArray(
113
    completion: Completion | undefined, completionInput: CompletionInput
114
  ): string[] {
115
    const defaultCompletions = completionInput.defaultCompletions;
13✔
116

117
    if (!completion) {
13✔
118
      return defaultCompletions;
7✔
119
    }
120

121
    const customCompletionArray = this.getCustomCompletionArray(
6✔
122
      completion, completionInput
123
    );
124

125
    return completion.merge
6✔
126
      ? customCompletionArray.concat(defaultCompletions)
127
      : customCompletionArray;
128
  }
129

130
  private getCustomCompletionArray(
131
    completion: Completion, completionInput: CompletionInput
132
  ): string[] {
133
    return ArrayUtils.concatAll(
6✔
134
      [], completion.completionArray, this.loadCompletionFromPath(completionInput)
135
    );
136
  }
137

138
  private loadCompletionFromPath(completionInput: CompletionInput): string[] | null {
139
    const modulePath = completionInput.modulePath;
6✔
140
    if (!modulePath) {
6✔
141
      return null;
3✔
142
    }
143

144
    /* eslint-disable @typescript-eslint/no-var-requires */
145
    const module = require(modulePath);
3✔
146
    return module(completionInput) as string[];
3✔
147
    /* eslint-enable @typescript-eslint/no-var-requires */
148
  }
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc