Class: KDirector::Dsls::Children::PackageJson
- Inherits:
-
KDirector::Directors::ChildDirector
- Object
- KDirector::Directors::BaseDirector
- KDirector::Directors::ChildDirector
- KDirector::Dsls::Children::PackageJson
- Defined in:
- lib/k_director/dsls/children/package_json.rb
Overview
PackageJson DSL provides package.json manipulation actions such as.
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.
Attributes inherited from KDirector::Directors::ChildDirector
Attributes inherited from KDirector::Directors::BaseDirector
#builder, #k_builder, #options
Instance Method Summary collapse
-
#add_script(key, value) ⇒ Object
Add a script with key and value (command line to run).
-
#create_yarn_lock ⇒ Object
Create yarn.lock ———————————————————————-.
-
#dependency_option ⇒ Object
Getter for dependency option.
-
#development ⇒ Object
Change context to development, new dependencies will be for development.
- #execute(command) ⇒ Object
-
#initialize(parent, **opts) ⇒ PackageJson
constructor
A new instance of PackageJson.
-
#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(key, group: nil) ⇒ Object
-
#remove_package_lock ⇒ Object
Remove package-lock.json ———————————————————————-.
-
#remove_script(key) ⇒ Object
Remove a script reference by key.
-
#remove_yarn_lock ⇒ Object
Remove yarn.lock ———————————————————————-.
-
#set(key, value, group: nil) ⇒ Object
Set a property value in the package.
-
#set_dependency_type(value) ⇒ Object
Fluent setter for target folder rubocop:disable Naming/AccessorMethodName.
-
#set_package_file(value) ⇒ Object
Fluent setter for package file rubocop:disable Naming/AccessorMethodName.
-
#settings(**opts) ⇒ Object
Settings.
-
#sort ⇒ Object
Sort the package.json keys using ‘npx sort-package-json`.
-
#write ⇒ Object
Write the package.json file.
Methods inherited from KDirector::Directors::ChildDirector
Methods inherited from KDirector::Directors::BaseDirector
#active?, #add, #add_clipboard, #add_file, #blueprint, builder_type, #configuration, #data, #debug, #debug_dom, #debug_options, default_builder_type, default_on_action, default_on_exist, defaults, #director_name, #director_name=, #dom, #fadd, #github, #inherited_opts, init, #json_dom, #oadd, #on_action, on_action, #on_exist, on_exist, #package_json, #play_actions, #run_script, #set_current_folder_action, #tadd, #template_base_folder, #typed_dom
Constructor Details
#initialize(parent, **opts) ⇒ PackageJson
Returns a new instance of PackageJson.
15 16 17 18 19 20 |
# File 'lib/k_director/dsls/children/package_json.rb', line 15 def initialize(parent, **opts) super(parent, **opts) set_package_file('package.json') set_dependency_type(:development) end |
Instance Attribute Details
#dependency_type ⇒ Object
Returns the value of attribute dependency_type.
13 14 15 |
# File 'lib/k_director/dsls/children/package_json.rb', line 13 def dependency_type @dependency_type end |
#package ⇒ Object
Load the package.json into a memory as object
154 155 156 157 158 159 160 |
# File 'lib/k_director/dsls/children/package_json.rb', line 154 def package return @package if defined? @package load @package end |
#package_file ⇒ Object
Returns the value of attribute package_file.
12 13 14 |
# File 'lib/k_director/dsls/children/package_json.rb', line 12 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)
134 135 136 137 138 |
# File 'lib/k_director/dsls/children/package_json.rb', line 134 def add_script(key, value) set(key, value, group: 'scripts') self end |
#create_yarn_lock ⇒ Object
Create yarn.lock
291 292 293 294 295 |
# File 'lib/k_director/dsls/children/package_json.rb', line 291 def create_yarn_lock run_command 'yarn install --check-files' self end |
#dependency_option ⇒ Object
Getter for dependency option
233 234 235 |
# File 'lib/k_director/dsls/children/package_json.rb', line 233 def dependency_option dependency_type == :development ? '-D' : '-P' end |
#development ⇒ Object
Change context to development, new dependencies will be for development
34 35 36 37 38 |
# File 'lib/k_director/dsls/children/package_json.rb', line 34 def development set_dependency_type(:development) self end |
#execute(command) ⇒ Object
316 317 318 319 320 |
# File 'lib/k_director/dsls/children/package_json.rb', line 316 def execute(command) puts "RUN: #{command}" run_command command load 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.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/k_director/dsls/children/package_json.rb', line 99 def load raise KDirector::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
61 62 63 64 65 |
# File 'lib/k_director/dsls/children/package_json.rb', line 61 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
68 69 70 71 72 73 74 75 76 |
# File 'lib/k_director/dsls/children/package_json.rb', line 68 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
322 323 324 325 326 327 328 |
# File 'lib/k_director/dsls/children/package_json.rb', line 322 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
45 46 47 48 49 50 51 |
# File 'lib/k_director/dsls/children/package_json.rb', line 45 def npm_init run_command 'npm init -y' load self end |
#npm_install(packages, options: nil) ⇒ Object Also known as: npm_i
Space separated list of packages
54 55 56 57 58 |
# File 'lib/k_director/dsls/children/package_json.rb', line 54 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
80 81 82 83 84 85 86 87 88 |
# File 'lib/k_director/dsls/children/package_json.rb', line 80 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
312 313 314 |
# File 'lib/k_director/dsls/children/package_json.rb', line 312 def (, *) ( & ).any? end |
#parse_options(options = nil, required_options = nil) ⇒ Object
Helpers
301 302 303 304 305 306 307 308 309 310 |
# File 'lib/k_director/dsls/children/package_json.rb', line 301 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
27 28 29 30 31 |
# File 'lib/k_director/dsls/children/package_json.rb', line 27 def production set_dependency_type(:production) self end |
#remove(key, group: nil) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/k_director/dsls/children/package_json.rb', line 198 def remove(key, group: nil) load key = key.to_s.strip if group.nil? @package.delete(key) else group = group.to_s.strip @package[group]&.delete(key) end @package['scripts']&.delete(key) write self end |
#remove_package_lock ⇒ Object
Remove package-lock.json
269 270 271 272 273 274 275 |
# File 'lib/k_director/dsls/children/package_json.rb', line 269 def remove_package_lock file = File.join(k_builder.target_folder, 'package-lock.json') FileUtils.rm_rf(file) self end |
#remove_script(key) ⇒ Object
Remove a script reference by key
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/k_director/dsls/children/package_json.rb', line 122 def remove_script(key) remove(key, group: 'scripts') # load # @package['scripts']&.delete(key) # write self end |
#remove_yarn_lock ⇒ Object
Remove yarn.lock
280 281 282 283 284 285 286 |
# File 'lib/k_director/dsls/children/package_json.rb', line 280 def remove_yarn_lock file = File.join(k_builder.target_folder, 'yarn.lock') FileUtils.rm_rf(file) self end |
#set(key, value, group: nil) ⇒ Object
Set a property value in the package
188 189 190 191 192 193 194 195 196 |
# File 'lib/k_director/dsls/children/package_json.rb', line 188 def set(key, value, group: nil) load set_key_value(key, value, group: group) write self end |
#set_dependency_type(value) ⇒ Object
Fluent setter for target folder rubocop:disable Naming/AccessorMethodName
242 243 244 245 246 |
# File 'lib/k_director/dsls/children/package_json.rb', line 242 def set_dependency_type(value) self.dependency_type = value self end |
#set_package_file(value) ⇒ Object
Fluent setter for package file rubocop:disable Naming/AccessorMethodName
254 255 256 257 258 |
# File 'lib/k_director/dsls/children/package_json.rb', line 254 def set_package_file(value) self.package_file = value self end |
#settings(**opts) ⇒ Object
Settings
Add multiple settings to the package.json
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/k_director/dsls/children/package_json.rb', line 170 def settings(**opts) load group = opts.delete(:group) opts.each do |key, value| set_key_value(key, value, group: group) end write self end |
#sort ⇒ Object
Sort the package.json keys using ‘npx sort-package-json`
221 222 223 224 225 226 227 |
# File 'lib/k_director/dsls/children/package_json.rb', line 221 def sort run_command 'npx sort-package-json' load self end |
#write ⇒ Object
Write the package.json file
111 112 113 114 115 116 117 118 119 |
# File 'lib/k_director/dsls/children/package_json.rb', line 111 def write # puts 'writing...' content = JSON.pretty_generate(@package) File.write(package_file, content) self end |