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

Add CLI tool #523

Merged
merged 8 commits into from
Feb 25, 2024
Merged

Add CLI tool #523

merged 8 commits into from
Feb 25, 2024

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Feb 24, 2024

Closes #479, CC @danielbayley & @antonmedv

Here's the npx yaml -h output, as currently implemented. I'm still contemplating a few additions like a node style normaliser, and the docs will need an update.

yaml: A command-line YAML processor and inspector

Reads stdin and writes output to stdout and errors & warnings to stderr.

Usage:
  yaml          Process a YAML stream, outputting it as YAML
  yaml cst      Parse the CST of a YAML stream
  yaml lex      Parse the lexical tokens of a YAML stream
  yaml valid    Validate a YAML stream, returning 0 on success

Options:
  --help, -h    Show this message.
  --json, -j    Output JSON.

Additional options for bare "yaml" command:
  --doc, -d     Output pretty-printed JS Document objects.
  --single, -1  Require the input to consist of a single YAML document.
  --strict, -s  Stop on errors.
  --visit, -v   Apply a visitor to each document (requires a path to import)
  --yaml 1.1    Set the YAML version. (default: 1.2)

@eemeli
Copy link
Owner Author

eemeli commented Feb 24, 2024

Added a --visit option. With that, you can define a visitor like:

// strip-styles.mjs
export default {
  Collection(_, node) {
    delete node.flow
  },
  Scalar(_, node) {
    delete node.format
    delete node.type
  }
}

and then apply it:

echo '{"hello":"world"}' | npx yaml --visit ./strip-styles.mjs

for this output:

hello: world

@eemeli eemeli marked this pull request as ready for review February 25, 2024 15:25
@eemeli eemeli merged commit e6a5e28 into main Feb 25, 2024
16 checks passed
@eemeli eemeli deleted the cli branch February 25, 2024 15:31
@danielbayley
Copy link
Contributor

danielbayley commented Jun 30, 2024

@eemeli Nice! I had actually already worked up a PR for a CLI, but it got sidelined… This is working well, though missing the option to output pretty-printed JSON… Hence my PR in #559.

Also to reiterate what I mentioned in #479 (comment):

it should include aliases to seamlessly replace yaml2json and json2yaml that come with yamljs here, making replacing that library with this (better one) easier…

which I think would help adoption…

@eemeli
Copy link
Owner Author

eemeli commented Jul 1, 2024

Also to reiterate what I mentioned in #479 (comment):

it should include aliases to seamlessly replace yaml2json and json2yaml that come with yamljs here, making replacing that library with this (better one) easier…

which I think would help adoption…

Please submit this as a separate issue.

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.

Should a basic CLI be included?
2 participants