class KBuilder::PackageJson::PackageBuilder
Configuration
currently comes from KBuilder
and stores template folders and target folders if configured
Attributes
In memory representation of the package.json file that is being created
Public Class Methods
# 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 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
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
Getter for dependency option
# File lib/k_builder/package_json/package_builder.rb, line 182 def dependency_option dependency_type == :development ? '-D' : '-P' end
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
# File lib/k_builder/package_json/package_builder.rb, line 235 def execute(command) puts "RUN: #{command}" rc command load end
# 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 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
# 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
# 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
# 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
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
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
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
# File lib/k_builder/package_json/package_builder.rb, line 231 def options_any?(options, *any_options) (options & any_options).any? end
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
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
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
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 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 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
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
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 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