From d8728c7d889d8c1eb8d26514d28afa380f0e8a16 Mon Sep 17 00:00:00 2001 From: venkatesh21bit Date: Wed, 25 Dec 2024 22:42:58 +0530 Subject: [PATCH 01/10] tests --- bin/index.js | 10 ++++++---- package.json | 6 ++++-- templates/basic/basic_api.test.js | 9 +++++++++ templates/basic/server.js | 5 +++-- test/init.test.js | 8 ++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 templates/basic/basic_api.test.js diff --git a/bin/index.js b/bin/index.js index 97568bc..ea3f679 100755 --- a/bin/index.js +++ b/bin/index.js @@ -77,8 +77,8 @@ program console.error(error); } }); - -function initCommand(options) { +//Changing sync to async to optimize further +async function initCommand(options) { const selectedTemplate = options.template || "basic"; // Default to 'basic' if no template is specified if (!templates[selectedTemplate]) { @@ -108,7 +108,9 @@ function initCommand(options) { const copySpinner = createSpinner("Creating server files...").start(); try { - fs.copySync(templatePath, destinationPath); + await fs.copy(templatePath, targetDir, { + filter: (src) => !src.includes("node_modules"), // Skiping node_modules to optimize + }); copySpinner.success({ text: "Created server files successfully." }); } catch (err) { @@ -133,7 +135,7 @@ function initCommand(options) { const addDependencies = createSpinner("Adding dependency packages...").start(); try { const packageJsonPath = path.join(targetDir, "package.json"); - const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8"); + const packageJsonContent = await fs.readJSON(packageJsonPath, "utf8"); const packageJson = JSON.parse(packageJsonContent); packageJson.dependencies = packageJson.dependencies || {}; diff --git a/package.json b/package.json index ef0a97e..68fbcab 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,13 @@ "dependencies": { "chalk": "^5.3.0", "commander": "^12.1.0", + "express": "^4.21.2", "figlet": "^1.7.0", "fs-extra": "^11.2.0", - "nanospinner": "^1.1.0" + "nanospinner": "^1.1.0", + "supertest": "^7.0.0" }, "devDependencies": { "jest": "^29.7.0" } -} \ No newline at end of file +} diff --git a/templates/basic/basic_api.test.js b/templates/basic/basic_api.test.js new file mode 100644 index 0000000..96e2ad5 --- /dev/null +++ b/templates/basic/basic_api.test.js @@ -0,0 +1,9 @@ +const request = require("supertest"); +const app = require("./server.js"); +describe("API Endpoints", () => { + it("should return Hello World on GET /", async () => { + const res = await request(app).get("/"); + expect(res.statusCode).toEqual(200); + expect(res.text).toBe("Hello World!"); + }); +}); diff --git a/templates/basic/server.js b/templates/basic/server.js index d182777..be4248e 100644 --- a/templates/basic/server.js +++ b/templates/basic/server.js @@ -1,5 +1,4 @@ -import express from "express"; - +const express = require("express"); const app = express(); const port = 3000; @@ -10,3 +9,5 @@ app.get("/", (req, res) => { app.listen(port, () => { console.log(`Example app listening on port ${port}.`); }); + +module.exports = app; \ No newline at end of file diff --git a/test/init.test.js b/test/init.test.js index a5937e7..d58d734 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const util = require('node:util'); +const { afterEach } = require('node:test'); const exec = util.promisify(require('node:child_process').exec); const tempDir = path.join(__dirname, 'temp'); @@ -66,9 +67,16 @@ describe('init', () => { }); test('no templates passed, should default to basic', async () => { + + console.time('Hash Calculation'); const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'basic')); + console.timeEnd('Hash Calculation'); + console.time('Command Execution'); await exec(`node ../../bin/index.js init`, { cwd: tempDir }); + console.timeEnd('Command Execution'); + console.time('Hash Verification'); const commandHash = computeSHA256Hash(tempDir); + console.timeEnd('Hash Verification'); expect(commandHash).toEqual(originalHash); }) From 391825df006596d74376a555836f1b70766157a7 Mon Sep 17 00:00:00 2001 From: Ashrockzzz2003 Date: Sat, 28 Dec 2024 18:28:47 +0530 Subject: [PATCH 02/10] Convert tests to ES6 format. --- package.json | 2 +- templates/basic/server.js | 5 ++- .../{basic_api.test.js => server.test.js} | 5 ++- test/help.test.js | 5 ++- test/init.test.js | 39 ++++++++++--------- test/list.test.js | 5 ++- test/version.test.js | 7 ++-- 7 files changed, 38 insertions(+), 30 deletions(-) rename templates/basic/{basic_api.test.js => server.test.js} (75%) diff --git a/package.json b/package.json index 68fbcab..484fa90 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "main": "index.js", "scripts": { "dev": "node bin/index.js init", - "test": "jest" + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" }, "author": "Abhinav Ramakrishnan, Ashwin Narayanan S", "license": "ISC", diff --git a/templates/basic/server.js b/templates/basic/server.js index be4248e..1125ee8 100644 --- a/templates/basic/server.js +++ b/templates/basic/server.js @@ -1,4 +1,5 @@ -const express = require("express"); +import express from "express"; + const app = express(); const port = 3000; @@ -10,4 +11,4 @@ app.listen(port, () => { console.log(`Example app listening on port ${port}.`); }); -module.exports = app; \ No newline at end of file +export default app; \ No newline at end of file diff --git a/templates/basic/basic_api.test.js b/templates/basic/server.test.js similarity index 75% rename from templates/basic/basic_api.test.js rename to templates/basic/server.test.js index 96e2ad5..3532d9c 100644 --- a/templates/basic/basic_api.test.js +++ b/templates/basic/server.test.js @@ -1,5 +1,6 @@ -const request = require("supertest"); -const app = require("./server.js"); +import app from "./server.js"; +import request from "supertest"; + describe("API Endpoints", () => { it("should return Hello World on GET /", async () => { const res = await request(app).get("/"); diff --git a/test/help.test.js b/test/help.test.js index 68d9664..944e7c4 100644 --- a/test/help.test.js +++ b/test/help.test.js @@ -1,5 +1,6 @@ -const util = require('node:util'); -const exec = util.promisify(require('node:child_process').exec); +import { exec as execCallback } from 'child_process'; +import { promisify } from 'util'; +const exec = promisify(execCallback); const help = `Usage: qse [options] [command] diff --git a/test/init.test.js b/test/init.test.js index d58d734..e611161 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -1,34 +1,37 @@ -const fs = require('fs'); -const path = require('path'); -const crypto = require('crypto'); -const util = require('node:util'); -const { afterEach } = require('node:test'); -const exec = util.promisify(require('node:child_process').exec); +import { createHash } from 'node:crypto'; +import path from 'node:path'; +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs'; +import { fileURLToPath } from 'url'; +import { exec as execCallback } from 'child_process'; +import { promisify } from 'util'; + +const exec = promisify(execCallback); +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const tempDir = path.join(__dirname, 'temp'); function initTempDirectory() { - if (fs.existsSync(tempDir)) { - fs.rmSync(tempDir, { recursive: true }); + if (existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); } - fs.mkdirSync(tempDir); + mkdirSync(tempDir); } function clearTempDirectory() { - if (fs.existsSync(tempDir)) { - fs.rmSync(tempDir, { recursive: true }); + if (existsSync(tempDir)) { + rmSync(tempDir, { recursive: true }); } } function traverseDirectory(dirName, hash) { - const files = fs.readdirSync(dirName); + const files = readdirSync(dirName); for (const file of files) { const filePath = path.join(dirName, file); - const stats = fs.statSync(filePath); + const stats = statSync(filePath); if (stats.isDirectory()) { traverseDirectory(filePath, hash); } else { - const data = fs.readFileSync(filePath); + const data = readFileSync(filePath); hash.update(data); } } @@ -37,19 +40,19 @@ function traverseDirectory(dirName, hash) { // Ignore node_modules and package-lock.json // and compute the SHA256 hash. function computeSHA256Hash(dirName) { - const hash = crypto.createHash('sha256'); - const files = fs.readdirSync(dirName); + const hash = createHash('sha256'); + const files = readdirSync(dirName); for (const file of files) { if (file === 'node_modules' || file === 'package-lock.json' || file === 'package.json') { continue; } const filePath = path.join(dirName, file); - const stats = fs.statSync(filePath); + const stats = statSync(filePath); if (stats.isDirectory()) { traverseDirectory(filePath, hash); } else { - const data = fs.readFileSync(filePath); + const data = readFileSync(filePath); hash.update(data); } } diff --git a/test/list.test.js b/test/list.test.js index 2f6153c..ddfa384 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -1,5 +1,6 @@ -const util = require('node:util'); -const exec = util.promisify(require('node:child_process').exec); +import { exec as execCallback } from 'child_process'; +import { promisify } from 'util'; +const exec = promisify(execCallback); const list = `Available Commands: - -v, --version: Prints current qse version diff --git a/test/version.test.js b/test/version.test.js index 024c7d0..034c4ca 100644 --- a/test/version.test.js +++ b/test/version.test.js @@ -1,6 +1,7 @@ -const util = require('node:util'); -const exec = util.promisify(require('node:child_process').exec); -const packageJson = require('../package.json') +import packageJson from '../package.json'; +import { exec as execCallback } from 'child_process'; +import { promisify } from 'util'; +const exec = promisify(execCallback); describe('Version Command', () => { test('package.json has version', () => { From e03794416db3c3dfcac08b0e44e3cad9ac7ac641 Mon Sep 17 00:00:00 2001 From: K Venkatesh <143533105+venkatesh21bit@users.noreply.github.com> Date: Sat, 28 Dec 2024 19:26:54 +0530 Subject: [PATCH 03/10] Update server.js --- templates/basic/server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templates/basic/server.js b/templates/basic/server.js index 1125ee8..d19462d 100644 --- a/templates/basic/server.js +++ b/templates/basic/server.js @@ -7,8 +7,10 @@ app.get("/", (req, res) => { return res.send("Hello World!"); }); +if (process.env.NODE_ENV !== "test") { app.listen(port, () => { console.log(`Example app listening on port ${port}.`); }); +} -export default app; \ No newline at end of file +export default app; From 71d85fee320275d5f1e950bfb0f88d68fc94f9d1 Mon Sep 17 00:00:00 2001 From: K Venkatesh <143533105+venkatesh21bit@users.noreply.github.com> Date: Sat, 28 Dec 2024 19:50:30 +0530 Subject: [PATCH 04/10] Update index.js --- bin/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/index.js b/bin/index.js index ea3f679..a78fb5b 100755 --- a/bin/index.js +++ b/bin/index.js @@ -108,9 +108,7 @@ async function initCommand(options) { const copySpinner = createSpinner("Creating server files...").start(); try { - await fs.copy(templatePath, targetDir, { - filter: (src) => !src.includes("node_modules"), // Skiping node_modules to optimize - }); + await fs.copy(templatePath, destinationPath); copySpinner.success({ text: "Created server files successfully." }); } catch (err) { From efb9a33f9c7dc66dd28eeadb599ecec008d69298 Mon Sep 17 00:00:00 2001 From: K Venkatesh <143533105+venkatesh21bit@users.noreply.github.com> Date: Sat, 28 Dec 2024 20:47:51 +0530 Subject: [PATCH 05/10] Update init.test.js const hashes --- test/init.test.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/init.test.js b/test/init.test.js index e611161..59d7f91 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -59,6 +59,10 @@ function computeSHA256Hash(dirName) { return hash.digest('hex'); } +// Precomputed hashes +const BASIC_TEMPLATE_HASH = '0b54214731f56e8f661a943b8612b2adadc1a7739cd6d055fd3f88bba5657c16'; +const EXPRESS_PG_SEQUELIZE_TEMPLATE_HASH = '13bd37f300eb11bc7c63012b3c9635d6adcaecff46ded44997f947fd4adf3afb'; +const EXPRESS_MYSQL_TEMPLATE_HASH = '689d3c2957c6efcb17a0dffcc61447bbd92c27d3f9f3b0233c744f9659893534'; describe('init', () => { beforeEach(() => { @@ -84,28 +88,25 @@ describe('init', () => { }) test('basic', async () => { - const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'basic')); await exec(`node ../../bin/index.js init -t basic`, { cwd: tempDir }); const commandHash = computeSHA256Hash(tempDir); - expect(commandHash).toEqual(originalHash); + expect(commandHash).toEqual(BASIC_TEMPLATE_HASH); }); test('express_pg_sequelize', async () => { - const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'express_pg_sequelize')); await exec(`node ../../bin/index.js init -t express_pg_sequelize`, { cwd: tempDir }); const commandHash = computeSHA256Hash(tempDir); - expect(commandHash).toEqual(originalHash); + expect(commandHash).toEqual(EXPRESS_PG_SEQUELIZE_TEMPLATE_HASH); }, 10000); test('express_mysql', async () => { - const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'express_mysql')); await exec(`node ../../bin/index.js init -t express_mysql`, { cwd: tempDir }); const commandHash = computeSHA256Hash(tempDir); - expect(commandHash).toEqual(originalHash); + expect(commandHash).toEqual(EXPRESS_MYSQL_TEMPLATE_HASH); }, 10000); test('invalid template name passed', async () => { const { stdout, stderr } = await exec(`node ../../bin/index.js init -t invalid_name`, { cwd: tempDir }); expect(stderr).toContain(`Template invalid_name does not exist. To see available templates use "qse list".`); }); -}); \ No newline at end of file +}); From 6846850b174ad8249e71d1ba70160d47f9cc2789 Mon Sep 17 00:00:00 2001 From: K Venkatesh <143533105+venkatesh21bit@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:03:48 +0530 Subject: [PATCH 06/10] Update init.test.js --- test/init.test.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/init.test.js b/test/init.test.js index 59d7f91..ac1f628 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -59,10 +59,6 @@ function computeSHA256Hash(dirName) { return hash.digest('hex'); } -// Precomputed hashes -const BASIC_TEMPLATE_HASH = '0b54214731f56e8f661a943b8612b2adadc1a7739cd6d055fd3f88bba5657c16'; -const EXPRESS_PG_SEQUELIZE_TEMPLATE_HASH = '13bd37f300eb11bc7c63012b3c9635d6adcaecff46ded44997f947fd4adf3afb'; -const EXPRESS_MYSQL_TEMPLATE_HASH = '689d3c2957c6efcb17a0dffcc61447bbd92c27d3f9f3b0233c744f9659893534'; describe('init', () => { beforeEach(() => { @@ -88,21 +84,24 @@ describe('init', () => { }) test('basic', async () => { + const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'basic')); await exec(`node ../../bin/index.js init -t basic`, { cwd: tempDir }); const commandHash = computeSHA256Hash(tempDir); - expect(commandHash).toEqual(BASIC_TEMPLATE_HASH); + expect(commandHash).toEqual(originalHash); }); test('express_pg_sequelize', async () => { + const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'express_pg_sequelize')); await exec(`node ../../bin/index.js init -t express_pg_sequelize`, { cwd: tempDir }); const commandHash = computeSHA256Hash(tempDir); - expect(commandHash).toEqual(EXPRESS_PG_SEQUELIZE_TEMPLATE_HASH); + expect(commandHash).toEqual(originalHash); }, 10000); test('express_mysql', async () => { + const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'express_mysql')); await exec(`node ../../bin/index.js init -t express_mysql`, { cwd: tempDir }); const commandHash = computeSHA256Hash(tempDir); - expect(commandHash).toEqual(EXPRESS_MYSQL_TEMPLATE_HASH); + expect(commandHash).toEqual(originalHash); }, 10000); test('invalid template name passed', async () => { From 5e778f86e609668fcd637cc127f392593b1f3405 Mon Sep 17 00:00:00 2001 From: K Venkatesh <143533105+venkatesh21bit@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:09:42 +0530 Subject: [PATCH 07/10] Update init.test.js --- test/init.test.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/init.test.js b/test/init.test.js index ac1f628..2a81203 100644 --- a/test/init.test.js +++ b/test/init.test.js @@ -71,15 +71,9 @@ describe('init', () => { test('no templates passed, should default to basic', async () => { - console.time('Hash Calculation'); const originalHash = computeSHA256Hash(path.join(__dirname, '..', 'templates', 'basic')); - console.timeEnd('Hash Calculation'); - console.time('Command Execution'); await exec(`node ../../bin/index.js init`, { cwd: tempDir }); - console.timeEnd('Command Execution'); - console.time('Hash Verification'); const commandHash = computeSHA256Hash(tempDir); - console.timeEnd('Hash Verification'); expect(commandHash).toEqual(originalHash); }) From 9ac2fd68c2157ccc8bcf42fd044e304deb134a38 Mon Sep 17 00:00:00 2001 From: K Venkatesh <143533105+venkatesh21bit@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:11:37 +0530 Subject: [PATCH 08/10] Update server.js --- templates/basic/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/basic/server.js b/templates/basic/server.js index d19462d..092874d 100644 --- a/templates/basic/server.js +++ b/templates/basic/server.js @@ -8,7 +8,7 @@ app.get("/", (req, res) => { }); if (process.env.NODE_ENV !== "test") { -app.listen(port, () => { + app.listen(port, () => { console.log(`Example app listening on port ${port}.`); }); } From 664301d8a4f6cbfa1befc62fe00fc089bbab0d71 Mon Sep 17 00:00:00 2001 From: Ashrockzzz2003 Date: Sat, 28 Dec 2024 22:23:58 +0530 Subject: [PATCH 09/10] Update formatting. --- bin/index.js | 2 +- templates/basic/server.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/index.js b/bin/index.js index a78fb5b..923d618 100755 --- a/bin/index.js +++ b/bin/index.js @@ -77,7 +77,7 @@ program console.error(error); } }); -//Changing sync to async to optimize further + async function initCommand(options) { const selectedTemplate = options.template || "basic"; // Default to 'basic' if no template is specified diff --git a/templates/basic/server.js b/templates/basic/server.js index 092874d..095fc7f 100644 --- a/templates/basic/server.js +++ b/templates/basic/server.js @@ -9,8 +9,8 @@ app.get("/", (req, res) => { if (process.env.NODE_ENV !== "test") { app.listen(port, () => { - console.log(`Example app listening on port ${port}.`); -}); + console.log(`Example app listening on port ${port}.`); + }); } export default app; From d6426da233684823e8c6a173111aaa298f8156ab Mon Sep 17 00:00:00 2001 From: Ashrockzzz2003 Date: Mon, 30 Dec 2024 09:08:04 +0530 Subject: [PATCH 10/10] Moved express and supertest to dev deps. --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 484fa90..05275c0 100644 --- a/package.json +++ b/package.json @@ -14,13 +14,13 @@ "dependencies": { "chalk": "^5.3.0", "commander": "^12.1.0", - "express": "^4.21.2", "figlet": "^1.7.0", "fs-extra": "^11.2.0", - "nanospinner": "^1.1.0", - "supertest": "^7.0.0" + "nanospinner": "^1.1.0" }, "devDependencies": { - "jest": "^29.7.0" + "jest": "^29.7.0", + "supertest": "^7.0.0", + "express": "^4.21.2" } }