@putout/operator-match-files 1.1.0 → 1.3.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/lib/match-files.js +35 -8
- package/package.json +2 -1
package/lib/match-files.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {parse} = require('@putout/
|
|
4
|
-
const {print} = require('@putout/printer');
|
|
3
|
+
const {parse, print} = require('@putout/engine-parser');
|
|
5
4
|
const {transform} = require('putout/transform');
|
|
6
5
|
const {findPlaces} = require('putout/find-places');
|
|
7
6
|
|
|
@@ -17,10 +16,12 @@ const {
|
|
|
17
16
|
writeFileContent,
|
|
18
17
|
} = require('@putout/operator-filesystem');
|
|
19
18
|
|
|
19
|
+
const isObject = (a) => a && typeof a === 'object';
|
|
20
20
|
const {entries} = Object;
|
|
21
21
|
const report = ({message}) => message;
|
|
22
22
|
|
|
23
23
|
module.exports.matchFiles = (files) => {
|
|
24
|
+
check(files);
|
|
24
25
|
const traverse = createTraverse(files);
|
|
25
26
|
|
|
26
27
|
return {
|
|
@@ -30,13 +31,12 @@ module.exports.matchFiles = (files) => {
|
|
|
30
31
|
};
|
|
31
32
|
};
|
|
32
33
|
|
|
33
|
-
function fix({path, matchedJS, matchedAST, plugins}) {
|
|
34
|
+
function fix({filename, path, matchedJS, matchedAST, plugins}) {
|
|
34
35
|
transform(matchedAST, matchedJS, {
|
|
35
36
|
plugins,
|
|
36
37
|
});
|
|
37
38
|
|
|
38
|
-
const
|
|
39
|
-
const matchedJSON = fromJS(updatedMatchedJS);
|
|
39
|
+
const matchedJSON = magicPrint(filename, matchedAST);
|
|
40
40
|
|
|
41
41
|
writeFileContent(path, matchedJSON);
|
|
42
42
|
}
|
|
@@ -50,9 +50,7 @@ const createTraverse = (files) => ({push}) => ({
|
|
|
50
50
|
return;
|
|
51
51
|
|
|
52
52
|
const fileContent = readFileContent(filePath) || '{}';
|
|
53
|
-
|
|
54
|
-
const matchedJS = toJS(fileContent);
|
|
55
|
-
const matchedAST = parse(matchedJS);
|
|
53
|
+
const [matchedJS, matchedAST] = magicParse(filename, fileContent);
|
|
56
54
|
|
|
57
55
|
const plugins = [
|
|
58
56
|
[`match-file/${filename}`, plugin],
|
|
@@ -68,6 +66,7 @@ const createTraverse = (files) => ({push}) => ({
|
|
|
68
66
|
const {message} = places[0];
|
|
69
67
|
|
|
70
68
|
push({
|
|
69
|
+
filename,
|
|
71
70
|
message,
|
|
72
71
|
plugins,
|
|
73
72
|
path: filePath,
|
|
@@ -77,3 +76,31 @@ const createTraverse = (files) => ({push}) => ({
|
|
|
77
76
|
}
|
|
78
77
|
},
|
|
79
78
|
});
|
|
79
|
+
|
|
80
|
+
function magicParse(name, content) {
|
|
81
|
+
if (/\.json$/.test(name)) {
|
|
82
|
+
const js = toJS(content);
|
|
83
|
+
const ast = parse(js);
|
|
84
|
+
|
|
85
|
+
return [js, ast];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [content, parse(content)];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function magicPrint(name, ast) {
|
|
92
|
+
if (/\.json$/.test(name)) {
|
|
93
|
+
const js = print(ast);
|
|
94
|
+
|
|
95
|
+
return fromJS(js);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return print(ast);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function check(files) {
|
|
102
|
+
for (const [, plugin] of entries(files)) {
|
|
103
|
+
if (!isObject(plugin))
|
|
104
|
+
throw Error(`☝️ Looks like provided to 'matchFiles()' typeof of plugin is not an 'object' but '${typeof plugin}'`);
|
|
105
|
+
}
|
|
106
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-match-files",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout operator adds ability to match files to plugins",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"match-files"
|
|
39
39
|
],
|
|
40
40
|
"devDependencies": {
|
|
41
|
+
"@putout/plugin-nodejs": "*",
|
|
41
42
|
"@putout/test": "^7.0.0",
|
|
42
43
|
"c8": "^8.0.0",
|
|
43
44
|
"eslint": "^8.0.1",
|