@chegg-configs/lint-staged 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @chegg-configs/lint-staged might be problematic. Click here for more details.

Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -0
  3. package/index.js +95 -0
  4. package/package.json +23 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License Copyright (c) 2021
2
+
3
+ Permission is hereby granted, free
4
+ of charge, to any person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use, copy, modify, merge,
7
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ (including the next paragraph) shall be included in all copies or substantial
13
+ portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # @chegg-configs/lint-staged
2
+
3
+ A base configuration for chegg lint-staged — a popular tool for running linters on staged Git files.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm i @chegg-configs/lint-staged
9
+ // or
10
+ yarn add @chegg-configs/lint-staged
11
+ ```
12
+
13
+ ### Usage
14
+ Add to your `.lintstagedrc.js`:
15
+
16
+ ```js
17
+ module.exports = {
18
+ ...require("@chegg-configs/lint-staged"),
19
+ };
20
+ ```
package/index.js ADDED
@@ -0,0 +1,95 @@
1
+ const path = require('path');
2
+ const micromatch = require('micromatch');
3
+
4
+ /**
5
+ * lint-staged Config generator
6
+ *
7
+ * @param {string} [dir]
8
+ * @param {import("./types").CommandMappings} [mapping]
9
+ * @return {import("./types").LintStagedCommandMapper}
10
+ */
11
+ export default function (dir, mapping) {
12
+ return (allStagedFiles) => {
13
+ /**
14
+ * @type {string[]}
15
+ */
16
+ const commandList = [];
17
+ const cwd = process.cwd();
18
+
19
+ dir = !dir
20
+ ? // 引数がないならカレントディレクトリ
21
+ cwd
22
+ : // 絶対パスかどうか
23
+ path.isAbsolute(dir)
24
+ ? // 絶対パスならそのまま
25
+ dir
26
+ : // 相対パスなら絶対パスに変換
27
+ path.resolve(cwd, dir);
28
+
29
+ mapping = mapping ?? defaultMapping;
30
+
31
+ Object.entries(mapping).forEach(([ext, commandTypes]) => {
32
+ commandTypes.forEach((commandType) => {
33
+ const shell = commands[commandType];
34
+
35
+ if (!shell) {
36
+ return;
37
+ }
38
+
39
+ const pattern = path
40
+ .resolve(dir, '**', `{*.${ext},.*.${ext}}`)
41
+ .replaceAll(path.sep, '/');
42
+
43
+ const files = allStagedFiles.map((f) => f.replaceAll(path.sep, '/'));
44
+
45
+ const targetFiles = micromatch(files, pattern);
46
+
47
+ if (targetFiles.length <= 0) {
48
+ return;
49
+ }
50
+
51
+ commandList.push(shell + ' ' + targetFiles.map((f) => `"${f}"`).join(' '));
52
+ });
53
+ });
54
+
55
+ return commandList;
56
+ };
57
+ }
58
+
59
+ /**
60
+ * @type {Record<import("./types").CommandType, string>}
61
+ */
62
+ export const commands = {
63
+ cspell: 'cspell --no-must-find-files --show-suggestions',
64
+ eslint: 'eslint --fix',
65
+ markuplint: 'markuplint',
66
+ prettier: 'prettier --write',
67
+ puglint: 'pug-lint',
68
+ stylelint: 'stylelint --fix',
69
+ };
70
+
71
+ /**
72
+ * @type {import("./types").CommandMappings}
73
+ */
74
+ export const defaultMapping = {
75
+ md: ['prettier', 'textlint', 'cspell'],
76
+ mdx: ['prettier', 'textlint', 'cspell'],
77
+ json: ['prettier', 'cspell'],
78
+ yaml: ['cspell'],
79
+ yml: ['cspell'],
80
+ js: ['eslint', 'prettier', 'cspell'],
81
+ cjs: ['eslint', 'prettier', 'cspell'],
82
+ mjs: ['eslint', 'prettier', 'cspell'],
83
+ jsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
84
+ ts: ['eslint', 'prettier', 'cspell'],
85
+ cts: ['eslint', 'prettier', 'cspell'],
86
+ mts: ['eslint', 'prettier', 'cspell'],
87
+ tsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
88
+ html: ['markuplint', 'prettier', 'cspell'],
89
+ pug: ['markuplint', 'prettier', 'cspell'],
90
+ css: ['stylelint', 'prettier', 'cspell'],
91
+ scss: ['stylelint', 'prettier', 'cspell'],
92
+ astro: ['eslint', 'markuplint', 'prettier', 'cspell'],
93
+ svelte: ['eslint', 'markuplint', 'prettier', 'cspell'],
94
+ vue: ['eslint', 'markuplint', 'prettier', 'cspell'],
95
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@chegg-configs/lint-staged",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "description": "A base configuration for chegg lint-staged — a popular tool for running linters on staged Git files.",
6
+ "license": "MIT",
7
+ "author": "hchgg-lpc",
8
+ "main": "li",
9
+ "repository": "https://www.github.com/hchgg-lpc/chegg-configs/lint-staged",
10
+ "scripts": {
11
+ "build": "node build.js",
12
+ "test": "exit 0"
13
+ },
14
+ "dependencies": {
15
+ "micromatch": "^4.0.5"
16
+ },
17
+ "devDependencies": {
18
+ "lint-staged": "^13.2.3"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ }
23
+ }