Skip to content

Instantly share code, notes, and snippets.

@mikeerickson
Last active November 23, 2018 18:18
Show Gist options
  • Save mikeerickson/2f876f65a2562b5ff49f3a4fcf506bd6 to your computer and use it in GitHub Desktop.
Save mikeerickson/2f876f65a2562b5ff49f3a4fcf506bd6 to your computer and use it in GitHub Desktop.
Install JS Dev Tools

Install JS Developer Tools

Installs common development tools that I like to use in each project

Installation

  1. Make sure project has been initialized with npm init
  2. Execute npx command
$ npx https://gist.github.com/mikeerickson/2f876f65a2562b5ff49f3a4fcf506bd6

Details

  • Installs chalk
  • Installs and configure commitlint and default commitlint.config.js
  • Installs and configure husky
  • Installs and configure eslint and default eslintrc.js
  • Installs and configure prettier, eslint-plugin-prettier and default .prettierrc
#!/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");
}
{
"name": "install-js-dev-tools",
"version": "0.0.1",
"bin": "./index.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment