@darksnyder/license-key
Version:
License key Generator
71 lines (59 loc) ⢠2.21 kB
JavaScript
// Import necessary modules
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs-extra";
import prompts from "prompts";
import chalk from "chalk";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const TEMPLATE_DIR = path.join(__dirname, "../template");
async function createProject() {
// Prompt for the project (root directory) name.
const response = await prompts({
type: "text",
name: "projectName",
message: "Enter your root directory name:",
validate: (name) => (name ? true : "Directory name cannot be empty"),
});
const projectName = response.projectName.trim();
if (!projectName) {
console.log(chalk.red("ā Invalid directory name. Exiting."));
process.exit(1);
}
const targetDir = path.join(process.cwd(), projectName);
// Ensure the target directory exists
if (!fs.existsSync(targetDir)) {
console.log(chalk.blue(`\nš Creating directory: ${projectName}...\n`));
fs.mkdirSync(targetDir);
} else {
console.log(chalk.yellow(`Directory "${projectName}" already exists.`));
}
// Iterate over each file in the template directory
const templateFiles = fs.readdirSync(TEMPLATE_DIR);
for (const file of templateFiles) {
const sourceFile = path.join(TEMPLATE_DIR, file);
const targetFile = path.join(targetDir, file);
if (fs.existsSync(targetFile)) {
const { replace } = await prompts({
type: "confirm",
name: "replace",
message: `File "${file}" already exists. Do you want to replace it?`,
initial: false,
});
if (!replace) {
console.log(chalk.yellow(`āļø Skipped "${file}"`));
continue; // Skip this file
}
}
try {
fs.copySync(sourceFile, targetFile, { overwrite: true });
console.log(chalk.green(`ā
Copied "${file}"`));
} catch (err) {
console.error(chalk.red(`ā Error copying "${file}": ${err.message}`));
}
}
process.chdir(targetDir);
console.log(chalk.green(`ā
Your License Key generating file is ready!\n`));
}
createProject();