Last active
November 23, 2018 18:25
-
-
Save mikeerickson/8ce0ec92e1bab85cf9f000c6cfd4e186 to your computer and use it in GitHub Desktop.
Install Common Development Tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const PWD = process.env.PWD; | |
const fs = require("fs"); | |
const path = require("path"); | |
const { spawnSync } = require("child_process"); | |
const spawnOptions = { | |
cwd: path.normalize("./"), | |
shell: true, | |
env: process.env, | |
stdio: "inherit" | |
}; | |
const pkgInfoPath = path.join(PWD, "./package.json"); | |
const pkgInfoLockPath = path.join(PWD, "./package-lock.json"); | |
if (!fs.existsSync(pkgInfoPath)) { | |
console.log("\nPlease initialize your project (npm init -y) before installing dependencies"); | |
process.exit(1); | |
} | |
const pkgInfo = require(pkgInfoPath); | |
if (!fs.existsSync(".git")) { | |
spawnSync("git init", [], spawnOptions); | |
} | |
if (fs.existsSync(pkgInfoLockPath)) { | |
fs.unlinkSync(pkgInfoLockPath); | |
} | |
const installModules = | |
"npm install --save-dev chalk dumper.js husky @commitlint/{config-conventional,cli} eslint eslint-plugin-prettier prettier"; | |
spawnSync(installModules, [], spawnOptions); | |
const commitConfigPath = path.join(PWD, "commitlint.config.js"); | |
const commitLintData = "module.exports = { extends: [\"@commitlint/config-conventional\"] };"; | |
if (!fs.existsSync(commitConfigPath)) { | |
fs.writeFile(commitConfigPath, commitLintData + "\n", err => { | |
if (err) console.error(err); | |
}); | |
} | |
const eslintPath = path.join(PWD, ".eslintrc.js"); | |
const eslintData = `module.exports = { | |
env: { | |
es6: true, | |
node: true, | |
jest: true | |
}, | |
plugins: ["prettier"], | |
extends: "eslint:recommended", | |
parserOptions: { | |
ecmaVersion: 2017 | |
}, | |
rules: { | |
"prettier/prettier": "error", | |
indent: ["error", 2], | |
quotes: ["error", "double"], | |
semi: ["error", "always"], | |
"no-unused-vars": "off", | |
"no-undef": "off", | |
"no-console": [ | |
"error", | |
{ | |
allow: ["log", "error", "dir"] | |
} | |
] | |
} | |
}; | |
`; | |
if (!fs.existsSync(eslintPath)) { | |
fs.writeFile(eslintPath, eslintData, err => { | |
if (err) console.error(err); | |
}); | |
} | |
const prettierPath = path.join(PWD, ".prettierrc"); | |
const prettierData = `{ | |
"no-empty-interface": false, | |
"semi": true, | |
"printWidth": 110, | |
"singleQuote": false | |
} | |
`; | |
if (!fs.existsSync(prettierPath)) { | |
fs.writeFile(prettierPath, prettierData, err => { | |
if (err) console.error(err); | |
}); | |
} | |
if (fs.existsSync(pkgInfoPath)) { | |
let ret = pkgInfo.scripts.hasOwnProperty("pre-commit"); | |
console.log("==> Adding pre-commit script..."); | |
if (!pkgInfo.scripts.hasOwnProperty("pre-commit")) { | |
pkgInfo.scripts["pre-commit"] = "npm run test && npm run lint || true"; | |
fs.writeFileSync(pkgInfoPath, JSON.stringify(pkgInfo, null, 2)); | |
} | |
console.log("==> Adding linting script..."); | |
if (!pkgInfo.scripts.hasOwnProperty("lint")) { | |
pkgInfo.scripts.lint = "eslint . || true"; | |
fs.writeFileSync(pkgInfoPath, JSON.stringify(pkgInfo, null, 2)); | |
} | |
const huskyConfig = { | |
hooks: { | |
"pre-commit": "npm run pre-commit", | |
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | |
} | |
}; | |
console.log("==> Adding husky script..."); | |
if (!pkgInfo.hasOwnProperty("husky")) { | |
pkgInfo.husky = huskyConfig; | |
fs.writeFileSync(pkgInfoPath, JSON.stringify(pkgInfo, null, 2)); | |
} | |
console.log("\n✅ Initialization Complete"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment