task-script-support-cli 0.2.12 → 0.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  To get started, first run `npm install`, then:
4
4
 
5
5
  ```
6
- npm start -- --help
6
+ npm start
7
7
  ```
8
8
 
9
9
  ### Building the project
@@ -0,0 +1,8 @@
1
+ import { singleton } from "tsyringe";
2
+ import { Command } from "../wrappers/command";
3
+ import ShowHelp from "../tasks/show-help";
4
+
5
+ @singleton()
6
+ export class HelpCommand extends Command {
7
+ tasks = [ShowHelp];
8
+ }
@@ -12,11 +12,18 @@ initializeInjectables();
12
12
 
13
13
  import { UtilService } from "./services/util-service";
14
14
  import { VerifyCommand } from "./commands/verify";
15
+ import { HelpCommand } from "./commands/help-command";
15
16
 
16
17
  const name = UtilService.getAppName();
18
+ const yargsInstance = yargs(hideBin(process.argv));
19
+ container.registerInstance("yargs", yargsInstance);
17
20
 
18
- yargs(hideBin(process.argv))
21
+ yargsInstance
19
22
  .usage(`${UtilService.titleizeAll(name)} CLI Client`)
23
+ .command({
24
+ command: "$0",
25
+ handler: container.resolve(HelpCommand).handler,
26
+ })
20
27
  .command(
21
28
  "verify",
22
29
  "check the app is working",
@@ -0,0 +1,19 @@
1
+ import { AppTask } from "../wrappers/app-task";
2
+ import { autoInjectable, inject } from "tsyringe";
3
+ import type { Yargs } from "../types/state";
4
+
5
+ /**
6
+ * Show the yargs help menu
7
+ */
8
+ @autoInjectable()
9
+ export default class ShowHelp extends AppTask {
10
+ loggerName = "ShowHelp";
11
+
12
+ constructor(@inject("yargs") private yargs: Yargs) {
13
+ super();
14
+ }
15
+
16
+ async run() {
17
+ this.yargs.showHelp();
18
+ }
19
+ }
@@ -1,4 +1,5 @@
1
1
  import { AppState as State, TaskClass } from "task-script-support";
2
+ import yargs from "yargs";
2
3
 
3
4
  // Add app data here as needed. Use readonly for immutable fields.
4
5
  //
@@ -31,3 +32,5 @@ export const EnvironmentConfigKeys = {
31
32
  };
32
33
 
33
34
  export type AppTaskClass = TaskClass<AppStateData, CLIArgs>;
35
+
36
+ export type Yargs = typeof yargs;
@@ -1,30 +1,77 @@
1
- import { describe, it, expect, vi } from "vitest";
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
2
  import CheckEnv from "../../src/tasks/check-env";
3
3
  import { LogService } from "../../src/services/log-service";
4
4
  import { CommandService } from "task-script-support";
5
5
  import { AppStateData, CLIArgs } from "../../src/types/state";
6
+ import { container } from "tsyringe";
6
7
 
7
8
  describe("CheckEnv", () => {
8
- it("should set environmentValidated to true", async () => {
9
- const mockLogService = {
9
+ let mockLogService = {} as LogService;
10
+ let mockCommandService = {} as CommandService<AppStateData, CLIArgs>;
11
+
12
+ beforeEach(() => {
13
+ container.reset();
14
+
15
+ mockLogService = {
10
16
  info: vi.fn(),
11
17
  debug: vi.fn(),
18
+ error: vi.fn(),
12
19
  } as unknown as LogService;
13
- const mockCommandService = {
20
+
21
+ mockCommandService = {
14
22
  setData: vi.fn((state, data) => ({
15
23
  ...state,
16
24
  data: { ...state.data, ...data },
17
25
  })),
18
26
  } as unknown as CommandService<AppStateData, CLIArgs>;
19
27
 
20
- const checkEnv = Object.create(CheckEnv.prototype);
21
- checkEnv._commandService = mockCommandService;
22
- checkEnv._logService = mockLogService;
23
- checkEnv.state = { id: "test", args: [], data: {} };
28
+ container.registerInstance(LogService, mockLogService);
29
+ container.registerInstance(
30
+ CommandService<AppStateData, CLIArgs>,
31
+ mockCommandService,
32
+ );
33
+ });
34
+
35
+ it("should set environmentValidated to true", async () => {
36
+ const checkEnv = container.resolve(CheckEnv);
37
+
24
38
  await checkEnv.run();
25
- expect(mockLogService.info).toHaveBeenCalledWith(
39
+ expect(mockLogService.debug).toHaveBeenCalledWith(
26
40
  "Running Check Environment",
27
41
  );
28
42
  expect(checkEnv.state.data.environmentValidated).toBe(true);
29
43
  });
44
+
45
+ it("should call exit on missing required environment variable", async () => {
46
+ const testError = new Error("process.exit called");
47
+ const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
48
+ throw testError;
49
+ });
50
+ const checkEnv = container.resolve(CheckEnv);
51
+ checkEnv.requiredEnv = ["MISSING_ENV_VAR"];
52
+ try {
53
+ expect(await checkEnv.run()).toThrow(testError);
54
+ } catch (err) {
55
+ expect(`${err}`).toContain("process.exit");
56
+ expect(err).toBe(testError);
57
+ }
58
+ expect(mockLogService.error).toHaveBeenCalled();
59
+ mockExit.mockRestore();
60
+ });
61
+
62
+ it("should group environment issues into single error log", async () => {
63
+ const testError = new Error("process.exit called");
64
+ const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
65
+ throw testError;
66
+ });
67
+ const checkEnv = container.resolve(CheckEnv);
68
+ checkEnv.requiredEnv = ["MISSING_ENV_1", "MISSING_ENV_2", "MISSING_ENV_3"];
69
+ try {
70
+ await checkEnv.run();
71
+ } catch (err) {
72
+ expect(err).toBe(testError);
73
+ }
74
+ expect(mockLogService.error).toHaveBeenCalledOnce();
75
+ mockExit.mockRestore();
76
+ });
30
77
  });
@@ -3,7 +3,7 @@
3
3
  To get started, first run `npm install`, then:
4
4
 
5
5
  ```
6
- npm start -- --help
6
+ npm start
7
7
  ```
8
8
 
9
9
  ### Building the project
@@ -0,0 +1,8 @@
1
+ import { singleton } from "tsyringe";
2
+ import { Command } from "../wrappers/command";
3
+ import ShowHelp from "../tasks/show-help";
4
+
5
+ @singleton()
6
+ export class HelpCommand extends Command {
7
+ tasks = [ShowHelp];
8
+ }
@@ -12,11 +12,18 @@ initializeInjectables();
12
12
 
13
13
  import { UtilService } from "./services/util-service";
14
14
  import { VerifyCommand } from "./commands/verify";
15
+ import { HelpCommand } from "./commands/help-command";
15
16
 
16
17
  const name = UtilService.getAppName();
18
+ const yargsInstance = yargs(hideBin(process.argv));
19
+ container.registerInstance("yargs", yargsInstance);
17
20
 
18
- yargs(hideBin(process.argv))
21
+ yargsInstance
19
22
  .usage(`${UtilService.titleizeAll(name)} CLI Client`)
23
+ .command({
24
+ command: "$0",
25
+ handler: container.resolve(HelpCommand).handler,
26
+ })
20
27
  .command(
21
28
  "verify",
22
29
  "check the app is working",
@@ -0,0 +1,19 @@
1
+ import { AppTask } from "../wrappers/app-task";
2
+ import { autoInjectable, inject } from "tsyringe";
3
+ import type { Yargs } from "../types/state";
4
+
5
+ /**
6
+ * Show the yargs help menu
7
+ */
8
+ @autoInjectable()
9
+ export default class ShowHelp extends AppTask {
10
+ loggerName = "ShowHelp";
11
+
12
+ constructor(@inject("yargs") private yargs: Yargs) {
13
+ super();
14
+ }
15
+
16
+ async run() {
17
+ this.yargs.showHelp();
18
+ }
19
+ }
@@ -1,4 +1,5 @@
1
1
  import { AppState as State, TaskClass } from "task-script-support";
2
+ import yargs from "yargs";
2
3
 
3
4
  // Add app data here as needed. Use readonly for immutable fields.
4
5
  //
@@ -31,3 +32,5 @@ export const EnvironmentConfigKeys = {
31
32
  };
32
33
 
33
34
  export type AppTaskClass = TaskClass<AppStateData, CLIArgs>;
35
+
36
+ export type Yargs = typeof yargs;
@@ -1,30 +1,77 @@
1
- import { describe, it, expect, vi } from "vitest";
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
2
  import CheckEnv from "../../src/tasks/check-env";
3
3
  import { LogService } from "../../src/services/log-service";
4
4
  import { CommandService } from "task-script-support";
5
5
  import { AppStateData, CLIArgs } from "../../src/types/state";
6
+ import { container } from "tsyringe";
6
7
 
7
8
  describe("CheckEnv", () => {
8
- it("should set environmentValidated to true", async () => {
9
- const mockLogService = {
9
+ let mockLogService = {} as LogService;
10
+ let mockCommandService = {} as CommandService<AppStateData, CLIArgs>;
11
+
12
+ beforeEach(() => {
13
+ container.reset();
14
+
15
+ mockLogService = {
10
16
  info: vi.fn(),
11
17
  debug: vi.fn(),
18
+ error: vi.fn(),
12
19
  } as unknown as LogService;
13
- const mockCommandService = {
20
+
21
+ mockCommandService = {
14
22
  setData: vi.fn((state, data) => ({
15
23
  ...state,
16
24
  data: { ...state.data, ...data },
17
25
  })),
18
26
  } as unknown as CommandService<AppStateData, CLIArgs>;
19
27
 
20
- const checkEnv = Object.create(CheckEnv.prototype);
21
- checkEnv._commandService = mockCommandService;
22
- checkEnv._logService = mockLogService;
23
- checkEnv.state = { id: "test", args: [], data: {} };
28
+ container.registerInstance(LogService, mockLogService);
29
+ container.registerInstance(
30
+ CommandService<AppStateData, CLIArgs>,
31
+ mockCommandService,
32
+ );
33
+ });
34
+
35
+ it("should set environmentValidated to true", async () => {
36
+ const checkEnv = container.resolve(CheckEnv);
37
+
24
38
  await checkEnv.run();
25
- expect(mockLogService.info).toHaveBeenCalledWith(
39
+ expect(mockLogService.debug).toHaveBeenCalledWith(
26
40
  "Running Check Environment",
27
41
  );
28
42
  expect(checkEnv.state.data.environmentValidated).toBe(true);
29
43
  });
44
+
45
+ it("should call exit on missing required environment variable", async () => {
46
+ const testError = new Error("process.exit called");
47
+ const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
48
+ throw testError;
49
+ });
50
+ const checkEnv = container.resolve(CheckEnv);
51
+ checkEnv.requiredEnv = ["MISSING_ENV_VAR"];
52
+ try {
53
+ expect(await checkEnv.run()).toThrow(testError);
54
+ } catch (err) {
55
+ expect(`${err}`).toContain("process.exit");
56
+ expect(err).toBe(testError);
57
+ }
58
+ expect(mockLogService.error).toHaveBeenCalled();
59
+ mockExit.mockRestore();
60
+ });
61
+
62
+ it("should group environment issues into single error log", async () => {
63
+ const testError = new Error("process.exit called");
64
+ const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
65
+ throw testError;
66
+ });
67
+ const checkEnv = container.resolve(CheckEnv);
68
+ checkEnv.requiredEnv = ["MISSING_ENV_1", "MISSING_ENV_2", "MISSING_ENV_3"];
69
+ try {
70
+ await checkEnv.run();
71
+ } catch (err) {
72
+ expect(err).toBe(testError);
73
+ }
74
+ expect(mockLogService.error).toHaveBeenCalledOnce();
75
+ mockExit.mockRestore();
76
+ });
30
77
  });
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "task-script-support-cli",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "main": "index.js",
5
5
  "type": "commonjs",
6
6
  "preferGlobal": true,
@@ -19,7 +19,7 @@ const util_service_1 = require("../../services/util-service");
19
19
  const file_service_1 = require("../../services/file-service");
20
20
  const newProjectMessage = (dest) => `
21
21
  Try:
22
- cd ${dest} && npm i && npm start -- help
22
+ cd ${dest} && npm i && npm start
23
23
  `;
24
24
  /**
25
25
  * Prints the generated output results
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "task-script-support-cli",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "main": "index.js",
5
5
  "type": "commonjs",
6
6
  "preferGlobal": true,
@@ -6,7 +6,7 @@ import { FileService } from "../../services/file-service";
6
6
 
7
7
  const newProjectMessage = (dest: string) => `
8
8
  Try:
9
- cd ${dest} && npm i && npm start -- help
9
+ cd ${dest} && npm i && npm start
10
10
  `;
11
11
 
12
12
  /**