sezna / nps

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json
MIT License
1.43k stars 93 forks source link

Export `getAvailableScripts` #229

Open sitch opened 3 years ago

sitch commented 3 years ago

In order to export a markdown table version of the nps output, it's much easier to leverage this function vs copy/pasting it.

What:

Why:

How:

Checklist:

sitch commented 3 years ago

Maybe there is an easier/more idiomatic way to integrate this? i.e. formatters and move the current chalk version into one, as well as having a markdown one.

codecov[bot] commented 3 years ago

Codecov Report

Merging #229 (414f844) into master (57989a2) will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #229   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           12        12           
  Lines          399       476   +77     
  Branches        98       117   +19     
=========================================
+ Hits           399       476   +77     
Impacted Files Coverage Δ
src/bin-utils/index.js 100.00% <ø> (ø)
src/index.js 100.00% <0.00%> (ø)
src/get-logger.js 100.00% <0.00%> (ø)
src/bin-utils/parser.js 100.00% <0.00%> (ø)
src/get-script-to-run.js 100.00% <0.00%> (ø)
src/kebab-and-camel-casify.js 100.00% <0.00%> (ø)
src/get-scripts-from-config.js 100.00% <0.00%> (ø)
src/bin-utils/initialize/index.js 100.00% <0.00%> (ø)
src/bin-utils/get-script-by-prefix.js 100.00% <0.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 57989a2...414f844. Read the comment docs.

sitch commented 3 years ago

If it's useful code more or less looks like:

const { getAvailableScripts } = require('nps/dist/bin-utils');
const config = require('package-scripts');

const markdownReplacements = [
  [/\*/g, '\\*'],
  [/_/g, '\\_'],
  [/-/g, '\\-'],
  [/\+/g, '\\+'],
  [/=/g, '\\='],
  [/#/g, '\\#'],
  [/`/g, '\\`'],
  [/~/g, '\\~'],
  [/&/g, '&amp;'],
  [/\|/g, '\\|'],
  [/\(/g, '\\('],
  [/\)/g, '\\)'],
  [/\[/g, '\\['],
  [/\]/g, '\\]'],
  [/</g, '&lt;'],
  [/>/g, '&gt;'],
  [/(\d+)\./g, '$1\\.'],
];

const escapeMarkdown = (text = '') =>
  markdownReplacements.reduce(
    (search, replacement) => search.replace(replacement[0], replacement[1]),
    text
  );

function codeEscape(script = '') {
  script = script.replace(/\n/g, '<br/>');
  return `<code>${escapeMarkdown(script)}</code>`;
}

function markdownTable({ scripts, options }) {
  const availableScripts = getAvailableScripts(scripts).filter((script) => !script.hiddenFromHelp);

  const header = `| Name | Description | Script |`;
  const separator = `| -- | -- | -- |`;
  const rows = availableScripts.map(
    ({ name, description, script }) =>
      `| \`${escapeMarkdown(name)}\` | ${escapeMarkdown(description)} | ${codeEscape(script)} |`
  );

  return [header, separator, ...rows].join('\n');
}