Skip to content

Commit b35a08b

Browse files
author
alexlee-dev
committed
🧹 Cleanup upon Error
1 parent 085a421 commit b35a08b

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Interactive option. Use the flag `--interactive` to use this mode.
1313
- Comments :)
14+
- Cleanup on global error
1415

1516
### Changed
1617

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ import {
2121
displaySuccessMessage,
2222
} from "./init";
2323
import { handleIncorrectApplicationName } from "./program";
24+
import { cleanupError } from "./util";
2425

2526
/**
2627
* Main CLI Program
2728
*/
2829
const main = async (): Promise<void> => {
30+
let applicationName;
31+
2932
try {
3033
// * Used to set the directory, application name, and inserted into templates
31-
let applicationName;
3234
let language: "js" | "ts";
3335
// * Default language is JavaScript
3436
language = "js";
@@ -110,6 +112,8 @@ const main = async (): Promise<void> => {
110112
// * Installs dev dependencies
111113
await installDevDependencies(applicationName, language);
112114

115+
throw new Error("FAKE ERROR");
116+
113117
// * Copies template files and inserts `applicationName` into the files
114118
await copyTemplateFiles(applicationName, language);
115119

@@ -119,7 +123,7 @@ const main = async (): Promise<void> => {
119123
// * Displays a success message to the user
120124
displaySuccessMessage(applicationName);
121125
} catch (error) {
122-
// TODO - Cleanup
126+
await cleanupError(applicationName);
123127
console.error(error);
124128
throw new Error(error);
125129
}

src/init.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ export const createProjectDirectory = async (
3434
);
3535
console.log();
3636

37-
// TODO - Interactive mode to fill in some of these values
38-
3937
const packageJson = {
4038
name: applicationName,
4139
version: "0.0.0",

src/util.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { spawn } from "child_process";
2+
import fs from "fs-extra";
3+
import path from "path";
24

35
/**
46
* Executes a command in a spawned process.
@@ -28,3 +30,20 @@ export const executeCommand = async (
2830
console.log({ message });
2931
});
3032
});
33+
34+
export const cleanupError = async (
35+
applicationName: string | undefined
36+
): Promise<void> => {
37+
try {
38+
if (!applicationName) {
39+
return;
40+
}
41+
42+
// * Application Directory
43+
const root = path.resolve(applicationName);
44+
45+
await executeCommand("rimraf", [root]);
46+
} catch (error) {
47+
throw new Error(error);
48+
}
49+
};

0 commit comments

Comments
 (0)