-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (73 loc) · 2.5 KB
/
index.js
File metadata and controls
87 lines (73 loc) · 2.5 KB
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
#!/usr/bin/env node
import { Command } from "commander";
import chalk from "chalk";
import fs from "fs-extra";
import path from "path";
import { simpleGit } from "simple-git";
import ora from "ora";
import inquirer from "inquirer";
import { execSync } from "child_process";
// Initialize the program for the command-line
const program = new Command();
program
.version("1.0.0")
.argument("<project-name>", "name of the project")
.action(async (projectName) => {
const projectPath = path.join(process.cwd(), projectName);
// Check if the project directory already exists
if (fs.existsSync(projectPath)) {
console.error(
chalk.red(`Error: Directory ${projectName} already exists.`)
);
process.exit(1);
}
// Prompt the user to select the template type
const answers = await inquirer.prompt([
{
type: "list",
name: "template",
message: "Which template would you like to use?",
choices: [
{ name: "Node JS with TypeScript", value: "node" },
{ name: "Next JS with TypeScript", value: "next" },
],
default: "node",
},
]);
// Determine the repository URL based on the user's selection
const repoUrl =
answers.template === "node"
? "https://github.com/pythonnelson/nodejs-starter-template.git"
: "https://github.com/pythonnelson/nextjs-start-template.git";
// Create the project directory
fs.mkdirSync(projectPath);
// Set up the spinner for visual feedback
const spinner = ora(
`Setting up ${
answers.template === "node" ? "Node Js" : "Next Js"
} project...`
).start();
const git = simpleGit();
try {
// Clone the selected template
await git.clone(repoUrl, projectPath);
spinner.succeed(
`${
answers.template === "node" ? "Node Js" : "Next Js"
} template setup successfully!`
);
// Navigate into the project directory
process.chdir(projectPath);
// Install dependencies
spinner.start("Installing dependencies...");
execSync("npm install", { stdio: "inherit" });
spinner.succeed("Dependencies installed successfully!");
console.log(chalk.green(`Project ${projectName} is ready!`));
console.log(chalk.green(`cd ${projectName} and run npm run dev`));
} catch (error) {
spinner.fail("Failed to set up the project.");
console.error(chalk.red(error.message));
process.exit(1);
}
});
program.parse(process.argv);