class Knitter::Yarn

Constants

PACKAGE_AREAS

Attributes

directory[R]

Public Class Methods

new(directory) click to toggle source
# File lib/knitter/yarn.rb, line 23
def initialize(directory)
    @directory = Pathname.new(directory)
end

Public Instance Methods

add(package) click to toggle source
# File lib/knitter/yarn.rb, line 39
def add(package)
    name = package.name
    unless package.version.latest?
        name << '@' << package.version.to_s
    end
    execute(
        *['add', name, package_save_as_flag(package)].compact
    )
end
init() click to toggle source
# File lib/knitter/yarn.rb, line 27
def init
    execute('init', '--yes')
end
install() click to toggle source
# File lib/knitter/yarn.rb, line 35
def install
    execute('install')
end
ok?() click to toggle source
# File lib/knitter/yarn.rb, line 31
def ok?
    @sh && @sh.status.exitstatus == 0
end
package_file() click to toggle source
# File lib/knitter/yarn.rb, line 49
def package_file
    @directory.join "package.json"
end
package_file_contents() click to toggle source
# File lib/knitter/yarn.rb, line 67
def package_file_contents
    JSON.parse(package_file.read)
end
packages() click to toggle source
# File lib/knitter/yarn.rb, line 53
def packages
    json = package_file_contents
    Enumerator.new do | enum |
        PACKAGE_AREAS.each do | type, area |
            (json[area] || []).each do | package, version |
                package = Package.new(package, yarn: self)
                package.version = version
                package.dependency_type = type
                enum << package
            end
        end
    end
end
valid?() click to toggle source
# File lib/knitter/yarn.rb, line 71
def valid?
    package_file.exist? && package_file_contents
end

Protected Instance Methods

execute(cmd, *args) click to toggle source
# File lib/knitter/yarn.rb, line 85
def execute(cmd, *args)
    @sh = Mixlib::ShellOut.new(
        "yarn", cmd, *args,
        cwd: @directory.to_s
    )
    @sh.run_command
    unless 0 == @sh.exitstatus
        raise CommandExecutionError.new(@sh.stderr)
    end
end
package_save_as_flag(package) click to toggle source
# File lib/knitter/yarn.rb, line 77
def package_save_as_flag(package)
    case package.dependency_type
    when :peer        then '--peer'
    when :optional    then '--optional'
    when :development then '--dev'
    end
end