-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·175 lines (154 loc) · 4.42 KB
/
index.js
File metadata and controls
executable file
·175 lines (154 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
/* eslint-disable no-console */
/**
* NOTE:
* disabling no-console rule
* because we do want to console.log some outputs
*/
const shell = require('shelljs');
const fs = require('fs');
const chalk = require('chalk');
const inquirer = require('inquirer');
const { esLintConfig } = require('./eslintConfig');
const { prettyOptions } = require('./prettierConfig');
// show a initial message
const init = () =>
console.log(chalk.bold.green(`Running sahaj on ${process.cwd()} directory.`));
// check if package.json file exists otherwise exit
const checkPageJSON = () => {
const cOut = shell.ls('package.json');
if (cOut.code !== 0) {
console.log(chalk.bold.red("FATAL: Couldn't find package.json file!!"));
shell.exit(1);
}
return true;
};
/**
* NOTE: esLint and prettier by default excludes node_modules folder.
* So for sourceFolder we can use current directory as the default value.
* It does not matter if it has node_modules folder in it.
*/
// ask questions
const askQuestions = () => {
const questions = [
{
name: 'sourceFolder',
type: 'input',
default: '.',
message:
'Which folder contains all source codes? Press ENTER if it is CURRENT folder.'
},
{
name: 'packager',
type: 'list',
choices: ['yarn', 'npm'],
default: 'yarn',
message: 'Which packager do you want to use?'
}
];
return inquirer.prompt(questions);
};
// generate install dependecy command
const installPackages = packager => {
// list of packages to install as dev dependency
const packages = [
'eslint',
'babel-eslint',
'eslint-config-airbnb',
'eslint-plugin-import',
'eslint-plugin-jsx-a11y',
'eslint-plugin-react',
'husky',
'lint-staged',
'prettier'
];
const list = packages.join(' ');
const command =
packager === 'yarn'
? `yarn add --dev ${list}`
: `npm install --save-dev ${list}`;
// execute the install command
shell.exec(command);
};
// prettify a file
const prettify = fileName => {
const command = `./node_modules/.bin/prettier --print-width 80 --single-quote --write ${fileName}`;
shell.exec(command);
};
// generate .eslintrc.json
const createESLintConfig = () => {
const fileName = '.eslintrc.json';
const json = JSON.stringify(esLintConfig);
fs.writeFileSync(fileName, json, 'utf8');
prettify(fileName);
};
// generate .prettierrc
const createPrettierrc = () => {
const fileName = '.prettierrc';
const json = JSON.stringify(prettyOptions);
fs.writeFileSync(fileName, json, 'utf8');
prettify(fileName);
};
// modify package.json content
const modifyPackageJson = sourceFolder => {
/**
* NOTE: sourceFolder is taken dynamically here
* because not all projects have "src" folder as the
* source code container. Some projects may have "js"
* this is really upto the developer.
* folder as the container folder.
*/
const fileName = 'package.json';
const lintCmd = `eslint '${sourceFolder}/**/*.js'`;
const formatCmd = `prettier --single-quote --jsx-bracket-same-line --write '${sourceFolder}/**/*.{css,js}'`;
const lintStaged = {
'*.js': [
'prettier --single-quote --jsx-bracket-same-line --write',
'eslint',
'git add'
]
};
const husky = {
hooks: {
'pre-commit': 'lint-staged'
}
};
// read package.json content
const content = fs.readFileSync(fileName);
// parse JSON content
const json = JSON.parse(content);
// make all modifications
json.scripts.lint = lintCmd;
json.scripts.format = formatCmd;
json['lint-staged'] = lintStaged;
json.husky = husky;
fs.writeFileSync(fileName, JSON.stringify(json), 'utf8');
prettify(fileName);
};
// run function for this script
const run = async () => {
// show init message
init();
// check package.json file
checkPageJSON();
// ask questions
const answers = await askQuestions();
const { sourceFolder, packager } = answers;
// exit if choice of packager is yarn and yarn is not installed
if (packager === 'yarn' && !shell.which('yarn')) {
console.log(chalk.bold.red("FATAL: Couldn't find yarn"));
shell.exit(1);
}
// install dev dependencies
installPackages(packager);
// create eslintrc.json file
createESLintConfig();
// create .prettierrc
createPrettierrc();
// modify package.json file
modifyPackageJson(sourceFolder);
// success message
console.log(chalk.bold.green('Successfully updated!!'));
};
// execute this script
run();