|
| 1 | +const fs = require('fs'); |
| 2 | + |
| 3 | +const PACKAGE_FILE = 'package.json'; |
| 4 | +const UTF_8 = 'utf-8'; |
| 5 | + |
| 6 | +const husky = { |
| 7 | + husky: { |
| 8 | + hooks: { |
| 9 | + 'pre-push': 'npm run push:pre', |
| 10 | + 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', |
| 11 | + 'prepare-commit-msg': 'exec < /dev/tty && true', |
| 12 | + }, |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +const DEV_DEPENDENCIES = [ |
| 17 | + '@babel/plugin-proposal-class-properties', |
| 18 | + '@commitlint/cli', |
| 19 | + '@commitlint/config-conventional', |
| 20 | + '@testing-library/jest-dom', |
| 21 | + '@testing-library/react', |
| 22 | + '@testing-library/user-event', |
| 23 | + '@types/jest', |
| 24 | + 'env-cmd', |
| 25 | + 'eslint-config-prettier', |
| 26 | + 'eslint-plugin-prettier', |
| 27 | + 'husky', |
| 28 | + 'jest-environment-jsdom-sixteen', |
| 29 | + 'json-server', |
| 30 | + 'msw', |
| 31 | + 'nodemon', |
| 32 | + 'npm-run-all', |
| 33 | + 'prettier', |
| 34 | +]; |
| 35 | + |
| 36 | +const deleteThisScript = () => { |
| 37 | + console.log('\nAuto deleting this script'); |
| 38 | + fs.unlink('post_create.js', (err) => { |
| 39 | + if (err) throw err; |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | +const moveDevDependencies = (oldContent, devDependencies = {}) => { |
| 44 | + for (let dependency in oldContent.dependencies) { |
| 45 | + if (DEV_DEPENDENCIES.includes(dependency)) { |
| 46 | + console.log(`Moving ${dependency} to devDependencies`); |
| 47 | + devDependencies[dependency] = oldContent.dependencies[dependency]; |
| 48 | + delete oldContent.dependencies[dependency]; |
| 49 | + } |
| 50 | + } |
| 51 | + return devDependencies; |
| 52 | +}; |
| 53 | + |
| 54 | +fs.readFile(PACKAGE_FILE, UTF_8, (err, data) => { |
| 55 | + if (err) throw err; |
| 56 | + |
| 57 | + const oldContent = JSON.parse(data); |
| 58 | + if (oldContent.husky && oldContent.devDependencies) { |
| 59 | + console.log(`Your ${PACKAGE_FILE} is already correct.`); |
| 60 | + deleteThisScript(); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const devDependencies = moveDevDependencies(oldContent); |
| 65 | + |
| 66 | + console.log(`\nAdding husky and devDependencies to new ${PACKAGE_FILE}`); |
| 67 | + const newContent = { ...oldContent, husky, devDependencies }; |
| 68 | + const newFileContent = JSON.stringify(newContent, null, 2); |
| 69 | + |
| 70 | + console.log(`\nWriting ${PACKAGE_FILE} to disk`); |
| 71 | + fs.writeFile(PACKAGE_FILE, newFileContent, UTF_8, (err) => { |
| 72 | + if (err) throw err; |
| 73 | + deleteThisScript(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments