@putout/operator-match-files 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) coderaiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @putout/operator-match-files [![NPM version][NPMIMGURL]][NPMURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/operator-match-files.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@putout/operator-match-files "npm"
5
+
6
+ 🐊[**Putout**](https://github.com/coderaiser/putout) operator adds ability to match files to plugins.
7
+
8
+ ## Install
9
+
10
+ ```
11
+ npm i putout @putout/operator-match-files
12
+ ```
13
+
14
+ ## API
15
+
16
+ If you want to create 🐊[**Putout**](https://github.com/coderaiser/putout) `plugin` that will match files according to your needs just:
17
+
18
+ ```js
19
+ const {operator} = require('putout');
20
+ const {matchFiles} = operator;
21
+ const updateTSConfig = require('../update-tsconfig');
22
+
23
+ module.exports = match - files({
24
+ 'tsconfig.json': updateTSConfig,
25
+ });
26
+ ```
27
+
28
+ This will help in case when `update-tsconfig` is disabled by default:
29
+
30
+ ```js
31
+ const updateTSConfig = require('./update-tsconfig');
32
+
33
+ module.exports.rules = {
34
+ 'update-tsconfig': ['off', updateTSConfig],
35
+ };
36
+ ```
37
+
38
+ And you want to help users avoid updating `.putout.json` config with:
39
+
40
+ ```json
41
+ {
42
+ "match": {
43
+ "tsconfig.json": {
44
+ "nextjs/update-tsconfig": "on"
45
+ }
46
+ },
47
+ "plugins": ["nextjs"]
48
+ }
49
+ ```
50
+
51
+ Instead of this, [`redlint`](https://github.com/putoutjs/redlint) can be used, it will generate `.filesystem.json` which can be processed by 🐊**Putout**.
52
+
53
+ ## License
54
+
55
+ MIT
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ const {parse} = require('@putout/babel');
4
+ const {print} = require('@putout/printer');
5
+ const {transform} = require('putout/transform');
6
+ const {findPlaces} = require('putout/find-places');
7
+
8
+ const {
9
+ __filesystem,
10
+ toJS,
11
+ fromJS,
12
+ } = require('@putout/operator-json');
13
+
14
+ const {
15
+ readFileContent,
16
+ findFile,
17
+ writeFileContent,
18
+ } = require('@putout/operator-filesystem');
19
+
20
+ const {entries} = Object;
21
+ const report = ({message}) => message;
22
+
23
+ module.exports.matchFiles = (files) => {
24
+ const traverse = createTraverse(files);
25
+
26
+ return {
27
+ fix,
28
+ traverse,
29
+ report,
30
+ };
31
+ };
32
+
33
+ function fix({path, matchedJS, matchedAST, plugins}) {
34
+ transform(matchedAST, matchedJS, {
35
+ plugins,
36
+ });
37
+
38
+ const updatedMatchedJS = print(matchedAST);
39
+ const matchedJSON = fromJS(updatedMatchedJS);
40
+
41
+ writeFileContent(path, matchedJSON);
42
+ }
43
+
44
+ const createTraverse = (files) => ({push}) => ({
45
+ [__filesystem]: (path) => {
46
+ for (const [filename, plugin] of entries(files)) {
47
+ const [filePath] = findFile(path, filename);
48
+ const fileContent = readFileContent(filePath) || '{}';
49
+
50
+ const matchedJS = toJS(fileContent);
51
+ const matchedAST = parse(matchedJS);
52
+
53
+ const plugins = [
54
+ [`match-file/${filename}`, plugin],
55
+ ];
56
+
57
+ const places = findPlaces(matchedAST, matchedJS, {
58
+ plugins,
59
+ });
60
+
61
+ if (!places.length)
62
+ continue;
63
+
64
+ const {message} = places[0];
65
+
66
+ push({
67
+ message,
68
+ plugins,
69
+ path: filePath,
70
+ matchedAST,
71
+ matchedJS,
72
+ });
73
+ }
74
+ },
75
+ });
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@putout/operator-match-files",
3
+ "version": "1.0.0",
4
+ "type": "commonjs",
5
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
+ "description": "🐊Putout operator adds ability to match files to plugins",
7
+ "homepage": "https://github.com/coderaiser/putout/tree/master/packages/operator-match-files#readme",
8
+ "main": "lib/match-files.js",
9
+ "release": false,
10
+ "tag": false,
11
+ "changelog": false,
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git://github.com/coderaiser/putout.git"
15
+ },
16
+ "scripts": {
17
+ "test": "madrun test",
18
+ "watch:test": "madrun watch:test",
19
+ "lint": "madrun lint",
20
+ "fresh:lint": "madrun fresh:lint",
21
+ "lint:fresh": "madrun lint:fresh",
22
+ "fix:lint": "madrun fix:lint",
23
+ "coverage": "madrun coverage",
24
+ "report": "madrun report"
25
+ },
26
+ "dependencies": {
27
+ "@putout/babel": "^1.5.5",
28
+ "@putout/engine-parser": "^9.0.0",
29
+ "@putout/operator-filesystem": "^2.1.1",
30
+ "@putout/operator-json": "^1.3.0",
31
+ "@putout/printer": "^6.4.1",
32
+ "putout": "^33.6.2"
33
+ },
34
+ "keywords": [
35
+ "putout",
36
+ "putout-operator",
37
+ "operator",
38
+ "match-files"
39
+ ],
40
+ "devDependencies": {
41
+ "@putout/test": "^7.0.0",
42
+ "c8": "^8.0.0",
43
+ "eslint": "^8.0.1",
44
+ "eslint-plugin-n": "^16.0.0",
45
+ "eslint-plugin-putout": "^21.0.0",
46
+ "lerna": "^6.0.1",
47
+ "madrun": "^9.0.0",
48
+ "montag": "^1.2.1",
49
+ "nodemon": "^3.0.1",
50
+ "supertape": "^8.0.0",
51
+ "try-catch": "^3.0.0"
52
+ },
53
+ "license": "MIT",
54
+ "engines": {
55
+ "node": ">=16"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ }
60
+ }