From 7901d325b8263cf20b6daa354ad73ee33986e08a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Mon, 31 Jul 2023 13:29:37 +0800 Subject: [PATCH] feat: ignores first impl --- .github/workflows/ci.yaml | 29 ++++++++++++---------------- LICENSE | 2 +- eslint.config.js | 4 ++++ lib/index.d.ts | 18 ++++++++++++++++++ lib/index.js | 31 +++++++++++++++++++++++++++--- lib/index.test.js | 10 ---------- package.json | 40 ++++++++++++++++++--------------------- readme.md | 37 +++++++++++++++++------------------- tests/index.spec.js | 13 +++++++++++++ tsconfig.json | 10 ++++++++++ 10 files changed, 121 insertions(+), 73 deletions(-) create mode 100644 eslint.config.js create mode 100644 lib/index.d.ts delete mode 100644 lib/index.test.js create mode 100644 tests/index.spec.js create mode 100644 tsconfig.json diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ce34731..d0f1ee9 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,11 +13,11 @@ jobs: name: Lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup Node.js - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: - node-version: '16.x' + node-version: "18.x" - name: Install dependencies run: npm install - name: Lint files @@ -27,19 +27,14 @@ jobs: strategy: matrix: os: [ubuntu-latest] - node: [17.x, 16.x, 14.x, 12.x, "12.22.0"] - include: - - os: windows-latest - node: "16.x" - - os: macOS-latest - node: "16.x" + node: [20.x, lts/*] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 - with: - node-version: ${{ matrix.node }} - - name: Install dependencies - run: npm install - - name: Run tests - run: npm run test \ No newline at end of file + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + - name: Install dependencies + run: npm install + - name: Run tests + run: npm run test:unit diff --git a/LICENSE b/LICENSE index a4e68e9..ad59297 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2018 薛定谔的猫 +Copyright (c) 唯然 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..cbc96df --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,4 @@ +import js from "@eslint/js"; +import { ignores } from "./lib/index.js"; + +export default [js.configs.recommended, ignores()]; diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..ffd7634 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,18 @@ +/** + * @param {object} [opts] + * @param {string?} [opts.cwd] + * @param {string?} [opts.filename] + * @param {string[]?} [opts.extraIgnores] + * @returns {{ignores: string[]}} the list of ignored files + */ +export function ignores( + opts?: + | { + cwd?: string | null | undefined; + filename?: string | null | undefined; + extraIgnores?: string[] | null | undefined; + } + | undefined +): { + ignores: string[]; +}; diff --git a/lib/index.js b/lib/index.js index a8c3618..7483189 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,30 @@ -"use strict"; +/** + * @fileoverview eslint ignore files automatically. + * @author 唯然 + */ +import process from "process"; +import fs from "fs"; +import path from "path"; -export function add(a, b) { - return a + b; +/** + * @param {object} [opts] + * @param {string} [opts.cwd] + * @param {string} [opts.filename] + * @param {string[]?} [opts.extraIgnores] + * @returns {{ignores: string[]}} the list of ignored files + */ +export function ignores(opts = {}) { + const ignoreFile = path.join( + opts.cwd || process.cwd(), + opts.filename || ".gitignore" + ); + const text = fs.readFileSync(ignoreFile, "utf-8"); + const lines = text + .split(/(\r\n|\n)+/) + .map((line) => line.trim()) + .filter((line) => line && !line.startsWith("#")); + + return { + ignores: [...lines, ...(opts.extraIgnores || [])], + }; } diff --git a/lib/index.test.js b/lib/index.test.js deleted file mode 100644 index dfa02a1..0000000 --- a/lib/index.test.js +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; - -import { strict as assert } from "assert"; -import { add } from "./index.js"; - -describe("add", () => { - it("1 + 1 === 2", () => { - assert.equal(add(1, 1), 2); - }); -}); diff --git a/package.json b/package.json index ef59489..495286c 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,19 @@ { - "name": "@weiran.zsd/js-starter", + "name": "@weiran.zsd/eslint-ignores", "version": "0.0.0", "private": false, - "description": "A full featured node.js starter.", + "description": "eslint ignore files automatically.", "keywords": [ - "Node.js", - "template", "eslint", + "eslint-plugin", + "gitignore", "esm" ], "license": "MIT", "author": "唯然=12" + "node": ">=16" } } diff --git a/readme.md b/readme.md index 6f7017e..ef9636b 100644 --- a/readme.md +++ b/readme.md @@ -1,31 +1,28 @@ -# Node.js starter +# eslint-ignores ## Usage ```sh -$ npx degit weiran.zsd/starter +$ npm install @weiran.zsd/eslint-ignores -D ``` -```sh -# run linting -$ npm run lint [-- --fix] +```js +// eslint.config.js +import js from "@eslint/js"; +import { ignores } from "@weiran.zsd/eslint-ignores"; -# run test -$ npm test +export default [js.configs.recommended, ignores()]; +``` -# watching test -$ npm run test:watch +## Options -# release -$ npm run release -``` +TODO - add options + + +## Credits + +* [aladdin-add](https://github.com/aladdin-add) -## Features +## License -* Node.js esm -* prettier -* eslint + eslint-plugin-node -* pre-commit with husky -* release with standard-version -* testing with jest -* gh actions +MIT. diff --git a/tests/index.spec.js b/tests/index.spec.js new file mode 100644 index 0000000..e8dcc4b --- /dev/null +++ b/tests/index.spec.js @@ -0,0 +1,13 @@ +import test from "node:test"; +import { strict as t } from "node:assert/strict"; +import { ignores } from "../lib/index.js"; + +test("ignores", function () { + t(typeof ignores === "function"); + + const config = ignores(); + + t(config); + t(Array.isArray(config.ignores)); + t(config.ignores.includes("/node_modules")); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7d651e9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@tsconfig/node20/tsconfig.json", + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "declaration": true, + "noEmit": true + }, + "include": ["lib/", "tests/index.spec.js"] +}