Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export getAvailableScripts #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Export getAvailableScripts #229

wants to merge 1 commit into from

Conversation

sitch
Copy link

@sitch sitch commented May 21, 2021

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:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

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

sitch commented May 21, 2021

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
Copy link

codecov bot commented May 21, 2021

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
Copy link
Author

sitch commented May 21, 2021

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');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant