prettify-bru 0.1.10 → 0.1.11

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 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 can get a non-destructive report of all the incorrect formatting by running:
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, finding all `.bru` files.
20
- With each file it will assess the formatting of the JSON/JavsScript inside the following types of block:
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
- .usage(`
9
- Usage: $0 [--write|-w] path
10
-
11
- Running the command with no arguments will modify all files
12
-
13
- `).options({
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._[0], argv.w);
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
- changed: false,
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.changed) {
38
- fileOutcome.changed = true
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, changed: false, error_message: null};
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.changed = true
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 changedFiles = [];
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
44
  let displayFilePath = filePath.replace(new RegExp('^' + cwd), "");
47
45
 
48
46
  if (outcome.changed) {
49
- changedFiles.push({displayFilePath, outcome});
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
- if (changedFiles.length) {
56
- console.log(`\x1b[4m\x1b[32mReformatted blocks in ${changedFiles.length} file(s):\x1b[0m\n`);
57
- changedFiles.forEach((r) => console.log(`✏️ \x1b[32m${r.displayFilePath}\x1b[0m`));
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
 
@@ -69,7 +70,7 @@ export async function main(cwd, path, write) {
69
70
  }
70
71
 
71
72
  console.log(
72
- `\x1b[35mProcessed ${files.length} .bru file(s).\x1b[0m Reformatted blocks in ${changedFiles.length}. Encountered errors in ${erroredFiles.length}.`
73
+ `\x1b[35mProcessed ${files.length} .bru file(s).\x1b[0m ${changeablePrefix} in ${changeableFiles.length}. Encountered errors in ${erroredFiles.length}.`
73
74
  );
74
75
  }
75
76
 
@@ -93,16 +94,14 @@ function walkDir(dir, onFile) {
93
94
  }
94
95
  }
95
96
 
96
- async function processFile(filePath) {
97
+ async function processFile(filePath, write) {
97
98
  const original = fs.readFileSync(filePath, "utf8");
98
99
 
99
100
  const fileOutcome = await formatBlocks(original);
100
101
 
101
- if (!fileOutcome.changed) {
102
- return fileOutcome
102
+ if (write && fileOutcome.changed) {
103
+ fs.writeFileSync(filePath, fileOutcome.newContents, "utf8");
103
104
  }
104
105
 
105
- fs.writeFileSync(filePath, fileOutcome.newContents, "utf8");
106
-
107
106
  return fileOutcome;
108
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettify-bru",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Prettifies JSON and JavaScript blocks in Bruno .bru files",
5
5
  "keywords": [
6
6
  "bruno",