Skip to content

Commit e0a5aaa

Browse files
committed
feat: update action result
1 parent 6d77cba commit e0a5aaa

File tree

11 files changed

+127
-96
lines changed

11 files changed

+127
-96
lines changed

src/actions/action-result.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
11
enum ResultType {
2-
Success,
3-
Warning,
4-
Error
2+
Success= 0,
3+
Warning = 2,
4+
Cancel = 130,
5+
Failure= 1
56
}
67

78
export class ActionResult {
8-
private readonly message: string | undefined;
9+
private readonly message: string;
910
private readonly resultType: ResultType;
1011

11-
private constructor(resultType: ResultType, message?: string) {
12+
private constructor(resultType: ResultType, message: string) {
1213
this.resultType = resultType;
1314
this.message = message;
1415
}
1516

16-
static error(message: string) {
17-
return new ActionResult(ResultType.Error, message);
17+
static success() {
18+
return new ActionResult(ResultType.Success, "Succeeded");
1819
}
1920

20-
static error2() {
21-
return new ActionResult(ResultType.Error);
21+
static failed() {
22+
return new ActionResult(ResultType.Failure, "Failed");
2223
}
2324

24-
static warning(message: string) {
25-
return new ActionResult(ResultType.Warning, message);
25+
static cancelled() {
26+
return new ActionResult(ResultType.Cancel, "Cancelled");
2627
}
2728

28-
static success() {
29-
return new ActionResult(ResultType.Success);
29+
static warning() {
30+
return new ActionResult(ResultType.Warning, "Warning");
3031
}
3132

32-
map(onError: (message: string) => void, onWarning?: (message: string) => void) {
33-
if (this.resultType === ResultType.Error) {
34-
onError(this.message!);
35-
} else if (this.resultType === ResultType.Warning && onWarning && this.message) {
36-
onWarning(this.message);
37-
}
33+
public getMessage() {
34+
return this.message;
3835
}
3936

40-
mapAll<T>(onSuccess: () => T, onWarning: (message: string) => T, onError: (message: string) => T) {
37+
public getExitCode() {
38+
return this.resultType.valueOf();
39+
}
40+
41+
public mapAll<T>(onSuccess: () => T, onFailure: () => T, onCancel: () => T, onWarning: () => T): T {
4142
switch (this.resultType) {
42-
case ResultType.Error:
43-
return onError(this.message!);
44-
case ResultType.Warning:
45-
return onWarning(this.message!);
46-
default:
43+
case ResultType.Success:
4744
return onSuccess();
45+
case ResultType.Failure:
46+
return onFailure();
47+
case ResultType.Cancel:
48+
return onCancel();
49+
case ResultType.Warning:
50+
return onWarning();
4851
}
4952
}
5053
}

src/actions/portal/copilot.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { FilePath } from "../../types/file/filePath.js";
99
import { FileName } from "../../types/file/fileName.js";
1010
import { FileService } from "../../infrastructure/file-service.js";
1111
import { LauncherService } from "../../infrastructure/launcher-service.js";
12+
import { format } from "../../prompts/format.js";
1213

1314
export class CopilotAction {
1415
private readonly apiService = new ApiService();
@@ -26,25 +27,31 @@ export class CopilotAction {
2627
public async execute(buildDirectory: DirectoryPath, force: boolean, enable: boolean): Promise<ActionResult> {
2728
const buildContext = new BuildContext(buildDirectory);
2829

29-
if (!(await buildContext.validate())) {
30-
return ActionResult.error("'src' directory is empty or not valid.");
30+
if (!await buildContext.validate()) {
31+
this.prompts.srcDirectoryEmpty(buildDirectory);
32+
return ActionResult.failed();
3133
}
3234

3335
const buildJson = await buildContext.getBuildFileContents();
3436

35-
if (!force && buildJson.apiCopilotConfig != null && !(await this.prompts.confirmOverwrite()))
36-
return ActionResult.error("Exiting without making any change.");
37+
if (!force && buildJson.apiCopilotConfig != null && !(await this.prompts.confirmOverwrite())) {
38+
this.prompts.cancelled();
39+
return ActionResult.cancelled();
40+
}
3741

3842
const response = await this.prompts.spinnerAccountInfo(
39-
() => this.apiService.getAccountInfo(this.configDir, this.authKey));
43+
this.apiService.getAccountInfo(this.configDir, this.authKey));
4044

4145
if (response.isErr()) {
42-
return ActionResult.error(response._unsafeUnwrapErr());
46+
// TODO: add error prompt response._unsafeUnwrapErr()
47+
return ActionResult.failed();
4348
}
4449

4550
const apiCopilotKey = await this.selectCopilotKey(response._unsafeUnwrap(), force);
4651
if (apiCopilotKey instanceof Error) {
47-
return ActionResult.error(apiCopilotKey.message);
52+
53+
// return ActionResult.error(apiCopilotKey.message);
54+
return ActionResult.failed();
4855
}
4956

5057
const welcomeMessage = await this.getWelcomeMessage();
@@ -59,7 +66,6 @@ export class CopilotAction {
5966

6067
this.prompts.copilotConfigured(enable, apiCopilotKey);
6168

62-
6369
return ActionResult.success();
6470
}
6571

@@ -69,7 +75,7 @@ export class CopilotAction {
6975
subscription.ApiCopilotKeys === undefined ||
7076
subscription.ApiCopilotKeys.length === 0
7177
) {
72-
return new Error("No copilot key found for the current subscription. Please contact support at support@apimatic.io.");
78+
return new Error(`No copilot key found for the current subscription. Please contact support at ${format.var('support@apimatic.io')}.`);
7379
}
7480

7581
if (subscription.ApiCopilotKeys.length === 1) {

src/actions/portal/generate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ export class GenerateAction {
2828
): Promise<ActionResult> => {
2929
if (buildDirectory.isEqual(portalDirectory)) {
3030
this.prompts.directoryCannotBeSame(portalDirectory);
31-
return ActionResult.error2();
31+
return ActionResult.failed();
3232
}
3333

3434
const buildContext = new BuildContext(buildDirectory);
3535
if (!(await buildContext.validate())) {
3636
this.prompts.srcDirectoryEmpty(buildDirectory);
37-
return ActionResult.error2();
37+
return ActionResult.failed();
3838
}
3939

4040
const portalContext = new PortalContext(portalDirectory);
4141
if (!force && (await portalContext.exists()) && !(await this.prompts.overwritePortal(portalDirectory))) {
4242
this.prompts.portalDirectoryNotEmpty();
43-
return ActionResult.error2();
43+
return ActionResult.failed();
4444
}
4545

4646
return await withDirPath(async (tempDirectory) => {
@@ -66,7 +66,7 @@ export class GenerateAction {
6666
await this.launcherService.openFile(reportPath);
6767
this.prompts.portalGenerationErrorWithReport(reportPath);
6868
}
69-
return ActionResult.error2();
69+
return ActionResult.failed();
7070
}
7171
);
7272
});

src/actions/portal/serve.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,42 @@ export class PortalServeAction {
3434
false
3535
);
3636

37-
return result.mapAll<Promise<Result<string, string>>>(
38-
async () => {
37+
if (result.)
38+
39+
return await result.mapAll(async () => {
3940
const setupServerResult = await this.serveHandler.setupServer(paths.destinationDirectoryPath);
40-
if (setupServerResult.isFailed()) {
41-
return Result.failure(setupServerResult.error!);
42-
}
43-
44-
const startServerResult = await this.serveHandler.startServer(paths, flags, generatePortal);
45-
if (startServerResult.isFailed()) {
46-
return Result.failure(startServerResult.error!);
47-
}
48-
49-
return Result.success(`Portal was successfully served.`);
50-
},
51-
async (message) => Result.failure(message),
52-
async (message) => Result.failure(message)
53-
);
41+
if (setupServerResult.isFailed()) {
42+
return Result.failure(setupServerResult.error!);
43+
}
44+
45+
const startServerResult = await this.serveHandler.startServer(paths, flags, generatePortal);
46+
if (startServerResult.isFailed()) {
47+
return Result.failure(startServerResult.error!);
48+
}
49+
50+
return Result.success(`Portal was successfully served.`);
51+
},
52+
async () => Result.failure("Failed to generate portal."),
53+
async () => Result.failure("Failed to start server."),
54+
async () => Result.failure("Failed to serve portal."));
55+
56+
57+
// return result.mapAll<Promise<Result<string, string>>>(
58+
// async () => {
59+
// const setupServerResult = await this.serveHandler.setupServer(paths.destinationDirectoryPath);
60+
// if (setupServerResult.isFailed()) {
61+
// return Result.failure(setupServerResult.error!);
62+
// }
63+
//
64+
// const startServerResult = await this.serveHandler.startServer(paths, flags, generatePortal);
65+
// if (startServerResult.isFailed()) {
66+
// return Result.failure(startServerResult.error!);
67+
// }
68+
//
69+
// return Result.success(`Portal was successfully served.`);
70+
// },
71+
// async (message) => Result.failure(message),
72+
// async (message) => Result.failure(message)
73+
// );
5474
}
5575
}

src/actions/sdk/generate.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ export class GenerateAction {
3333
zipSdk: boolean
3434
): Promise<ActionResult> => {
3535
if (specDirectory.isEqual(sdkDirectory)) {
36-
return ActionResult.error(`The spec directory and sdk directory cannot be the same: "${specDirectory}"`);
36+
// return ActionResult.error(`The spec directory and sdk directory cannot be the same: "${specDirectory}"`);
37+
return ActionResult.failed();
3738
}
3839

3940
const specContext = new SpecContext(specDirectory);
4041
if (!(await specContext.validate())) {
41-
return ActionResult.error(`The spec directory is either empty or invalid: "${specDirectory}"`);
42+
// return ActionResult.error(`The spec directory is either empty or invalid: "${specDirectory}"`);
43+
return ActionResult.failed();
4244
}
4345

4446
const sdkContext = new SdkContext(sdkDirectory, platform);
4547
if (!force && (await sdkContext.exists()) && !(await this.prompts.overwriteSdk(sdkDirectory))) {
46-
return ActionResult.error(
47-
"Please enter a different destination folder or remove the existing files and try again."
48-
);
48+
// return ActionResult.error(
49+
// "Please enter a different destination folder or remove the existing files and try again."
50+
// );
51+
return ActionResult.failed();
4952
}
5053

5154
return await withDirPath(async (tempDirectory) => {
@@ -59,7 +62,8 @@ export class GenerateAction {
5962

6063
if (!response.isSuccess()) {
6164
this.prompts.displaySdkGenerationErrorMessage();
62-
return ActionResult.error(response.error!);
65+
// return ActionResult.error(response.error!);
66+
return ActionResult.failed();
6367
}
6468

6569
const tempSdkFilePath = new FilePath(tempDirectory, new FileName("sdk.zip"));

src/application/portal/serve/portal-watcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export class PortalWatcher {
9898
true,
9999
false
100100
);
101-
result.map((error) => console.log(error));
101+
102+
//result.map((error) => console.log(error));
102103
}
103104
}

src/commands/portal/copilot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DirectoryPath } from "../../types/file/directoryPath.js";
33
import { PortalCopilotPrompts } from "../../prompts/portal/copilot.js";
44
import { FlagsProvider } from "../../types/flags-provider.js";
55
import { CopilotAction } from "../../actions/portal/copilot.js";
6+
import { outro } from "../../prompts/format.js";
67

78
const DEFAULT_WORKING_DIRECTORY = "./";
89

@@ -38,6 +39,5 @@ export default class PortalCopilotEnable extends Command {
3839
const buildDirectory = input ? new DirectoryPath(input, "src") : workingDirectory.join("src");
3940
const copilotConfigAction = new CopilotAction(new DirectoryPath(this.config.configDir), authKey);
4041
const result = await copilotConfigAction.execute(buildDirectory, force, !disable);
41-
result.map((message) => this.prompts.logError(message));
42-
}
42+
outro(result);}
4343
}

src/commands/portal/generate.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ export class PortalGenerate extends Command {
4444
intro('Generate Portal');
4545
const action = new GenerateAction(this.getConfigDir(), authKey);
4646
const result = await action.execute(buildDirectory, portalDirectory, force, zipPortal);
47-
const exitCode = result.mapAll(() => 0, () => 1, ()=> 2);
48-
//outro(exitCode);
49-
this.error("some error" + exitCode);
47+
outro(result);
5048
}
5149

5250

src/commands/sdk/generate.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FlagsProvider } from "../../types/flags-provider.js";
44
import { SdkGeneratePrompts } from "../../prompts/sdk/generate.js";
55
import { GenerateAction } from "../../actions/sdk/generate.js";
66
import { LanguagePlatform } from "../../types/sdk/generate.js";
7+
import { outro } from "../../prompts/format.js";
78

89
const DEFAULT_WORKING_DIRECTORY = "./";
910

@@ -50,11 +51,7 @@ export default class SdkGenerate extends Command {
5051

5152
const action = new GenerateAction(this.getConfigDir(), authKey);
5253
const result = await action.execute(specDirectory, sdkDirectory, platform as LanguagePlatform, force, zipSdk);
53-
result.mapAll(
54-
() => this.prompts.displayOutroMessage(sdkDirectory),
55-
(message) => this.prompts.logError(message),
56-
(message) => this.prompts.logError(message)
57-
);
54+
outro(result);
5855
}
5956

6057
private getConfigDir = () => {

src/prompts/format.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pc from "picocolors";
22
import { Result } from "neverthrow";
33
import { intro as i, outro as o, spinner } from '@clack/prompts';
4+
import { ActionResult } from "../actions/action-result.js";
45

56

67
export const format = {
@@ -24,10 +25,15 @@ export function intro(text: string) {
2425
i(format.intro(` ${text} `));
2526
}
2627

27-
export function outro(exitCode: number) {
28-
const message =
29-
exitCode === 0 ? format.outroSuccess(' success ') : exitCode === 2 ? format.outroFailure(' failure ') : format.outroWarning(' warning ');
30-
o(message);
28+
export function outro(result: ActionResult) {
29+
30+
const exitCode = result.getExitCode();
31+
const message = result.getMessage();
32+
const outroMessage = result.mapAll(() => format.outroSuccess(message),
33+
() => format.outroFailure(message),
34+
() => format.outroFailure(message),
35+
() => format.outroWarning(message));
36+
o(outroMessage);
3137
process.exitCode = exitCode;
3238
}
3339

0 commit comments

Comments
 (0)