@putout/operator-match-files 1.0.1 → 1.2.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.
Files changed (2) hide show
  1. package/lib/match-files.js +30 -8
  2. package/package.json +2 -1
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {parse} = require('@putout/babel');
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
 
@@ -30,13 +29,12 @@ module.exports.matchFiles = (files) => {
30
29
  };
31
30
  };
32
31
 
33
- function fix({path, matchedJS, matchedAST, plugins}) {
32
+ function fix({filename, path, matchedJS, matchedAST, plugins}) {
34
33
  transform(matchedAST, matchedJS, {
35
34
  plugins,
36
35
  });
37
36
 
38
- const updatedMatchedJS = print(matchedAST);
39
- const matchedJSON = fromJS(updatedMatchedJS);
37
+ const matchedJSON = magicPrint(filename, matchedAST);
40
38
 
41
39
  writeFileContent(path, matchedJSON);
42
40
  }
@@ -45,10 +43,12 @@ const createTraverse = (files) => ({push}) => ({
45
43
  [__filesystem]: (path) => {
46
44
  for (const [filename, plugin] of entries(files)) {
47
45
  const [filePath] = findFile(path, filename);
48
- const fileContent = readFileContent(filePath) || '{}';
49
46
 
50
- const matchedJS = toJS(fileContent);
51
- const matchedAST = parse(matchedJS);
47
+ if (!filePath)
48
+ return;
49
+
50
+ const fileContent = readFileContent(filePath) || '{}';
51
+ const [matchedJS, matchedAST] = magicParse(filename, fileContent);
52
52
 
53
53
  const plugins = [
54
54
  [`match-file/${filename}`, plugin],
@@ -64,6 +64,7 @@ const createTraverse = (files) => ({push}) => ({
64
64
  const {message} = places[0];
65
65
 
66
66
  push({
67
+ filename,
67
68
  message,
68
69
  plugins,
69
70
  path: filePath,
@@ -73,3 +74,24 @@ const createTraverse = (files) => ({push}) => ({
73
74
  }
74
75
  },
75
76
  });
77
+
78
+ function magicParse(name, content) {
79
+ if (/\.json$/.test(name)) {
80
+ const js = toJS(content);
81
+ const ast = parse(js);
82
+
83
+ return [js, ast];
84
+ }
85
+
86
+ return [content, parse(content)];
87
+ }
88
+
89
+ function magicPrint(name, ast) {
90
+ if (/\.json$/.test(name)) {
91
+ const js = print(ast);
92
+
93
+ return fromJS(js);
94
+ }
95
+
96
+ return print(ast);
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-match-files",
3
- "version": "1.0.1",
3
+ "version": "1.2.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",