dorgan

dorgan

YarnParser - A parser for Yarn lockfiles

Someone in the Elixir’s Discord guild needed a parser for yarn lock files, and I wanted to try and learn parser combinators, so I wrote a yarn lock files parser

hex.pm
Documentation
GitHub repo

Example usage:

iex> input = 
  """
  # Some comment
  # yarn lockfile v1
  key1, key2:
    val1 true
    subkey1:
      val2 123
  """

iex> {:ok, parsed} = YarnParser.decode(input)
iex> parsed
{:ok,
  %{
    "comments" => ["# Some comment", "# yarn lockfile v1"],
    "key1" => %{
      "val1" => true,
      "subkey1" => %{
        "val2" => 123
      }
    },
    "key2" => %{
      "val1" => true,
      "subkey1" => %{
        "val2" => 123
      }
    }
  }
}

iex> YarnParser.get_version(parsed)
1

It currently doesn’t handle merge conflicts like the original parser and I need to improve error messages(mostly regarding invalid indentation).

First Post!

dorgan

dorgan

I published version 0.3.0, it renames YarnParser.parse to YarnParser.decode and adds encoding functionality:

%{
  "prop1" => "val1",
  "block1" => %{
    "prop2" => true
  },
  "prop3" => 123
} |> YarnParser.encode()
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1

block1:
  prop2 true

prop1 val1
prop3 123

Most Liked

kitplummer

kitplummer

I’m pretty sure I know who it was that was looking to parse a yarn.lock file. :smiley: I’ve been building some tooling to support analysis of dependencies in different language ecosystems as part of a research project. I asked a student to look into it - as both an exercise in understanding the “lock” model, as well as digging into parsers and behaviours in Elixir. We’ve been using the YarnParser, which works great.

axelson

axelson

Scenic Core Team

It could also be used to generate a bill of materials like the one used in SBoM - Mix task to generate a Software Bill-of-Materials (SBoM)

dorgan

dorgan

Version 0.4.0 was published

It turns out Yarn 2 now uses YAML instead of their own format, so now YarnParser uses YamlElixir to parse newer lockfiles. Version detection is automatic.

There was also a change in the API, the decode function returns a YarnLock struct that holds lockfile metadata and dependencies. It should be easier to get the lockfile’s version and other stuff now.

Where Next?

Popular in Announcing Top

bryanjos
Hi, I just published version 0.23.0 of Elixirscript. https://github.com/bryanjos/elixirscript/blob/master/CHANGELOG.md Most of the chan...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
tmbb
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement