diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit new file mode 100755 index 0000000..18bd756 --- /dev/null +++ b/.git-hooks/pre-commit @@ -0,0 +1,70 @@ +#!/usr/bin/env node + +const { execSync } = require("child_process"); +const { existsSync, promises: fsPromises } = require("fs"); +const { join, resolve } = require("path"); + +const excludedDirs = ["node_modules"]; + +const getAllJsFiles = async (dir) => { + const getFilesRecursively = async (currentDir) => { + const files = await fsPromises.readdir(currentDir); + let jsFiles = []; + + for (const file of files) { + const filePath = join(currentDir, file); + const fileStat = await fsPromises.stat(filePath); + + if (fileStat.isDirectory() && !excludedDirs.includes(file)) { + jsFiles.push(...(await getFilesRecursively(filePath))); + } else if (file.endsWith(".js")) { + jsFiles.push(filePath); + } + } + + return jsFiles; + }; + + try { + return await getFilesRecursively(dir); + } catch (error) { + console.error("Error occurred while getting JS files:", error.message); + throw error; + } +}; + +const runPrettierOnAllJsFiles = async () => { + try { + const projectDir = resolve(process.cwd()); + const allJsFiles = await getAllJsFiles(projectDir); + + console.log("Code formatting..."); + + if (allJsFiles.length > 0) { + let prettierCmd = "npx prettier"; + const localPrettierPath = resolve(projectDir, "node_modules/.bin/prettier"); + if (existsSync(localPrettierPath)) { + prettierCmd = localPrettierPath; + } + + prettierCmd += ` --config ${resolve(projectDir, ".prettierrc.json")} --write ${allJsFiles.join(" ")}`; + + execSync(prettierCmd, { stdio: "inherit" }); + + console.log("Prettier formatting applied to all JS files."); + + const gitAddCmd = `git add ${allJsFiles.join(" ")}`; + execSync(gitAddCmd); + + console.log("Staged all JS files."); + } else { + console.log("No JS files found in the repository."); + } + } catch (error) { + console.error("An error occurred during Prettier execution:", error.message); + process.exit(1); + } +}; + +runPrettierOnAllJsFiles(); + diff --git a/README.md b/README.md index dc4cce4..b5f6441 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ If you prefer the command line way of downloading and installing things, then fe ##### `git clone https://github.com/lugenx/ecohabit.git` - Open the cloned folder in VS Code. Open the terminal and make sure its pointing to the root of the cloned project. +- Set custom path for git hooks using terminal command `git config core.hooksPath .git-hooks` in the root directory of the cloned repository; + - Running Backend Node JS Application: - Change the directory to server folder using terminal command: `cd server` diff --git a/client/package-lock.json b/client/package-lock.json index da24e10..296b844 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -26,6 +26,9 @@ "react-scripts": "5.0.1", "styled-components": "^5.3.6", "web-vitals": "^2.1.4" + }, + "devDependencies": { + "prettier": "^3.0.1" } }, "node_modules/@adobe/css-tools": { @@ -14367,6 +14370,21 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", + "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/pretty-bytes": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz", @@ -27731,6 +27749,12 @@ "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==" }, + "prettier": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", + "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "dev": true + }, "pretty-bytes": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz", diff --git a/client/package.json b/client/package.json index d54ddfa..16d1b1b 100644 --- a/client/package.json +++ b/client/package.json @@ -45,5 +45,8 @@ "last 1 firefox version", "last 1 safari version" ] + }, + "devDependencies": { + "prettier": "^3.0.1" } } diff --git a/client/src/components/Main.js b/client/src/components/Main.js index d8c778a..9d6c446 100644 --- a/client/src/components/Main.js +++ b/client/src/components/Main.js @@ -30,7 +30,8 @@ const Main = () => { flexDirection="column" minHeight="550px" justifyContent="space-between" - marginTop="auto"> + marginTop="auto" + > { . - - Join a community of like-minded individuals and make a positive impact - on the planet → + + Join a community of like-minded individuals and make a positive + impact on the planet → diff --git a/client/src/components/Recycle.js b/client/src/components/Recycle.js index b122ac7..3655624 100644 --- a/client/src/components/Recycle.js +++ b/client/src/components/Recycle.js @@ -58,7 +58,7 @@ const Recycle = () => { sx={{ display: "flex", flexFlow: "row wrap", - justifyContent: "center", + justifyContent: "center", gap: 2, m: "0 0 20px 0", }} diff --git a/mockapi-earth911/package-lock.json b/mockapi-earth911/package-lock.json index 8e590ca..6dfcc83 100644 --- a/mockapi-earth911/package-lock.json +++ b/mockapi-earth911/package-lock.json @@ -13,6 +13,9 @@ "express": "^4.18.2", "express-rate-limit": "^6.7.0", "node-fetch": "^3.3.0" + }, + "devDependencies": { + "prettier": "^3.0.1" } }, "node_modules/accepts": { @@ -502,6 +505,21 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, + "node_modules/prettier": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", + "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -1038,6 +1056,12 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, + "prettier": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", + "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "dev": true + }, "proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", diff --git a/mockapi-earth911/package.json b/mockapi-earth911/package.json index 70c76fc..97868c5 100644 --- a/mockapi-earth911/package.json +++ b/mockapi-earth911/package.json @@ -15,5 +15,8 @@ "author": "", "license": "ISC", "description": "", - "type": "module" + "type": "module", + "devDependencies": { + "prettier": "^3.0.1" + } } diff --git a/server/package-lock.json b/server/package-lock.json index 61c43c0..08a300b 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -17,6 +17,9 @@ "mongoose": "^6.11.3", "node-fetch": "^3.3.0", "validator": "^13.9.0" + }, + "devDependencies": { + "prettier": "^3.0.1" } }, "node_modules/@aws-crypto/crc32": { @@ -2388,6 +2391,21 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, + "node_modules/prettier": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", + "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -4694,6 +4712,12 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, + "prettier": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.1.tgz", + "integrity": "sha512-fcOWSnnpCrovBsmFZIGIy9UqK2FaI7Hqax+DIO0A9UxeVoY4iweyaFjS5TavZN97Hfehph0nhsZnjlVKzEQSrQ==", + "dev": true + }, "proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", diff --git a/server/package.json b/server/package.json index 08388be..f71102f 100644 --- a/server/package.json +++ b/server/package.json @@ -19,5 +19,8 @@ "node-fetch": "^3.3.0", "validator": "^13.9.0" }, - "type": "module" + "type": "module", + "devDependencies": { + "prettier": "^3.0.1" + } } diff --git a/server/src/controller/habitController.js b/server/src/controller/habitController.js index 594a860..7a5723c 100644 --- a/server/src/controller/habitController.js +++ b/server/src/controller/habitController.js @@ -34,9 +34,7 @@ const getHabit = async (req, res) => { try { //check if string format is correct if (!mongoose.Types.ObjectId.isValid(id)) { - return res - .status(400) - .json({ error: "Bad Request" }); + return res.status(400).json({ error: "Bad Request" }); } const habit = await Habit.findById(id); @@ -57,9 +55,7 @@ const updateHabit = async (req, res) => { try { if (!mongoose.Types.ObjectId.isValid(id)) { - return res - .status(400) - .json({ error: "Bad Request" }); + return res.status(400).json({ error: "Bad Request" }); } const habit = await Habit.findOneAndUpdate({ _id: id }, { ...req.body }); @@ -78,9 +74,7 @@ const deleteHabit = async (req, res) => { try { if (!mongoose.Types.ObjectId.isValid(id)) { - return res - .status(400) - .json({ error: "Bad Request" }); + return res.status(400).json({ error: "Bad Request" }); } const habit = await Habit.findOneAndDelete({ _id: id }); diff --git a/server/src/middleware/errorHandler.js b/server/src/middleware/errorHandler.js index 90d2215..14a4769 100644 --- a/server/src/middleware/errorHandler.js +++ b/server/src/middleware/errorHandler.js @@ -13,4 +13,3 @@ const errorHandler = (err, req, res, next) => { }; export { notFound, errorHandler }; - diff --git a/server/src/services/earth911services.js b/server/src/services/earth911services.js index 5fc3cdb..8f05bc5 100644 --- a/server/src/services/earth911services.js +++ b/server/src/services/earth911services.js @@ -1,41 +1,52 @@ import fetch from "node-fetch"; const getPostalData = async (country, postalCode) => { - const response = (await fetch(`${process.env.EARTH911_BASE_URL}/earth911.getPostalData?api_key=${process.env.EARTH911_API_KEY}&country=${country}&postal_code=${postalCode}`)); - return await response.json(); -} + const response = await fetch( + `${process.env.EARTH911_BASE_URL}/earth911.getPostalData?api_key=${process.env.EARTH911_API_KEY}&country=${country}&postal_code=${postalCode}` + ); + return await response.json(); +}; const searchLocations = async (longitude, latitude) => { - const response = await fetch(`${process.env.EARTH911_BASE_URL}/earth911.searchLocations?api_key=${process.env.EARTH911_API_KEY}&longitude=${longitude}&latitude=${latitude}`); - return await response.json(); -} + const response = await fetch( + `${process.env.EARTH911_BASE_URL}/earth911.searchLocations?api_key=${process.env.EARTH911_API_KEY}&longitude=${longitude}&latitude=${latitude}` + ); + return await response.json(); +}; const getLocationDetails = async (locationIds) => { - const response = await fetch(`${process.env.EARTH911_BASE_URL}/earth911.getLocationDetails?api_key=${process.env.EARTH911_API_KEY}&${locationIds}`); - return await response.json(); -} - + const response = await fetch( + `${process.env.EARTH911_BASE_URL}/earth911.getLocationDetails?api_key=${process.env.EARTH911_API_KEY}&${locationIds}` + ); + return await response.json(); +}; const getCenterDataWithPostal = async (country, postal_code) => { - try { - console.log("getCenterDataWithPostal"); - const postalData = (await getPostalData(country, postal_code)).result; - if (!postalData) return []; - const locationsData = (await searchLocations(postalData.longitude, postalData.latitude)).result; - if (!locationsData) return []; - const query = ('location_id[]=' + locationsData.map((location) => location.location_id).join("&location_id[]=")); - const locationDetails = (await getLocationDetails(query)).result; - if (!locationDetails) return []; - return locationsData.map((location) => { - const locationDetail = locationDetails[location.location_id] || {}; - return { - ...location, - detail: locationDetail - } - }); - } catch (err) { - console.error(`Error fetching from EARTH911 Api`, err.message); - } -} + try { + console.log("getCenterDataWithPostal"); + const postalData = (await getPostalData(country, postal_code)).result; + if (!postalData) return []; + const locationsData = ( + await searchLocations(postalData.longitude, postalData.latitude) + ).result; + if (!locationsData) return []; + const query = + "location_id[]=" + + locationsData + .map((location) => location.location_id) + .join("&location_id[]="); + const locationDetails = (await getLocationDetails(query)).result; + if (!locationDetails) return []; + return locationsData.map((location) => { + const locationDetail = locationDetails[location.location_id] || {}; + return { + ...location, + detail: locationDetail, + }; + }); + } catch (err) { + console.error(`Error fetching from EARTH911 Api`, err.message); + } +}; export { getCenterDataWithPostal };