yarn_lock_parser 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26d73d4701952d2f74f79cd274d75ac70c6f738d7a66ad151a2ceab3f9b122f2
4
+ data.tar.gz: 60866fe908018e7877b05498b499a0df99491b9ea2d9ca35d0c28f9d9dca19a2
5
+ SHA512:
6
+ metadata.gz: f54fd15a0185ca00b61d558b29c526e70560fd31533f11d0214fe93837654ea8135bbc326f87b80ffd810715a33186225478e938facd8944a48aab1fb925f2e3
7
+ data.tar.gz: dbe5c11eeb901e1deca8034e51d57bbc5d6eb3dd3894ae8aba732a91ebaba4d8f0fa617215ab6923909594a696345b92bef4f0ce26232c8ad5ae7ce94c9e24b1
@@ -0,0 +1,30 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ version: 2.6.x
20
+
21
+ - name: Publish to RubyGems
22
+ run: |
23
+ mkdir -p $HOME/.gem
24
+ touch $HOME/.gem/credentials
25
+ chmod 0600 $HOME/.gem/credentials
26
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
27
+ gem build *.gemspec
28
+ gem push *.gem
29
+ env:
30
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.6
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rake
@@ -0,0 +1,67 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+ /.bundle/
58
+ /.yardoc
59
+ /_yardoc/
60
+ /coverage/
61
+ /doc/
62
+ /pkg/
63
+ /spec/reports/
64
+ /tmp/
65
+
66
+ # rspec failure tracking
67
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,32 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'pkg/**/*'
4
+ - 'spec/fixtures/**/*'
5
+ - 'vendor/bundle/**/*'
6
+
7
+ Layout/LineLength:
8
+ Enabled: false
9
+
10
+ Metrics/CyclomaticComplexity:
11
+ Enabled: false
12
+
13
+ Style/CommentedKeyword:
14
+ Enabled: false
15
+
16
+ Metrics/ModuleLength:
17
+ Enabled: false
18
+
19
+ Metrics/MethodLength:
20
+ Enabled: false
21
+
22
+ Style/Documentation:
23
+ Enabled: false
24
+
25
+ Metrics/AbcSize:
26
+ Enabled: false
27
+
28
+ Metrics/PerceivedComplexity:
29
+ Enabled: false
30
+
31
+ Metrics/BlockLength:
32
+ Enabled: false
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.6
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at eldemcan@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in yarn_lock_parser.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yarn_lock_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (11.1.3)
10
+ diff-lcs (1.3)
11
+ rake (12.3.3)
12
+ rspec (3.9.0)
13
+ rspec-core (~> 3.9.0)
14
+ rspec-expectations (~> 3.9.0)
15
+ rspec-mocks (~> 3.9.0)
16
+ rspec-core (3.9.2)
17
+ rspec-support (~> 3.9.3)
18
+ rspec-expectations (3.9.2)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.9.0)
21
+ rspec-mocks (3.9.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-support (3.9.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ byebug (~> 11.1)
31
+ rake (~> 12.0)
32
+ rspec (~> 3.0)
33
+ yarn_lock_parser!
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Can Eldem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Can Eldem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # YarnLockParser
2
+
3
+ This gem is used for parsing yarn.lock files. It is ported from [yarn repository](https://github.com/yarnpkg/yarn/blob/master/src/lockfile/parse.js)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'yarn_lock_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yarn_lock_parser
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ res = YarnLockParser::Parser.parse('spec/fixtures/long_yarn.lock')
25
+
26
+ [{:name=>"accepts", :version=>"1.3.7"}, {:name=>"array-flatten", :version=>"1.1.1"},
27
+ {:name=>"body-parser", :version=>"1.19.0"}, {:name=>"bytes", :version=>"3.1.0"},
28
+ {:name=>"content-disposition", :version=>"0.5.3"}, {:name=>"content-type", :version=>"1.0.4"},
29
+ {:name=>"cookie-signature", :version=>"1.0.6"}, {:name=>"cookie", :version=>"0.4.0"}, {:name=>"debug", :version=>"2.6.9"}, {:name=>"depd", :version=>"1.1.2"}, {:name=>"destroy", :version=>"1.0.4"}, {:name=>"ee-first", :version=>"1.1.1"}, {:name=>"encodeurl", :version=>"1.0.2"}, {:name=>"escape-html", :version=>"1.0.3"}, {:name=>"etag", :version=>"1.8.1"}, {:name=>"express", :version=>"4.17.1"}, {:name=>"finalhandler", :version=>"1.1.2"}, {:name=>"forwarded", :version=>"0.1.2"}, {:name=>"fresh", :version=>"0.5.2"}, {:name=>"http-errors", :version=>"1.7.2"}, {:name=>"http-errors", :version=>"1.7.3"}, {:name=>"iconv-lite", :version=>"0.4.24"}, {:name=>"inherits", :version=>"2.0.3"}, {:name=>"inherits", :version=>"2.0.4"}, {:name=>"ipaddr.js", :version=>"1.9.1"}, {:name=>"jquery", :version=>"3.4.0"}, {:name=>"media-typer", :version=>"0.3.0"}, {:name=>"merge-descriptors", :version=>"1.0.1"}, {:name=>"methods", :version=>"1.1.2"}, {:name=>"mime-db", :version=>"1.43.0"}, {:name=>"mime-types", :version=>"2.1.26"}, {:name=>"mime", :version=>"1.6.0"}, {:name=>"ms", :version=>"2.0.0"}, {:name=>"ms", :version=>"2.1.1"}, {:name=>"negotiator", :version=>"0.6.2"}, {:name=>"on-finished", :version=>"2.3.0"}, {:name=>"parseurl", :version=>"1.3.3"}, {:name=>"path-to-regexp", :version=>"0.1.7"}, {:name=>"proxy-addr", :version=>"2.0.6"}, {:name=>"qs", :version=>"6.7.0"}, {:name=>"range-parser", :version=>"1.2.1"}, {:name=>"raw-body", :version=>"2.4.0"}, {:name=>"safe-buffer", :version=>"5.1.2"}, {:name=>"safer-buffer", :version=>"2.1.2"}, {:name=>"send", :version=>"0.17.1"}, {:name=>"serve-static", :version=>"1.14.1"}, {:name=>"setprototypeof", :version=>"1.1.1"}, {:name=>"statuses", :version=>"1.5.0"}, {:name=>"toidentifier", :version=>"1.0.0"},...]
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eldemcan/yarn_lock_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/eldemcan/yarn_lock_parser/blob/master/CODE_OF_CONDUCT.md).
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
46
+
47
+ ## Code of Conduct
48
+
49
+ Everyone interacting in the YarnLockParser project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/yarn_lock_parser/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'yarn_lock_parser'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,234 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yarn_lock_parser/version"
4
+
5
+ module YarnLockParser
6
+ class Parser
7
+ LOCKFILE_VERSION = 1
8
+
9
+ TOKEN_TYPES = {
10
+ boolean: "BOOLEAN",
11
+ string: "STRING",
12
+ identifier: "IDENTIFIER",
13
+ eof: "EOF",
14
+ colon: "COLON",
15
+ newline: "NEWLINE",
16
+ comment: "COMMENT",
17
+ indent: "INDENT",
18
+ invalid: "INVALID",
19
+ number: "NUMBER",
20
+ comma: "COMMA",
21
+ }.freeze
22
+
23
+ Token = Struct.new(:type, :value)
24
+
25
+ class << self
26
+ attr_accessor :tokens, :token
27
+
28
+ def parse(file_path)
29
+ yarn_file = File.read(file_path)
30
+
31
+ return nil unless compatible?(yarn_file)
32
+
33
+ tokenise(yarn_file)
34
+ res = parse_tokenized_items
35
+ convert_objects(res)
36
+ end
37
+
38
+ private
39
+
40
+ def tokenise(input)
41
+ @tokens = []
42
+ last_new_line = false
43
+
44
+ build_token = proc { |type, value| Token.new(type, value) }
45
+ until input.empty?
46
+ chop = 0
47
+ if input[0] == "\n" || input[0] == "\r"
48
+ chop += 1
49
+ chop += 1 if input[1] == "\n"
50
+
51
+ tokens << build_token.call(TOKEN_TYPES[:newline])
52
+ elsif input[0] == "#"
53
+ chop += 1
54
+ next_new_line = input.index("\n", chop)
55
+ next_new_line = input.length if next_new_line == -1
56
+ val = input[chop..next_new_line]
57
+ chop = next_new_line
58
+ tokens << build_token.call(TOKEN_TYPES[:comment], val)
59
+ elsif input[0] == " "
60
+ if last_new_line
61
+ indent_size = count_indent_size(input)
62
+ tokens << build_token.call(TOKEN_TYPES[:indent], indent_size / 2)
63
+ else
64
+ chop += 1
65
+ end
66
+ elsif input[0] == '"'
67
+ chop, val = extract_value(input)
68
+ tokens << build_token.call(TOKEN_TYPES[:string], val.gsub(/\"|:/, ""))
69
+ elsif /^[0-9]/.match?(input)
70
+ val = /^[0-9]+/.match(input).to_s
71
+ chop = val.length
72
+ tokens << build_token.call(TOKEN_TYPES[:number], val.to_i)
73
+ elsif /^true/.match?(input)
74
+ tokens << build_token.call(TOKEN_TYPES[:boolean], true)
75
+ chop = 4
76
+ elsif /^false/.match?(input)
77
+ tokens << build_token.call(TOKEN_TYPES[:boolean], false)
78
+ chop = 5
79
+ elsif input[0] == ":"
80
+ chop += 1
81
+ tokens << build_token.call(TOKEN_TYPES[:colon])
82
+ elsif input[0] == ","
83
+ chop += 1
84
+ tokens << build_token.call(TOKEN_TYPES[:comma])
85
+ elsif input.scan(%r{^[a-zA-Z/.-]}).size.positive?
86
+ chop = count_special_characters(input)
87
+ name = input[0..chop - 1]
88
+ tokens << build_token.call(TOKEN_TYPES[:string], name)
89
+ else
90
+ tokens << build_token.call(TOKEN_TYPES[:invalid])
91
+ next
92
+ end
93
+
94
+ if chop.nil?
95
+ tokens << build_token.call(TOKEN_TYPES[:invalid])
96
+ next
97
+ end
98
+ last_new_line = input[0] == "\n" || (input[0] == "\r" && input[1] == "\n")
99
+ input = input[chop..-1]
100
+ end
101
+ tokens << build_token.call(TOKEN_TYPES[:eof])
102
+
103
+ self.tokens = tokens.to_enum
104
+ self.token = tokens.peek
105
+ end
106
+
107
+ def parse_tokenized_items(indent = 0)
108
+ obj = {}
109
+ loop do
110
+ prop_token = token
111
+ if prop_token.type == TOKEN_TYPES[:newline]
112
+ next_token = tokens.next
113
+ self.token = next_token
114
+ if indent.zero?
115
+ next
116
+ end # if we have 0 indentation then the next token doesn't matter
117
+
118
+ if next_token.type != TOKEN_TYPES[:indent]
119
+ break
120
+ end # if we have no indentation after a newline then we've gone down a level
121
+
122
+ break if next_token.value != indent
123
+
124
+ self.token = tokens.next
125
+ elsif prop_token.type == TOKEN_TYPES[:indent]
126
+ break if prop_token.value != indent
127
+
128
+ self.token = tokens.next
129
+ elsif prop_token.type == TOKEN_TYPES[:eof]
130
+ break
131
+ elsif prop_token.type == TOKEN_TYPES[:string]
132
+ key = prop_token.value
133
+
134
+ raise StandartError, "Expected a key" if key.nil?
135
+
136
+ keys = [key]
137
+ self.token = tokens.next
138
+
139
+ # support multiple keys
140
+ while token.type == TOKEN_TYPES[:comma]
141
+ self.token = tokens.next
142
+
143
+ raise StandardError, "Expected string" if token.type != TOKEN_TYPES[:string]
144
+
145
+ keys << token.value
146
+ self.token = tokens.next
147
+ end
148
+
149
+ was_colon = token.type == TOKEN_TYPES[:colon]
150
+
151
+ self.token = tokens.next if was_colon
152
+
153
+ if valid_prop_value_token?(token)
154
+ keys.each do |k|
155
+ obj[k] = token.value
156
+ end
157
+
158
+ self.token = tokens.next
159
+ elsif was_colon
160
+ # parse object
161
+ val = parse_tokenized_items(indent + 1)
162
+
163
+ keys.each do |k|
164
+ obj[k] = val
165
+ end
166
+
167
+ break if indent.positive? && token.type != TOKEN_TYPES[:indent]
168
+ else
169
+ raise StandardError, "Invalid value type"
170
+ end
171
+ elsif prop_token.type == TOKEN_TYPES[:comment]
172
+ self.token = tokens.next
173
+ next
174
+ else
175
+ raise StandardError, "Unkown token #{token}"
176
+ end
177
+ end
178
+
179
+ obj
180
+ end
181
+
182
+ def count_special_characters(input)
183
+ i = 0
184
+ i += 1 until [":", "\n", "\r", ",", " "].include?(input[i]) || input[i].nil?
185
+
186
+ i
187
+ end
188
+
189
+ def count_indent_size(input)
190
+ indent_size = 1
191
+ indent_size += 1 while input[indent_size] == " "
192
+
193
+ raise StandardError, "Invalid number of spaces" if indent_size.odd?
194
+
195
+ indent_size
196
+ end
197
+
198
+ def extract_value(input)
199
+ between_quotes = /\"(.*?)\"/
200
+ val = input.match(between_quotes).to_s
201
+
202
+ [val.size, val]
203
+ end
204
+
205
+ def compatible?(yarn_file)
206
+ version_regex = /yarn lockfile v(#{LOCKFILE_VERSION})$/.freeze
207
+
208
+ lines = yarn_file.split("\n", 2) # get first two lines
209
+
210
+ lines.each do |line|
211
+ return true if line.match?(version_regex)
212
+ end
213
+
214
+ false
215
+ end
216
+
217
+ def convert_objects(dependencies)
218
+ deps = Set.new
219
+ dependencies.each do |dependency, details|
220
+ name = dependency.match(/(^.*)(@)(.*)/i).captures.first
221
+ version = details["version"]
222
+ deps.add({ name: name, version: version })
223
+ end
224
+
225
+ deps.to_a
226
+ end
227
+
228
+ # boolean
229
+ def valid_prop_value_token?(token)
230
+ [TOKEN_TYPES[:boolean], TOKEN_TYPES[:string], TOKEN_TYPES[:number]].include?(token.type)
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YarnLockParser
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/yarn_lock_parser/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "yarn_lock_parser"
7
+ spec.version = YarnLockParser::VERSION
8
+ spec.authors = ["Can Eldem"]
9
+ spec.email = ["eldemcan@gmail.com"]
10
+
11
+ spec.summary = "Yarn lock parser ported from js yarn library to ruby"
12
+ spec.description = "Parser for yarn.lock files."
13
+ spec.homepage = "https://rubygems.org/yarn_lock_parser"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/eldemcan/yarn_lock"
21
+ spec.metadata["changelog_uri"] = "https://github.com/eldemcan/yarn_lock/blob/master/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency 'byebug', '~> 11.1'
33
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yarn_lock_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Can Eldem
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '11.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '11.1'
27
+ description: Parser for yarn.lock files.
28
+ email:
29
+ - eldemcan@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".github/workflows/gem-push.yml"
35
+ - ".github/workflows/ruby.yml"
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - ".travis.yml"
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - bin/console
48
+ - bin/setup
49
+ - lib/yarn_lock_parser.rb
50
+ - lib/yarn_lock_parser/version.rb
51
+ - yarn_lock_parser.gemspec
52
+ homepage: https://rubygems.org/yarn_lock_parser
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://rubygems.org/yarn_lock_parser
57
+ source_code_uri: https://github.com/eldemcan/yarn_lock
58
+ changelog_uri: https://github.com/eldemcan/yarn_lock/blob/master/CHANGELOG.md
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.3.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.0.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Yarn lock parser ported from js yarn library to ruby
78
+ test_files: []