Skip to content

Commit 7187151

Browse files
Merge pull request #6 from TECHOUS/dev
Code Style Changes
2 parents 0c0e639 + c492349 commit 7187151

20 files changed

+858
-359
lines changed

.github/job.js

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
const { default: axios } = require('axios');
2-
const codeRatingModel = require('../models/codeRatingModel');
3-
const mongoose = require('mongoose');
4-
const fs = require('fs');
1+
const {default: axios} = require('axios')
2+
const codeRatingModel = require('../models/codeRatingModel')
3+
const mongoose = require('mongoose')
4+
const fs = require('fs')
5+
6+
require('dotenv').config()
57

68
// function to read the code file and sends the username in callback function
79
const getUserNameFromCodeBaseFile = (fileName) => {
@@ -10,78 +12,89 @@ const getUserNameFromCodeBaseFile = (fileName) => {
1012
if (err) {
1113
reject(err)
1214
} else {
13-
const lines = buf.toString().split("\n");
14-
let username = "";
15+
const lines = buf.toString().split('\n')
16+
let username = ''
1517
for (let i = 0; i < lines.length; i++) {
1618
if (lines[i].search('AUTHOR:') != -1) {
1719
username = lines[i].split('AUTHOR:')[1].trim()
18-
break;
20+
break
1921
}
2022
}
2123
fileName = fileName.split('/')[2]
22-
resolve({ username, fileName })
24+
resolve({username, fileName})
2325
}
2426
})
2527
})
2628
}
2729

2830
// function to connect to mongodb using mongoose
2931
const connectMongoDB = () => {
30-
require('dotenv').config()
31-
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
32-
const db = mongoose.connection;
33-
db.on('error', console.error.bind(console, 'Connection error:'));
34-
return db;
32+
mongoose.connect(process.env.MONGODB_URI, {
33+
useNewUrlParser: true,
34+
useUnifiedTopology: true,
35+
useCreateIndex: true,
36+
})
37+
const db = mongoose.connection
38+
db.on('error', console.error.bind(console, 'Connection error:'))
39+
return db
3540
}
3641

3742
// uses single call to mongo db
3843
const insertDataInMongo = (records) => {
3944
const db = connectMongoDB()
4045

41-
codeRatingModel.insertMany(records,{ordered: false})
46+
codeRatingModel
47+
.insertMany(records, {ordered: false})
4248
.then(() => {
43-
console.log("Data added successfully")
44-
db.close();
49+
console.log('Data added successfully')
50+
db.close()
4551
})
4652
.catch((err) => {
47-
console.log("Data is not added: " + err);
48-
db.close();
53+
console.log('Data is not added: ' + err)
54+
db.close()
4955
})
5056
}
5157

5258
// this function handles calling the API
5359
const callGithubCodeBaseAPI = () => {
54-
axios.get('https://api.github.com/repos/TechOUs/HacktoberFest21Community/contents/CodeBase')
55-
.then(res => {
56-
60+
axios
61+
.get(
62+
`https://api.github.com/repos/TECHOUS/${process.env.CODE_SRC}/contents/CodeBase`
63+
)
64+
.then((res) => {
5765
// get all the names from the files
5866
const filePromises = res.data.map((codeObject) => {
59-
return getUserNameFromCodeBaseFile(`HacktoberFest21Community/${codeObject.path}`)
67+
return getUserNameFromCodeBaseFile(
68+
`${process.env.CODE_SRC}/${codeObject.path}`
69+
)
6070
})
6171

6272
// resolve/reject all the file promises
63-
Promise.allSettled(filePromises)
64-
.then((fileObjects) => {
65-
66-
// creating a map of filename to usernames
67-
const fileMap = new Map(fileObjects.map((fileObject) => {
68-
return [fileObject.value.fileName, fileObject.value.username];
69-
}))
70-
71-
const dataToInsert = res.data.map((data) => {
72-
return {
73-
codeId: data.sha,
74-
codeRating: 0,
75-
codeUrl: data.url,
76-
codeName: data.name,
77-
userName: fileMap.get(data.name)
78-
}
73+
Promise.allSettled(filePromises).then((fileObjects) => {
74+
// creating a map of filename to usernames
75+
const fileMap = new Map(
76+
fileObjects.map((fileObject) => {
77+
return [
78+
fileObject.value.fileName,
79+
fileObject.value.username,
80+
]
7981
})
82+
)
8083

81-
insertDataInMongo(dataToInsert);
84+
const dataToInsert = res.data.map((data) => {
85+
return {
86+
codeId: data.sha,
87+
codeRating: 0,
88+
codeUrl: data.url,
89+
codeName: data.name,
90+
userName: fileMap.get(data.name),
91+
}
8292
})
93+
94+
insertDataInMongo(dataToInsert)
95+
})
8396
})
84-
.catch(err => console.log(err))
97+
.catch((err) => console.log(err))
8598
}
8699

87-
callGithubCodeBaseAPI();
100+
callGithubCodeBaseAPI()

.github/job.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# /bin/bash
22
# this shell script it to read all the code from the community project and
33
# upload to mongodb run this script locally only from root
4-
# bash .github/job.sh
54

6-
git clone "https://github.com/TechOUs/HacktoberFest21Community.git"
5+
git clone "https://github.com/TECHOUS/$CODE_SRC.git"
76

87
node .github/job.js
98

109
# cleanup
1110
echo "Cleanup..."
12-
rm -rf HacktoberFest21Community
11+
12+
rm -rf $CODE_SRC
13+
1314
echo "Cleanup...Done"

.github/workflows/Vercel Deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v2
1414
- uses: actions/setup-node@v2
1515
with:
16-
node-version: '14.x'
16+
node-version: '16.x'
1717
- run: yarn install
1818
- run: yarn run test
1919
env:

.github/workflows/codebase-engine.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ jobs:
99
- uses: actions/checkout@v2
1010
- uses: actions/setup-node@v2
1111
with:
12-
node-version: '14.x'
13-
- run: npm install
12+
node-version: '16.x'
13+
- run: yarn install
1414
- run: bash .github/job.sh
1515
env:
1616
MONGODB_URI: ${{ secrets.MONGODB_URI }}
1717
RATE_LIMIT_MINUTES: ${{ secrets.RATE_LIMIT_MINUTES }}
1818
RATE_LIMIT_MAX_REQUEST: ${{ secrets.RATE_LIMIT_MAX_REQUEST }}
19-
CACHE_STORAGE_SECONDS: ${{ secrets.CACHE_STORAGE_SECONDS }}
19+
CACHE_STORAGE_SECONDS: ${{ secrets.CACHE_STORAGE_SECONDS }}
20+
CODE_SRC: ${{secrets.CODE_SRC}}

.prettierignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ignore files for prettier
2+
3+
.github/workflows
4+
.vercel
5+
node_modules
6+
.env
7+
.gitignore
8+
.vercelignore
9+
README.md
10+
vercel.json
11+
yarn.lock

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"semi": false,
4+
"bracketSpacing": false,
5+
"tabWidth": 4
6+
}

.vercelignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ node_modules
55
test
66
.env
77
.gitignore
8+
.prettierignore
9+
.prettierrc.json
810
README.md
9-
HacktoberFest21Community
11+
coderatefest

app.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
const express = require('express');
2-
const cors = require('cors');
3-
const rateLimit = require("express-rate-limit");
1+
const express = require('express')
2+
const cors = require('cors')
3+
const rateLimit = require('express-rate-limit')
44

5-
const app = express();
5+
const app = express()
66

77
// exclusing dotenv config from production
88
if (process.env.NODE_ENV !== 'production') require('dotenv').config()
99

1010
// setting up the rate limiter
1111
const rateLimiter = rateLimit({
12-
windowMs: process.env.RATE_LIMIT_MINUTES * 60 * 1000, // 1 minute
12+
windowMs: process.env.RATE_LIMIT_MINUTES * 60 * 1000, // 1 minute
1313
max: process.env.RATE_LIMIT_MAX_REQUEST,
1414
message: {
1515
status: 429,
16-
error: "Too many requests, please try again later!!!"
17-
}
18-
});
16+
error: 'Too many requests, please try again later!!!',
17+
},
18+
})
1919

2020
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
2121
// see https://expressjs.com/en/guide/behind-proxies.html
22-
app.set('trust proxy', 1);
22+
app.set('trust proxy', 1)
2323

2424
// rate limiting middleware
25-
app.use(rateLimiter);
25+
app.use(rateLimiter)
2626

2727
// CORS Middleware
28-
app.use(cors());
28+
app.use(cors())
2929

30-
// express middleware handling the body parsing
31-
app.use(express.json());
30+
// express middleware handling the body parsing
31+
app.use(express.json())
3232

3333
// express middleware handling the form parsing
34-
app.use(express.urlencoded({extended: false}));
34+
app.use(express.urlencoded({extended: false}))
3535

3636
// middleware for handling sample api routes
37-
app.use('/api/v1', require('./routes/api/API'));
37+
app.use('/api/v1', require('./routes/api/v1/API'))
3838

39-
module.exports = app;
39+
module.exports = app

bin/www

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env node
22

3-
const app = require('../app');
3+
const app = require('../app')
44

55
// use port from environment variables for production
6-
const PORT = process.env.PORT || 5000;
6+
const PORT = process.env.PORT || 5000
77

8-
app.listen(PORT,()=>{
9-
console.log(`server running on port ${PORT}`);
10-
})
8+
app.listen(PORT, () => {
9+
console.log(`server running on port ${PORT}`)
10+
})

models/accessToken.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const mongoose = require('mongoose');
1+
const mongoose = require('mongoose')
22

3-
const Schema = mongoose.Schema;
3+
const Schema = mongoose.Schema
44

55
const accessTokenModel = new Schema({
66
token: {
77
type: String,
88
unique: true,
9-
required: true
9+
required: true,
1010
},
1111
timestamp: {
12-
type: Date
13-
}
12+
type: Date,
13+
},
1414
})
1515

16-
module.exports = mongoose.model('accessToken', accessTokenModel);
16+
module.exports = mongoose.model('accessToken', accessTokenModel)

0 commit comments

Comments
 (0)