├── .eslintrc.json ├── .gitignore ├── .prettierrc.json ├── .vscode └── settings.json ├── README.md ├── eslint_config_file.json ├── index.js ├── package.json ├── prettier_config_file.json ├── script.js └── utils ├── ask-questions.js ├── break-line.js ├── color-functions.js ├── print.js ├── run-system-command.js ├── save-file.js ├── to-json.js └── yes-no-answer.js /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es2021": true, 5 | "node": true, 6 | "jest": true 7 | }, 8 | "extends": [ 9 | "eslint:recommended", 10 | "plugin:prettier/recommended" 11 | ], 12 | "parserOptions": { 13 | "ecmaFeatures": { 14 | "jsx": true 15 | }, 16 | "ecmaVersion": 12, 17 | "sourceType": "module" 18 | }, 19 | "plugins": [], 20 | "rules": { 21 | "no-unused-vars": [ 22 | "error", 23 | { 24 | "argsIgnorePattern": "^_", 25 | "varsIgnorePattern": "^_" 26 | } 27 | ] 28 | } 29 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | yarn-debug.log* 9 | yarn-error.log* 10 | lerna-debug.log* 11 | 12 | # Diagnostic reports (https://nodejs.org/api/report.html) 13 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 14 | 15 | # Runtime data 16 | pids 17 | *.pid 18 | *.seed 19 | *.pid.lock 20 | 21 | # Directory for instrumented libs generated by jscoverage/JSCover 22 | lib-cov 23 | 24 | # Coverage directory used by tools like istanbul 25 | coverage 26 | *.lcov 27 | 28 | # nyc test coverage 29 | .nyc_output 30 | 31 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 32 | .grunt 33 | 34 | # Bower dependency directory (https://bower.io/) 35 | bower_components 36 | 37 | # node-waf configuration 38 | .lock-wscript 39 | 40 | # Compiled binary addons (https://nodejs.org/api/addons.html) 41 | build/Release 42 | 43 | # Dependency directories 44 | node_modules/ 45 | jspm_packages/ 46 | 47 | # Snowpack dependency directory (https://snowpack.dev/) 48 | web_modules/ 49 | 50 | # TypeScript cache 51 | *.tsbuildinfo 52 | 53 | # Optional npm cache directory 54 | .npm 55 | 56 | # Optional eslint cache 57 | .eslintcache 58 | 59 | # Microbundle cache 60 | .rpt2_cache/ 61 | .rts2_cache_cjs/ 62 | .rts2_cache_es/ 63 | .rts2_cache_umd/ 64 | 65 | # Optional REPL history 66 | .node_repl_history 67 | 68 | # Output of 'npm pack' 69 | *.tgz 70 | 71 | # Yarn Integrity file 72 | .yarn-integrity 73 | 74 | # dotenv environment variables file 75 | .env 76 | .env.test 77 | 78 | # parcel-bundler cache (https://parceljs.org/) 79 | .cache 80 | .parcel-cache 81 | 82 | # Next.js build output 83 | .next 84 | out 85 | 86 | # Nuxt.js build / generate output 87 | .nuxt 88 | dist 89 | 90 | # Gatsby files 91 | .cache/ 92 | # Comment in the public line in if your project uses Gatsby and not Next.js 93 | # https://nextjs.org/blog/next-9-1#public-directory-support 94 | # public 95 | 96 | # vuepress build output 97 | .vuepress/dist 98 | 99 | # Serverless directories 100 | .serverless/ 101 | 102 | # FuseBox cache 103 | .fusebox/ 104 | 105 | # DynamoDB Local files 106 | .dynamodb/ 107 | 108 | # TernJS port file 109 | .tern-port 110 | 111 | # Stores VSCode versions used for testing VSCode extensions 112 | .vscode-test 113 | 114 | # yarn v2 115 | .yarn/cache 116 | .yarn/unplugged 117 | .yarn/build-state.yml 118 | .yarn/install-state.gz 119 | .pnp.* -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "trailingComma": "all", 4 | "singleQuote": true, 5 | "printWidth": 80, 6 | "tabWidth": 2, 7 | "endOfLine": "lf" 8 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "window.zoomLevel": 0, 3 | // Auto fix on save 4 | "editor.codeActionsOnSave": { 5 | "source.fixAll.eslint": true, 6 | "source.fixAll": true 7 | }, 8 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-prettier 2 | 3 | Just a simple script I was testing. It configures Eslint and Prettier. You can test it your self: 4 | 5 | ``` 6 | npx https://github.com/luizomf/eslint-prettier 7 | ``` 8 | 9 | **It requires Node, Npm (Npx) and Git installed.** 10 | -------------------------------------------------------------------------------- /eslint_config_file.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "es2021": true, 5 | "node": true, 6 | "jest": true 7 | }, 8 | "extends": [ 9 | "eslint:recommended", 10 | "plugin:prettier/recommended" 11 | ], 12 | "parserOptions": { 13 | "ecmaFeatures": { 14 | "jsx": true 15 | }, 16 | "ecmaVersion": 12, 17 | "sourceType": "module" 18 | }, 19 | "plugins": [], 20 | "rules": { 21 | "no-unused-vars": [ 22 | "error", 23 | { 24 | "argsIgnorePattern": "^_", 25 | "varsIgnorePattern": "^_" 26 | } 27 | ] 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | require('./script.js'); 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-prettier-setup", 3 | "version": "1.0.0", 4 | "description": "Configura o eslint e o prettier.", 5 | "bin": "./index.js", 6 | "devDependencies": { 7 | "@typescript-eslint/eslint-plugin": "^4.21.0", 8 | "@typescript-eslint/parser": "^4.21.0", 9 | "eslint": "^7.24.0", 10 | "eslint-config-prettier": "^8.1.0", 11 | "eslint-plugin-prettier": "^3.3.1", 12 | "prettier": "^2.2.1" 13 | }, 14 | "dependencies": {} 15 | } 16 | -------------------------------------------------------------------------------- /prettier_config_file.json: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "trailingComma": "all", 4 | "singleQuote": true, 5 | "printWidth": 80, 6 | "tabWidth": 2, 7 | "endOfLine": "lf" 8 | } 9 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const { askPromise } = require('./utils/ask-questions'); 3 | const color = require('./utils/color-functions'); 4 | const { saveFile } = require('./utils/save-file'); 5 | const toJson = require('./utils/to-json'); 6 | const { isYes, isNo } = require('./utils/yes-no-answer'); 7 | const { 8 | print, 9 | printBgGreen, 10 | printRed, 11 | printGreen, 12 | printYellow, 13 | printWhite, 14 | printCyan, 15 | printMagenta, 16 | } = require('./utils/print'); 17 | const { breakLine } = require('./utils/break-line'); 18 | const { runSystemCommand } = require('./utils/run-system-command'); 19 | 20 | const Y = 'Y'; 21 | const N = 'N'; 22 | const yesNoPhrase = color.cyan(`[${Y}]es [${N}]o (Default "${N}"): `); 23 | 24 | breakLine(); 25 | printBgGreen('Instalação e configuração do Eslint'); 26 | breakLine(); 27 | 28 | askPromise 29 | .question(color.yellow(`Is "Git" installed? ${yesNoPhrase}`)) 30 | .then((answer) => { 31 | askPromise.answers = { ...askPromise.answers, gitInstalled: answer }; 32 | if (isNo(answer)) { 33 | printRed(`[N]o selected!`); 34 | printRed(`Please, install git from https://git-scm.com/downloads.`); 35 | process.exit(); 36 | } 37 | 38 | return askPromise.question( 39 | color.yellow(`Install and configure Eslint and Prettier? ${yesNoPhrase}`), 40 | ); 41 | }) 42 | .then((answer) => { 43 | askPromise.answers = { ...askPromise.answers, allowExecution: answer }; 44 | 45 | if (isNo(answer)) { 46 | printRed(`[N]o selected!`); 47 | askPromise.close(); 48 | process.exit(); 49 | return; 50 | } 51 | 52 | return askPromise.question( 53 | color.yellow(`Do you use React? ${yesNoPhrase}`), 54 | ); 55 | }) 56 | .then((answer) => { 57 | let usingReact = isYes(answer); 58 | askPromise.answers = { ...askPromise.answers, usingReact }; 59 | 60 | return askPromise.question( 61 | color.yellow(`Do you use TypeScript? ${yesNoPhrase}`), 62 | ); 63 | }) 64 | .then((usingTypeScriptAnswer) => { 65 | let usingTypeScript = isYes(usingTypeScriptAnswer); 66 | askPromise.answers = { ...askPromise.answers, usingTypeScript }; 67 | 68 | breakLine(); 69 | printGreen(`Ok! Trying to install packages.`); 70 | printGreen(`This may take a while. Please wait...`); 71 | 72 | askPromise.close(); 73 | 74 | executeNpmCommand(); 75 | }); 76 | 77 | breakLine(); 78 | 79 | const executeNpmCommand = () => { 80 | let npmCommand = 'npm install -D eslint eslint-config-prettier '; 81 | npmCommand += ' eslint-plugin-prettier prettier'; 82 | 83 | const eslintConfigObj = require('./eslint_config_file'); 84 | const prettierConfigObj = require('./prettier_config_file'); 85 | 86 | if (askPromise.answers.usingReact) { 87 | eslintConfigObj.extends = [ 88 | ...eslintConfigObj.extends, 89 | 'plugin:react/recommended', 90 | 'plugin:react-hooks/recommended', 91 | ]; 92 | eslintConfigObj.settings = { 93 | react: { 94 | version: 'detect', 95 | }, 96 | }; 97 | eslintConfigObj.plugins.push('react'); 98 | npmCommand += ' eslint-plugin-react eslint-plugin-react-hooks '; 99 | } 100 | 101 | if (askPromise.answers.usingTypeScript) { 102 | eslintConfigObj.extends = [ 103 | ...eslintConfigObj.extends, 104 | 'plugin:@typescript-eslint/recommended', 105 | ]; 106 | eslintConfigObj.parser = '@typescript-eslint/parser'; 107 | eslintConfigObj.plugins.push('@typescript-eslint'); 108 | npmCommand += ' @typescript-eslint/eslint-plugin @typescript-eslint/parser'; 109 | } 110 | 111 | breakLine(); 112 | printYellow(`Eslint configuration to be applied: `); 113 | print(color.white, toJson(eslintConfigObj)); 114 | 115 | breakLine(); 116 | printYellow(`Prettier configuration to be applied: `); 117 | printWhite(toJson(prettierConfigObj)); 118 | 119 | const eslintFilePath = path.resolve('.', '.eslintrc.json'); 120 | const prettierFilePath = path.resolve('.', '.prettierrc.json'); 121 | breakLine(); 122 | 123 | const systemCommandCallback = (error, stdout) => { 124 | if (error) { 125 | breakLine(); 126 | printRed(`An error occurred:`); 127 | 128 | breakLine(); 129 | printRed(`${error.message}`); 130 | 131 | process.exit(); 132 | return; 133 | } 134 | 135 | printCyan(`Installation completed:`); 136 | breakLine(); 137 | printCyan(`${stdout}`); 138 | 139 | saveFile(eslintFilePath, eslintConfigObj); 140 | saveFile(prettierFilePath, prettierConfigObj); 141 | 142 | printGreen(`.eslintrc.json saved: ${eslintFilePath}`); 143 | printGreen(`.prettierrc.json saved: ${prettierFilePath}`); 144 | 145 | breakLine(); 146 | printMagenta(`Seems like everything is fine!`); 147 | printMagenta(`You may need to reload your editor 😊!`); 148 | 149 | print(color.magenta, 'BYE!'); 150 | 151 | process.exit(); 152 | return; 153 | }; 154 | 155 | runSystemCommand(npmCommand, systemCommandCallback); 156 | }; 157 | -------------------------------------------------------------------------------- /utils/ask-questions.js: -------------------------------------------------------------------------------- 1 | const nodeReadLine = require('readline'); 2 | 3 | const readline = nodeReadLine.createInterface({ 4 | input: process.stdin, 5 | output: process.stdout, 6 | }); 7 | 8 | const ask = (question, callback) => { 9 | return readline.question(question, (answer) => { 10 | return callback(answer, readline.close.bind(readline)); 11 | }); 12 | }; 13 | 14 | const askPromise = { 15 | question: (question) => 16 | new Promise((resolve) => { 17 | readline.question(question, (answer) => { 18 | return resolve(answer); 19 | }); 20 | }), 21 | close: readline.close.bind(readline), 22 | answers: {}, 23 | }; 24 | 25 | module.exports = { 26 | ask, 27 | askPromise, 28 | }; 29 | -------------------------------------------------------------------------------- /utils/break-line.js: -------------------------------------------------------------------------------- 1 | const breakLine = () => console.log(); 2 | 3 | module.exports = { 4 | breakLine, 5 | }; 6 | -------------------------------------------------------------------------------- /utils/color-functions.js: -------------------------------------------------------------------------------- 1 | const endColor = '\u001b[0m'; 2 | const bgGreen = (...args) => `\u001b[30m\u001b[42m${args.join(' ')}${endColor}`; 3 | const white = (...args) => `\u001b[37m${args.join(' ')}${endColor}`; 4 | const cyan = (...args) => `\u001b[36m${args.join(' ')}${endColor}`; 5 | const magenta = (...args) => `\u001b[35m${args.join(' ')}${endColor}`; 6 | const yellow = (...args) => `\u001b[33m${args.join(' ')}${endColor}`; 7 | const green = (...args) => `\u001b[32m${args.join(' ')}${endColor}`; 8 | const red = (...args) => `\u001b[31m${args.join(' ')}${endColor}`; 9 | const blue = (...args) => `\u001b[34m${args.join(' ')}${endColor}`; 10 | const black = (...args) => `\u001b[30m${args.join(' ')}${endColor}`; 11 | 12 | module.exports = { 13 | endColor, 14 | bgGreen, 15 | white, 16 | cyan, 17 | magenta, 18 | yellow, 19 | green, 20 | red, 21 | blue, 22 | black, 23 | }; 24 | -------------------------------------------------------------------------------- /utils/print.js: -------------------------------------------------------------------------------- 1 | const colors = require('./color-functions'); 2 | const print = (color, ...args) => console.log(color(args)); 3 | const printBgGreen = (...args) => print(colors.bgGreen, ...args); 4 | const printGreen = (...args) => print(colors.green, ...args); 5 | const printRed = (...args) => print(colors.red, ...args); 6 | const printBlue = (...args) => print(colors.blue, ...args); 7 | const printCyan = (...args) => print(colors.cyan, ...args); 8 | const printMagenta = (...args) => print(colors.magenta, ...args); 9 | const printYellow = (...args) => print(colors.yellow, ...args); 10 | const printBlack = (...args) => print(colors.black, ...args); 11 | const printWhite = (...args) => print(colors.white, ...args); 12 | 13 | module.exports = { 14 | print, 15 | printGreen, 16 | printBgGreen, 17 | printRed, 18 | printBlue, 19 | printCyan, 20 | printMagenta, 21 | printYellow, 22 | printBlack, 23 | printWhite, 24 | }; 25 | -------------------------------------------------------------------------------- /utils/run-system-command.js: -------------------------------------------------------------------------------- 1 | const { exec: runSystemCommand } = require('child_process'); 2 | 3 | module.exports = { 4 | runSystemCommand, 5 | }; 6 | -------------------------------------------------------------------------------- /utils/save-file.js: -------------------------------------------------------------------------------- 1 | const fileSystem = require('fs'); 2 | const toJson = require('./to-json'); 3 | 4 | const saveFile = (filePath, obj) => { 5 | return fileSystem.writeFileSync(filePath, toJson(obj)); 6 | }; 7 | 8 | module.exports = { 9 | saveFile, 10 | }; 11 | -------------------------------------------------------------------------------- /utils/to-json.js: -------------------------------------------------------------------------------- 1 | const toJson = (obj) => JSON.stringify(obj, null, 2); 2 | 3 | module.exports = toJson; 4 | -------------------------------------------------------------------------------- /utils/yes-no-answer.js: -------------------------------------------------------------------------------- 1 | const isYes = (answer) => 2 | answer.toLocaleLowerCase().startsWith('y') || 3 | answer.toLocaleLowerCase().startsWith('s'); 4 | const isNo = (answer) => !isYes(answer); 5 | 6 | module.exports = { 7 | isYes, 8 | isNo, 9 | }; 10 | --------------------------------------------------------------------------------