prettify-bru 0.1.10 → 0.1.12
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/README.md +15 -3
- package/cli.js +13 -13
- package/lib/format.js +6 -6
- package/lib/main.js +18 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,20 +4,26 @@ A simple CLI tool to prettify the contents of `.bru` files.
|
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
+
Requires Node.js 20+
|
|
8
|
+
|
|
7
9
|
To install in your project, run:
|
|
8
10
|
|
|
9
11
|
```
|
|
10
12
|
npm i prettify-bru
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
Boom, now you
|
|
15
|
+
Boom, now you should be ready to go!
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Get a non-destructive report of all files that could potentially be re-formatted by running:
|
|
14
20
|
|
|
15
21
|
```
|
|
16
22
|
npx prettify-bru
|
|
17
23
|
```
|
|
18
24
|
|
|
19
|
-
The above command will walk subdirectories
|
|
20
|
-
With each file it will assess the formatting of the JSON/
|
|
25
|
+
The above command will walk all subdirectories finding `.bru` files.
|
|
26
|
+
With each file it will assess the formatting of the JSON/JavaScript inside the following types of block:
|
|
21
27
|
|
|
22
28
|
- `body:json` will be parsed with the JSON parser
|
|
23
29
|
- `script:pre-request` blocks parsed with Babel
|
|
@@ -31,3 +37,9 @@ npx prettify-bru --write
|
|
|
31
37
|
```
|
|
32
38
|
|
|
33
39
|
⚠️ This will modify the files in place, use with caution.
|
|
40
|
+
|
|
41
|
+
To just do a single subdirectory, provide the path as an argument, so search the folder names "speed-tests":
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
npx prettify-bru speed-tests
|
|
45
|
+
```
|
package/cli.js
CHANGED
|
@@ -5,18 +5,20 @@ import {hideBin} from 'yargs/helpers';
|
|
|
5
5
|
import {main} from './lib/main.js';
|
|
6
6
|
|
|
7
7
|
const argv = yargs(hideBin(process.argv))
|
|
8
|
-
.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
.command('$0 [path] [-w|--write]', `Formats all .bru files (including subdirectories)`, (yargs) => {
|
|
9
|
+
return yargs.positional('path', {
|
|
10
|
+
describe: 'The root path to search from',
|
|
11
|
+
type: 'string',
|
|
12
|
+
demandOption: false,
|
|
13
|
+
default: '',
|
|
14
|
+
defaultDescription: 'Current working directory'
|
|
15
|
+
})
|
|
16
|
+
})
|
|
17
|
+
.options({
|
|
14
18
|
w: {
|
|
19
|
+
alias: 'write',
|
|
15
20
|
type: 'boolean',
|
|
16
|
-
default: false
|
|
17
|
-
},
|
|
18
|
-
path: {
|
|
19
|
-
default: '.',
|
|
21
|
+
default: false,
|
|
20
22
|
},
|
|
21
23
|
})
|
|
22
24
|
.describe({
|
|
@@ -24,15 +26,13 @@ Running the command with no arguments will modify all files
|
|
|
24
26
|
h: 'Display the help message',
|
|
25
27
|
})
|
|
26
28
|
.boolean(['w', 'h'])
|
|
27
|
-
.help()
|
|
28
29
|
.alias('h', 'help')
|
|
29
|
-
.alias('w', 'write')
|
|
30
30
|
.parse();
|
|
31
31
|
|
|
32
32
|
if (argv.h) {
|
|
33
33
|
yargs.showHelp();
|
|
34
34
|
} else {
|
|
35
|
-
go(argv.
|
|
35
|
+
go(argv.path, argv.w);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
function go(path, write) {
|
package/lib/format.js
CHANGED
|
@@ -25,7 +25,7 @@ export async function formatBlocks(originalContents) {
|
|
|
25
25
|
|
|
26
26
|
let fileOutcome = {
|
|
27
27
|
newContents: originalContents.replace(/\r\n/g, "\n"),
|
|
28
|
-
|
|
28
|
+
changeable: false,
|
|
29
29
|
error_messages: []
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -34,8 +34,8 @@ export async function formatBlocks(originalContents) {
|
|
|
34
34
|
const blockOutcome = await formatBlock(fileOutcome.newContents, ...formattableBlocks[i])
|
|
35
35
|
if (blockOutcome.error_message !== null) {
|
|
36
36
|
fileOutcome.error_messages.push(blockOutcome.error_message)
|
|
37
|
-
} else if (blockOutcome.
|
|
38
|
-
fileOutcome.
|
|
37
|
+
} else if (blockOutcome.changeable) {
|
|
38
|
+
fileOutcome.changeable = true
|
|
39
39
|
fileOutcome.newContents = blockOutcome.fileContents;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -45,7 +45,7 @@ export async function formatBlocks(originalContents) {
|
|
|
45
45
|
|
|
46
46
|
async function formatBlock(fileContents, blockName, parser) {
|
|
47
47
|
|
|
48
|
-
let outcome = {fileContents,
|
|
48
|
+
let outcome = {fileContents, changeable: false, error_message: null};
|
|
49
49
|
|
|
50
50
|
const blockBodyRegex = new RegExp('\n' + blockName + ' [{]\\n(.+?)\\n}\\n', 's')
|
|
51
51
|
const match = fileContents.match(blockBodyRegex)
|
|
@@ -81,11 +81,11 @@ async function formatBlock(fileContents, blockName, parser) {
|
|
|
81
81
|
const formattedBody = indentedLines.join("\n");
|
|
82
82
|
|
|
83
83
|
if (formattedBody === rawBody) {
|
|
84
|
-
// Nothing has changed after formatting
|
|
84
|
+
// Nothing has changed after formatting, so this block is not changeable
|
|
85
85
|
return outcome
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
outcome.fileContents = fileContents.replace(rawBody, formattedBody)
|
|
89
|
-
outcome.
|
|
89
|
+
outcome.changeable = true
|
|
90
90
|
return outcome;
|
|
91
91
|
}
|
package/lib/main.js
CHANGED
|
@@ -15,10 +15,8 @@ import {formatBlocks} from './format.js'
|
|
|
15
15
|
*/
|
|
16
16
|
export async function main(cwd, path, write) {
|
|
17
17
|
|
|
18
|
-
if (path === '
|
|
18
|
+
if (path === '') {
|
|
19
19
|
path = cwd
|
|
20
|
-
} else if (!/^\//.test(path)) {
|
|
21
|
-
// Path is absolute, do not modify
|
|
22
20
|
} else {
|
|
23
21
|
// Append the relative path to the current working directory
|
|
24
22
|
path = cwd + '/' + path
|
|
@@ -37,24 +35,27 @@ export async function main(cwd, path, write) {
|
|
|
37
35
|
|
|
38
36
|
console.log(`Found ${files.length} .bru file(s)\n`);
|
|
39
37
|
|
|
40
|
-
let
|
|
38
|
+
let changeableFiles = [];
|
|
41
39
|
let erroredFiles = [];
|
|
42
40
|
|
|
43
41
|
for (const filePath of files) {
|
|
44
|
-
const outcome = await processFile(filePath);
|
|
42
|
+
const outcome = await processFile(filePath, write);
|
|
45
43
|
|
|
46
|
-
let displayFilePath = filePath.replace(new RegExp('^' + cwd), "");
|
|
44
|
+
let displayFilePath = filePath.replace(new RegExp('^' + cwd + '/'), "");
|
|
47
45
|
|
|
48
|
-
if (outcome.
|
|
49
|
-
|
|
46
|
+
if (outcome.changeable) {
|
|
47
|
+
changeableFiles.push({displayFilePath, outcome});
|
|
50
48
|
} else if (outcome.error_messages.length) {
|
|
51
49
|
erroredFiles.push({displayFilePath, outcome});
|
|
52
50
|
}
|
|
53
51
|
}
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
const changeablePrefix = write ? 'Reformatted blocks' : 'Found blocks that need reformatting';
|
|
54
|
+
|
|
55
|
+
if (changeableFiles.length) {
|
|
56
|
+
const emoji = write ? '✏️' : '🔍';
|
|
57
|
+
console.log(`\x1b[4m\x1b[32m${changeablePrefix} in ${changeableFiles.length} file(s):\x1b[0m\n`);
|
|
58
|
+
changeableFiles.forEach((r) => console.log(`${emoji} \x1b[32m${r.displayFilePath}\x1b[0m`));
|
|
58
59
|
console.log(" ")
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -68,8 +69,10 @@ export async function main(cwd, path, write) {
|
|
|
68
69
|
})
|
|
69
70
|
}
|
|
70
71
|
|
|
72
|
+
const requireNothing = files.length - changeableFiles.length - erroredFiles.length;
|
|
71
73
|
console.log(
|
|
72
|
-
`\x1b[35mProcessed ${files.length} .bru file(s)
|
|
74
|
+
`\x1b[35mProcessed ${files.length} .bru file(s):\x1b[0m ${changeablePrefix} in ${changeableFiles.length}. `
|
|
75
|
+
+ `Encountered errors in ${erroredFiles.length}. ${requireNothing} file(s) did not require any changes.`
|
|
73
76
|
);
|
|
74
77
|
}
|
|
75
78
|
|
|
@@ -93,16 +96,14 @@ function walkDir(dir, onFile) {
|
|
|
93
96
|
}
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
async function processFile(filePath) {
|
|
99
|
+
async function processFile(filePath, write) {
|
|
97
100
|
const original = fs.readFileSync(filePath, "utf8");
|
|
98
101
|
|
|
99
102
|
const fileOutcome = await formatBlocks(original);
|
|
100
103
|
|
|
101
|
-
if (
|
|
102
|
-
|
|
104
|
+
if (write && fileOutcome.changeable) {
|
|
105
|
+
fs.writeFileSync(filePath, fileOutcome.newContents, "utf8");
|
|
103
106
|
}
|
|
104
107
|
|
|
105
|
-
fs.writeFileSync(filePath, fileOutcome.newContents, "utf8");
|
|
106
|
-
|
|
107
108
|
return fileOutcome;
|
|
108
109
|
}
|