yarnlock 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f585247db16ab766b377683746a6284b41556ef2
4
- data.tar.gz: bbf0f0a483546a4f60f224402f7b8581fe7432a8
3
+ metadata.gz: af3024a8288b05e32849c894d206057929eb3aff
4
+ data.tar.gz: 63c3d23cc6244661469d2f8e2f0d5ef0d9568a6b
5
5
  SHA512:
6
- metadata.gz: 6b237ffefa43111e575cd1fb0c08fa180ece8ff3b15a5994d841a8f25545df32db72cd082e439db3ee83bfea470f00af4863e1b77542a277c787e2a31e78aab8
7
- data.tar.gz: a6ac1b293b0861019f1077fa5bc53e512dd96d709ee9eb30f6a64afc157f0b9137f384047742bbf80f7ec57ab94bf0e109c69ded58d50e5c0ee08366cb75867b
6
+ metadata.gz: 4f6c3dd8114faead13890685917e963483836574a56277f865553208d0462f9c9ecf6bfba013788197d2cd2d511d7c5fd90d2c397c45c71506066f87f2dd916d
7
+ data.tar.gz: a3252b023dc619b85834748278e70fb25001a995d457a397776d9f0475e1cff6e4490e23f09ce041ea18f5bd9138ee9fc6ff868a259729d18d8af749fdef3f09
data/README.md CHANGED
@@ -36,37 +36,72 @@ require 'yarnlock'
36
36
  # Parse string as in yarn.lock:
37
37
  parsed = Yarnlock.parse 'yarn_lock_text'
38
38
 
39
+ # Stringify parsed object from yarn.lock
40
+ string = Yarnlock.stringify parsed
41
+
39
42
  # Load from file path:
40
43
  parsed = Yarnlock.load 'yarn.lock'
41
-
42
- # Stringify parsed object from yarn.lock
43
- Yarnlock.stringify parsed
44
44
  ```
45
45
 
46
46
  ### Parsed object structure
47
47
 
48
- `Yarnlock.parse` returns `Yarnlock::Entry::Collection` object that is actually extends `Hash` and is holding each entries defined at `yarn.lock`.
48
+ Original `parse` function of `@yarnpkg/lockfile` returns pure JSON object in javascript context, so that this library parse JSON and convert it to ruby object for general purpose.
49
+
50
+ `Yarnlock.parse` returns just an `array` containing each entries defined at `yarn.lock`. Entries are represented as `Yarnlock::Entry` instances. Additionally array extends `Yarnlock::Entry::Collection` module to provide some useful enumeration and JSON serialization.
51
+
52
+ ## API
53
+
54
+ ### `Yarnlock::Entry::Collection`
55
+
56
+ is a module that is used for `Yarnlock.stringify` to make `string` as same as `yarn.lock`. In other words, You can modify entries and generate customized `yarn.lock` programmatically. Also it provides an enumeration method to retrieve entries for each packages:
57
+
58
+ #### `package_with_versions` `[Hash<String, <String, Yarnlock::Entry>>]`
59
+
60
+ returns 2 dimensional hash for iterate package with every resolved versions.
49
61
 
50
- That hash has 3 dimension, such like:
62
+ - The key of 1st level is the package name that is found on [Yarn repository](https://yarnpkg.com), can be used for dependency specification in package.json.
63
+ - The key of 2nd level is the resolved version for range of versions. It is parsed as `Semantic::Version`
64
+ - The value is `Yarnlock.Entry` instance.
65
+
66
+ For example:
51
67
 
52
68
  ```ruby
53
- parsed['@yarnpkg/lockfile']['1.0.0'] = Yarnlock::Entry.new
69
+ yarnlock = Yarnlock.load 'yarn.lock'
70
+ yarnlock.package_with_versions.each do |package, versions|
71
+ puts package # '@yarnpkg/lockfile'
72
+ versions.each do |version, entry|
73
+ puts version # <Semantic::Version:0x007fe286056110 @major=1, @minor=0, @patch=0, @pre=nil, @build=nil, @version="1.0.0">
74
+ puts entry.resolved # 'https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.0.0.tgz#33d1dbb659a23b81f87f048762b35a446172add3'
75
+ end
76
+ end
77
+ ```
78
+
79
+
80
+ #### `highest_version_packages` `[Hash<String, Yarnlock::Entry>]`
81
+
82
+ returns hash keyed by package, valued by entry. Since `yarn install` command will install highest resolved version of each package, You can take such package + version pair like:
83
+
84
+ ```ruby
85
+ yarnlock = Yarnlock.load 'yarn.lock'
86
+ yarnlock.highest_version_packages.each do |package, entry|
87
+ puts package # '@yarnpkg/lockfile'
88
+ puts entry.version # <Semantic::Version:0x007fe286056110 @major=1, @minor=0, @patch=0, @pre=nil, @build=nil, @version="1.0.0">
89
+ puts entry.resolved # 'https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.0.0.tgz#33d1dbb659a23b81f87f048762b35a446172add3'
90
+ end
54
91
  ```
55
92
 
56
- The key of 1st level is the package name that is found on [Yarn repository](https://yarnpkg.com), can be used for dependency specification in `package.json`.
57
- The key of 2nd level is the resolved version for range of versions.
58
- The value is `Yarnlock.Entry` object that represents a entry of `yarn.lock`.
93
+ ### `Yarnlock::Entry`
59
94
 
60
- `Yarnlock.Entry` is a pure class that holds parsed information from a entry. You can access attribute to get information what you need:
95
+ is a pure class that holds parsed information from a entry. You can access attribute to get information what you need:
61
96
 
62
- - `package` `[String]` The package name. Same as 1st level key of `Yarnlock::Entry::Collection`.
63
- - `package` `[String]` Resolved version. Same as 2nd level key of `Yarnlock::Entry::Collection`.
97
+ - `package` `[String]` The package name.
98
+ - `version` `[Semantic::Version]` Resolved version. This is not a just string but a `Semantic::Version` object to useful for compare for. See [jlindsey/semantic: Ruby Semantic Version class](https://github.com/jlindsey/semantic/) for details.
64
99
  - `version_ranges` `[Array]` Version ranges, this holds multiple ranges like `['^2.1.0', '^2.1.1']`.
65
100
  - You can see like `"@yarnpkg/lockfile@^1.0.0":` in `yarn.lock`, range of versions is `^1.0.0` of that, specified by `*dependencies` at `package.json` and its sub dependencies.
66
101
  - `resolved` `[String]` Resolved registry location for tar ball.
67
102
  - `dependencies` `[Hash]` Sub dependencies keyed by package name and valued by version range.
68
103
 
69
- ### Options
104
+ ## Options
70
105
 
71
106
  You can configure some options to change behavior like:
72
107
 
data/lib/yarnlock.rb CHANGED
@@ -23,7 +23,7 @@ module Yarnlock
23
23
  parsed = JSON.parse json_string
24
24
 
25
25
  raise "An error was occurred when parsing yarn.lock: #{parsed}" unless parsed.is_a? Hash
26
- raise "Could not parse yarn.lock: #{parsed['reason']}" unless parsed['type'] == 'success'
26
+ raise "Could not parse yarn.lock: #{parsed['reason']}" if parsed['type'] == 'failure'
27
27
 
28
28
  return parsed['object'] unless config.return_collection
29
29
  Entry::Collection.parse parsed['object']
@@ -34,7 +34,7 @@ module Yarnlock
34
34
  parsed = JSON.parse json_string
35
35
 
36
36
  raise "An error was occurred when stringing object: #{parsed}" unless parsed.is_a? Hash
37
- raise "Could not stringing object: #{parsed['reason']}" unless parsed['type'] == 'success'
37
+ raise "Could not stringing object: #{parsed['reason']}" if parsed['type'] == 'failure'
38
38
 
39
39
  parsed['yarnlock']
40
40
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'semantic/core_ext'
4
+
3
5
  module Yarnlock
4
6
  class Entry
5
7
  attr_accessor :package, :version_ranges, :version, :resolved, :dependencies
@@ -15,7 +17,7 @@ module Yarnlock
15
17
  @version_ranges << version_range
16
18
  end
17
19
 
18
- @version = entry['version']
20
+ @version = entry['version'].to_version
19
21
  @resolved = entry['resolved']
20
22
  @dependencies = entry['dependencies']
21
23
 
@@ -28,7 +30,7 @@ module Yarnlock
28
30
  end.join(', ')
29
31
  {
30
32
  pattern => {
31
- 'version' => version,
33
+ 'version' => version.to_s,
32
34
  'resolved' => resolved,
33
35
  'dependencies' => dependencies
34
36
  }.reject { |_, v| v.nil? } # Hash#compact is not supported in Ruby < 2.4
@@ -1,26 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'semantic'
4
+
3
5
  module Yarnlock
4
6
  class Entry
5
- class Collection < Hash
7
+ module Collection
6
8
  def self.parse(raw_entries)
7
- collection = new
8
- raw_entries.each do |pattern, raw_entry|
9
- entry = Yarnlock::Entry.parse pattern, raw_entry
10
- collection[entry.package] ||= {}
11
- collection[entry.package][entry.version] = entry
9
+ raw_entries.map do |pattern, raw_entry|
10
+ Entry.parse pattern, raw_entry
11
+ end.extend(self)
12
+ end
13
+
14
+ def package_with_versions
15
+ each_with_object({}) do |entry, packages|
16
+ packages[entry.package] ||= {}
17
+ packages[entry.package][entry.version] = entry
18
+ end
19
+ end
20
+
21
+ def highest_version_packages
22
+ each_with_object({}) do |entry, packages|
23
+ packages[entry.package] = [entry, packages[entry.package]].compact.max_by(&:version)
12
24
  end
13
- collection
14
25
  end
15
26
 
16
27
  def as_json(_options = {})
17
- entries = {}
18
- each_value do |versions|
19
- versions.each_value do |entry|
20
- entries.merge! entry.to_h
21
- end
28
+ each_with_object({}) do |entry, entries|
29
+ entries.merge! entry.to_h
22
30
  end
23
- entries
24
31
  end
25
32
 
26
33
  def to_json(*options)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yarnlock
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/yarnlock.gemspec CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ['lib']
24
24
 
25
+ spec.add_dependency 'semantic', '~> 1.6'
26
+
25
27
  spec.add_development_dependency 'bundler', '~> 1.16'
26
28
  spec.add_development_dependency 'rake', '~> 10.0'
27
29
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yarnlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroki Shimizu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-12 00:00:00.000000000 Z
11
+ date: 2018-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: semantic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement