Skip to content

Commit cd56428

Browse files
committed
chore: cleanup
1 parent 83fccb4 commit cd56428

File tree

9 files changed

+130
-56
lines changed

9 files changed

+130
-56
lines changed

lib/commands/start.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { ICommand, ICommandParameter } from "../common/definitions/commands";
2+
import { printHeader } from "../common/header";
23
import { injector } from "../common/yok";
34
import { IStartService } from "../definitions/start-service";
45

56
export class StartCommand implements ICommand {
67
constructor(private $startService: IStartService) {}
78
async execute(args: string[]): Promise<void> {
9+
printHeader();
810
this.$startService.start();
911
return;
1012
}

lib/common/definitions/key-commands.d.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ export type IKeysLowerCase =
2727
| "y"
2828
| "z";
2929

30-
export type IValidKeyCommands = IKeysLowerCase | `${Uppercase<IKeysLowerCase>}`;
30+
export type IKeysUpperCase = Uppercase<IKeysLowerCase>;
31+
32+
export const enum SpecialKeys {
33+
CtrlC = "\u0003",
34+
QuestionMark = "?",
35+
}
36+
37+
export type IKeysSpecial = `${SpecialKeys}`;
38+
39+
export type IValidKeyName = IKeysLowerCase | IKeysUpperCase | IKeysSpecial;
3140

3241
export interface IKeyCommandHelper {
3342
attachKeyCommands: (
3443
platform: IKeyCommandPlatform,
3544
processType: SupportedProcessType
3645
) => void;
3746

38-
addOverride(key: IValidKeyCommands, execute: () => Promise<boolean>);
39-
removeOverride(key: IValidKeyCommands);
47+
addOverride(key: IValidKeyName, execute: () => Promise<boolean>);
48+
removeOverride(key: IValidKeyName);
4049
printCommands(platform: IKeyCommandPlatform): void;
4150
}
4251

4352
export type SupportedProcessType = "start" | "run";
4453

4554
export interface IKeyCommand {
46-
key: IValidKeyCommands;
55+
key: IValidKeyName;
4756
platform: IKeyCommandPlatform;
4857
description: string;
4958
willBlockKeyCommandExecution?: boolean;

lib/common/definitions/yok.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IDisposable, IDictionary } from "../declarations";
22
import { ICommand } from "./commands";
3-
import { IKeyCommand, IValidKeyCommands } from "./key-commands";
3+
import { IKeyCommand, IValidKeyName } from "./key-commands";
44

55
interface IInjector extends IDisposable {
66
require(name: string, file: string): void;
@@ -9,7 +9,7 @@ interface IInjector extends IDisposable {
99
requirePublicClass(names: string | string[], file: string): void;
1010
requireCommand(name: string, file: string): void;
1111
requireCommand(names: string[], file: string): void;
12-
requireKeyCommand(name: string, file: string): void;
12+
requireKeyCommand(name: IValidKeyName, file: string): void;
1313
/**
1414
* Resolves an implementation by constructor function.
1515
* The injector will create new instances for every call.
@@ -28,7 +28,7 @@ interface IInjector extends IDisposable {
2828
register(name: string, resolver: any, shared?: boolean): void;
2929
registerCommand(name: string, resolver: any): void;
3030
registerCommand(names: string[], resolver: any): void;
31-
registerKeyCommand(key: IValidKeyCommands, resolver: any): void;
31+
registerKeyCommand(key: IValidKeyName, resolver: any): void;
3232
getRegisteredCommandsNames(includeDev: boolean): string[];
3333
getRegisteredKeyCommandsNames(): string[];
3434
dynamicCallRegex: RegExp;

lib/common/header.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
import { color, stripColors } from "../color";
2+
import { IStaticConfig } from "../declarations";
3+
import { injector } from "./yok";
24

35
export function printHeader() {
46
if (process.env.HIDE_HEADER) return;
5-
const version = "8.5.3";
6-
const middle = [
7-
color.dim("│ "),
8-
color.cyanBright.bold(" {N} NativeScript "),
7+
8+
const $staticConfig: IStaticConfig = injector.resolve("$staticConfig");
9+
const version = $staticConfig.version;
10+
11+
const header = [
12+
color.dim("│ "),
13+
color.cyanBright.bold("{N} NativeScript "),
914
color.whiteBright.bold("CLI"),
1015
color.dim(` [v${version}] `),
11-
color.dim(" │"),
16+
// color.dim(" │"),
1217
].join("");
13-
const middle2 = [
18+
const tagLine = [
1419
color.dim("│ "),
15-
color.whiteBright.bold(" Empower JavaScript with native APIs "),
16-
color.dim(" │"),
20+
color.dim(" → "),
21+
color.whiteBright.bold("Empower JavaScript with native APIs "),
22+
// color.dim(" │"),
1723
].join("");
1824

19-
const end = [color.dim("─┘")].join("");
25+
const headerLength = stripColors(header).length;
26+
const tagLineLength = stripColors(tagLine).length;
27+
const width = Math.max(headerLength, tagLineLength);
2028

21-
const width = stripColors(middle).length;
22-
const endWidth = stripColors(end).length;
23-
console.info(" ");
24-
console.info(" " + color.dim("┌" + "─".repeat(width - 2) + "┐"));
25-
console.info(" " + middle);
26-
console.info(" " + middle2);
27-
console.info(" " + color.dim("└" + "─".repeat(width - endWidth - 1)) + end);
29+
console.info(" " + color.dim("┌" + "─".repeat(width - 1) + "┐"));
30+
console.info(
31+
" " + header + " ".repeat(width - headerLength) + color.dim("│")
32+
);
33+
console.info(
34+
" " + tagLine + " ".repeat(width - tagLineLength) + color.dim("│")
35+
);
36+
console.info(" " + color.dim("└" + "─".repeat(width - 1) + "┘"));
2837
}

lib/common/services/commands-service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
ICommand,
2020
ISimilarCommand,
2121
} from "../definitions/commands";
22-
import { printHeader } from "../header";
2322

2423
class CommandArgumentsValidationHelper {
2524
constructor(public isValid: boolean, _remainingArguments: string[]) {
@@ -183,7 +182,6 @@ export class CommandsService implements ICommandsService {
183182
: canExecuteResult;
184183

185184
if (canExecute) {
186-
printHeader();
187185
await this.executeCommandAction(
188186
commandName,
189187
commandArguments,

lib/helpers/key-command-helper.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { color } from "../color";
1+
import { color, stripColors } from "../color";
22
import {
33
IKeyCommandHelper,
44
IKeyCommandPlatform,
5-
IValidKeyCommands,
5+
IValidKeyName,
6+
SpecialKeys,
67
SupportedProcessType,
78
} from "../common/definitions/key-commands";
89
import { injector } from "../common/yok";
@@ -13,25 +14,25 @@ export default class KeyCommandHelper implements IKeyCommandHelper {
1314
private processType: SupportedProcessType;
1415
private overrides: { [key: string]: () => Promise<boolean> } = {};
1516

16-
public addOverride(key: IValidKeyCommands, execute: () => Promise<boolean>) {
17+
public addOverride(key: IValidKeyName, execute: () => Promise<boolean>) {
1718
this.overrides[key] = execute;
1819
}
1920

20-
public removeOverride(key: IValidKeyCommands) {
21+
public removeOverride(key: IValidKeyName) {
2122
this.overrides[key] = undefined;
2223
}
2324

2425
private onKeyPressed = async (data: Buffer) => {
2526
const key = data.toString();
2627

2728
// Allow Ctrl + C always.
28-
if (this.keyCommandExecutionBlocked && key !== "\u0003") return;
29+
if (this.keyCommandExecutionBlocked && key !== SpecialKeys.CtrlC) return;
2930

3031
try {
3132
const exists = injector.getRegisteredKeyCommandsNames().includes(key);
3233

3334
if (exists) {
34-
const keyCommand = injector.resolveKeyCommand(key as IValidKeyCommands);
35+
const keyCommand = injector.resolveKeyCommand(key as IValidKeyName);
3536

3637
if (
3738
keyCommand.platform === "all" ||
@@ -57,6 +58,16 @@ export default class KeyCommandHelper implements IKeyCommandHelper {
5758
}
5859
}
5960

61+
if (keyCommand.key !== SpecialKeys.CtrlC) {
62+
const line = ` ${color.dim("→")} ${color.bold(keyCommand.key)}${
63+
keyCommand.description
64+
}`;
65+
const lineLength = stripColors(line).length - 1;
66+
console.log(color.dim(` ┌${"─".repeat(lineLength)}┐`));
67+
console.log(line + color.dim(" │"));
68+
console.log(color.dim(` └${"─".repeat(lineLength)}┘`));
69+
console.log("");
70+
}
6071
const result = await keyCommand.execute(this.platform);
6172
this.keyCommandExecutionBlocked = false;
6273

@@ -78,7 +89,7 @@ export default class KeyCommandHelper implements IKeyCommandHelper {
7889
public printCommands(platform: IKeyCommandPlatform) {
7990
const commands = injector.getRegisteredKeyCommandsNames();
8091
const commandHelp = commands.reduce((arr, key) => {
81-
const command = injector.resolveKeyCommand(key as IValidKeyCommands);
92+
const command = injector.resolveKeyCommand(key as IValidKeyName);
8293

8394
if (
8495
!command.description ||
@@ -102,6 +113,7 @@ export default class KeyCommandHelper implements IKeyCommandHelper {
102113
)}, you can press the following keys any time (make sure the terminal has focus).`,
103114
"",
104115
...commandHelp,
116+
"",
105117
].join("\n")
106118
);
107119
}

lib/key-commands/bootstrap.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SpecialKeys } from "../common/definitions/key-commands";
12
import { injector } from "../common/yok";
23

34
const path = "./key-commands/index";
@@ -11,4 +12,6 @@ injector.requireKeyCommand("R", path);
1112
injector.requireKeyCommand("w", path);
1213
injector.requireKeyCommand("c", path);
1314
injector.requireKeyCommand("n", path);
14-
injector.requireKeyCommand("\u0003" as any, path);
15+
16+
injector.requireKeyCommand(SpecialKeys.QuestionMark, path);
17+
injector.requireKeyCommand(SpecialKeys.CtrlC, path);

0 commit comments

Comments
 (0)