Skip to content

Commit 4a057f9

Browse files
committed
refactor(app): es6 syntatic sugar and add eslint
1 parent 445dc1f commit 4a057f9

29 files changed

+351
-241
lines changed

.gitignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pids
1212
lib-cov
1313

1414
# Coverage directory used by tools like istanbul
15-
coverage
15+
coverage/
1616

1717
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
1818
.grunt
@@ -31,3 +31,9 @@ node_modules
3131

3232
# Optional REPL history
3333
.node_repl_history
34+
35+
#compiled code
36+
lib/
37+
38+
# IDE
39+
.idea/

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/

bin/generate-container.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/commands/generate-container.js');

bin/generate-dumb.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/commands/generate-dumb.js');

bin/generate-fullstack.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/commands/generate-fullstack.js');

bin/generate-functional.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/commands/generate-functional.js');

bin/generate-model.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/commands/generate-model.js');
File renamed without changes.
File renamed without changes.
File renamed without changes.

package.json

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
11
{
22
"name": "mern-cli",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "A cli for generating MERN boilerplate",
55
"main": "index.js",
66
"scripts": {
7+
"compile": "babel --presets es2015,stage-0 -d lib/ src/",
8+
"lint": "eslint . --ignore-path .gitignore",
9+
"lint:staged": "eslint-staged",
10+
"prepublish": "npm run compile",
11+
"pretest": "npm run lint",
712
"test": "ava tests/*.js"
813
},
14+
"pre-commit": "lint:staged",
15+
"eslintConfig": {
16+
"parser": "babel-eslint",
17+
"extends": "airbnb-base",
18+
"env": {
19+
"node": true,
20+
"es6": true,
21+
"shelljs": true
22+
},
23+
"parserOptions": {
24+
"ecmaVersion": 6,
25+
"sourceType": "module"
26+
},
27+
"rules": {
28+
"import/no-unresolved": 2,
29+
"comma-dangle": [
30+
2,
31+
"always-multiline"
32+
],
33+
"indent": [
34+
2,
35+
4,
36+
{
37+
"SwitchCase": 1
38+
}
39+
],
40+
"max-len": 0,
41+
"no-console": 0,
42+
"prefer-template": 2,
43+
"no-use-before-define": 0,
44+
"newline-per-chained-call": 0,
45+
"arrow-body-style": [
46+
2,
47+
"as-needed"
48+
]
49+
}
50+
},
951
"engines": {
1052
"node": ">=4"
1153
},
@@ -45,6 +87,14 @@
4587
},
4688
"devDependencies": {
4789
"ava": "^0.11.0",
90+
"babel-cli": "^6.9.0",
91+
"babel-eslint": "^6.0.4",
92+
"babel-preset-es2015": "^6.9.0",
93+
"babel-preset-stage-0": "^6.5.0",
94+
"eslint": "^2.10.2",
95+
"eslint-config-airbnb-base": "^3.0.1",
96+
"eslint-plugin-import": "^1.8.0",
97+
"lint-staged": "^0.2.2",
4898
"pify": "^2.3.0"
4999
}
50100
}

settings.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
2-
PROJECT_DIR : __dirname,
3-
BLUEPRINT_NAME : 'blueprint.js',
4-
BLUEPRINT_DIRECTORY_NAME : 'blueprints',
2+
PROJECT_DIR: __dirname,
3+
BLUEPRINT_NAME: 'blueprint.ejs',
4+
BLUEPRINT_DIRECTORY_NAME: 'blueprints',
55
CONFIG_FILE_NAME: 'mern.json',
6-
COMPONENTS: ['container','dumb','functional']
7-
};
6+
COMPONENTS: ['container', 'dumb', 'functional'],
7+
};

src/commands/generate-container.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import program from 'commander';
2+
import Generate from '../tasks/generate';
3+
4+
program
5+
.action(() => {
6+
new Generate().run(['container', ...program.args]);
7+
})
8+
.parse(process.argv);
9+
10+
if (!program.args.length) {
11+
program.help();
12+
}

src/commands/generate-dumb.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import program from 'commander';
2+
import Generate from '../tasks/generate';
3+
4+
program
5+
.action(() => {
6+
new Generate().run(['dumb', ...program.args]);
7+
})
8+
.parse(process.argv);
9+
10+
if (!program.args.length) {
11+
program.help();
12+
}

src/commands/generate-fullstack.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import program from 'commander';
2+
import Generate from '../tasks/generate';
3+
4+
program
5+
.action(() => {
6+
new Generate().run(['fullstack', ...program.args]);
7+
})
8+
.parse(process.argv);
9+
10+
if (!program.args.length) {
11+
program.help();
12+
}

src/commands/generate-functional.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import program from 'commander';
2+
import Generate from '../tasks/generate';
3+
4+
program
5+
.action(() => {
6+
new Generate().run(['functional', ...program.args]);
7+
})
8+
.parse(process.argv);
9+
10+
if (!program.args.length) {
11+
program.help();
12+
}

src/commands/generate-model.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import program from 'commander';
2+
import Generate from '../tasks/generate';
3+
4+
program
5+
.action(() => {
6+
new Generate().run(['model', ...program.args]);
7+
})
8+
.parse(process.argv);
9+
10+
if (!program.args.length) {
11+
program.help();
12+
}

src/commands/generate.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
var program = require('commander');
2-
var generate = require('../tasks/generate');
3-
4-
//program.description need to be refractored
1+
import program from 'commander';
52

63
program
7-
.description('Generate components, routes, controllers, models using mern generator\n\n merng dumb <componentname>\n merng functional <componentName>\n merng container <componentName>\n merng route <routeName>\n merng model <modelName>\n merng fullstack <modelName>\n')
8-
.action(function() {
9-
new generate().run(program.args);
10-
})
11-
.parse(process.argv);
4+
.description('Generate components, routes, controllers, models using mern generator')
5+
.command('dumb [component_name]', 'Generate a dumb component')
6+
.command('functional [component_name]', 'Generate a functional component')
7+
.command('container [component_name]', 'Generate a container component')
8+
.command('model [model_name]', 'Generate a mongoose model')
9+
.command('fullstack [component_name]', 'Generate a dumb component, controller and mongoose model')
10+
.parse(process.argv);
1211

13-
if(!program.args.length) {
14-
program.help();
15-
}
12+
if (!program.args.length) {
13+
program.help();
14+
}

src/commands/main.js

+40-39
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
var program = require('commander');
2-
var chalk = require('chalk');
3-
var elegantSpinner = require('elegant-spinner');
4-
var logUpdate = require('log-update');
5-
var frame = elegantSpinner();
1+
import program from 'commander';
2+
import chalk from 'chalk';
3+
import elegantSpinner from 'elegant-spinner';
4+
import logUpdate from 'log-update';
5+
import pjson from '../../package.json';
66
require('shelljs/global');
7-
var pjson = require('../../package.json');
87

8+
const frame = elegantSpinner();
99

1010
program
11-
.version(pjson.version)
12-
.description('Create a MERN app in current directory!')
13-
.option('-v, --version', 'check version')
14-
.parse(process.argv);
11+
.version(pjson.version)
12+
.description('Create a MERN app in current directory!')
13+
.option('-v, --version', 'check version')
14+
.parse(process.argv);
1515

1616

1717
if (!which('git')) {
18-
console.log(chalk.red('Sorry, this script requires git'));
19-
exit(1);
18+
console.log(chalk.red('Sorry, this script requires git'));
19+
exit(1);
2020
}
2121

22-
if(program.args.length > 1) {
23-
console.log(chalk.red('Please give only one argument as a directory name!!!'));
24-
exit(1);
22+
if (program.args.length > 1) {
23+
console.log(chalk.red('Please give only one argument as a directory name!!!'));
24+
exit(1);
2525
}
2626

27-
if(program.args.length === 1) {
28-
if(test('-d', program.args[0])) {
29-
console.log(chalk.red(program.args[0]+ ' directory already exits! Please choose some another name!!!'));
30-
exit(1);
31-
}
32-
33-
mkdir('-p', program.args[0]);
34-
cd(program.args[0]);
35-
}
36-
exec('git init');
37-
38-
var interval = setInterval(function() {
39-
logUpdate("Fetching the boilerplate..." + chalk.cyan.bold.dim(frame()));
40-
}, 50)
41-
42-
var e = exec('git pull https://github.com/Hashnode/mern-starter.git', function(code, stdout, stderr) {
43-
clearInterval(interval);
44-
logUpdate.clear();
45-
if(code !== 0) {
46-
console.log(chalk.red.bold('Error! Try again'));
47-
exit(1);
48-
}
49-
console.log(chalk.green.bold('Completed.....You are good to go!'));
50-
});
27+
if (program.args.length === 1) {
28+
if (test('-d', program.args[0])) {
29+
console.log(chalk.red(`${program.args[0]} directory already exits! Please choose some another name!!!`));
30+
exit(1);
31+
}
32+
33+
mkdir('-p', program.args[0]);
34+
cd(program.args[0]);
35+
}
36+
37+
exec('git init');
38+
39+
const interval = setInterval(() => {
40+
logUpdate(`Fetching the boilerplate...${chalk.cyan.bold.dim(frame())}`);
41+
}, 50);
42+
43+
exec('git pull https://github.com/Hashnode/mern-starter.git', (code) => {
44+
clearInterval(interval);
45+
logUpdate.clear();
46+
if (code !== 0) {
47+
console.log(chalk.red.bold('Error! Try again'));
48+
exit(1);
49+
}
50+
console.log(chalk.green.bold('Completed.....You are good to go!'));
51+
});

src/tasks/checkAndWrite.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
var fileExists = require('../util/fileExists');
2-
var fs = require('fs');
1+
import fileExists from '../util/fileExists';
2+
import fs from 'fs';
33

4-
module.exports = function(baseDirectoryMapping, blueprint, file, entityName, ui) {
5-
if(fileExists(process.cwd() + baseDirectoryMapping[blueprint] + '/' + entityName + '.js')) {
6-
ui.writeError("File already exists please choose another name.");
7-
return;
8-
}
9-
fs.writeFile(process.cwd() + baseDirectoryMapping[blueprint] + '/' + entityName + '.js',file , function(err) {
10-
if(err) {
11-
ui.writeError(err);
12-
}
13-
14-
else {
15-
ui.writeInfoLine("Created " + entityName + '.js ' + "in " + process.cwd() + baseDirectoryMapping[blueprint]);
16-
}
17-
});
18-
}
4+
export default (baseDirectoryMapping, blueprint, file, entityName, ui) => {
5+
if (fileExists(`${process.cwd() + baseDirectoryMapping[blueprint]}/${entityName}.js`)) {
6+
ui.writeError('File already exists please choose another name.');
7+
return;
8+
}
9+
fs.writeFile(`${process.cwd() + baseDirectoryMapping[blueprint]}/${entityName}.js`, file, err => {
10+
if (err) {
11+
ui.writeError(err);
12+
} else {
13+
ui.writeInfoLine(`Created ${entityName}.js in ${process.cwd()}${baseDirectoryMapping[blueprint]}`);
14+
}
15+
});
16+
};

0 commit comments

Comments
 (0)