Skip to content

Commit 13bbd4f

Browse files
author
alexlee-dev
committed
✏️ Add gitignore and modify how errors are handled
1 parent 5107e9d commit 13bbd4f

3 files changed

Lines changed: 68 additions & 56 deletions

File tree

src/index.ts

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,49 @@ import {
1212
import { handleIncorrectApplicationName } from "./program";
1313

1414
const main = async (): Promise<void> => {
15-
let applicationName;
16-
const program = new commander.Command("create-cli-application")
17-
.version("0.0.0")
18-
.arguments("<application-name>")
19-
.usage(`${chalk.yellowBright("<application-name>")} [options]`)
20-
.action((name) => {
21-
applicationName = name;
22-
})
23-
.on("--help", () => {
24-
console.log(
25-
`\nOnly ${chalk.yellowBright("<application-name>")} is required.`
26-
);
27-
console.log(`\nIf you run into a problem, please open up a new issue:`);
28-
console.log(
29-
`${chalk.cyan(
30-
"https://github.com/alexlee-dev/create-cli-application/issues/new"
31-
)}\n`
32-
);
33-
})
34-
.parse(process.argv);
35-
36-
if (applicationName === "." || !applicationName) {
37-
return handleIncorrectApplicationName(program);
15+
try {
16+
let applicationName;
17+
const program = new commander.Command("create-cli-application")
18+
.version("0.0.0")
19+
.arguments("<application-name>")
20+
.usage(`${chalk.yellowBright("<application-name>")} [options]`)
21+
.action((name) => {
22+
applicationName = name;
23+
})
24+
.on("--help", () => {
25+
console.log(
26+
`\nOnly ${chalk.yellowBright("<application-name>")} is required.`
27+
);
28+
console.log(`\nIf you run into a problem, please open up a new issue:`);
29+
console.log(
30+
`${chalk.cyan(
31+
"https://github.com/alexlee-dev/create-cli-application/issues/new"
32+
)}\n`
33+
);
34+
})
35+
.parse(process.argv);
36+
37+
if (applicationName === "." || !applicationName) {
38+
return handleIncorrectApplicationName(program);
39+
}
40+
41+
// TODO - Catch names like "my.app.name" or "my app name"
42+
43+
await createProjectDirectory(applicationName);
44+
45+
await installDependencies(applicationName);
46+
47+
await installDevDependencies(applicationName);
48+
49+
await copyTemplateFiles(applicationName);
50+
51+
await createTSConfig(applicationName);
52+
53+
displaySuccessMessage(applicationName);
54+
} catch (error) {
55+
// TODO - Cleanup
56+
console.error(error);
3857
}
39-
40-
// TODO - Catch names like "my.app.name" or "my app name"
41-
42-
await createProjectDirectory(applicationName);
43-
44-
await installDependencies(applicationName);
45-
46-
await installDevDependencies(applicationName);
47-
48-
await copyTemplateFiles(applicationName);
49-
50-
await createTSConfig(applicationName);
51-
52-
displaySuccessMessage(applicationName);
5358
};
5459

5560
export default main;

src/init.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export const createProjectDirectory = async (
3939
license: "",
4040
};
4141

42-
let spinner = ora();
42+
let spinner = ora("Creating Application directory and package.json");
4343

4444
try {
45-
spinner.start("Creating Application directory and package.json");
45+
spinner.start();
4646
await fs.writeFile(
4747
path.join(root, "package.json"),
4848
JSON.stringify(packageJson, null, 2) + os.EOL
@@ -51,8 +51,8 @@ export const createProjectDirectory = async (
5151
"Application Directory and package.json created successfully"
5252
);
5353
} catch (error) {
54-
spinner.fail(error);
55-
console.error(error);
54+
spinner.fail();
55+
throw new Error(error);
5656
}
5757
};
5858

@@ -61,18 +61,18 @@ export const installDependencies = async (
6161
): Promise<void> => {
6262
const root = path.resolve(applicationName);
6363

64-
let spinner = ora();
64+
let spinner = ora("Installing dependencies");
6565

6666
try {
67-
spinner.start("Installing dependencies");
67+
spinner.start();
6868
const installCommand = "npm";
6969
let installArgs = ["install", "--save"];
7070
installArgs = installArgs.concat(dependencies);
7171
await executeCommand(installCommand, installArgs, { cwd: root });
7272
spinner.succeed("Dependencies installed successfully");
7373
} catch (error) {
74-
spinner.fail(error);
75-
console.error(error);
74+
spinner.fail();
75+
throw new Error(error);
7676
}
7777
};
7878

@@ -81,18 +81,18 @@ export const installDevDependencies = async (
8181
): Promise<void> => {
8282
const root = path.resolve(applicationName);
8383

84-
let spinner = ora();
84+
let spinner = ora("Installing devDependencies");
8585

8686
try {
87-
spinner.start("Installing devDependencies");
87+
spinner.start();
8888
const installCommand = "npm";
8989
let installArgs = ["install", "--save"];
9090
installArgs = installArgs.concat(devDependencies);
9191
await executeCommand(installCommand, installArgs, { cwd: root });
9292
spinner.succeed("DevDependencies installed successfully");
9393
} catch (error) {
94-
spinner.fail(error);
95-
console.error(error);
94+
spinner.fail();
95+
throw new Error(error);
9696
}
9797
};
9898

@@ -101,9 +101,10 @@ export const copyTemplateFiles = async (
101101
): Promise<void> => {
102102
const root = path.resolve(applicationName);
103103

104-
let spinner = ora();
104+
let spinner = ora("Copying template files");
105105

106106
try {
107+
spinner.start();
107108
await fs.copy(
108109
path.join(__dirname, "template/ts/src"),
109110
path.join(root, "/src")
@@ -116,10 +117,14 @@ export const copyTemplateFiles = async (
116117
path.join(__dirname, "template/README.md"),
117118
path.join(root, "/README.md")
118119
);
120+
await fs.copy(
121+
path.join(__dirname, "template/gitignore"),
122+
path.join(root, "/.gitignore")
123+
);
119124
spinner.succeed("Template files copied successfully");
120125
} catch (error) {
121-
spinner.fail(error);
122-
console.error(error);
126+
spinner.fail();
127+
throw new Error(error);
123128
}
124129
};
125130

@@ -128,7 +133,7 @@ export const createTSConfig = async (
128133
): Promise<void> => {
129134
const root = path.resolve(applicationName);
130135

131-
let spinner = ora();
136+
let spinner = ora("Creating tsconfig.json");
132137

133138
const tsConfig = {
134139
compilerOptions: {
@@ -144,15 +149,15 @@ export const createTSConfig = async (
144149
};
145150

146151
try {
147-
spinner.start("Creating tsconfig.json");
152+
spinner.start();
148153
await fs.writeFile(
149154
path.join(root, "tsconfig.json"),
150155
JSON.stringify(tsConfig, null, 2) + os.EOL
151156
);
152157
spinner.succeed("tsconfig.json created successfully");
153158
} catch (error) {
154-
spinner.fail(error);
155-
console.error(error);
159+
spinner.fail();
160+
throw new Error(error);
156161
}
157162
};
158163

src/template/gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
build/

0 commit comments

Comments
 (0)