Skip to content

Commit 3749ffe

Browse files
author
alexlee-dev
committed
✏️ Add Support for JS default and TS as option
1 parent eeca4e0 commit 3749ffe

5 files changed

Lines changed: 50 additions & 13 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ export const dependencies = [
88
];
99

1010
export const devDependencies = [
11+
"@babel/core",
12+
"@babel/cli",
13+
"@babel/preset-env",
14+
"@babel/plugin-transform-runtime",
15+
"@babel/runtime",
16+
];
17+
18+
export const devDependenciesTS = [
1119
"@types/clear",
1220
"@types/configstore",
1321
"@types/inquirer",

src/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ import { handleIncorrectApplicationName } from "./program";
2020
const main = async (): Promise<void> => {
2121
try {
2222
let applicationName;
23+
let language: "js" | "ts";
24+
language = "js";
2325
const program = new commander.Command("create-cli-application")
24-
.version("0.0.0")
26+
.version("0.2.0")
2527
.arguments("<application-name>")
2628
.usage(`${chalk.yellowBright("<application-name>")} [options]`)
2729
.action((name) => {
2830
applicationName = name;
2931
})
32+
.option(
33+
"--typescript",
34+
"use TypeScript as the cli application source language"
35+
)
3036
.on("--help", () => {
3137
console.log(
3238
`\nOnly ${chalk.yellowBright("<application-name>")} is required.`
@@ -40,21 +46,22 @@ const main = async (): Promise<void> => {
4046
})
4147
.parse(process.argv);
4248

49+
// TODO - Catch names like "my.app.name" or "my app name"
4350
if (applicationName === "." || !applicationName) {
4451
return handleIncorrectApplicationName(program);
4552
}
4653

47-
// TODO - Catch names like "my.app.name" or "my app name"
54+
if (program.typescript) language = "ts";
4855

49-
await createProjectDirectory(applicationName);
56+
await createProjectDirectory(applicationName, language);
5057

5158
await installDependencies(applicationName);
5259

53-
await installDevDependencies(applicationName);
60+
await installDevDependencies(applicationName, language);
5461

55-
await copyTemplateFiles(applicationName);
62+
await copyTemplateFiles(applicationName, language);
5663

57-
await createTSConfig(applicationName);
64+
if (language === "ts") await createTSConfig(applicationName);
5865

5966
displaySuccessMessage(applicationName);
6067
} catch (error) {

src/init.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import ora from "ora";
44
import os from "os";
55
import path from "path";
66

7-
import { dependencies, devDependencies } from "./constants";
7+
import { dependencies, devDependencies, devDependenciesTS } from "./constants";
88
import { executeCommand } from "./util";
99

1010
export const createProjectDirectory = async (
11-
applicationName: string
11+
applicationName: string,
12+
language: "js" | "ts"
1213
): Promise<void> => {
1314
const root = path.resolve(applicationName);
1415
// const originalDirectory = process.cwd();
@@ -30,7 +31,7 @@ export const createProjectDirectory = async (
3031
[applicationName]: "./index.js",
3132
},
3233
scripts: {
33-
build: "rimraf build && tsc",
34+
build: language === "ts" ? "rimraf build && tsc" : "babel src -d build",
3435
start: "node build/index.js -- start",
3536
test: 'echo "Error: no test specified" && exit 1',
3637
},
@@ -77,7 +78,8 @@ export const installDependencies = async (
7778
};
7879

7980
export const installDevDependencies = async (
80-
applicationName: string
81+
applicationName: string,
82+
language: "js" | "ts"
8183
): Promise<void> => {
8284
const root = path.resolve(applicationName);
8385

@@ -88,6 +90,13 @@ export const installDevDependencies = async (
8890
const installCommand = "npm";
8991
let installArgs = ["install", "--save"];
9092
installArgs = installArgs.concat(devDependencies);
93+
94+
if (language === "ts") {
95+
installArgs = installArgs.concat(devDependenciesTS);
96+
} else {
97+
installArgs = installArgs.concat(devDependencies);
98+
}
99+
91100
await executeCommand(installCommand, installArgs, { cwd: root });
92101
spinner.succeed("DevDependencies installed successfully");
93102
} catch (error) {
@@ -97,7 +106,8 @@ export const installDevDependencies = async (
97106
};
98107

99108
export const copyTemplateFiles = async (
100-
applicationName: string
109+
applicationName: string,
110+
language: "js" | "ts"
101111
): Promise<void> => {
102112
const root = path.resolve(applicationName);
103113

@@ -106,7 +116,7 @@ export const copyTemplateFiles = async (
106116
try {
107117
spinner.start();
108118
await fs.copy(
109-
path.join(__dirname, "template/ts/src"),
119+
path.join(__dirname, `template/${language}/src`),
110120
path.join(root, "/src")
111121
);
112122
await fs.copy(
@@ -121,6 +131,12 @@ export const copyTemplateFiles = async (
121131
path.join(__dirname, "template/gitignore"),
122132
path.join(root, "/.gitignore")
123133
);
134+
if (language === "js") {
135+
await fs.copy(
136+
path.join(__dirname, "template/babelrc"),
137+
path.join(root, "/.babelrc")
138+
);
139+
}
124140
spinner.succeed("Template files copied successfully");
125141
} catch (error) {
126142
spinner.fail();

src/template/babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": ["@babel/plugin-transform-runtime"],
3+
"presets": [
4+
"@babel/preset-env"
5+
]
6+
}

0 commit comments

Comments
 (0)