@putout/cli-process-file 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 +21 -0
- package/README.md +42 -0
- package/lib/printer/printer.mjs +61 -0
- package/lib/process-file.js +102 -0
- package/lib/simple-import.js +6 -0
- package/package.json +63 -0
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,42 @@
|
|
|
1
|
+
# @putout/cli-process-file [![NPM version][NPMIMGURL]][NPMURL]
|
|
2
|
+
|
|
3
|
+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/cli-process-file.svg?style=flat&longCache=true
|
|
4
|
+
[NPMURL]: https://npmjs.org/package/@putout/cli-process-file "npm"
|
|
5
|
+
|
|
6
|
+
Process file using 🐊**Putout**, **Samadhi** and **ESLint**.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm i @putout/cli-process-file
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import initProcessFile from '@putout/cli-process-file';
|
|
18
|
+
|
|
19
|
+
const processFile = initProcessFile({
|
|
20
|
+
fix: true,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const {code, places} = await processFile({
|
|
24
|
+
source: `
|
|
25
|
+
const a = b.a
|
|
26
|
+
`,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// returns
|
|
30
|
+
['const {a} = b;', [{
|
|
31
|
+
rule: 'no-undef (eslint)',
|
|
32
|
+
message: '\'b\' is not defined.',
|
|
33
|
+
position: {
|
|
34
|
+
line: 2,
|
|
35
|
+
column: 13,
|
|
36
|
+
},
|
|
37
|
+
}]];
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import deepMerge from 'deepmerge';
|
|
2
|
+
|
|
3
|
+
const {isArray} = Array;
|
|
4
|
+
const maybeArray = (a) => isArray(a) ? a : [a];
|
|
5
|
+
|
|
6
|
+
const DefaultMarkdown = {
|
|
7
|
+
format: {
|
|
8
|
+
endOfFile: '',
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const JSON = {
|
|
13
|
+
format: {
|
|
14
|
+
quote: '"',
|
|
15
|
+
},
|
|
16
|
+
semantics: {
|
|
17
|
+
trailingComma: false,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const configurePrinter = (name, printerOptions) => {
|
|
22
|
+
const [printer = 'putout', options = {}] = maybeArray(printerOptions);
|
|
23
|
+
const ext = name
|
|
24
|
+
.split('.')
|
|
25
|
+
.pop();
|
|
26
|
+
|
|
27
|
+
if (printer !== 'putout')
|
|
28
|
+
return printerOptions;
|
|
29
|
+
|
|
30
|
+
const mergedOptions = deepMerge(parseOptions(ext), options);
|
|
31
|
+
|
|
32
|
+
return [printer, mergedOptions];
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function parseOptions(ext) {
|
|
36
|
+
if (ext === 'json')
|
|
37
|
+
return JSON;
|
|
38
|
+
|
|
39
|
+
if (ext === 'yml{json}')
|
|
40
|
+
return JSON;
|
|
41
|
+
|
|
42
|
+
if (ext === 'md{json}')
|
|
43
|
+
return {
|
|
44
|
+
format: {
|
|
45
|
+
...DefaultMarkdown.format,
|
|
46
|
+
...JSON.format,
|
|
47
|
+
},
|
|
48
|
+
semantics: {
|
|
49
|
+
...DefaultMarkdown.semantics,
|
|
50
|
+
...JSON.semantics,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
if (ext === 'md{js}')
|
|
55
|
+
return DefaultMarkdown;
|
|
56
|
+
|
|
57
|
+
if (ext === 'md{ts}')
|
|
58
|
+
return DefaultMarkdown;
|
|
59
|
+
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const tryToCatch = require('try-to-catch');
|
|
4
|
+
const eslint = require('@putout/eslint');
|
|
5
|
+
const {parseMatch} = require('putout/parse-match');
|
|
6
|
+
const {mergeOptions} = require('putout/merge-options');
|
|
7
|
+
const parseError = require('putout/parse-error');
|
|
8
|
+
const {putoutAsync} = require('putout');
|
|
9
|
+
|
|
10
|
+
const {simpleImport} = require('./simple-import');
|
|
11
|
+
|
|
12
|
+
const getMatchedOptions = (name, options) => {
|
|
13
|
+
if (!name.includes('{'))
|
|
14
|
+
return options;
|
|
15
|
+
|
|
16
|
+
return mergeOptions(options, parseMatch(name, options.match));
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
module.exports = ({fix, fixCount, isFlow, logError, raw}) => async function processFile(overrides) {
|
|
20
|
+
const {
|
|
21
|
+
name = '<input>',
|
|
22
|
+
source,
|
|
23
|
+
startLine,
|
|
24
|
+
options = {},
|
|
25
|
+
again,
|
|
26
|
+
} = overrides;
|
|
27
|
+
|
|
28
|
+
const {configurePrinter} = await import('./printer/printer.mjs');
|
|
29
|
+
const isTS = /\.tsx?$/.test(name) || /{tsx?}$/.test(name);
|
|
30
|
+
const {printer, ...matchedOptions} = getMatchedOptions(name, options);
|
|
31
|
+
|
|
32
|
+
const [e, result] = await tryToCatch(putoutAsync, source, {
|
|
33
|
+
fix,
|
|
34
|
+
fixCount,
|
|
35
|
+
isTS,
|
|
36
|
+
isFlow,
|
|
37
|
+
...matchedOptions,
|
|
38
|
+
printer: configurePrinter(name, printer),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!again && e) {
|
|
42
|
+
raw && logError(e);
|
|
43
|
+
|
|
44
|
+
const {lint} = await simpleImport('samadhi');
|
|
45
|
+
const [code, places] = await lint(source, {
|
|
46
|
+
startLine,
|
|
47
|
+
fix,
|
|
48
|
+
isTS,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (!fix && places.length)
|
|
52
|
+
return {
|
|
53
|
+
code,
|
|
54
|
+
places,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
if (fix && !places.length)
|
|
58
|
+
return await processFile({
|
|
59
|
+
again: true,
|
|
60
|
+
name,
|
|
61
|
+
source: code,
|
|
62
|
+
startLine,
|
|
63
|
+
options,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const {code = source} = result || {};
|
|
68
|
+
const allPlaces = result ? result.places : parseError(e);
|
|
69
|
+
|
|
70
|
+
const [newCode, newPlaces] = await eslint({
|
|
71
|
+
name,
|
|
72
|
+
code,
|
|
73
|
+
fix,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
allPlaces.push(...newPlaces);
|
|
77
|
+
|
|
78
|
+
const places = formatPlaces(startLine, allPlaces);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
places,
|
|
82
|
+
code: newCode,
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function formatPlaces(line = 1, places) {
|
|
87
|
+
const newPlaces = [];
|
|
88
|
+
|
|
89
|
+
for (const place of places) {
|
|
90
|
+
const {position} = place;
|
|
91
|
+
|
|
92
|
+
newPlaces.push({
|
|
93
|
+
...place,
|
|
94
|
+
position: {
|
|
95
|
+
...position,
|
|
96
|
+
line: line + position.line,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return newPlaces;
|
|
102
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@putout/cli-process-file",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
5
|
+
"description": "Run all 🐊Putout linters",
|
|
6
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/cli-process-file#readme",
|
|
7
|
+
"main": "lib/process-file.js",
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"release": false,
|
|
10
|
+
"tag": false,
|
|
11
|
+
"changelog": false,
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://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/eslint": "^3.8.0",
|
|
28
|
+
"@putout/formatter-eslint": "^2.0.2",
|
|
29
|
+
"deepmerge": "^4.3.1",
|
|
30
|
+
"samadhi": "^2.14.1",
|
|
31
|
+
"try-catch": "^3.0.0",
|
|
32
|
+
"try-to-catch": "^3.0.0"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"putout",
|
|
36
|
+
"cli"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@putout/operate": "^12.17.0",
|
|
40
|
+
"c8": "^10.0.0",
|
|
41
|
+
"eslint": "^9.0.0",
|
|
42
|
+
"eslint-plugin-n": "^17.0.0",
|
|
43
|
+
"eslint-plugin-putout": "^24.0.0",
|
|
44
|
+
"lerna": "^6.0.1",
|
|
45
|
+
"madrun": "^10.0.0",
|
|
46
|
+
"mock-import": "^4.2.1",
|
|
47
|
+
"mock-require": "^3.0.3",
|
|
48
|
+
"montag": "^1.2.1",
|
|
49
|
+
"nodemon": "^3.0.1",
|
|
50
|
+
"putout": "*",
|
|
51
|
+
"supertape": "^10.0.0"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"putout": ">=38"
|
|
55
|
+
},
|
|
56
|
+
"license": "MIT",
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=18"
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
}
|
|
63
|
+
}
|