json 安装JS Dev Tools

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json 安装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

```bash
$ 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"
}

以上是关于json 安装JS Dev Tools的主要内容,如果未能解决你的问题,请参考以下文章

关于新版vue-cli安装json-server在build文件里没生成出dev-server文件

sh IBM Dev Tools安装程序脚本

npm 安装 --save-dev 与 --save的使用与区别

npm 安装 --save-dev 与 --save的使用与区别

dev tools杂谈

安装vmware tools