Module: Cyborg::NPM

Extended by:
NPM
Included in:
NPM
Defined in:
lib/cyborg/command/npm.rb

Constant Summary collapse

DEPENDENCIES =
YAML.load %Q{
private: true
devDependencies:
  browserify:   ^16.2.1
  browserify-incremental: ^3.1.1
  svgo:         ^1.0.5
}

Instance Method Summary collapse

Instance Method Details

#package_pathObject



25
26
27
# File 'lib/cyborg/command/npm.rb', line 25

def package_path
  File.join(Dir.pwd, 'package.json')
end

#read_package_jsonObject



37
38
39
# File 'lib/cyborg/command/npm.rb', line 37

def read_package_json
  JSON.parse File.read(package_path)
end

#setupObject



15
16
17
18
19
20
21
22
23
# File 'lib/cyborg/command/npm.rb', line 15

def setup
  puts "\nInstalling npm dependencies…".bold

  if File.exist?(package_path)
    update_package_json
  else
    write_package_json(DEPENDENCIES)
  end
end

#update_package_jsonObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cyborg/command/npm.rb', line 41

def update_package_json
  package = read_package_json
  package['dependencies']    ||= {}
  package['devDependencies'] ||= {}

  deps = DEPENDENCIES['devDependencies']

  deps.keys.each do |dep|
    d = deps[dep]

    if package['devDependencies'][dep].nil? && package['dependencies'][dep].nil?
      package['devDependencies'][dep] = d
    end
  end

  package.delete('dependencies') if package['dependencies'].empty?
  
  write_package_json(package)
end

#write_package_json(contents) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cyborg/command/npm.rb', line 29

def write_package_json(contents)
  File.open(package_path, 'w') do |io|
    io.write(JSON.pretty_generate(contents))
  end

  puts "create".rjust(12).colorize(:green).bold + "  #{package_path}"
end