Skip to content

Commit

Permalink
feat: added support for nano-staged
Browse files Browse the repository at this point in the history
Signed-off-by: prisis <[email protected]>
  • Loading branch information
prisis committed Nov 6, 2024
1 parent 8f87808 commit 500c5cd
Show file tree
Hide file tree
Showing 5 changed files with 3,344 additions and 601 deletions.
53 changes: 42 additions & 11 deletions packages/lint-staged-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<h1>Shareable Lint-Staged Config</h1>

Package that contains shareable configuration for [lint-staged](https://github.com/okonet/lint-staged) — a popular tool for running linters on staged Git files.
Package that contains shareable configuration for [lint-staged][lint-staged] / [nano-staged][nano-staged] — a popular tool for running linters on staged Git files.

And optionally for [husky](https://github.com/typicode/husky) — a popular choice for configuring git hooks.

Expand All @@ -24,19 +24,21 @@ And optionally for [husky](https://github.com/typicode/husky) — a popular choi

Simplify your project setup with our comprehensive collection of preconfigured configuration files.
Designed for effortless integration, these ready-to-use files can be seamlessly imported into your projects.
These functions streamline the utilization of [lint-staged](https://github.com/okonet/lint-staged) alongside popular tools such as [ESLint](https://eslint.org), [Prettier](https://prettier.io), [StyleLint](https://stylelint.io) and [SecretLint](https://github.com/secretlint/secretlint).
These functions streamline the utilization of [lint-staged][lint-staged] / [nano-staged][nano-staged] alongside popular tools such as [ESLint](https://eslint.org), [Prettier](https://prettier.io), [StyleLint](https://stylelint.io) and [SecretLint](https://github.com/secretlint/secretlint).

Say goodbye to complex configurations and enjoy a smoother development experience with our all-inclusive package.

## Purpose

- Enhance your development workflow with hassle-free and readily shareable lint-staged configuration files.
- Enhance your development workflow with hassle-free and readily shareable lint-staged / nano-staged configuration files.
- These files empower developers to maintain a uniform coding style and detect prevalent errors even before they are added to the source control system.
- Additionally, our inclusive helper module offers convenient utility functions that seamlessly integrate lint-staged with popular linting and formatting tools.

## Install

```bash
npm install --dev-save @anolilab/lint-staged-config nano-staged
// or
npm install --dev-save @anolilab/lint-staged-config lint-staged

Optional:
Expand All @@ -45,6 +47,8 @@ npm install --dev-save husky is-ci
```

```sh
yarn add -D @anolilab/lint-staged-config nano-staged
// or
yarn add -D @anolilab/lint-staged-config lint-staged

Optional:
Expand All @@ -53,6 +57,8 @@ yarn add -D husky is-ci
```

```sh
pnpm add -D @anolilab/lint-staged-config nano-staged
// or
pnpm add -D @anolilab/lint-staged-config lint-staged

Optional:
Expand All @@ -62,21 +68,28 @@ pnpm add -D husky is-ci

## Usage

If you don’t have a `.lintstagedrc.js`, we can create the file for you after installing `@anolilab/lint-staged-config`, call `pnpm lint-stage-config:install`.
If you don’t have a `.lintstagedrc.js` or `.nano-staged.js`, we can create the file for you after installing `@anolilab/lint-staged-config`, call `pnpm lint-stage-config:install`.

If you already have a `.lintstagedrc.js`, then you can extend the `.lintstagedrc.js`, with `@anolilab/lint-staged-config`.
If you already have a `.lintstagedrc.js` or `.nano-staged.js`, then you can extend the `.lintstagedrc.js` or `.nano-staged.js`, with `@anolilab/lint-staged-config`.

> Note: If the script detects an existing `.lintstagedrc.js` file, it will not overwrite it.
> Note: If the script detects an existing `.lintstagedrc.js` or `.nano-staged.js` file, it will not overwrite it.

The content of the `.lintstagedrc.js` should look like this:
The content of the `.lintstagedrc.js` or `.nano-staged.js` should look like this:

```js
const config = require("@anolilab/lint-staged-config");

module.exports = {
...config,
};

// or

import config from "@anolilab/lint-staged-config";

export default {
...config,
};
```
### Config

Expand Down Expand Up @@ -172,8 +185,6 @@ Adding the following script to your root `package.json` file makes it easier to
pnpm pkg set scripts.prepare="is-ci || husky install || exit 0"
```

For `npm` users, replace `pnpm` with `npm` in the above command.

This script will install husky only if it is not running in a CI environment.

After that you can find a `.husky` folder in your root directory, with a `pre-commit` (if not please create it) file.
Expand All @@ -191,11 +202,29 @@ The file content can look like this:
echo --------------------------------------------
echo Starting Git hook: pre-commit

pnpx lint-staged --verbose --concurrent false
./node_modules/.bin/lint-staged --verbose --concurrent false

echo Finished Git hook: pre-commit
echo --------------------------------------------
```

or with `nano-staged`:

```bash
#!/bin/sh

. "$(dirname "$0")/_/husky.sh"

# The hook should exit with non-zero status after issuing
# an appropriate message if it wants to stop the commit.

echo --------------------------------------------
echo Starting Git hook: pre-commit

./node_modules/.bin/nano-staged

echo Finished Git hook: pre-commit
echo --------------------------------------------
```

Our package includes a `lint-stage-config:install` command to add the `pre-commit`, `common.sh`, `prepare-commit-msg hooks to your `.husky` folder.
Expand Down Expand Up @@ -232,3 +261,5 @@ The anolilab javascript-style-guide is open-sourced software licensed under the
[license-url]: LICENSE.md "license"
[npm-image]: https://img.shields.io/npm/v/@anolilab/lint-staged-config/latest.svg?style=for-the-badge&logo=npm
[npm-url]: https://www.npmjs.com/package/@anolilab/lint-staged-config/v/latest "npm"
[lint-staged]: https://github.com/okonet/lint-staged
[nano-staged]: https://github.com/usmanyunusov/nano-staged
76 changes: 56 additions & 20 deletions packages/lint-staged-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"anolilab",
"lint",
"lint-staged",
"nano-staged",
"config",
"lint-staged-config"
"lint-staged-config",
"nano-staged-config"
],
"homepage": "https://anolilab.com/nodejs/packages/lint-staged-config",
"repository": {
Expand Down Expand Up @@ -121,51 +123,79 @@
"bin": {
"lint-stage-config:install": "./dist/bin.mjs"
},
"typesVersions": {
">=5.0": {
".": [
"./dist/index.d.ts"
],
"group/eslint": [
"./dist/groups/eslint/index.d.ts"
],
"group/json": [
"./dist/groups/json.d.ts"
],
"group/markdown": [
"./dist/groups/markdown.d.ts"
],
"group/secretlint": [
"./dist/groups/secretlint.d.ts"
],
"group/stylesheets": [
"./dist/groups/stylesheets.d.ts"
],
"group/tests": [
"./dist/groups/tests.d.ts"
],
"group/typescript": [
"./dist/groups/typescript.d.ts"
]
}
},
"files": [
"dist",
"README.md",
"CHANGELOG.md",
"LICENSE.md"
],
"scripts": {
"build": "cross-env NODE_ENV=development packem build",
"build:prod": "cross-env NODE_ENV=production packem build",
"build": "packem build --development",
"build:prod": "packem build --production",
"clean": "rimraf node_modules dist",
"test": "vitest --config ./vitest.config.ts",
"test:coverage": "vitest --config ./vitest.config.ts --run --coverage"
},
"dependencies": {
"@anolilab/package-json-utils": "3.0.9",
"@visulima/fs": "2.1.10",
"@visulima/fs": "2.3.0",
"shell-quote": "^1.8.1"
},
"devDependencies": {
"@anolilab/semantic-release-pnpm": "^1.1.3",
"@anolilab/semantic-release-preset": "9.0.0",
"@total-typescript/ts-reset": "^0.5.1",
"@anolilab/semantic-release-pnpm": "^1.1.5",
"@anolilab/semantic-release-preset": "9.0.2",
"@total-typescript/ts-reset": "^0.6.1",
"@types/lint-staged": "^13.3.0",
"@types/shell-quote": "^1.7.5",
"@visulima/packem": "^1.0.0-alpha.49",
"@vitest/coverage-v8": "^1.6.0",
"cross-env": "^7.0.3",
"esbuild": "^0.23.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.7",
"rimraf": "^5.0.8",
"semantic-release": "^24.0.0",
"type-fest": "^4.21.0",
"typescript": "5.5.3",
"vitest": "^1.6.0"
"@visulima/packem": "^1.5.1",
"@vitest/coverage-v8": "^2.1.4",
"esbuild": "^0.24.0",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"rimraf": "^6.0.1",
"semantic-release": "^24.2.0",
"type-fest": "^4.26.1",
"typescript": "5.6.3",
"vitest": "^2.1.4"
},
"peerDependencies": {
"eslint": "^8.49.0",
"husky": "^8.x || ^9.x",
"jest": "^27.x || ^28.x",
"lint-staged": "^13.x || ^14.x|| ^15.x",
"nano-staged": "^0.8",
"prettier": "^3.x",
"secretlint": "^7.x || ^8.x",
"stylelint": "^15.x || ^16.x",
"vitest": "^1.x"
"vitest": "^1.x || ^2.x"
},
"peerDependenciesMeta": {
"eslint": {
Expand All @@ -185,10 +215,16 @@
},
"vitest": {
"optional": true
},
"lint-staged": {
"optional": true
},
"nano-staged": {
"optional": true
}
},
"engines": {
"node": ">=18.* <=22.*"
"node": ">=18.* <=23.*"
},
"publishConfig": {
"access": "public",
Expand Down
4 changes: 4 additions & 0 deletions packages/lint-staged-config/packem.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default defineConfig({
license: {
path: "./LICENSE.md",
},
node10Compatibility: {
typeScriptVersion: ">=5.0",
writeToPackageJson: true,
},
},
transformer,
});
57 changes: 48 additions & 9 deletions packages/lint-staged-config/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import getNearestConfigPath from "./utils/get-nearest-config-path";

console.log("Configuring @anolilab/lint-staged-config", projectPath, "\n");

const configFile = ".lintstagedrc";

const checkIfFileExists = (filename: string): boolean => {
if (isAccessibleSync(filename)) {
console.warn(`⚠️ ${filename} already exists;`);
Expand All @@ -23,7 +21,9 @@ const checkIfFileExists = (filename: string): boolean => {
/**
* Writes .lintstagedrc.js if it doesn't exist. Warns if it exists.
*/
const writeLintstagedRc = async () => {
const writeLintStagedRc = async () => {
const configFile = ".lintstagedrc";

// eslint-disable-next-line no-restricted-syntax,no-loops/no-loops
for (const filename of [
configFile,
Expand All @@ -42,16 +42,47 @@ const writeLintstagedRc = async () => {
}
}

const lintstagedPath = join(projectPath, ".lintstagedrc.js");
const filePath = join(projectPath, ".lintstagedrc.js");
const content = `${packageIsTypeModule ? 'import config from "@anolilab/lint-staged-config"' : 'const config = require("@anolilab/lint-staged-config")'};
${packageIsTypeModule ? "export default" : "module.exports ="} {
...config,
};
`;

writeFileSync(filePath, content);
};

const writeNanoStagedRc = async () => {
const configFile = ".nano-staged";

// eslint-disable-next-line no-restricted-syntax,no-loops/no-loops
for (const filename of [
configFile,
`${configFile}.js`,
`${configFile}.cjs`,
`${configFile}.mjs`,
`${configFile}.json`,
`${configFile.replace(".", "")}.js`,
`${configFile.replace(".", "")}.cjs`,
`${configFile.replace(".", "")}.mjs`,
`${configFile.replace(".", "")}.json`,
".nanostagedrc",
]) {
if (checkIfFileExists(join(projectPath, filename))) {
return;
}
}

const filePath = join(projectPath, ".nano-staged.js");
const content = `${packageIsTypeModule ? 'import config from "@anolilab/lint-staged-config"' : 'const config = require("@anolilab/lint-staged-config")'};
${packageIsTypeModule ? "export default" : "module.exports ="} {
...config,
};
`;

writeFileSync(lintstagedPath, content, );
writeFileSync(filePath, content);
};

/**
Expand Down Expand Up @@ -98,7 +129,7 @@ if [ "$IS_WINDOWS" = "true" ]; then
exec < /dev/tty
fi
fi
`
`,
);
}

Expand Down Expand Up @@ -132,7 +163,7 @@ ${hasPnpm ? "pnpx" : "npx"} lint-staged --verbose --concurrent false
echo Finished Git hook: pre-commit
echo --------------------------------------------
`
`,
);
}

Expand Down Expand Up @@ -184,15 +215,23 @@ fi
echo Finished Git hook: prepare-commit-msg
echo --------------------------------------------
`
`,
);
}
};

// eslint-disable-next-line unicorn/prefer-top-level-await
(async () => {
const hasLintStaged = hasDependency("lint-staged") || hasDevDependency("lint-staged");
const hasNanoStaged = hasDependency("nano-staged") || hasDevDependency("nano-staged");

try {
await writeLintstagedRc();
if (hasLintStaged) {
await writeLintStagedRc();
} else if (hasNanoStaged) {
await writeNanoStagedRc();
}

await writeHuskyFiles();

console.log("😎 Everything went well, have fun!");
Expand Down
Loading

0 comments on commit 500c5cd

Please sign in to comment.