溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用命令行CLI一鍵生成各種煩人的lint配置

發(fā)布時(shí)間:2023-02-24 10:51:41 來源:億速云 閱讀:122 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“怎么用命令行CLI一鍵生成各種煩人的lint配置”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用命令行CLI一鍵生成各種煩人的lint配置”吧!

create-lint-config

這個(gè)工具叫做 create-lint-config,一個(gè)一鍵創(chuàng)建所有的 lint 配置的 CLI 命令行工具。前端工程中特別多的配置文件例如 Eslint、Prettier 等讓我們心煩意亂。我們的目標(biāo)是快速而輕松地生成這些配置!

使用

在你的項(xiàng)目根目錄執(zhí)行以下命令:

# npm
npm create lint-config@latest
# yarn
yarn create lint-config
# pnpm
pnpm create lint-config@latest

執(zhí)行結(jié)果如下:

怎么用命令行CLI一鍵生成各種煩人的lint配置

這個(gè)命令,一次執(zhí)行,創(chuàng)建了 Eslint、StyleLint、prettier、commitlint、husy、lint-staged 等所有配置文件。

現(xiàn)有功能

  • 生成 Eslint 配置。

  • 生成 prettier 配置。

  • 生成 stylelint 配置。

  • 生成 husky 配置。

  • 生成 commitlint 配置。

  • 自動(dòng)安裝依賴。

  • 期待更多。

源碼解讀

#!/usr/bin/env node
async function install({ pkgManager, cwd, _arguments }: { pkgManager: string; cwd: string; arguments: array }) {}
async function init() {
  // 拷貝配置文件基礎(chǔ)模板,包括 Eslint、StyleLint、prettier、commitlint、husy、lint-staged
  await spinner({
    start: `Base template copying...`,
    end: 'Template copied',
    while: () => {
      try {
        copy('base')
      } catch (e) {
        error('error', e)
        process.exit(1)
      }
    },
  })
  // 安裝 husky
  await spinner({
    start: `Husky installing...`,
    end: 'Husky installed',
    while: () =>
      install({
        cwd: process.cwd(),
        pkgManager: 'npx',
        _arguments: ['husky', 'install'],
      }).catch((e) => {
        error('error', e)
        process.exit(1)
      }),
  })
  // husky 寫入 commit-msg 校驗(yàn)指令,使用 commitlint
  await spinner({
    start: `Adding commit-msg lint...`,
    end: 'Commit-msg lint added',
    while: () =>
      install({
        cwd: process.cwd(),
        pkgManager: 'npx',
        _arguments: ['husky', 'add', '.husky/commit-msg', 'npx --no-install commitlint --edit ""'],
      }).catch((e) => {
        error('error', e)
        process.exit(1)
      }),
  })
  // husky 寫入 pre-commit校驗(yàn)指令,使用 lint-staged 執(zhí)行 elint 等
  await spinner({
    start: `Adding lint-staged...`,
    end: 'Lint-staged added',
    while: () =>
      install({
        cwd: process.cwd(),
        pkgManager: 'npx',
        _arguments: ['husky', 'add', '.husky/pre-commit', 'npx lint-staged'],
      }).catch((e) => {
        error('error', e)
        process.exit(1)
      }),
  })
  // 安裝依賴
  await spinner({
    start: `Dependencies installing with npm...`,
    end: 'Dependencies installed',
    while: () =>
      install({
        cwd: process.cwd(),
        pkgManager: 'npm',
        _arguments: ['install'],
      }).catch((e) => {
        error('error', e)
        process.exit(1)
      }),
  })
}
init().catch((e) => {
  console.error(e)
})

TODO

  • 支持通過--template標(biāo)志來選擇模板,創(chuàng)建更多的配置文件模板,包括 ts、vue、react、node 等等

  • 支持更靈活的交互式選項(xiàng)?,F(xiàn)在只能一鍵生成默認(rèn)的模板,有些配置可能是一些人不需要的,后續(xù)計(jì)劃可以更靈活。

感謝各位的閱讀,以上就是“怎么用命令行CLI一鍵生成各種煩人的lint配置”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么用命令行CLI一鍵生成各種煩人的lint配置這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI