From d440f2fd4bd04538432d772a8fe04fff905ec83e Mon Sep 17 00:00:00 2001 From: alok Date: Sat, 30 Nov 2019 12:49:28 +0530 Subject: [PATCH 1/2] added prettier and changed format command for all code --- .prettierignore | 8 ++++ .prettierrc | 10 ++++ package.json | 67 +++++++++++++++----------- scripts/constants.js | 12 ++--- scripts/create.js | 54 ++++++++++----------- scripts/dependencies.js | 104 ++++++++++++++++++++-------------------- scripts/helper.js | 104 ++++++++++++++++++++-------------------- start.js | 28 +++++------ template/package.json | 2 +- 9 files changed, 208 insertions(+), 181 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..5d482e8 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,8 @@ +#ignore node_modules +node_modules + +#ignore build folder +dist + +#ignore package-lock.json +package-lock.json \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..32f86e9 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,10 @@ +{ + "tabWidth": 2, + "semi": true, + "singleQuote": false, + "useTabs": true, + "trailingComma": "all", + "jsxBracketSameLine": true, + "jsxSingleQuote": true + } + \ No newline at end of file diff --git a/package.json b/package.json index 243ae9a..f3109f8 100644 --- a/package.json +++ b/package.json @@ -1,31 +1,40 @@ { - "name": "create-react-webpack", - "version": "0.1.2", - "description": "create-react-webpack ", - "main": "scripts/create.js", - "bin": { - "create-react-webpack": "./start.js" - }, - "preferGlobal": true, - "scripts": { - "create-react-webpack": "node scripts/create.js" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/AlokTakshak/create-react-webpack.git" - }, - "keywords": [ - "React", - "Webpack" - ], - "author": "Alok Takshak", - "license": "ISC", - "bugs": { - "url": "https://github.com/AlokTakshak/create-react-webpack/issues" - }, - "homepage": "https://github.com/AlokTakshak/create-react-webpack#readme", - "dependencies": { - "chalk": "^2.4.2", - "update-notifier": "^3.0.1" - } + "name": "create-react-webpack", + "version": "0.1.2", + "description": "create-react-webpack ", + "main": "scripts/create.js", + "bin": { + "create-react-webpack": "./start.js" + }, + "preferGlobal": true, + "scripts": { + "create-react-webpack": "node scripts/create.js", + "format": "prettier --write \"./**/*.{js,jsx,json}\"" + }, + "husky": { + "hooks": { + "pre-commit": "pretty-quick --staged" + } + }, + "repository": { + "type": "git", + "url": "git+https://github.com/AlokTakshak/create-react-webpack.git" + }, + "keywords": [ + "React", + "Webpack" + ], + "author": "Alok Takshak", + "license": "ISC", + "bugs": { + "url": "https://github.com/AlokTakshak/create-react-webpack/issues" + }, + "homepage": "https://github.com/AlokTakshak/create-react-webpack#readme", + "dependencies": { + "chalk": "^2.4.2", + "husky": "^3.1.0", + "prettier": "^1.19.1", + "pretty-quick": "^2.0.1", + "update-notifier": "^3.0.1" + } } diff --git a/scripts/constants.js b/scripts/constants.js index 2ad4a2d..c81d068 100644 --- a/scripts/constants.js +++ b/scripts/constants.js @@ -1,14 +1,14 @@ const CLEAN_NPM_CACHE = "npm cache clean --force"; const SPECIALCHAR = RegExp("/[!@#$%^&*()-=_,.?~:;\\{}|<>]/g"); const UNNECESSORY_FOLDERS_FOR_DEV = RegExp( - "^node_modules|build|dist|server$", - "i" + "^node_modules|build|dist|server$", + "i", ); const UNNECESSORY_FOLDERS_FOR_PROD = RegExp("^node_modules|build|dist$", "i"); module.exports = { - CLEAN_NPM_CACHE, - SPECIALCHAR, - UNNECESSORY_FOLDERS_FOR_DEV, - UNNECESSORY_FOLDERS_FOR_PROD + CLEAN_NPM_CACHE, + SPECIALCHAR, + UNNECESSORY_FOLDERS_FOR_DEV, + UNNECESSORY_FOLDERS_FOR_PROD, }; diff --git a/scripts/create.js b/scripts/create.js index 780d69d..ce283b1 100755 --- a/scripts/create.js +++ b/scripts/create.js @@ -2,10 +2,10 @@ const path = require("path"); const { copyDirectory, ErrorMessage, SuccessMessage } = require("./helper"); const { CLEAN_NPM_CACHE, SPECIALCHAR } = require("./constants"); const { - installDependencies, - getDependencies, - getDevDependencies, - getProdDependencies + installDependencies, + getDependencies, + getDevDependencies, + getProdDependencies, } = require("./dependencies"); var args = process.argv.slice(2); @@ -13,35 +13,35 @@ var dirName = args[0]; var end_to_end = args[1]; if (dirName === "undefined") { - throw new Error(ErrorMessage("directory name can't be empty")); + throw new Error(ErrorMessage("directory name can't be empty")); } else if (dirName[0].match("^[A-Z0-9]")) { - throw new Error( - ErrorMessage("directory name can't start from capital letters ") - ); + throw new Error( + ErrorMessage("directory name can't start from capital letters "), + ); } else if (dirName.match(SPECIALCHAR)) { - throw new Error( - ErrorMessage( - "directory name can't start from capital letters or contain special characters in it" - ) - ); + throw new Error( + ErrorMessage( + "directory name can't start from capital letters or contain special characters in it", + ), + ); } else { - //using process.cwd for getting current path - const TEMPLATE_PATH = path.join(__dirname, "../template"); - let destination = path.join(process.cwd() + "/" + args[0]); - const prod = end_to_end == "-e"; + //using process.cwd for getting current path + const TEMPLATE_PATH = path.join(__dirname, "../template"); + let destination = path.join(process.cwd() + "/" + args[0]); + const prod = end_to_end == "-e"; - copyDirectory(TEMPLATE_PATH, destination, prod); + copyDirectory(TEMPLATE_PATH, destination, prod); - let commands = [], - options; + let commands = [], + options; - commands.push(CLEAN_NPM_CACHE); - commands.push(getDependencies()); - prod && commands.push(getProdDependencies()); - commands.push(getDevDependencies()); + commands.push(CLEAN_NPM_CACHE); + commands.push(getDependencies()); + prod && commands.push(getProdDependencies()); + commands.push(getDevDependencies()); - options = { cwd: destination, stdio: "inherit" }; + options = { cwd: destination, stdio: "inherit" }; - installDependencies(commands, options); - console.log(SuccessMessage("Successfully created the directory")); + installDependencies(commands, options); + console.log(SuccessMessage("Successfully created the directory")); } diff --git a/scripts/dependencies.js b/scripts/dependencies.js index e49ee5e..a810842 100644 --- a/scripts/dependencies.js +++ b/scripts/dependencies.js @@ -2,29 +2,29 @@ const child_process = require("child_process"); const DEPENDENCIES = ["prop-types", "react", "react-dom", "react-hot-loader"]; const DEV_DEPENDENCIES = [ - "@babel/cli", - "@babel/core", - "@babel/plugin-proposal-class-properties", - "@babel/preset-env", - "@babel/preset-react", - "babel-loader", - "brotli-webpack-plugin", - "compression-webpack-plugin", - "css-loader", - "eslint", - "eslint-plugin-react", - "file-loader", - "html-webpack-plugin", - "husky", - "jest", - "prettier", - "pretty-quick", - "style-loader", - "webpack", - "webpack-cli", - "webpack-dev-server", - "webpack-manifest-plugin", - "webpack-merge" + "@babel/cli", + "@babel/core", + "@babel/plugin-proposal-class-properties", + "@babel/preset-env", + "@babel/preset-react", + "babel-loader", + "brotli-webpack-plugin", + "compression-webpack-plugin", + "css-loader", + "eslint", + "eslint-plugin-react", + "file-loader", + "html-webpack-plugin", + "husky", + "jest", + "prettier", + "pretty-quick", + "style-loader", + "webpack", + "webpack-cli", + "webpack-dev-server", + "webpack-manifest-plugin", + "webpack-merge", ]; const PROD_DEPENDENCIES = ["express", "express-static-gzip"]; @@ -32,33 +32,33 @@ const PROD_DEPENDENCIES = ["express", "express-static-gzip"]; * Returns npm command for installing dependencies */ function getDependencies() { - var installCommand = "npm install --save"; - DEPENDENCIES.forEach(dependency => { - installCommand += ` ${dependency} `; - }); - return installCommand; + var installCommand = "npm install --save"; + DEPENDENCIES.forEach(dependency => { + installCommand += ` ${dependency} `; + }); + return installCommand; } /** * Returns npm command for installing dev-dependencies */ function getDevDependencies() { - var installCommand = "npm install --save-dev"; - DEV_DEPENDENCIES.forEach(dependency => { - installCommand += ` ${dependency} `; - }); - return installCommand; + var installCommand = "npm install --save-dev"; + DEV_DEPENDENCIES.forEach(dependency => { + installCommand += ` ${dependency} `; + }); + return installCommand; } /** * Returns npm command for installing prod dependencies */ function getProdDependencies() { - var installCommand = "npm install --save"; - PROD_DEPENDENCIES.forEach(dependency => { - installCommand += ` ${dependency} `; - }); - return installCommand; + var installCommand = "npm install --save"; + PROD_DEPENDENCIES.forEach(dependency => { + installCommand += ` ${dependency} `; + }); + return installCommand; } /** @@ -69,22 +69,22 @@ function getProdDependencies() { * @param {String} options.stdio process's stdio config */ function installDependencies(commands, options) { - options.stdio = options.stdio || "inherit"; + options.stdio = options.stdio || "inherit"; - if (commands) { - try { - commands.forEach(command => { - child_process.execSync(command, options); - }); - } catch (error) { - throw error; - } - } + if (commands) { + try { + commands.forEach(command => { + child_process.execSync(command, options); + }); + } catch (error) { + throw error; + } + } } module.exports = { - installDependencies, - getDependencies, - getDevDependencies, - getProdDependencies + installDependencies, + getDependencies, + getDevDependencies, + getProdDependencies, }; diff --git a/scripts/helper.js b/scripts/helper.js index 3371d35..dfd6716 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -2,8 +2,8 @@ const fs = require("fs"); const path = require("path"); const chalk = require("chalk"); const { - UNNECESSORY_FOLDERS_FOR_DEV, - UNNECESSORY_FOLDERS_FOR_PROD + UNNECESSORY_FOLDERS_FOR_DEV, + UNNECESSORY_FOLDERS_FOR_PROD, } = require("./constants"); /** @@ -13,35 +13,35 @@ const { * @param {boolean} prod tells if user require prod environment */ function copyDirectory(source, destination, prod) { - let UNNECESSORY_FOLDERS = prod - ? UNNECESSORY_FOLDERS_FOR_PROD - : UNNECESSORY_FOLDERS_FOR_DEV; + let UNNECESSORY_FOLDERS = prod + ? UNNECESSORY_FOLDERS_FOR_PROD + : UNNECESSORY_FOLDERS_FOR_DEV; - createDirectory(destination); + createDirectory(destination); - var content = fs.readdirSync(source); - for (let i = 0; i < content.length; i++) { - let currentFile = fs.lstatSync(path.join(source, content[i])); + var content = fs.readdirSync(source); + for (let i = 0; i < content.length; i++) { + let currentFile = fs.lstatSync(path.join(source, content[i])); - if (String(content[i]).match(UNNECESSORY_FOLDERS)) { - continue; - } else if (currentFile.isDirectory()) { - copyDirectory( - path.join(source, content[i]), - path.join(destination, content[i]), - prod - ); - } else if (currentFile.isSymbolicLink()) { - var symlink = fs.readlinkSync(source, content[i]); - fs.symlinkSync(symlink, path.join(destination, content[i])); - } else { - copyFile( - path.join(source, content[i]), - path.join(destination, content[i]), - prod - ); - } - } + if (String(content[i]).match(UNNECESSORY_FOLDERS)) { + continue; + } else if (currentFile.isDirectory()) { + copyDirectory( + path.join(source, content[i]), + path.join(destination, content[i]), + prod, + ); + } else if (currentFile.isSymbolicLink()) { + var symlink = fs.readlinkSync(source, content[i]); + fs.symlinkSync(symlink, path.join(destination, content[i])); + } else { + copyFile( + path.join(source, content[i]), + path.join(destination, content[i]), + prod, + ); + } + } } /** @@ -51,18 +51,18 @@ function copyDirectory(source, destination, prod) { * @param {boolean} prod tells if user require prod environment */ function copyFile(source, destination, prod) { - var inputFile, outputFile; - if (source.match(".json$")) { - inputFile = JSON.parse(fs.readFileSync(source, "utf8")); - if (prod && source.match("package.json$")) { - inputFile.scripts.start = "node server/"; - } - fs.writeFileSync(destination, JSON.stringify(inputFile, null, 2), "utf8"); - } else { - inputFile = fs.createReadStream(source); - outputFile = fs.createWriteStream(destination); - inputFile.pipe(outputFile); - } + var inputFile, outputFile; + if (source.match(".json$")) { + inputFile = JSON.parse(fs.readFileSync(source, "utf8")); + if (prod && source.match("package.json$")) { + inputFile.scripts.start = "node server/"; + } + fs.writeFileSync(destination, JSON.stringify(inputFile, null, 2), "utf8"); + } else { + inputFile = fs.createReadStream(source); + outputFile = fs.createWriteStream(destination); + inputFile.pipe(outputFile); + } } /** @@ -70,25 +70,25 @@ function copyFile(source, destination, prod) { * @param {String} destination path of destination directory */ function createDirectory(destination) { - if (fs.existsSync(destination)) { - throw new Error(ErrorMessage("Directory Already Exists!")); - } else { - fs.mkdirSync(destination); - } + if (fs.existsSync(destination)) { + throw new Error(ErrorMessage("Directory Already Exists!")); + } else { + fs.mkdirSync(destination); + } } function ErrorMessage(message) { - return chalk.bold.red(message); + return chalk.bold.red(message); } function SuccessMessage(message) { - return chalk.bold.green(message); + return chalk.bold.green(message); } module.exports = { - copyDirectory, - copyFile, - createDirectory, - ErrorMessage, - SuccessMessage + copyDirectory, + copyFile, + createDirectory, + ErrorMessage, + SuccessMessage, }; diff --git a/start.js b/start.js index 32bf324..e4adf9b 100755 --- a/start.js +++ b/start.js @@ -7,24 +7,24 @@ const pkg = require("./package.json"); const { ErrorMessage, SuccessMessage } = require("./scripts/helper"); const notifier = updateNotifier({ - pkg, - updateCheckInterval: 1000 * 60 * 60 * 24 + pkg, + updateCheckInterval: 1000 * 60 * 60 * 24, }); if (notifier.update && notifier.update.latest !== pkg.version) { - const old = notifier.update.current; - const latest = notifier.update.latest; + const old = notifier.update.current; + const latest = notifier.update.latest; - var message = - `${SuccessMessage("New")} version of ${SuccessMessage( - pkg.name - )} available! ${ErrorMessage(old)} -> ${SuccessMessage(latest)}\n` + - `Run ${SuccessMessage(`npm install -g ${pkg.name}`)} to update!`; + var message = + `${SuccessMessage("New")} version of ${SuccessMessage( + pkg.name, + )} available! ${ErrorMessage(old)} -> ${SuccessMessage(latest)}\n` + + `Run ${SuccessMessage(`npm install -g ${pkg.name}`)} to update!`; - notifier.notify({ message: message }); - console.log("______________________________________________________________"); - console.log(message); - console.log("______________________________________________________________"); + notifier.notify({ message: message }); + console.log("______________________________________________________________"); + console.log(message); + console.log("______________________________________________________________"); } var args = process.argv.slice(2); var dirName = args[0]; @@ -32,5 +32,5 @@ var end_to_end = args[1]; var p = path.join(__dirname, "scripts/create.js"); child_process.execSync(`node ${p} ${dirName} ${end_to_end}`, { - stdio: "inherit" + stdio: "inherit", }); diff --git a/template/package.json b/template/package.json index e11553d..1523fb0 100644 --- a/template/package.json +++ b/template/package.json @@ -9,7 +9,7 @@ "dev:hot": "npm run dev -- --hot", "docker:dev": "node docker/dev.js", "test": "jest", - "format": "pretty-quick", + "format": "prettier --write \"./**/*.{js,jsx,json}\"", "lint": "eslint ./" }, "husky": { From 2868a64f84e460fa617ecb9da57a976a16ac48e0 Mon Sep 17 00:00:00 2001 From: alok Date: Sat, 30 Nov 2019 12:50:57 +0530 Subject: [PATCH 2/2] changed version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f3109f8..f806558 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-react-webpack", - "version": "0.1.2", + "version": "0.1.3", "description": "create-react-webpack ", "main": "scripts/create.js", "bin": {