Skip to content

Commit

Permalink
feat: add -n option to customize package name during initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
KiranRajeev-KV committed Dec 24, 2024
1 parent da5a32d commit c631901
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
4 changes: 4 additions & 0 deletions bin/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const commands = {
flags: "-t, --template <template>",
description: "Specify template to use",
},
{
flags: "-n, --name <name>",
description: "Specify the name of the package",
},
],
},
list: {
Expand Down
60 changes: 48 additions & 12 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ program
.command(commands.init.command)
.description(commands.init.description)
.option(commands.init.options[0].flags, commands.init.options[0].description)
.option(commands.init.options[1].flags, commands.init.options[1].description)
.action((options) => {
toolIntro();
initCommand(options);
Expand All @@ -35,12 +36,20 @@ program
Object.keys(commands).forEach((cmd) => {
const commandInfo = commands[cmd];
if (commandInfo.command) {
console.log(`- ${commandInfo.command}${commandInfo.description ? ": " + commandInfo.description : ""}`);
console.log(
`- ${commandInfo.command}${
commandInfo.description ? ": " + commandInfo.description : ""
}`
);
}

if (commandInfo.options) {
commandInfo.options.forEach((option) => {
console.log(` (Options: ${option.flags}${option.description ? " - " + option.description : ""})`);
console.log(
` (Options: ${option.flags}${
option.description ? " - " + option.description : ""
})`
);
});
}
});
Expand Down Expand Up @@ -80,17 +89,26 @@ program

function initCommand(options) {
const selectedTemplate = options.template || "basic"; // Default to 'basic' if no template is specified
const packageName = options.name || "quick-start-express-server"; // Default to 'quick-start-express-server' if no name is specified

if (!templates[selectedTemplate]) {
console.error(chalk.bgRed.white(`Template ${selectedTemplate} does not exist. To see available templates use "qse list".`));
console.error(
chalk.bgRed.white(
`Template ${selectedTemplate} does not exist. To see available templates use "qse list".`
)
);
return;
}

console.log("Starting server initialization...");

const targetDir = process.cwd();
const parentDir = path.dirname(__dirname);
const templatePath = path.join(parentDir, "templates", templates[selectedTemplate].name);
const templatePath = path.join(
parentDir,
"templates",
templates[selectedTemplate].name
);
const destinationPath = path.join(targetDir);
const npmInit = chalk.yellow.bold("npm init");

Expand All @@ -99,6 +117,12 @@ function initCommand(options) {
try {
execSync("npm init -y", { stdio: "ignore", cwd: targetDir });

const packageJsonPath = path.join(targetDir, "package.json");
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
packageJson.name = packageName;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

initSpinner.success({ text: `${npmInit} completed successfully.` });
} catch (err) {
initSpinner.error({ text: `Error running ${npmInit}:\n` });
Expand All @@ -116,51 +140,63 @@ function initCommand(options) {
console.error(err.message);
}

const addTypeDeclaration = createSpinner("Adding type declaration...").start();
const addTypeDeclaration = createSpinner(
"Adding type declaration..."
).start();
try {
const packageJsonPath = path.join(targetDir, "package.json");
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
packageJson.type = "module";
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

addTypeDeclaration.success({ text: "Added type declaration successfully." });
addTypeDeclaration.success({
text: "Added type declaration successfully.",
});
} catch (err) {
addTypeDeclaration.error({ text: "Error adding type declaration.\n" });
console.error(err.message);
}

const addDependencies = createSpinner("Adding dependency packages...").start();
const addDependencies = createSpinner(
"Adding dependency packages..."
).start();
try {
const packageJsonPath = path.join(targetDir, "package.json");
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
packageJson.dependencies = packageJson.dependencies || {};

templates[selectedTemplate].dependencies.forEach((dependency) => {
packageJson.dependencies[`${dependency.name}`] = dependency.version;
});
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));

addDependencies.success({ text: "Added dependency packages successfully." });
addDependencies.success({
text: "Added dependency packages successfully.",
});
} catch (err) {
addDependencies.error("Error adding dependency packages.\n");
console.error(err.message);
}

const installDependencies = createSpinner("Installing dependency packages...").start();
const installDependencies = createSpinner(
"Installing dependency packages..."
).start();
try {
execSync("npm i", { stdio: "ignore", cwd: targetDir });

installDependencies.success({ text: "Installed dependencies successfully." });
installDependencies.success({
text: "Installed dependencies successfully.",
});
} catch (err) {
installDependencies.error({ text: "Error installing dependencies.\n" });
console.error(err);
}

console.log(chalk.green.bold("\nSetup complete! To run your server:"));
console.log(chalk.yellow("Run:"), chalk.white.bold("npm start"));
};
}

const toolIntro = () => {
console.log(
Expand Down

0 comments on commit c631901

Please sign in to comment.