markdown-table-formatter 1.1.0 → 1.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.4.0] 2022-07-25
4
+
5
+ - Add --verbose mode
6
+ - Dependencies: run `npm audit fix` to secure dependencies
7
+ - CI: Upgrade [MegaLinter](https://oxsecurity.github.io/megalinter/latest/) to v6
8
+
9
+ ## [1.3.0] 2022-01-31
10
+
11
+ - Allow to override the table columns padding number of spaces using argument **--columnpadding**
12
+ - CI: Upgrade MegaLinter to v5 + fix CI tests on Windows & Mac
13
+
14
+ ## [1.2.0] 2021-08-20
15
+
16
+ - Allow to call markdown-table-formatter with a glob parameter to find files to format
17
+ - Allow to call markdown-table-formatter without arguments (**/*.md will be used by default to collect files)
18
+ - Fix logs format
19
+
3
20
  ## [1.1.0] 2021-04-05
4
21
 
5
22
  - Upgrade markdown-table-prettify to 3.4.0 so it can take in account inline disable comments
package/README.md CHANGED
@@ -33,19 +33,32 @@ npm install markdown-table-formatter -g
33
33
  ## Usage
34
34
 
35
35
  ```shell
36
- markdown-table-formatter [OPTIONS] (file(s))
36
+ markdown-table-formatter [OPTIONS] (file(s) or glob expression)
37
37
  ```
38
38
 
39
39
  ### Options
40
40
 
41
- | Parameter | Description | Default value |
42
- |------------------|-----------------------------------|---------------|
43
- | -c<br/>--check | Check only, does not update files | `false` |
41
+ | Parameter | Description | Default value |
42
+ |----------------|-----------------------------------|---------------|
43
+ | -c<br/>--check | Check only, does not update files | `false` |
44
+ | -p<br/>--columnpadding | Override column padding number of spaces |
44
45
  | -h<br/>--help | Displays help | <!-- --> |
45
46
  | -v<br/>--version | Displays version | <!-- --> |
46
47
 
47
48
  ## Examples
48
49
 
50
+ Format all markdown files found within current directory
51
+
52
+ ```shell
53
+ markdown-table-formatter
54
+ ```
55
+
56
+ Format all markdown files found with a glob expression
57
+
58
+ ```shell
59
+ markdown-table-formatter **/docs/*.md
60
+ ```
61
+
49
62
  Format markdown tables in a single file
50
63
 
51
64
  ```shell
package/lib/cli.js CHANGED
@@ -1,62 +1,77 @@
1
1
  #! /usr/bin/env node
2
2
  const { MarkdownTableFormatter } = require("./markdown-table-formatter");
3
3
  const optionsDefinition = require("./options");
4
+ const glob = require("glob");
4
5
 
5
6
  class MarkdownTableFormatterCli {
6
- "use strict";
7
+ "use strict";
7
8
 
8
- options = {};
9
+ options = {};
9
10
 
10
- async run(args) {
11
- this.options = optionsDefinition.parse(args);
11
+ async run(args) {
12
+ this.options = optionsDefinition.parse(args);
12
13
 
13
- // Show version
14
- if (this.options.version) {
15
- let version = process.env.npm_package_version;
16
- if (!version) {
17
- try {
18
- const FindPackageJson = require("find-package-json");
19
- const finder = FindPackageJson(__dirname);
20
- version = finder.next().value.version;
21
- } catch {
22
- version = "error";
23
- }
24
- }
25
- const message = `markdown-table-formatter version ${version}`;
26
- console.log(message);
27
- return { stdout: message };
14
+ // Show version
15
+ if (this.options.version) {
16
+ let version = process.env.npm_package_version;
17
+ if (!version) {
18
+ try {
19
+ const FindPackageJson = require("find-package-json");
20
+ const finder = FindPackageJson(__dirname);
21
+ version = finder.next().value.version;
22
+ } catch {
23
+ version = "error";
28
24
  }
25
+ }
26
+ const message = `markdown-table-formatter version ${version}`;
27
+ console.log(message);
28
+ return { stdout: message };
29
+ }
29
30
 
30
- // Show help ( index or for an options)
31
- if (this.options.help) {
32
- let helpContent = "";
33
- if (this.options._.length) {
34
- helpContent = optionsDefinition.generateHelpForOption(this.options._[0]);
35
- } else {
36
- helpContent = optionsDefinition.generateHelp();
37
- }
38
- console.info(helpContent);
39
- return { stdout: helpContent };
40
- }
31
+ // Show help ( index or for an options)
32
+ if (this.options.help) {
33
+ let helpContent = "";
34
+ if (this.options._.length) {
35
+ helpContent = optionsDefinition.generateHelpForOption(
36
+ this.options._[0]
37
+ );
38
+ } else {
39
+ helpContent = optionsDefinition.generateHelp();
40
+ }
41
+ console.info(helpContent);
42
+ return { stdout: helpContent };
43
+ }
41
44
 
42
- // Processing formatting
43
- const formatter = new MarkdownTableFormatter(this.options);
44
- const files = this.options._;
45
- if (files.length === 0) {
46
- throw new Error("You must send a file or a list of files as argument");
47
- }
48
- const { status, updates } = await formatter.run(files);
49
- // Manage result
50
- if (status === 0 && updates.length === 0) {
51
- console.log("No markdown table formatting has been applied.");
52
- } else if (status === 0 && updates > 0) {
53
- console.log(`Formatted markdown tables in ${updates.length} files: - \n${updates.join("\n- ")}`);
54
- } else if (status === 1) {
55
- console.log(`${updates.length} files contain markdown tables to format: - \n${updates.join("\n- ")}`);
56
- }
57
- process.exitCode = status;
58
- return { status, updates };
45
+ // Processing formatting
46
+ const formatter = new MarkdownTableFormatter(this.options);
47
+ let files = this.options._;
48
+ if (files.length === 0) {
49
+ // use default glob expression **/*.md
50
+ files = glob.sync("**/*.md", {});
51
+ } else if (files.length === 1 && files[0].includes("*")) {
52
+ // use sent glob expression
53
+ files = glob.sync(files[0], {});
54
+ }
55
+ const { status, updates } = await formatter.run(files, this.options);
56
+ // Manage result
57
+ if (status === 0 && updates.length === 0) {
58
+ console.log("No markdown table formatting has been applied.");
59
+ } else if (status === 0 && updates > 0) {
60
+ console.log(
61
+ `Formatted markdown tables in ${
62
+ updates.length
63
+ } files:\n- ${updates.join("\n- ")}`
64
+ );
65
+ } else if (status === 1) {
66
+ console.log(
67
+ `${
68
+ updates.length
69
+ } files contain markdown tables to format:\n- ${updates.join("\n- ")}`
70
+ );
59
71
  }
72
+ process.exitCode = status;
73
+ return { status, updates };
74
+ }
60
75
  }
61
76
 
62
77
  module.exports = { MarkdownTableFormatterCli };
package/lib/index.js CHANGED
@@ -9,9 +9,9 @@ const runningAsScript = !module.parent;
9
9
 
10
10
  // Run asynchronously to use the returned status for process.exit
11
11
  if (runningAsScript) {
12
- (async () => {
13
- await new MarkdownTableFormatterCli().run(process.argv);
14
- })();
12
+ (async () => {
13
+ await new MarkdownTableFormatterCli().run(process.argv);
14
+ })();
15
15
  }
16
16
 
17
17
  module.exports = { MarkdownTableFormatter, MarkdownTableFormatterCli };
@@ -4,61 +4,79 @@ const fse = require("fs-extra");
4
4
  const { CliPrettify } = require("markdown-table-prettify");
5
5
 
6
6
  class MarkdownTableFormatter {
7
- "use strict";
7
+ "use strict";
8
8
 
9
- /**
10
- * Creates a MarkdownTableFormatter instance
11
- * @param {object} [opts] - Run options
12
- * @param {string} [opts.check] - Define if we check only
13
- */
14
- constructor(opts) {
15
- this.checkOnly = opts.check;
16
- }
9
+ /**
10
+ * Creates a MarkdownTableFormatter instance
11
+ * @param {object} [opts] - Run options
12
+ * @param {string} [opts.check] - Define if we check only
13
+ */
14
+ constructor(opts) {
15
+ this.checkOnly = opts.check;
16
+ }
17
17
 
18
- /**
19
- * Runs markdown table formatter
20
- * @param {string[]} [files] - List of files to format
21
- * @return {Promise<any>} - Command result (status, stdout, stderr, childJavaProcess)
22
- */
23
- async run(files) {
24
- const fileResults = [];
25
- // Read files and call prettify on each of them
26
- for (const file of files) {
27
- if (debug.enabled) {
28
- debug(`[file] ${file}`);
29
- }
30
- const markdown = await fse.readFile(file);
31
- const markdownString = markdown.toString();
32
- if (debug.enabled) {
33
- debug(`[file before]\n${markdownString}`);
34
- }
35
- const markdownStringPrettified = CliPrettify.prettify(markdownString);
36
- const updated = markdownStringPrettified !== markdownString;
37
- // Update file if necessary
38
- if (updated && !this.checkOnly) {
39
- await fse.writeFile(file, markdownStringPrettified);
40
- debug(`[updated] ${file} with:\n${markdownStringPrettified}`);
41
- }
42
- const fileResult = {
43
- file,
44
- updated,
45
- before: updated ? markdownString : null,
46
- after: updated ? markdownStringPrettified : null
47
- };
48
- fileResults.push(fileResult);
49
- }
50
- const updates = fileResults.filter(fileResult => fileResult.updated).map(fileResult => fileResult.file);
51
- let status = 0;
52
- if (this.checkOnly && updates.length > 0) {
53
- status = 1;
18
+ /**
19
+ * Runs markdown table formatter
20
+ * @param {string[]} [files] - List of files to format
21
+ * @return {Promise<any>} - Command result (status, stdout, stderr, childJavaProcess)
22
+ */
23
+ async run(files, options = {}) {
24
+ if (options.verbose) {
25
+ console.info("Markdown files to analyze:");
26
+ for (const file of files) {
27
+ console.info(`- ${file}`);
28
+ }
29
+ }
30
+ const fileResults = [];
31
+ // Read files and call prettify on each of them
32
+ for (const file of files) {
33
+ if (debug.enabled) {
34
+ debug(`[file] ${file}`);
35
+ }
36
+ const markdown = await fse.readFile(file);
37
+ const markdownString = markdown.toString();
38
+ if (debug.enabled) {
39
+ debug(`[file before]\n${markdownString}`);
40
+ }
41
+ const prettifyOptions = {};
42
+ if (options.columnpadding) {
43
+ prettifyOptions.columnPadding = options.columnpadding;
44
+ }
45
+ const markdownStringPrettified = CliPrettify.prettify(
46
+ markdownString,
47
+ prettifyOptions
48
+ );
49
+ const updated = markdownStringPrettified !== markdownString;
50
+ // Update file if necessary
51
+ if (updated && !this.checkOnly) {
52
+ await fse.writeFile(file, markdownStringPrettified);
53
+ debug(`[updated] ${file} with:\n${markdownStringPrettified}`);
54
+ if (options.verbose) {
55
+ console.info(`- Updated file ${file}`);
54
56
  }
55
- const result = {
56
- status,
57
- fileResults,
58
- updates
59
- };
60
- return result;
57
+ }
58
+ const fileResult = {
59
+ file,
60
+ updated,
61
+ before: updated ? markdownString : null,
62
+ after: updated ? markdownStringPrettified : null,
63
+ };
64
+ fileResults.push(fileResult);
65
+ }
66
+ const updates = fileResults
67
+ .filter((fileResult) => fileResult.updated)
68
+ .map((fileResult) => fileResult.file);
69
+ let status = 0;
70
+ if (this.checkOnly && updates.length > 0) {
71
+ status = 1;
61
72
  }
73
+ const result = {
74
+ status,
75
+ fileResults,
76
+ updates,
77
+ };
78
+ return result;
79
+ }
62
80
  }
63
81
 
64
82
  module.exports = { MarkdownTableFormatter };
package/lib/options.js CHANGED
@@ -17,30 +17,43 @@ const optionator = require("optionator");
17
17
 
18
18
  // exports "parse(args)", "generateHelp()", and "generateHelpForOption(optionName)"
19
19
  module.exports = optionator({
20
- prepend: "markdown-table-formatter [options]",
21
- defaults: {
22
- concatRepeatedArrays: true,
23
- mergeRepeatedObjects: true
20
+ prepend: "markdown-table-formatter [options]",
21
+ defaults: {
22
+ concatRepeatedArrays: true,
23
+ mergeRepeatedObjects: true,
24
+ },
25
+ options: [
26
+ {
27
+ option: "check",
28
+ alias: "c",
29
+ type: "Boolean",
30
+ description:
31
+ "Check only (exits 1 if wrongly formatted markdown tables are found)",
24
32
  },
25
- options: [
26
- {
27
- option: "check",
28
- alias: "c",
29
- type: "Boolean",
30
- description: "Check only (exits 1 if wrongly formatted markdown tables are found)"
31
- },
32
- {
33
- option: "help",
34
- alias: "h",
35
- type: "Boolean",
36
- description: "Show help"
37
- },
38
- {
39
- option: "version",
40
- alias: "v",
41
- type: "Boolean",
42
- description: "Show version"
43
- }
44
- ],
45
- mutuallyExclusive: [["check", "help", "version"]]
33
+ {
34
+ option: "columnpadding",
35
+ alias: "p",
36
+ type: "Int",
37
+ description: "Override table columns padding number of spaces",
38
+ },
39
+ {
40
+ option: "help",
41
+ alias: "h",
42
+ type: "Boolean",
43
+ description: "Show help",
44
+ },
45
+ {
46
+ option: "version",
47
+ alias: "v",
48
+ type: "Boolean",
49
+ description: "Show version",
50
+ },
51
+ {
52
+ option: "verbose",
53
+ alias: "z",
54
+ type: "Boolean",
55
+ description: "Verbose mode",
56
+ },
57
+ ],
58
+ mutuallyExclusive: [["check", "help", "version"]],
46
59
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-table-formatter",
3
- "version": "1.1.0",
3
+ "version": "1.4.0",
4
4
  "description": "Format markdown tables in files",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -36,11 +36,12 @@
36
36
  "debug": "^4.1.1",
37
37
  "find-package-json": "^1.2.0",
38
38
  "fs-extra": "^9.0.1",
39
- "markdown-table-prettify": "^3.4.0",
39
+ "glob": "^7.1.6",
40
+ "markdown-table-prettify": "^3.6.0",
40
41
  "optionator": "^0.9.1"
41
42
  },
42
43
  "devDependencies": {
43
- "babel-eslint": "^10.1.0",
44
+ "@babel/eslint-parser": "^7.16.5",
44
45
  "eslint": "^7.6.0",
45
46
  "mega-linter-runner": "^4.23.1",
46
47
  "mocha": "^8.1.1",