class KBuilder::PackageJson::PackageBuilder

Configuration currently comes from KBuilder and stores template folders and target folders if configured

Attributes

dependency_type[RW]
package[W]

In memory representation of the package.json file that is being created

package_file[R]

Public Class Methods

new(configuration = nil) click to toggle source
Calls superclass method
# File lib/k_builder/package_json/package_builder.rb, line 14
def initialize(configuration = nil)
  super(configuration)

  set_package_file('package.json')
  set_dependency_type(:development)
end

Public Instance Methods

add_script(key, value) click to toggle source

Add a script with key and value (command line to run)

# File lib/k_builder/package_json/package_builder.rb, line 132
def add_script(key, value)
  load

  @package['scripts'][key] = value

  write

  self
end
context() click to toggle source

This is all wrong, but useful for now

# File lib/k_builder/package_json/package_builder.rb, line 260
def context
  @context ||= KUtil.data.to_open_struct(configuration)
end
dependency_option() click to toggle source

Getter for dependency option

# File lib/k_builder/package_json/package_builder.rb, line 182
def dependency_option
  dependency_type == :development ? '-D' : '-P'
end
development() click to toggle source

Change context to development, new dependencies will be for development

# File lib/k_builder/package_json/package_builder.rb, line 33
def development
  set_dependency_type(:development)

  self
end
execute(command) click to toggle source
# File lib/k_builder/package_json/package_builder.rb, line 235
def execute(command)
  puts "RUN: #{command}"
  rc command
  load
end
get_group(key) click to toggle source
# File lib/k_builder/package_json/package_builder.rb, line 264
def get_group(key)
  group = context.package_json.package_groups[key]

  raise KBuilder::PackageJson::Error, "unknown package group: #{key}" if group.nil?

  group
end
load() click to toggle source

Load the existing package.json into memory

ToDo: Would be useful to record the update timestamp on the package so that we only load if the in memory package is not the latest.

The reason this can happen, is because external tools such are npm install are updating the package.json and after this happens we need to call load, but if there is any bug in the code we may for get to load, or we may load multiple times.

# File lib/k_builder/package_json/package_builder.rb, line 98
def load
  raise KBuilder::PackageJson::Error, 'package.json does not exist' unless File.exist?(package_file)

  puts 'loading...'

  content = File.read(package_file)
  @package = JSON.parse(content)

  self
end
npm_a(packages, options: nil)
Alias for: npm_add
npm_add(packages, options: nil) click to toggle source
# File lib/k_builder/package_json/package_builder.rb, line 60
def npm_add(packages, options: nil)
  npm_add_or_install(packages, parse_options(options, '--package-lock-only --no-package-lock'))

  self
end
Also aliased as: npm_a
npm_add_group(key, options: nil) click to toggle source
# File lib/k_builder/package_json/package_builder.rb, line 67
def npm_add_group(key, options: nil)
  group = get_group(key)

  puts "Adding #{group.description}"

  npm_add(group.package_names, options: options)

  self
end
Also aliased as: npm_ag
npm_add_or_install(packages, options) click to toggle source
# File lib/k_builder/package_json/package_builder.rb, line 241
def npm_add_or_install(packages, options)
  # if -P or -D is not in the options then use the current builder dependency option
  options.push dependency_option unless options_any?(options, '-P', '-D')
  packages = packages.join(' ') if packages.is_a?(Array)
  command = "npm install #{options.join(' ')} #{packages}"
  execute command
end
npm_ag(key, options: nil)
Alias for: npm_add_group
npm_i(packages, options: nil)
Alias for: npm_install
npm_init() click to toggle source

Init an NPN package

run npm init -y

Note: npm init does not support –silent operation

# File lib/k_builder/package_json/package_builder.rb, line 44
def npm_init
  rc 'npm init -y'

  load

  self
end
npm_install(packages, options: nil) click to toggle source

Space separated list of packages

# File lib/k_builder/package_json/package_builder.rb, line 53
def npm_install(packages, options: nil)
  npm_add_or_install(packages, parse_options(options))

  self
end
Also aliased as: npm_i
npm_install_group(key, options: nil) click to toggle source

Add a group of NPN packages which get defined in configuration

# File lib/k_builder/package_json/package_builder.rb, line 79
def npm_install_group(key, options: nil)
  group = get_group(key)

  puts "Installing #{group.description}"

  npm_install(group.package_names, options: options)

  self
end
options_any?(options, *any_options) click to toggle source
# File lib/k_builder/package_json/package_builder.rb, line 231
def options_any?(options, *any_options)
  (options & any_options).any?
end
package() click to toggle source

Load the package.json into a memory as object

# File lib/k_builder/package_json/package_builder.rb, line 156
def package
  return @package if defined? @package

  load

  @package
end
package_file=(_value) click to toggle source

Setter for target folder

# File lib/k_builder/package_json/package_builder.rb, line 207
def package_file=(_value)
  @package_file = File.join(target_folder, 'package.json')
end
parse_options(options = nil, required_options = nil) click to toggle source

Helpers


# File lib/k_builder/package_json/package_builder.rb, line 220
def parse_options(options = nil, required_options = nil)
  options = [] if options.nil?
  options = options.split if options.is_a?(String)
  options.reject(&:empty?)

  required_options = [] if required_options.nil?
  required_options = required_options.split if required_options.is_a?(String)

  options | required_options
end
production() click to toggle source

Change context to production, new dependencies will be for production

# File lib/k_builder/package_json/package_builder.rb, line 26
def production
  set_dependency_type(:production)

  self
end
remove_script(key) click to toggle source

Remove a script reference by key

# File lib/k_builder/package_json/package_builder.rb, line 121
def remove_script(key)
  load

  @package['scripts']&.delete(key)

  write

  self
end
set(key, value) click to toggle source

Set a property value in the package

# File lib/k_builder/package_json/package_builder.rb, line 168
def set(key, value)
  load

  @package[key] = value

  write

  self
end
set_dependency_type(value) click to toggle source

Fluent setter for target folder

# File lib/k_builder/package_json/package_builder.rb, line 190
def set_dependency_type(value)
  self.dependency_type = value

  self
end
set_package_file(value) click to toggle source

Fluent setter for target folder

# File lib/k_builder/package_json/package_builder.rb, line 200
def set_package_file(value)
  self.package_file = value

  self
end
write() click to toggle source

Write the package.json file

# File lib/k_builder/package_json/package_builder.rb, line 110
def write
  puts 'writing...'

  content = JSON.pretty_generate(@package)

  File.write(package_file, content)

  self
end