markdown-table-formatter 1.2.0 → 1.3.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,10 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.3.0] 2022-01-31
4
+
5
+ - Allow to override the table columns padding number of spaces using argument **--columnpadding**
6
+ - CI: Upgrade MegaLinter to v5 + fix CI tests on Windows & Mac
7
+
3
8
  ## [1.2.0] 2021-08-20
4
9
 
5
10
  - Allow to call markdown-table-formatter with a glob parameter to find files to format
package/README.md CHANGED
@@ -38,9 +38,10 @@ markdown-table-formatter [OPTIONS] (file(s) or glob expression)
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
 
package/lib/cli.js CHANGED
@@ -4,65 +4,74 @@ const optionsDefinition = require("./options");
4
4
  const glob = require("glob");
5
5
 
6
6
  class MarkdownTableFormatterCli {
7
- "use strict";
7
+ "use strict";
8
8
 
9
- options = {};
9
+ options = {};
10
10
 
11
- async run(args) {
12
- this.options = optionsDefinition.parse(args);
11
+ async run(args) {
12
+ this.options = optionsDefinition.parse(args);
13
13
 
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";
24
- }
25
- }
26
- const message = `markdown-table-formatter version ${version}`;
27
- console.log(message);
28
- 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";
29
24
  }
25
+ }
26
+ const message = `markdown-table-formatter version ${version}`;
27
+ console.log(message);
28
+ return { stdout: message };
29
+ }
30
30
 
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(this.options._[0]);
36
- } else {
37
- helpContent = optionsDefinition.generateHelp();
38
- }
39
- console.info(helpContent);
40
- return { stdout: helpContent };
41
- }
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
+ }
42
44
 
43
- // Processing formatting
44
- const formatter = new MarkdownTableFormatter(this.options);
45
- let files = this.options._;
46
- if (files.length === 0) {
47
- // use default glob expression **/*.md
48
- files = glob.sync("**/*.md", {});
49
- }
50
- else if(files.length === 1 && files[0].includes('*')) {
51
- // use sent glob expression
52
- files = glob.sync(files[0], {});
53
- }
54
- const { status, updates } = await formatter.run(files);
55
- // Manage result
56
- if (status === 0 && updates.length === 0) {
57
- console.log("No markdown table formatting has been applied.");
58
- } else if (status === 0 && updates > 0) {
59
- console.log(`Formatted markdown tables in ${updates.length} files:\n- ${updates.join("\n- ")}`);
60
- } else if (status === 1) {
61
- console.log(`${updates.length} files contain markdown tables to format:\n- ${updates.join("\n- ")}`);
62
- }
63
- process.exitCode = status;
64
- 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
+ );
65
71
  }
72
+ process.exitCode = status;
73
+ return { status, updates };
74
+ }
66
75
  }
67
76
 
68
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,70 @@ 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;
54
- }
55
- const result = {
56
- status,
57
- fileResults,
58
- updates
59
- };
60
- return result;
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
+ 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 prettifyOptions = {};
36
+ if (options.columnpadding) {
37
+ prettifyOptions.columnPadding = options.columnpadding;
38
+ }
39
+ const markdownStringPrettified = CliPrettify.prettify(
40
+ markdownString,
41
+ prettifyOptions
42
+ );
43
+ const updated = markdownStringPrettified !== markdownString;
44
+ // Update file if necessary
45
+ if (updated && !this.checkOnly) {
46
+ await fse.writeFile(file, markdownStringPrettified);
47
+ debug(`[updated] ${file} with:\n${markdownStringPrettified}`);
48
+ }
49
+ const fileResult = {
50
+ file,
51
+ updated,
52
+ before: updated ? markdownString : null,
53
+ after: updated ? markdownStringPrettified : null,
54
+ };
55
+ fileResults.push(fileResult);
56
+ }
57
+ const updates = fileResults
58
+ .filter((fileResult) => fileResult.updated)
59
+ .map((fileResult) => fileResult.file);
60
+ let status = 0;
61
+ if (this.checkOnly && updates.length > 0) {
62
+ status = 1;
61
63
  }
64
+ const result = {
65
+ status,
66
+ fileResults,
67
+ updates,
68
+ };
69
+ return result;
70
+ }
62
71
  }
63
72
 
64
73
  module.exports = { MarkdownTableFormatter };
package/lib/options.js CHANGED
@@ -17,30 +17,37 @@ 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
+ mutuallyExclusive: [["check", "help", "version"]],
46
53
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-table-formatter",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Format markdown tables in files",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -37,11 +37,11 @@
37
37
  "find-package-json": "^1.2.0",
38
38
  "fs-extra": "^9.0.1",
39
39
  "glob": "^7.1.6",
40
- "markdown-table-prettify": "^3.4.0",
40
+ "markdown-table-prettify": "^3.6.0",
41
41
  "optionator": "^0.9.1"
42
42
  },
43
43
  "devDependencies": {
44
- "babel-eslint": "^10.1.0",
44
+ "@babel/eslint-parser": "^7.16.5",
45
45
  "eslint": "^7.6.0",
46
46
  "mega-linter-runner": "^4.23.1",
47
47
  "mocha": "^8.1.1",