Class: KBuilder::PackageJson::PackageBuilder
- Inherits:
-
BaseBuilder
- Object
- BaseBuilder
- KBuilder::PackageJson::PackageBuilder
- Defined in:
- lib/k_builder/package_json/package_builder.rb
Overview
Configuration currently comes from KBuilder and stores template folders and target folders if configured
Instance Attribute Summary collapse
-
#dependency_type ⇒ Object
Returns the value of attribute dependency_type.
-
#package ⇒ Object
Load the package.json into a memory as object.
-
#package_file ⇒ Object
Returns the value of attribute package_file.
Instance Method Summary collapse
-
#add_script(key, value) ⇒ Object
Add a script with key and value (command line to run).
-
#context ⇒ Object
This is all wrong, but useful for now.
-
#dependency_option ⇒ Object
Getter for dependency option.
-
#development ⇒ Object
Change context to development, new dependencies will be for development.
- #execute(command) ⇒ Object
- #get_group(key) ⇒ Object
-
#initialize(configuration = nil) ⇒ PackageBuilder
constructor
A new instance of PackageBuilder.
-
#load ⇒ Object
Load the existing package.json into memory.
- #npm_add(packages, options: nil) ⇒ Object (also: #npm_a)
- #npm_add_group(key, options: nil) ⇒ Object (also: #npm_ag)
- #npm_add_or_install(packages, options) ⇒ Object
-
#npm_init ⇒ Object
Init an NPN package.
-
#npm_install(packages, options: nil) ⇒ Object
(also: #npm_i)
Space separated list of packages.
-
#npm_install_group(key, options: nil) ⇒ Object
Add a group of NPN packages which get defined in configuration.
- #options_any?(options, *any_options) ⇒ Boolean
-
#parse_options(options = nil, required_options = nil) ⇒ Object
———————————– Helpers ———————————–.
-
#production ⇒ Object
Change context to production, new dependencies will be for production.
-
#remove_script(key) ⇒ Object
Remove a script reference by key.
-
#set(key, value) ⇒ Object
Set a property value in the package.
-
#set_dependency_type(value) ⇒ Object
Fluent setter for target folder.
-
#set_package_file(value) ⇒ Object
Fluent setter for target folder.
-
#write ⇒ Object
Write the package.json file.
Constructor Details
#initialize(configuration = nil) ⇒ PackageBuilder
Returns a new instance of PackageBuilder.
14 15 16 17 18 19 |
# 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 |
Instance Attribute Details
#dependency_type ⇒ Object
Returns the value of attribute dependency_type.
12 13 14 |
# File 'lib/k_builder/package_json/package_builder.rb', line 12 def dependency_type @dependency_type end |
#package ⇒ Object
Load the package.json into a memory as object
156 157 158 159 160 161 162 |
# File 'lib/k_builder/package_json/package_builder.rb', line 156 def package return @package if defined? @package load @package end |
#package_file ⇒ Object
Returns the value of attribute package_file.
11 12 13 |
# File 'lib/k_builder/package_json/package_builder.rb', line 11 def package_file @package_file end |
Instance Method Details
#add_script(key, value) ⇒ Object
Add a script with key and value (command line to run)
132 133 134 135 136 137 138 139 140 |
# 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 ⇒ Object
This is all wrong, but useful for now
260 261 262 |
# File 'lib/k_builder/package_json/package_builder.rb', line 260 def context @context ||= KUtil.data.to_open_struct(configuration) end |
#dependency_option ⇒ Object
Getter for dependency option
182 183 184 |
# File 'lib/k_builder/package_json/package_builder.rb', line 182 def dependency_option dependency_type == :development ? '-D' : '-P' end |
#development ⇒ Object
Change context to development, new dependencies will be for development
33 34 35 36 37 |
# File 'lib/k_builder/package_json/package_builder.rb', line 33 def development set_dependency_type(:development) self end |
#execute(command) ⇒ Object
235 236 237 238 239 |
# File 'lib/k_builder/package_json/package_builder.rb', line 235 def execute(command) puts "RUN: #{command}" rc command load end |
#get_group(key) ⇒ Object
264 265 266 267 268 269 270 |
# 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 ⇒ Object
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.
98 99 100 101 102 103 104 105 106 107 |
# 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_add(packages, options: nil) ⇒ Object Also known as: npm_a
60 61 62 63 64 |
# File 'lib/k_builder/package_json/package_builder.rb', line 60 def npm_add(packages, options: nil) npm_add_or_install(packages, (, '--package-lock-only --no-package-lock')) self end |
#npm_add_group(key, options: nil) ⇒ Object Also known as: npm_ag
67 68 69 70 71 72 73 74 75 |
# 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: ) self end |
#npm_add_or_install(packages, options) ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/k_builder/package_json/package_builder.rb', line 241 def npm_add_or_install(packages, ) # if -P or -D is not in the options then use the current builder dependency option .push dependency_option unless (, '-P', '-D') packages = packages.join(' ') if packages.is_a?(Array) command = "npm install #{.join(' ')} #{packages}" execute command end |
#npm_init ⇒ Object
Init an NPN package
run npm init -y
Note: npm init does not support –silent operation
44 45 46 47 48 49 50 |
# 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) ⇒ Object Also known as: npm_i
Space separated list of packages
53 54 55 56 57 |
# File 'lib/k_builder/package_json/package_builder.rb', line 53 def npm_install(packages, options: nil) npm_add_or_install(packages, ()) self end |
#npm_install_group(key, options: nil) ⇒ Object
Add a group of NPN packages which get defined in configuration
79 80 81 82 83 84 85 86 87 |
# 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: ) self end |
#options_any?(options, *any_options) ⇒ Boolean
231 232 233 |
# File 'lib/k_builder/package_json/package_builder.rb', line 231 def (, *) ( & ).any? end |
#parse_options(options = nil, required_options = nil) ⇒ Object
Helpers
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/k_builder/package_json/package_builder.rb', line 220 def ( = nil, = nil) = [] if .nil? = .split if .is_a?(String) .reject(&:empty?) = [] if .nil? = .split if .is_a?(String) | end |
#production ⇒ Object
Change context to production, new dependencies will be for production
26 27 28 29 30 |
# File 'lib/k_builder/package_json/package_builder.rb', line 26 def production set_dependency_type(:production) self end |
#remove_script(key) ⇒ Object
Remove a script reference by key
121 122 123 124 125 126 127 128 129 |
# 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) ⇒ Object
Set a property value in the package
168 169 170 171 172 173 174 175 176 |
# 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) ⇒ Object
Fluent setter for target folder
190 191 192 193 194 |
# 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) ⇒ Object
Fluent setter for target folder
200 201 202 203 204 |
# File 'lib/k_builder/package_json/package_builder.rb', line 200 def set_package_file(value) self.package_file = value self end |
#write ⇒ Object
Write the package.json file
110 111 112 113 114 115 116 117 118 |
# 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 |