Skip to content

Commit 1768704

Browse files
a
1 parent ed3f293 commit 1768704

File tree

5 files changed

+172
-3
lines changed

5 files changed

+172
-3
lines changed

Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Added mongodb file with all
3434
>> DELETE
3535
Operation just call mongodb file where ever required
3636

37+
#Added Redis Sample code
38+
* Connect application with redis
39+
* Install redis server
40+
* Sample code is in src/node-redis-sample.js
41+
3742
## Added view engine layer
3843
* added ejs script
3944

bin/cli.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs-extra');
4+
const path = require('path');
5+
const https = require('https');
6+
const { exec } = require('child_process');
7+
8+
const packageJson = require('../package.json');
9+
10+
const scripts = `"start": "node start.js",
11+
"dev": "nodemon start.js",
12+
"test": "mocha --timeout 10000"`;
13+
14+
const jestConfig = `"license": "ISC",
15+
"jest": {
16+
"moduleFileExtensions": [
17+
"js",
18+
"jsx"
19+
],
20+
"moduleDirectories": [
21+
"node_modules"
22+
],
23+
"setupFiles": [
24+
"<rootDir>/src/tests/setup.js"
25+
],
26+
"moduleNameMapper": {
27+
"\\\\.(css|styl|less|sass|scss)$": "identity-obj-proxy"
28+
},
29+
"transform": {
30+
"^.+\\\\.js$": "babel-jest",
31+
"^.+\\\\.jsx$": "babel-jest",
32+
"\\\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/src/tests/__mock__/fileTransformer.js"
33+
}
34+
}`;
35+
36+
/**
37+
* we pass the object key dependency || devdependency to this function
38+
* @param {object} deps object key that we want to extract
39+
* @returns {string} a string of 'dependencies@version'
40+
* that we can attach to an `npm i {value}` to install
41+
* every dep the exact version speficied in package.json
42+
*/
43+
const getDeps = deps =>
44+
Object.entries(deps)
45+
.map(dep => `${dep[0]}@${dep[1]}`)
46+
.toString()
47+
.replace(/,/g, ' ')
48+
.replace(/^/g, '')
49+
// exclude the plugin only used in this file, nor relevant to the boilerplate
50+
.replace(/fs-extra[^\s]+/g, '');
51+
52+
console.log('Initializing project..');
53+
54+
// create folder and initialize npm
55+
exec(
56+
`mkdir ${process.argv[2]} && cd ${process.argv[2]} && npm init -f`,
57+
(initErr, initStdout, initStderr) => {
58+
if (initErr) {
59+
console.error(`Everything was fine, then it wasn't:
60+
${initErr}`);
61+
return;
62+
}
63+
const packageJSON = `${process.argv[2]}/package.json`;
64+
// replace the default scripts, with the webpack scripts in package.json
65+
fs.readFile(packageJSON, (err, file) => {
66+
if (err) throw err;
67+
const data = file
68+
.toString()
69+
.replace('"test": "echo \\"Error: no test specified\\" && exit 1"', scripts)
70+
.replace('"license": "ISC"', jestConfig);
71+
fs.writeFile(packageJSON, data, err2 => err2 || true);
72+
});
73+
74+
const filesToCopy = ['README.md','.babelrc'];
75+
76+
for (let i = 0; i < filesToCopy.length; i += 1) {
77+
fs
78+
.createReadStream(path.join(__dirname, `../${filesToCopy[i]}`))
79+
.pipe(fs.createWriteStream(`${process.argv[2]}/${filesToCopy[i]}`));
80+
}
81+
82+
// npm will remove the .gitignore file when the package is installed, therefore it cannot be copied
83+
// locally and needs to be downloaded. See https://github.com/Kornil/simple-react-app/issues/12
84+
https.get(
85+
'https://raw.githubusercontent.com/Aakashdeveloper/create-node-app/master/.gitignore',
86+
(res) => {
87+
res.setEncoding('utf8');
88+
let body = '';
89+
res.on('data', (data) => {
90+
body += data;
91+
});
92+
res.on('end', () => {
93+
fs.writeFile(`${process.argv[2]}/.gitignore`, body, { encoding: 'utf-8' }, (err) => {
94+
if (err) throw err;
95+
});
96+
});
97+
},
98+
);
99+
100+
console.log('npm init -- done\n');
101+
102+
// installing dependencies
103+
console.log('Installing deps -- it might take a few minutes..');
104+
const devDeps = getDeps(packageJson.devDependencies);
105+
const deps = getDeps(packageJson.dependencies);
106+
exec(
107+
`cd ${process.argv[2]} && npm i -D ${devDeps} && npm i -S ${deps}`,
108+
(npmErr, npmStdout, npmStderr) => {
109+
if (npmErr) {
110+
console.error(`it's always npm, ain't it?
111+
${npmErr}`);
112+
return;
113+
}
114+
console.log(npmStdout);
115+
console.log('Dependencies installed');
116+
117+
console.log('Copying additional files..');
118+
// copy additional source files
119+
fs
120+
.copy(path.join(__dirname, '../src'), `${process.argv[2]}/src`)
121+
.then(() =>
122+
console.log(`All done!\nYour project is now started into ${
123+
process.argv[2]
124+
} folder, refer to the README for the project structure.\nHappy Coding!`))
125+
.catch(err => console.error(err));
126+
},
127+
);
128+
},
129+
);

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aakashdeveloper/create-node-app",
3-
"version": "1.0.33",
3+
"version": "1.0.41",
44
"description": "The Seed will help you build node app with es6 very quick",
55
"main": "index.js",
66
"scripts": {
@@ -18,21 +18,30 @@
1818
"Es6",
1919
"Docker",
2020
"babel",
21-
"EJS"
21+
"EJS",
22+
"Redis",
23+
"MongoDB"
2224
],
2325
"author": "aakashdeveloper",
2426
"license": "MIT",
2527
"bugs": {
2628
"url": "https://github.com/Aakashdeveloper/create-node-app/issues"
2729
},
2830
"homepage": "https://github.com/Aakashdeveloper/create-node-app#readme",
31+
"bin": {
32+
"create-node-app": "./bin/cli.js"
33+
},
34+
"preferGlobal": true,
2935
"dependencies": {
3036
"@babel/core": "^7.4.3",
3137
"@babel/preset-env": "^7.4.3",
3238
"@babel/register": "^7.4.0",
3339
"babel-polyfill": "^6.26.0",
3440
"ejs": "^2.6.1",
3541
"express": "^4.16.4",
36-
"mongodb": "^3.2.3"
42+
"mongodb": "^3.2.3",
43+
"redis": "^2.8.0",
44+
"bcryptjs": "^2.4.3",
45+
"jsonwebtoken": "^8.5.1"
3746
}
3847
}

src/datatbase/redisconnect.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const express = require('express');
2+
const redis = require('redis');
3+
const app = express();
4+
5+
const client = redis.createClient({
6+
host: '127.0.0.1',
7+
port: 6379
8+
})
9+
10+
client.set('visits',0)
11+
12+
app.get('/',(req,res) => {
13+
client.get('visits',(err,visits) => {
14+
res.send('Number of visit '+ visits)
15+
client.set('visits',parseInt(visits)+1)
16+
})
17+
18+
})
19+
20+
app.listen(6700,() => {
21+
console.log(`Server running on port 6700`)
22+
})

test/setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configure } from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
3+
4+
configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)