setup-next-project
Version:
CLI to quickly create a pre-configured Next.js project with modular components and features
98 lines ⢠3.88 kB
JavaScript
import chalk from "chalk";
import { Command } from "commander";
import fs from "fs-extra";
import inquirer from "inquirer";
import path from "path";
import { createProject } from "./utils/createProject.js";
import { getAllAvailableFeatures } from "./utils/features.js";
import { validateProjectName } from "./utils/validation.js";
const program = new Command();
program
.name("setup-next-project")
.description("CLI to quickly create a pre-configured Next.js project")
.version("1.9.1");
program
.argument("[project-name]", "Project name")
.option("-f, --features <features>", "Comma-separated list of features to include")
.option("-y, --yes", "Use default values")
.action(async (projectName, options) => {
try {
console.log(chalk.cyan.bold("\nš Setup Next Project\n"));
let finalProjectName = projectName;
let selectedFeatures = [];
// If no project name provided, ask for it
if (!finalProjectName) {
const { name } = await inquirer.prompt([
{
type: "input",
name: "name",
message: "What is your project name?",
default: "my-next-project",
validate: validateProjectName,
},
]);
finalProjectName = name;
}
// Validate project name
const validationResult = validateProjectName(finalProjectName);
if (validationResult !== true) {
console.error(chalk.red(`ā ${validationResult}`));
process.exit(1);
}
// Check if folder already exists
const projectPath = path.resolve(process.cwd(), finalProjectName);
if (fs.existsSync(projectPath)) {
const { overwrite } = await inquirer.prompt([
{
type: "confirm",
name: "overwrite",
message: `The folder "${finalProjectName}" already exists. Do you want to replace it?`,
default: false,
},
]);
if (!overwrite) {
console.log(chalk.yellow("ā Operation cancelled."));
process.exit(0);
}
await fs.remove(projectPath);
}
// Parse features from command line
if (options.features) {
selectedFeatures = options.features.split(',').map((f) => f.trim());
}
// Get available features (including components)
const availableFeatures = await getAllAvailableFeatures();
// If no features specified and not --yes mode, ask for them
if (selectedFeatures.length === 0 && !options.yes) {
const { features } = await inquirer.prompt([
{
type: "checkbox",
name: "features",
message: "Which features would you like to add to your Next.js project?",
choices: availableFeatures.map((f) => ({
name: `${f.name} - ${f.description}`,
value: f.id,
checked: false,
})),
},
]);
selectedFeatures = features;
}
// Create project
await createProject({
projectName: finalProjectName,
features: selectedFeatures,
projectPath,
autoInstall: true, // Installation always enabled
});
// The project is created and dependencies are installed automatically
}
catch (error) {
console.error(chalk.red("\nā Error creating project:"));
console.error(chalk.red(error instanceof Error ? error.message : String(error)));
process.exit(1);
}
});
program.parse();
//# sourceMappingURL=index.js.map