Skip to content

Commit 085a421

Browse files
author
alexlee-dev
committed
💬 Add Comments
1 parent 29a20b2 commit 085a421

11 files changed

Lines changed: 128 additions & 3 deletions

File tree

CHANGELOG.md

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

1212
- Interactive option. Use the flag `--interactive` to use this mode.
13+
- Comments :)
1314

1415
### Changed
1516

src/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* These dependencies are required for the cli application regardless of language.
3+
*/
14
export const dependencies = [
25
"boxen",
36
"chalk",
@@ -7,6 +10,9 @@ export const dependencies = [
710
"pickitt",
811
];
912

13+
/**
14+
* These dev dependencies are for JavaScript projects.
15+
*/
1016
export const devDependencies = [
1117
"@babel/core",
1218
"@babel/cli",
@@ -15,6 +21,9 @@ export const devDependencies = [
1521
"@babel/runtime",
1622
];
1723

24+
/**
25+
* These dev dependencies are for TypeScript projects.
26+
*/
1827
export const devDependenciesTS = [
1928
"@types/clear",
2029
"@types/configstore",

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import chalk from "chalk";
33
import commander from "commander";
44
import inquirer from "inquirer";
55

6+
/**
7+
* Initialize Sentry
8+
*/
69
Sentry.init({
710
dsn:
811
"https://55c913cc3d394f71ba669fda095698fd@o202486.ingest.sentry.io/5254191",
@@ -19,11 +22,20 @@ import {
1922
} from "./init";
2023
import { handleIncorrectApplicationName } from "./program";
2124

25+
/**
26+
* Main CLI Program
27+
*/
2228
const main = async (): Promise<void> => {
2329
try {
30+
// * Used to set the directory, application name, and inserted into templates
2431
let applicationName;
2532
let language: "js" | "ts";
33+
// * Default language is JavaScript
2634
language = "js";
35+
36+
/**
37+
* The program that parses the initial user input
38+
*/
2739
const program = new commander.Command("create-cli-application")
2840
.version("0.4.0")
2941
.arguments("<application-name>")
@@ -55,12 +67,15 @@ const main = async (): Promise<void> => {
5567
.parse(process.argv);
5668

5769
// TODO - Catch names like "my.app.name" or "my app name"
70+
71+
// * Application Name must exist, and not consist of illegal characters
5872
if (applicationName === "." || !applicationName) {
5973
return handleIncorrectApplicationName(program);
6074
}
6175

6276
if (program.interactive) {
6377
// * Interactive walk-thru
78+
6479
// * Language
6580
const answers = await inquirer.prompt([
6681
{
@@ -76,23 +91,32 @@ const main = async (): Promise<void> => {
7691
const languageChoice: "js" | "ts" = answers.language;
7792
language = languageChoice;
7893

94+
// TODO - Author Name
7995
// TODO - Compiler Choice (Babel vs. other)
8096
// TODO - Add Prettier
8197
// TODO - Add ESLint / Other Linter
8298
// TODO - Menu Color Option
8399
}
100+
101+
// * Set language to 'ts' if user passed --typescript flag
84102
if (program.typescript && !program.interactive) language = "ts";
85103

104+
// * Creates a project directory and package.json
86105
await createProjectDirectory(applicationName, language);
87106

107+
// * Installs dependencies
88108
await installDependencies(applicationName);
89109

110+
// * Installs dev dependencies
90111
await installDevDependencies(applicationName, language);
91112

113+
// * Copies template files and inserts `applicationName` into the files
92114
await copyTemplateFiles(applicationName, language);
93115

116+
// * Creates a tsconfig.json file
94117
if (language === "ts") await createTSConfig(applicationName);
95118

119+
// * Displays a success message to the user
96120
displaySuccessMessage(applicationName);
97121
} catch (error) {
98122
// TODO - Cleanup

src/init.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ import path from "path";
77
import { dependencies, devDependencies, devDependenciesTS } from "./constants";
88
import { executeCommand } from "./util";
99

10+
/**
11+
* Creates a project directory and a package.json inside that new directory.
12+
* @param applicationName Name of application.
13+
* @param language Language of application
14+
*/
1015
export const createProjectDirectory = async (
1116
applicationName: string,
1217
language: "js" | "ts"
1318
): Promise<void> => {
19+
// * Application Directory
1420
const root = path.resolve(applicationName);
1521

22+
// ? Needed?
1623
fs.ensureDirSync(root);
1724

1825
console.log();
@@ -51,6 +58,7 @@ export const createProjectDirectory = async (
5158

5259
try {
5360
spinner.start();
61+
// * Create package.json
5462
await fs.writeFile(
5563
path.join(root, "package.json"),
5664
JSON.stringify(packageJson, null, 2) + os.EOL
@@ -64,9 +72,14 @@ export const createProjectDirectory = async (
6472
}
6573
};
6674

75+
/**
76+
* Installs dependencies.
77+
* @param applicationName Name of application.
78+
*/
6779
export const installDependencies = async (
6880
applicationName: string
6981
): Promise<void> => {
82+
// * Application Directory
7083
const root = path.resolve(applicationName);
7184

7285
let spinner = ora("Installing dependencies");
@@ -76,6 +89,7 @@ export const installDependencies = async (
7689
const installCommand = "npm";
7790
let installArgs = ["install", "--save"];
7891
installArgs = installArgs.concat(dependencies);
92+
// * Create a process that installs the dependencies
7993
await executeCommand(installCommand, installArgs, { cwd: root });
8094
spinner.succeed("Dependencies installed successfully");
8195
} catch (error) {
@@ -84,10 +98,16 @@ export const installDependencies = async (
8498
}
8599
};
86100

101+
/**
102+
* Installs dev dependencies.
103+
* @param applicationName Name of application.
104+
* @param language Language of application.
105+
*/
87106
export const installDevDependencies = async (
88107
applicationName: string,
89108
language: "js" | "ts"
90109
): Promise<void> => {
110+
// * Application Directory
91111
const root = path.resolve(applicationName);
92112

93113
let spinner = ora("Installing devDependencies");
@@ -104,6 +124,7 @@ export const installDevDependencies = async (
104124
installArgs = installArgs.concat(devDependencies);
105125
}
106126

127+
// * Creates a process that installs the dev dependencies
107128
await executeCommand(installCommand, installArgs, { cwd: root });
108129
spinner.succeed("DevDependencies installed successfully");
109130
} catch (error) {
@@ -112,16 +133,24 @@ export const installDevDependencies = async (
112133
}
113134
};
114135

136+
// TODO - Refactor this function :)
137+
/**
138+
* Copies template files and inserts values into the files.
139+
* @param applicationName Name of application.
140+
* @param language Language of application.
141+
*/
115142
export const copyTemplateFiles = async (
116143
applicationName: string,
117144
language: "js" | "ts"
118145
): Promise<void> => {
146+
// * Application Directory
119147
const root = path.resolve(applicationName);
120148

121149
let spinner = ora("Copying template files");
122150

123151
try {
124152
spinner.start();
153+
// * Copy template files based on language
125154
await fs.copy(
126155
path.join(__dirname, `template/${language}/src`),
127156
path.join(root, "/src")
@@ -252,13 +281,19 @@ export const copyTemplateFiles = async (
252281
}
253282
};
254283

284+
/**
285+
* Creates a tsconfig.json file in the application directory.
286+
* @param applicationName Name of application.
287+
*/
255288
export const createTSConfig = async (
256289
applicationName: string
257290
): Promise<void> => {
291+
// * Application Directory
258292
const root = path.resolve(applicationName);
259293

260294
let spinner = ora("Creating tsconfig.json");
261295

296+
// * Basic tsconfig needed to build the application
262297
const tsConfig = {
263298
compilerOptions: {
264299
target: "es5",
@@ -274,6 +309,7 @@ export const createTSConfig = async (
274309

275310
try {
276311
spinner.start();
312+
// * Create tsconfig.json file
277313
await fs.writeFile(
278314
path.join(root, "tsconfig.json"),
279315
JSON.stringify(tsConfig, null, 2) + os.EOL
@@ -285,7 +321,12 @@ export const createTSConfig = async (
285321
}
286322
};
287323

324+
/**
325+
* Display a success message to the user.
326+
* @param applicationName Name of application.
327+
*/
288328
export const displaySuccessMessage = (applicationName: string): void => {
329+
// * Application Directory
289330
const root = path.resolve(applicationName);
290331

291332
console.log();

src/program.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import chalk from "chalk";
22
import commander from "commander";
33

4+
/**
5+
* Handles an incorrectly named application.
6+
* @param program Program parsing user input.
7+
*/
48
export const handleIncorrectApplicationName = (program: commander.Command) => {
59
console.error("\nPlease specify the name of your application:");
610
console.log(
@@ -13,5 +17,6 @@ export const handleIncorrectApplicationName = (program: commander.Command) => {
1317
console.log(
1418
`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`
1519
);
20+
// * Exit the program
1621
process.exit(1);
1722
};

src/template/js/src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import { titleScreen } from "pickitt";
66
import { displayMainMenu, interpretMenuAction } from "./menu";
77
import setup from "./setup";
88

9+
/**
10+
* Main Program.
11+
*/
912
const main = async () => {
13+
// * Action Emitter keeps track of user input in the menu.
1014
const menuActionEmitter = new EventEmitter.EventEmitter();
1115
menuActionEmitter.on("actionCompleted", async (state) => {
16+
// * Display title screen
1217
await titleScreen("___APP NAME___");
18+
// * Display main menu
1319
await displayMainMenu(state);
20+
// * When user makes a choice, interpret the choice as an action
1421
await interpretMenuAction(state);
1522
});
1623

24+
// * Store a config file on the user's machine
1725
const config = new Configstore("___APP NAME___");
1826

27+
// * Application State
1928
const state = {
2029
config,
2130
menuAction: null,
@@ -26,13 +35,14 @@ const main = async () => {
2635
const isSetUp = config.get("isSetUp");
2736

2837
if (!isSetUp) {
38+
// * The user has not gone through a setup process
39+
// * Set up the user
2940
await setup(state);
3041
clear();
3142
}
3243

3344
await titleScreen("___APP NAME___");
3445
await displayMainMenu(state);
35-
3646
await interpretMenuAction(state);
3747
} catch (e) {
3848
console.error("ERROR");
@@ -41,6 +51,7 @@ const main = async () => {
4151
}
4252
};
4353

54+
// * Handle local development with `npm start`
4455
if (process.argv[3] === "start") main();
4556

4657
export default main;

src/template/js/src/setup.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import chalk from "chalk";
22
import clear from "clear";
33
import inquirer from "inquirer";
44

5+
/**
6+
* Walk the user through an initial setup and store values on the machine.
7+
* @param {object} state Application State
8+
*/
59
const setup = async (state) => {
610
try {
711
clear();
@@ -19,9 +23,12 @@ const setup = async (state) => {
1923
message: `Please enter your ${chalk.yellowBright("special key")}:`,
2024
},
2125
]);
26+
// * Template value. Change to be required setup or don't set up at all :)
2227
const specialKey = answers.specialKey;
2328
state.config.set("specialKey", specialKey);
2429

30+
// * Tell the application that the user has gone through this process.
31+
// * This value persists thanks to the locally stored configuration.
2532
state.config.set("isSetUp", true);
2633
} catch (error) {
2734
console.error(error);

src/template/ts/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@ import { displayMainMenu, interpretMenuAction } from "./menu";
77
import setup from "./setup";
88
import { AppState } from "./types";
99

10+
/**
11+
* Main Program.
12+
*/
1013
const main = async (): Promise<void> => {
14+
// * Action Emitter keeps track of user input in the menu.
1115
const menuActionEmitter = new EventEmitter.EventEmitter();
1216
menuActionEmitter.on("actionCompleted", async (state: AppState) => {
17+
// * Display title screen
1318
await titleScreen("___APP NAME___");
19+
// * Display main menu
1420
await displayMainMenu(state);
21+
// * When user makes a choice, interpret the choice as an action
1522
await interpretMenuAction(state);
1623
});
1724

25+
// * Store a config file on the user's machine
1826
const config = new Configstore("___APP NAME___");
1927

28+
// * Application State
2029
const state: AppState = {
2130
config,
2231
menuAction: null,
@@ -27,13 +36,14 @@ const main = async (): Promise<void> => {
2736
const isSetUp: boolean = config.get("isSetUp");
2837

2938
if (!isSetUp) {
39+
// * The user has not gone through a setup process
40+
// * Set up the user
3041
await setup(state);
3142
clear();
3243
}
3344

3445
await titleScreen("___APP NAME___");
3546
await displayMainMenu(state);
36-
3747
await interpretMenuAction(state);
3848
} catch (e) {
3949
console.error("ERROR");
@@ -42,6 +52,7 @@ const main = async (): Promise<void> => {
4252
}
4353
};
4454

55+
// * Handle local development with `npm start`
4556
if (process.argv[3] === "start") main();
4657

4758
export default main;

0 commit comments

Comments
 (0)