Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): enable graceful exit #199

Merged
merged 1 commit into from
May 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
@@ -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,
@@ -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;
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;
@@ -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 ??
@@ -43,7 +45,7 @@ export async function createStack(args: CreateStackArgs): Promise<config.Stack>
disabled: "(Coming soon)",
},
],
}));
}).catch(handleIquirerError));

args.provisionType =
args.provisionType ??
@@ -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("./")) {
@@ -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");
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");

@@ -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");
@@ -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) {
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;
@@ -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.");
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";

/**
@@ -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
@@ -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();