Skip to content

Commit

Permalink
feat(cli): enable graceful exit (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianzs authored May 5, 2024
1 parent e4e19cd commit c128904
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-gifts-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@plutolang/cli": patch
---

feat(cli): enable graceful exit
10 changes: 6 additions & 4 deletions apps/cli/src/builder/project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { resolve } from "path";
import { input, select } from "@inquirer/prompts";
import { LanguageType, PlatformType, ProvisionType, config } from "@plutolang/base";

import { createStack } from "./stack";
import { resolve } from "path";
import { handleIquirerError } from "./utils";

export interface CreateProjectArgs {
name?: string;
Expand All @@ -27,14 +29,14 @@ export async function createProject(args: CreateProjectArgs): Promise<config.Pro
value: LanguageType.Python,
},
],
}));
}).catch(handleIquirerError));

args.name =
args.name ??
(await input({
message: "Project name",
default: "hello-pluto",
}));
}).catch(handleIquirerError));

const sta = await createStack({
name: args.stack,
Expand All @@ -43,7 +45,7 @@ export async function createProject(args: CreateProjectArgs): Promise<config.Pro
});

const projectRoot = resolve(args.rootpath ?? `./${args.name}`);
const proj = new config.Project(args.name, projectRoot, args.language);
const proj = new config.Project(args.name!, projectRoot, args.language!);
proj.addStack(sta);
proj.current = sta.name;
return proj;
Expand Down
10 changes: 6 additions & 4 deletions apps/cli/src/builder/stack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { input, select } from "@inquirer/prompts";
import { ProvisionType, PlatformType, config } from "@plutolang/base";

import { handleIquirerError } from "./utils";

export interface CreateStackArgs {
name?: string;
platformType?: PlatformType;
Expand All @@ -13,7 +15,7 @@ export async function createStack(args: CreateStackArgs): Promise<config.Stack>
(await input({
message: "Stack name",
default: "dev",
}));
}).catch(handleIquirerError));

args.platformType =
args.platformType ??
Expand Down Expand Up @@ -43,7 +45,7 @@ export async function createStack(args: CreateStackArgs): Promise<config.Stack>
disabled: "(Coming soon)",
},
],
}));
}).catch(handleIquirerError));

args.provisionType =
args.provisionType ??
Expand All @@ -60,7 +62,7 @@ export async function createStack(args: CreateStackArgs): Promise<config.Stack>
disabled: "(Coming soon)",
},
],
}));
}).catch(handleIquirerError));

return new config.Stack(args.name, args.platformType, args.provisionType);
return new config.Stack(args.name!, args.platformType!, args.provisionType!);
}
5 changes: 5 additions & 0 deletions apps/cli/src/builder/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExitError } from "../errors";

export function handleIquirerError(): any {
throw new ExitError();
}
7 changes: 6 additions & 1 deletion apps/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import fs from "fs";
import path from "path";
import assert from "assert";

import { createProject } from "../builder";
import logger from "../log";
import { dumpProject, isPlutoProject } from "../utils";

import { NewOptions, genInitFiles } from "./new";
import { formatError } from "./utils";

export async function init(opts: NewOptions) {
if (isPlutoProject("./")) {
Expand All @@ -27,7 +31,8 @@ export async function init(opts: NewOptions) {
platformType: opts.platform,
provisionType: opts.provision,
rootpath: "./",
});
}).catch(formatError);
assert(proj, "Failed to create a project.");

genInitFiles("./", proj.language);
const pkgJsonPath = path.join("./", "package.json");
Expand Down
12 changes: 7 additions & 5 deletions apps/cli/src/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from "path";
import fs from "fs";
import assert from "assert";
import * as fs from "fs-extra";
import { ProvisionType, PlatformType, LanguageType } from "@plutolang/base";
import { createProject } from "../builder";
import logger from "../log";
import { dumpProject } from "../utils";
import { ensureDirSync } from "fs-extra";
import { createProject } from "../builder";
import { formatError } from "./utils";

const TEMPLATE_DIR = path.join(__dirname, "../../template");

Expand All @@ -23,7 +24,8 @@ export async function create(opts: NewOptions) {
language: opts.language,
platformType: opts.platform,
provisionType: opts.provision,
});
}).catch(formatError);
assert(proj, "Failed to create a project.");

genInitFiles(proj.name, proj.language);
const pkgJsonPath = path.join(proj.name, "package.json");
Expand All @@ -35,7 +37,7 @@ export async function create(opts: NewOptions) {
}

export function genInitFiles(destdir: string, language: string) {
ensureDirSync(destdir);
fs.ensureDirSync(destdir);

const queue: string[] = [""];
while (queue.length) {
Expand Down
6 changes: 5 additions & 1 deletion apps/cli/src/commands/stack_new.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import assert from "assert";
import { ProvisionType, PlatformType } from "@plutolang/base";
import { createStack } from "../builder";
import logger from "../log";
import { dumpProject, isPlutoProject, loadProject } from "../utils";
import { resolve } from "path";
import { formatError } from "./utils";

interface NewOptions {
name?: string;
Expand All @@ -21,7 +23,9 @@ export async function newStack(opts: NewOptions) {
name: opts.name,
platformType: opts.platform,
provisionType: opts.provision,
});
}).catch(formatError);
assert(sta, "Failed to create a stack.");

proj.addStack(sta);
dumpProject(proj);
logger.info("Created a stack.");
Expand Down
29 changes: 27 additions & 2 deletions apps/cli/src/commands/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "fs";
import path from "path";
import * as yaml from "js-yaml";
import { LanguageType, ProvisionType } from "@plutolang/base";
import { core, LanguageType, ProvisionType } from "@plutolang/base";
import { Architecture } from "@plutolang/base/arch";
import { core } from "@plutolang/base";
import { ExitError } from "../errors";
import { isPlutoProject, loadProject } from "../utils";

/**
Expand Down Expand Up @@ -133,3 +133,28 @@ export function getDefaultEntrypoint(lang: LanguageType): string {
throw new Error(`Invalid language type: ${lang}`);
}
}

export function formatError(e: any) {
if (e instanceof ExitError) {
exitGracefully();
return;
}

if (e instanceof Error) {
console.error(e.message);
} else {
console.error(e);
}

if (process.env.DEBUG) {
console.error(e);
}
}

export function exitGracefully(sig?: string) {
if (process.env.DEBUG) {
console.warn(`\nReceived ${sig}. Exiting...`);
}
console.log("Bye~ 👋");
process.exit(1);
}
6 changes: 6 additions & 0 deletions apps/cli/src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class ExitError extends Error {
constructor(msg?: string) {
super(msg);
this.name = "ExitError";
}
}
11 changes: 11 additions & 0 deletions apps/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import * as cmd from "./commands";
import { checkUpdate, version } from "./utils";
import logger from "./log";

function exitGracefully(sig: string) {
if (process.env.DEBUG) {
console.warn(`\nReceived ${sig}. Exiting...`);
}
console.log("\nBye~ 👋");
process.exit(1);
}

process.on("SIGINT", () => exitGracefully("SIGINT"));
process.on("SIGTERM", () => exitGracefully("SIGTERM"));

async function main() {
checkUpdate();

Expand Down

0 comments on commit c128904

Please sign in to comment.