-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Abhinav-ark/main
Initial setup, basic template, init and clear commands.
- Loading branch information
Showing
6 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Node Modules | ||
|
||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export const metadata = { | ||
name: "Quick Start Express", | ||
version: "v1.0.0", | ||
description: | ||
"A simple CLI tool to generate Express servers from multiple available templates.", | ||
oneLineDescription: "A simple Express.js server generator CLI tool.", | ||
}; | ||
|
||
export const commands = { | ||
version: { | ||
versionFlags: "-v, --version", | ||
}, | ||
init: { | ||
command: "init", | ||
description: "Initialize a new Express server.", | ||
}, | ||
clear: { | ||
command: "clear", | ||
description: "Clear the directory.", | ||
}, | ||
}; | ||
|
||
export const templates = { | ||
basic: { | ||
name: "basic", | ||
dependencies: [ | ||
{ | ||
name: "express", | ||
version: "^4.17.1", | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#!/usr/bin/env node | ||
|
||
import { program } from "commander"; | ||
import fs from "fs-extra"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
import { execSync } from "child_process"; | ||
import figlet from "figlet"; | ||
import chalk from "chalk"; | ||
import { createSpinner } from "nanospinner"; | ||
import { metadata, commands, templates } from "./configs.js"; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const template = templates.basic; | ||
|
||
program | ||
.version(metadata.version, commands.version.versionFlags) | ||
.description(metadata.description); | ||
|
||
program | ||
.command(commands.init.command) | ||
.description(commands.init.description) | ||
.action(() => { | ||
toolIntro(); | ||
|
||
console.log("Starting server initialization..."); | ||
|
||
const targetDir = process.cwd(); | ||
const parentDir = path.dirname(__dirname); | ||
const templatePath = path.join(parentDir, "templates", template.name); | ||
const destinationPath = path.join(targetDir); | ||
const npmInit = chalk.yellow.bold("npm init"); | ||
|
||
const initSpinner = createSpinner(`Running ${npmInit}...`).start(); | ||
try { | ||
// execSync('npm init -y', { stdio: 'inherit', cwd: targetDir }); | ||
execSync("npm init -y", { stdio: "ignore", cwd: targetDir }); | ||
initSpinner.success({ text: `${npmInit} completed successfully.` }); | ||
} catch (err) { | ||
initSpinner.error({ text: `Error running ${npmInit}:\n` }); | ||
console.error(err.message); | ||
return; | ||
} | ||
|
||
//console.log(`Copying server template from ${templatePath} to ${destinationPath}`); | ||
|
||
const copySpinner = createSpinner("Creating server files...").start(); | ||
try { | ||
fs.copySync(templatePath, destinationPath); | ||
copySpinner.success({ text: "Created server files successfully." }); | ||
} catch (err) { | ||
copySpinner.error({ text: "Error creating server files.\n" }); | ||
console.error(err.message); | ||
} | ||
|
||
const addDependencies = createSpinner( | ||
"Adding dependency packages..." | ||
).start(); | ||
try { | ||
// Update package.json to add express as a dependency. | ||
const packageJsonPath = path.join(targetDir, "package.json"); | ||
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8"); | ||
const packageJson = JSON.parse(packageJsonContent); | ||
packageJson.dependencies = packageJson.dependencies || {}; | ||
// packageJson.dependencies.express = "^4.17.1"; | ||
template.dependencies.forEach((dependency) => { | ||
packageJson.dependencies[`${dependency.name}`] = dependency.version; | ||
}); | ||
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); | ||
|
||
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(); | ||
try { | ||
//execSync('npm i', { stdio: 'inherit', cwd: targetDir }); | ||
execSync("npm i", { stdio: "ignore", cwd: targetDir }); | ||
|
||
installDependencies.success({ | ||
text: "Installed dependencies successfully.", | ||
}); | ||
|
||
console.log(chalk.green.bold("\nSetup complete! To run your server:")); | ||
console.log(chalk.yellow("Run:"), chalk.white.bold("npm start")); | ||
} catch (err) { | ||
installDependencies.error({ text: "Error installing dependencies.\n" }); | ||
console.error(err); | ||
} | ||
}); | ||
|
||
program | ||
.command(commands.clear.command) | ||
.description(commands.clear.description) | ||
.action(() => { | ||
const targetDir = process.cwd(); | ||
console.log("Clearing Directory...", chalk.bgRed.white(targetDir)); | ||
const clearingDirectory = createSpinner("Deleting All Files...").start(); | ||
try { | ||
// Read the directory. | ||
const files = fs.readdirSync(targetDir); | ||
|
||
for (const file of files) { | ||
const filePath = path.join(targetDir, file); | ||
// if (file !== '.' && file !== '..') { | ||
fs.removeSync(filePath); | ||
// } | ||
} | ||
|
||
clearingDirectory.success({ | ||
text: "Successfully cleared project directory.", | ||
}); | ||
} catch (error) { | ||
clearingDirectory.error({ text: "Error clearing project directory" }); | ||
console.error(error); | ||
} | ||
}); | ||
|
||
const toolIntro = () => { | ||
console.log( | ||
figlet.textSync(metadata.name, { | ||
font: "Standard", | ||
horizontalLayout: "default", | ||
verticalLayout: "default", | ||
width: 90, | ||
whitespaceBreak: true, | ||
}) | ||
); | ||
|
||
console.log(chalk.green.bold(metadata.oneLineDescription)); | ||
}; | ||
|
||
program.parse(process.argv); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "qse", | ||
"version": "1.0.0", | ||
"description": "A simple CLI tool to generate Express servers from multiple available templates.", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "node bin/index.js init" | ||
}, | ||
"author": "Abhinav Ramakrishnan, Ashwin Narayanan S", | ||
"license": "ISC", | ||
"bin":"./bin/index.js", | ||
"dependencies": { | ||
"chalk": "^5.3.0", | ||
"commander": "^12.1.0", | ||
"figlet": "^1.7.0", | ||
"fs-extra": "^11.2.0", | ||
"nanospinner": "^1.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const express = require("express"); | ||
const app = express(); | ||
const port = 3000; | ||
|
||
app.get("/", (req, res) => { | ||
return res.send("Hello World!"); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Example app listening on port ${port}.`); | ||
}); |