-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
99 lines (89 loc) · 2.85 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
import { Command } from "commander";
import * as inquirer from "inquirer";
import chalk from "chalk";
import simpleGit from "simple-git";
import { promises as fs } from "fs";
import * as path from "path";
const program = new Command();
interface Answers {
projectName: string;
template: string;
}
const git = simpleGit();
const repoUrl = "https://github.com/onesamket/tw-stack.git";
const templates: { [key: string]: string } = {
"✨ - Nextjs Framework": "nextjs",
"⚡ - Remix framework": "remix",
"⚡ - Bun with HTMX": "bun-htmx",
"🏗️ - MERN stack": "mern",
"🌐 - SPA React": "react-spa",
"📱 - React native with expo 51": "expo",
"🔥 - React-Hono.js full-stack": "react-hono",
"😀 - react-hapi.js full-stack": "react-hapi",
};
program
.version("0.0.1")
.description("Scaffold pre-configured projects with tw-stack");
program
.command("create [project-name]")
.action(async (projectName: string | undefined) => {
if (!projectName) {
const projectAnswer = await inquirer.prompt([
{
type: "input",
name: "projectName",
message: "Enter the project name:",
validate: (input: string) =>
input ? true : "Project name is required.",
},
]);
projectName = projectAnswer.projectName;
}
const answers: Answers = await inquirer.prompt([
{
type: "list",
name: "template",
message: "Choose a template",
choices: Object.keys(templates),
},
]);
// @ts-ignore
const projectPath = path.join(process.cwd(), projectName);
try {
await fs.mkdir(projectPath);
} catch (err) {
if ((err as NodeJS.ErrnoException).code === "EEXIST") {
console.log(chalk.red("Error: Project already exists."));
process.exit(1);
} else {
throw err;
}
}
await cloneTemplate(templates[answers.template], projectPath);
console.log(chalk.green(`Project ${projectName} created successfully.`));
console.log(chalk.blue(`Navigate to your project directory:`));
console.log(chalk.blue(`Install dependencies using Bun:`));
console.log(chalk.yellow(`bun install`));
});
async function cloneTemplate(branchName: string, targetPath: string) {
try {
console.log(chalk.blue(`Cloning repository from ${repoUrl}...`));
await git.clone(repoUrl, targetPath, [
`--branch=${branchName}`,
`--single-branch`,
]);
console.log(chalk.green("Repository cloned successfully."));
console.log(chalk.green(`cd ${targetPath}`));
console.log(chalk.green(`bun install`));
console.log(
chalk.bgBlue.white(
`Checkout GitHub repository: https://github.com/onesamket/tw-stack`
)
);
} catch (err) {
console.error(chalk.red(`Error cloning repository: ${err}`));
process.exit(1);
}
}
program.parse(process.argv);