Module: InitBoilerplate
- Defined in:
- lib/init-boilerplate.rb
Constant Summary collapse
- CONFIGS =
[ PACKAGE = 'package.json'.freeze, PRETTIER = '.prettierrc'.freeze, NODEMON = 'nodemon.json'.freeze, TS_CONFIG = 'tsconfig.json'.freeze, LINT_STAGED = '.lintstagedrc'.freeze, HUSKY = '.huskyrc'.freeze, ES_LINT = '.eslintrc.json'.freeze, STYLE_LINT = '.stylelintrc.json'.freeze, COMMIT_LINT = '.commitlintrc.json'.freeze ].freeze
Class Method Summary collapse
- .add_templates ⇒ Object
- .check_already_init ⇒ Object
- .check_npm ⇒ Object
- .clean_directory ⇒ Object
- .configure_commitlint ⇒ Object
- .configure_eslint(react) ⇒ Object
- .configure_husky ⇒ Object
- .configure_nodemon ⇒ Object
- .configure_prettier ⇒ Object
- .configure_stylelint ⇒ Object
- .configure_typescript(react) ⇒ Object
- .create_node_directory(typescript) ⇒ Object
- .create_node_project ⇒ Object
- .create_react_project ⇒ Object
- .display_header ⇒ Object
- .display_menu ⇒ Object
- .display_pwd ⇒ Object
- .eslint_json_exists(react) ⇒ Object
- .main ⇒ Object
- .process_option(choice) ⇒ Object
- .update_node_package_json ⇒ Object
Class Method Details
.add_templates ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/init-boilerplate.rb', line 301 def self.add_templates puts 'Creating GitHub template files'.colorize(:green) issue_template_dir = '.github/ISSUE_TEMPLATE' pull_request_template_dir = '.github/PULL_REQUEST_TEMPLATE' FileUtils.mkdir_p issue_template_dir FileUtils.mkdir_p pull_request_template_dir issue_templates = [ 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/ISSUE_TEMPLATE/bug.md'.freeze, 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/ISSUE_TEMPLATE/epic.md'.freeze, 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/ISSUE_TEMPLATE/story.md'.freeze, 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/ISSUE_TEMPLATE/sub-task.md'.freeze, 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/ISSUE_TEMPLATE/task.md'.freeze ].freeze pull_request_template = 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/PULL_REQUEST_TEMPLATE/pull_request_template.md'.freeze readme_template = 'https://github.com/gordonpn/git-conventions-guide/raw/master/docs/README_template.md'.freeze issue_templates.each do |issue_template| tempfile = Down.download(issue_template) FileUtils.mv tempfile.path, "./#{issue_template_dir}/#{tempfile.original_filename}" end tempfile = Down.download(pull_request_template) FileUtils.mv tempfile.path, "./#{pull_request_template_dir}/#{tempfile.original_filename}" tempfile = Down.download(readme_template) FileUtils.mv tempfile.path, "./#{tempfile.original_filename}" end |
.check_already_init ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/init-boilerplate.rb', line 41 def self.check_already_init if File.file?(PACKAGE) puts "#{PACKAGE} exists and will be used".colorize(:green) else puts "#{PACKAGE} does not exist".colorize(:green) system('npm init -y') package_json = File.read(PACKAGE) package_hash = JSON.parse(package_json) package_hash['description'] = 'TODO' package_hash['repository'] = 'TODO' unless package_hash.key?('repository') File.open(PACKAGE, 'w+') do |file| file.write(JSON.pretty_generate(package_hash)) file.close end end end |
.check_npm ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/init-boilerplate.rb', line 58 def self.check_npm puts 'Checking npm...'.colorize(:yellow) return unless system('npm --version').nil? puts 'Error: npm is not installed'.colorize(:red) exit(1) end |
.clean_directory ⇒ Object
349 350 351 352 353 354 355 356 |
# File 'lib/init-boilerplate.rb', line 349 def self.clean_directory puts "\nCleaning directory...".colorize(:blue) FileUtils.rm_rf 'src' if File.directory?('src') CONFIGS.each do |config| File.delete(config) if File.exist?(config) end File.delete('package-lock.json') if File.exist?('package-lock.json') end |
.configure_commitlint ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/init-boilerplate.rb', line 209 def self.configure_commitlint puts 'Installing and configuring commitlint...'.colorize(:green) system('npm install --save-dev @commitlint/{config-conventional,cli} stylefmt') hash = { extends: ['@commitlint/config-conventional'] } File.open(COMMIT_LINT, 'w+') do |file| file.write(JSON.pretty_generate(hash)) file.close end end |
.configure_eslint(react) ⇒ Object
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 |
# File 'lib/init-boilerplate.rb', line 138 def self.configure_eslint(react) puts 'Installing and configuring ESLint...'.colorize(:green) system('npm install --save-dev eslint eslint-plugin-prettier eslint-config-prettier') system('npx eslint --init') if File.exist?(ES_LINT) eslint_json_exists(react) else hash = { plugins: [ 'prettier' ], rules: { 'prettier/prettier': 'error', 'no-console': 1 }, extends: [ 'prettier', 'plugin:prettier/recommended' ] } if react hash['plugins'] << 'react' hash['extends'] << 'plugin:react/recommended' hash['extends'] << 'prettier/react' end File.open(ES_LINT, 'w+') do |file| file.write(JSON.pretty_generate(hash)) file.close end end end |
.configure_husky ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/init-boilerplate.rb', line 221 def self.configure_husky system('git init') unless Dir.exist?('.git') puts 'Installing and configuring husky with lint-staged...'.colorize(:green) system('npx mrm lint-staged') package_json = File.read(PACKAGE) package_hash = JSON.parse(package_json) package_hash['husky']['hooks']['commit-msg'] = 'commitlint -E HUSKY_GIT_PARAMS' git_add = 'git add' package_hash['lint-staged'] = { '*.+(js|jsx|ts|tsx)': [ 'eslint --cache --fix', git_add ], '*.+(json|css|md|html|ts|tsx|jsx)': [ 'prettier --write', git_add ], '*.+(css|less|sass)': [ 'stylefmt', 'stylelint --fix', git_add ], '*.+(scss)': [ 'stylefmt', 'stylelint --fix --syntax=scss', git_add ] } File.open(PACKAGE, 'w+') do |file| file.write(JSON.pretty_generate(package_hash)) file.close end end |
.configure_nodemon ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/init-boilerplate.rb', line 78 def self.configure_nodemon puts 'Configuring nodemon...'.colorize(:green) system('npm install --save-dev nodemon') return "#{NODEMON} already exists and will not be configured" if File.exist?(NODEMON) hash = { watch: ['src'], ext: '.ts,.js', ignore: [], exec: 'ts-node ./src/index.ts' } File.open(NODEMON, 'w+') do |file| file.truncate(0) file.write(JSON.pretty_generate(hash)) file.close end end |
.configure_prettier ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/init-boilerplate.rb', line 109 def self.configure_prettier puts 'Installing and configuring prettier...'.colorize(:green) system('npm install --save-dev prettier') return "#{PRETTIER} already exists and will not be configured" if File.exist?(PRETTIER) hash = { trailingComma: 'es5', arrowParens: 'always', singleQuote: true, printWidth: 120 } File.open(PRETTIER, 'w+') do |file| file.write(JSON.pretty_generate(hash)) file.close end end |
.configure_stylelint ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/init-boilerplate.rb', line 193 def self.configure_stylelint puts 'Installing and configuring stylelint...'.colorize(:green) system('npm install --save-dev stylelint stylelint-config-standard stylelint-config-sass-guidelines stylelint-prettier stylelint-config-prettier') hash = { extends: %w[stylelint-config-standard stylelint-config-sass-guidelines stylelint-prettier/recommended], plugins: ['stylelint-prettier'], rules: { 'prettier/prettier': true } } File.open(STYLE_LINT, 'w+') do |file| file.write(JSON.pretty_generate(hash)) file.close end end |
.configure_typescript(react) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/init-boilerplate.rb', line 126 def self.configure_typescript(react) puts 'Installing and configuring typescript...'.colorize(:green) system('npm install --save-dev typescript @types/node ts-node rimraf') if react system('npm install --save-dev @types/react @types/react-dom @types/jest') else system('npm install --save-dev @types/express') system('npm install express') end system('npx tsc --init --rootDir src --outDir build --moduleResolution "node" --esModuleInterop --resolveJsonModule --sourceMap --module commonjs --allowJs true --noImplicitAny true --target "es6"') end |
.create_node_directory(typescript) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/init-boilerplate.rb', line 66 def self.create_node_directory(typescript) puts 'Creating src directory...'.colorize(:green) path = if typescript './src/index.ts' else './src/index.js' end dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.directory?(dir) File.new(path, 'w+') end |
.create_node_project ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/init-boilerplate.rb', line 255 def self.create_node_project puts 'Does your project use TypeScript? (y/n)'.colorize(:yellow) input = STDIN.gets.strip.chomp.downcase typescript = input == 'y' puts 'Make sure this is where you want to create your project (y/n)'.colorize(:magenta) display_pwd confirm = STDIN.gets.strip.chomp.downcase exit(0) if confirm != 'y' puts 'Creating node.js project'.colorize(:green) puts 'With TypeScript' if typescript check_npm check_already_init configure_typescript(false) if typescript create_node_directory(typescript) configure_nodemon update_node_package_json configure_prettier configure_eslint(false) configure_commitlint configure_husky end |
.create_react_project ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/init-boilerplate.rb', line 277 def self.create_react_project puts 'Does your project use TypeScript? (y/n)'.colorize(:yellow) input = STDIN.gets.strip.chomp.downcase typescript = input == 'y' puts 'Make sure this is where you want to create your project (y/n)'.colorize(:magenta) display_pwd confirm = STDIN.gets.strip.chomp.downcase exit(0) if confirm != 'y' puts 'Creating react.js project'.colorize(:green) puts 'With TypeScript' if typescript check_npm check_already_init if typescript configure_typescript(true) system('npx create-react-app . --template typescript') else system('npx create-react-app .') end configure_prettier configure_eslint(true) configure_commitlint configure_husky end |
.display_header ⇒ Object
19 20 21 22 23 24 |
# File 'lib/init-boilerplate.rb', line 19 def self.display_header puts "\n========================================".colorize(:blue) puts "\tInitialize Boilerplate".colorize(:blue) puts "========================================\n".colorize(:blue) display_pwd end |
.display_menu ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/init-boilerplate.rb', line 31 def self. puts "\n1. Create node.js project".colorize(:blue) puts '2. Create react.js project'.colorize(:blue) puts '3. Add GitHub templates'.colorize(:blue) puts "4. Exit\n".colorize(:blue) puts 'Select an option:'.colorize(:yellow) input = STDIN.gets.chomp.strip process_option(input.to_i) end |
.display_pwd ⇒ Object
26 27 28 29 |
# File 'lib/init-boilerplate.rb', line 26 def self.display_pwd puts "\nThe current working directory is:".colorize(:green) puts Dir.pwd.to_s.colorize(:green) end |
.eslint_json_exists(react) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/init-boilerplate.rb', line 170 def self.eslint_json_exists(react) es_lint = File.read(ES_LINT) es_lint_hash = JSON.parse(es_lint) if es_lint_hash.key?('plugins') es_lint_hash['plugins'] << 'prettier' else es_lint_hash['plugins'] = 'prettier' end es_lint_hash['rules']['prettier/prettier'] = 'error' es_lint_hash['rules']['no-console'] = 1 es_lint_hash['extends'] << 'prettier' es_lint_hash['extends'] << 'plugin:prettier/recommended' if react es_lint_hash['plugins'] << 'react' es_lint_hash['extends'] << 'plugin:react/recommended' es_lint_hash['extends'] << 'prettier/react' end File.open(ES_LINT, 'w+') do |file| file.write(JSON.pretty_generate(es_lint_hash)) file.close end end |
.main ⇒ Object
358 359 360 361 362 |
# File 'lib/init-boilerplate.rb', line 358 def self.main display_header clean_directory if ARGV[0] == 'clean' end |
.process_option(choice) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/init-boilerplate.rb', line 332 def self.process_option(choice) case choice when 4 puts "\nExiting...".colorize(:green) exit(0) when 3 add_templates when 2 create_react_project when 1 create_node_project else puts "\nYour input is invalid, returning to menu..." end end |
.update_node_package_json ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/init-boilerplate.rb', line 96 def self.update_node_package_json puts "Updating #{PACKAGE} scripts...".colorize(:green) package_json = File.read(PACKAGE) package_hash = JSON.parse(package_json) package_hash['scripts']['start:dev'] = 'nodemon' package_hash['scripts']['build'] = 'rimraf ./build && tsc' package_hash['scripts']['start'] = 'npm run build && node build/index.js' File.open(PACKAGE, 'w+') do |file| file.write(JSON.pretty_generate(package_hash)) file.close end end |