Skip to content

Commit eeca4e0

Browse files
author
alexlee-dev
committed
✏️ Add JS Template Files
1 parent 576e738 commit eeca4e0

4 files changed

Lines changed: 211 additions & 0 deletions

File tree

src/template/js/src/constants.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Options } from "boxen";
2+
3+
/**
4+
* Blank style applied to Boxen.
5+
*/
6+
export const blankBoxenStyle = {
7+
borderStyle: {
8+
topLeft: " ",
9+
topRight: " ",
10+
bottomLeft: " ",
11+
bottomRight: " ",
12+
horizontal: " ",
13+
vertical: " ",
14+
},
15+
float: "center",
16+
padding: { top: 0, bottom: 0, right: 1, left: 1 },
17+
};

src/template/js/src/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import clear from "clear";
2+
import Configstore from "configstore";
3+
import EventEmitter from "events";
4+
import { titleScreen } from "pickitt";
5+
6+
import { displayMainMenu, interpretMenuAction } from "./menu";
7+
import setup from "./setup";
8+
9+
const main = async () => {
10+
const menuActionEmitter = new EventEmitter.EventEmitter();
11+
menuActionEmitter.on("actionCompleted", async (state) => {
12+
await titleScreen("APP NAME");
13+
await displayMainMenu(state);
14+
await interpretMenuAction(state);
15+
});
16+
17+
const config = new Configstore("app-name");
18+
19+
const state = {
20+
config,
21+
menuAction: null,
22+
menuActionEmitter,
23+
};
24+
25+
try {
26+
const isSetUp = config.get("isSetUp");
27+
28+
if (!isSetUp) {
29+
await setup(state);
30+
clear();
31+
}
32+
33+
await titleScreen("APP NAME");
34+
await displayMainMenu(state);
35+
36+
await interpretMenuAction(state);
37+
} catch (e) {
38+
console.error("ERROR");
39+
console.log(state);
40+
console.error(e);
41+
}
42+
};
43+
44+
if (process.argv[3] === "start") main();
45+
46+
export default main;

src/template/js/src/menu.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import boxen from "boxen";
2+
import chalk from "chalk";
3+
import inquirer from "inquirer";
4+
import { titleScreen } from "pickitt";
5+
6+
import { blankBoxenStyle } from "./constants";
7+
8+
/**
9+
* Displays Main Menu to user.
10+
* @param {AppState} state State of application.
11+
* @returns {Promise} Resolves with menuAction value.
12+
*/
13+
export const displayMainMenu = (state) =>
14+
new Promise(async (resolve, reject) => {
15+
try {
16+
const { menuAction } = await inquirer.prompt([
17+
{
18+
type: "list",
19+
message: "Main Menu",
20+
name: "menuAction",
21+
choices: [
22+
{ value: "option1", name: "Option 1" },
23+
{ value: "option2", name: "Option 2" },
24+
{ value: "option3", name: "Option 3" },
25+
new inquirer.Separator(),
26+
{ value: "about", name: "About" },
27+
{ value: "exit", name: "Exit" },
28+
],
29+
},
30+
]);
31+
state.menuAction = menuAction;
32+
resolve(menuAction);
33+
} catch (e) {
34+
reject(e);
35+
}
36+
});
37+
38+
/**
39+
* Pauses the process execution and waits for the user to hit a key.
40+
* @returns {Promise} Resolves when user has entered a keystroke.
41+
* @async
42+
*/
43+
const keypress = async () => {
44+
try {
45+
process.stdin.setRawMode(true);
46+
return new Promise((resolve, reject) => {
47+
try {
48+
process.stdin.resume();
49+
process.stdin.once("data", () => {
50+
process.stdin.setRawMode(false);
51+
resolve();
52+
});
53+
} catch (e) {
54+
return reject(e);
55+
}
56+
});
57+
} catch (e) {
58+
throw new Error(e);
59+
}
60+
};
61+
62+
/**
63+
* Interprets user selected menu action.
64+
* @param {AppState} state State of application.
65+
* @returns {Promise}
66+
*/
67+
export const interpretMenuAction = async (state) => {
68+
try {
69+
if (state.menuAction === null) {
70+
throw new Error("menuAction can not be `null`");
71+
}
72+
const actions = {
73+
about: async (state) => {
74+
await titleScreen("APP NAME");
75+
console.log(
76+
boxen(chalk.yellow(`Author: `) + "YOUR NAME", blankBoxenStyle)
77+
);
78+
79+
console.log("Press any key to return to Main Menu ...");
80+
await keypress();
81+
state.menuActionEmitter.emit("actionCompleted", state);
82+
},
83+
option1: async (state) => {
84+
await titleScreen("APP NAME");
85+
console.log("Option 1 Logic would take place here :)");
86+
console.log("");
87+
88+
console.log("Press any key to return to Main Menu ...");
89+
await keypress();
90+
state.menuActionEmitter.emit("actionCompleted", state);
91+
},
92+
option2: async (state) => {
93+
await titleScreen("APP NAME");
94+
console.log("Option 2 Logic would take place here :)");
95+
console.log("");
96+
97+
console.log("Press any key to return to Main Menu ...");
98+
await keypress();
99+
state.menuActionEmitter.emit("actionCompleted", state);
100+
},
101+
option3: async (state) => {
102+
await titleScreen("APP NAME");
103+
console.log("Option 3 Logic would take place here :)");
104+
console.log("");
105+
106+
console.log("Press any key to return to Main Menu ...");
107+
await keypress();
108+
state.menuActionEmitter.emit("actionCompleted", state);
109+
},
110+
exit: (state) => process.exit(),
111+
};
112+
113+
await actions[state.menuAction](state);
114+
} catch (e) {
115+
throw new Error(e);
116+
}
117+
};

src/template/js/src/setup.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import chalk from "chalk";
2+
import clear from "clear";
3+
import inquirer from "inquirer";
4+
5+
const setup = async (state) => {
6+
try {
7+
clear();
8+
9+
console.log(
10+
`Welcome to ${chalk.yellowBright(
11+
"APP NAME"
12+
)}! Let's walk you through the initial set up.\n`
13+
);
14+
15+
const answers = await inquirer.prompt([
16+
{
17+
type: "input",
18+
name: "specialKey",
19+
message: `Please enter your ${chalk.yellowBright("special key")}:`,
20+
},
21+
]);
22+
const specialKey = answers.specialKey;
23+
state.config.set("specialKey", specialKey);
24+
25+
state.config.set("isSetUp", true);
26+
} catch (error) {
27+
console.error(error);
28+
}
29+
};
30+
31+
export default setup;

0 commit comments

Comments
 (0)