Class: Squared::Workspace::Project::Node
- Includes:
- Common::Prompt
- Defined in:
- lib/squared/workspace/project/node.rb
Constant Summary
Constants inherited from Base
Constants included from Common
Instance Attribute Summary collapse
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Attributes inherited from Base
#exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace
Class Method Summary collapse
- .aliasargs ⇒ Object
- .batchargs ⇒ Object
- .config?(val) ⇒ Boolean
- .populate ⇒ Object
- .prod? ⇒ Boolean
- .tasks ⇒ Object
Instance Method Summary collapse
- #bump(flag) ⇒ Object
- #compose(opts, flags = nil, script: false) ⇒ Object
- #copy(from: 'build', glob: '**/*', into: 'node_modules', scope: nil, also: nil, override: false) ⇒ Object
- #copy? ⇒ Boolean
- #depend(flag = nil, sync: invoked_sync?('depend', flag)) ⇒ Object
- #depend? ⇒ Boolean
- #dev? ⇒ Boolean
-
#initialize(**kwargs) ⇒ Node
constructor
A new instance of Node.
- #install_type(prog = nil) ⇒ Object
- #outdated(rev = nil, opts: []) ⇒ Object
- #outdated? ⇒ Boolean
- #pnpm? ⇒ Boolean
- #populate ⇒ Object
- #prod? ⇒ Boolean
- #ref ⇒ Object
- #version ⇒ Object
- #workspaces? ⇒ Boolean
- #yarn? ⇒ Boolean
Methods included from Common::Prompt
Methods inherited from Git
#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #stash, #status
Methods inherited from Base
#add, #allref, as_path, #base_path, #build, #build?, #clean, #clean?, #color, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, ref, #ref?, #script?, #test, #test?, to_s, #to_s, #to_sym, #variable_set, #variables, #with
Constructor Details
#initialize(**kwargs) ⇒ Node
Returns a new instance of Node.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/squared/workspace/project/node.rb', line 44 def initialize(*, **kwargs) super if @pass.include?(Node.ref) initialize_ref(Node.ref) initialize_logger(**kwargs) else initialize_build(Node.ref, prod: prod?, **kwargs) initialize_env(**kwargs) end @pm = {} @package = base_path('package.json') end |
Instance Attribute Details
#package ⇒ Object (readonly)
Returns the value of attribute package.
42 43 44 |
# File 'lib/squared/workspace/project/node.rb', line 42 def package @package end |
Class Method Details
.aliasargs ⇒ Object
20 21 22 |
# File 'lib/squared/workspace/project/node.rb', line 20 def aliasargs [ref, { refresh: :build }] end |
.batchargs ⇒ Object
16 17 18 |
# File 'lib/squared/workspace/project/node.rb', line 16 def batchargs [ref, { refresh: %i[build copy] }] end |
.config?(val) ⇒ Boolean
28 29 30 31 32 |
# File 'lib/squared/workspace/project/node.rb', line 28 def config?(val) return false unless (val = as_path(val)) val.join('package.json').exist? end |
.populate ⇒ Object
10 |
# File 'lib/squared/workspace/project/node.rb', line 10 def populate(*); end |
.prod? ⇒ Boolean
24 25 26 |
# File 'lib/squared/workspace/project/node.rb', line 24 def prod? ENV['NODE_ENV'] == 'production' end |
.tasks ⇒ Object
12 13 14 |
# File 'lib/squared/workspace/project/node.rb', line 12 def tasks %i[outdated].freeze end |
Instance Method Details
#bump(flag) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/squared/workspace/project/node.rb', line 440 def bump(flag) return unless (ver = version) seg = semscan(ver) case flag when :major if seg[0] != '0' || seg[2].nil? seg[0] = seg[0].succ else seg[2] = seg[2].succ end when :minor if seg[0] == '0' seg[4] &&= seg[4].succ else seg[2] = seg[2].succ end when :patch seg[4] &&= seg[4].succ end unless (out = seg.join) == ver begin doc = package.read if doc.sub!(/"version"\s*:\s*"#{ver}"/, "\"version\": \"#{out}\"") package.write(doc) log.info "bump version #{ver} to #{out} (#{flag})" if verbose major = flag == :major emphasize("version: #{out}", title: name, border: borderstyle, sub: [ headerstyle, { pat: /^(version:)( )(\S+)(.*)$/, styles: color(major ? :green : :yellow), index: 3 }, { pat: /^(version:)(.*)$/, styles: theme[major ? :major : :active] } ]) elsif stdin? puts out end else raise_error('not found', hint: 'version') end rescue StandardError => e log.debug e raise end end end |
#compose(opts, flags = nil, script: false) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/squared/workspace/project/node.rb', line 420 def compose(opts, flags = nil, script: false) return unless opts && script ret = session (if yarn? 'yarn' else pnpm? ? 'pnpm' : 'npm' end), 'run', flags append_loglevel case opts when ::Enumerable ret += opts.to_a when ::String ret << opts else raise_error("#{ret.first} script name", hint: opts.nil? ? 'missing' : 'invalid') end ret end |
#copy(from: 'build', glob: '**/*', into: 'node_modules', scope: nil, also: nil, override: false) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/squared/workspace/project/node.rb', line 148 def copy(from: 'build', glob: '**/*', into: 'node_modules', scope: nil, also: nil, override: false) if @copy && !override return super if runnable?(@copy) from = @copy[:from] if @copy.key?(:from) glob = @copy[:glob] if @copy.key?(:glob) into = @copy[:into] if @copy.key?(:into) scope = @copy[:scope] if @copy.key?(:scope) also = @copy[:also] if @copy.key?(:also) end items = [path != workspace.home && workspace.home? ? workspace.home : nil] items += as_a(also) if also items.each_with_index do |dir, i| if i == 0 next unless dev? && build? dest = dir else case dir when ::String dest = workspace.root_path(dir) when ::Symbol dest = Workspace.resolve(dir)&.path when ::Hash glob = '**/*' dest = nil scope = nil dir.each do |key, val| case key.to_sym when :target dest = val when :from from = val when :glob glob = val when :scope scope = val when :into into = val end end when Project::Base dest = dir.path end end next unless dest&.directory? from = base_path(from) dest = dest.join(into, scope || project) (glob = as_a(glob)).each { |val| log.info "cp #{from.join(val)} #{dest}" } copy_d(from, dest, glob: glob, verbose: verbose) end end |
#copy? ⇒ Boolean
502 503 504 |
# File 'lib/squared/workspace/project/node.rb', line 502 def copy? super || @copy.is_a?(::Hash) end |
#depend(flag = nil, sync: invoked_sync?('depend', flag)) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/squared/workspace/project/node.rb', line 202 def depend(flag = nil, sync: invoked_sync?('depend', flag)) if @depend && !flag super elsif outdated? if (yarn = install_type(:yarn)) > 0 cmd = session 'yarn' if yarn > 1 if flag == :dedupe cmd << 'dedupe' else cmd << 'install' << if flag == :force '--check-cache' elsif flag == :frozen '--immutable' end end else cmd << 'install' << if flag == :force '--force' elsif flag == :frozen '--frozen-lockfile' end cmd << '--production' if flag && prod? cmd << '--ignore-engines' unless option('ignore-engines', equals: '0') end elsif pnpm? cmd = session 'pnpm' if flag == :dedupe cmd << 'dedupe' else cmd << 'install' << if flag == :force '--force' elsif flag == :frozen '--frozen-lockfile' end end cmd << '--prod' if flag && prod? if (val = option('public-hoist-pattern', ignore: false)) split_escape(val).each { |opt| cmd << "--public-hoist-pattern=#{shell_escape(opt, quote: true)}" } end cmd << '--ignore-workspace' if env('NODE_WORKSPACES', equals: '0') append_nocolor option('no-color') else cmd = session 'npm' if flag == :dedupe cmd << 'dedupe' else cmd << 'install' << if flag == :force '--force' elsif flag == :frozen '--package-lock-only' end end cmd << '--omit=dev' if flag && prod? cmd << '--workspaces=false' if env('NODE_WORKSPACES', equals: '0') cmd << '--package-lock=false' if option('package-lock', equals: '0') append_nocolor option('no-color') end append_loglevel run(sync: sync) end end |
#depend? ⇒ Boolean
498 499 500 |
# File 'lib/squared/workspace/project/node.rb', line 498 def depend? @depend != false && (!@depend.nil? || outdated?) end |
#dev? ⇒ Boolean
561 562 563 |
# File 'lib/squared/workspace/project/node.rb', line 561 def dev? !Node.prod? && super end |
#install_type(prog = nil) ⇒ Object
491 492 493 494 495 496 |
# File 'lib/squared/workspace/project/node.rb', line 491 def install_type(prog = nil) prog ||= yarn? ? :yarn : :pnpm key = :"#{prog}?" __send__(key) if respond_to?(key) @pm[prog] || 0 end |
#outdated(rev = nil, opts: []) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/squared/workspace/project/node.rb', line 265 def outdated(rev = nil, opts: []) dryrun = opts.include?('dry-run') if pnpm? && read_packagemanager(version: '7.15') cmd = 'pnpm outdated' dryrun ||= !option('dry-run', prefix: 'pnpm').nil? else cmd = 'npm outdated' dryrun ||= !option('dry-run', prefix: 'npm').nil? end log.info cmd = ("#{cmd}#{dryrun ? ' --dry-run' : ''}", multiple: true) if invoked_sync?('outdated', rev) print_item = nil end data = nil pwd_set { data = `#{cmd} --json --loglevel=error` } json = JSON.parse(doc = package.read) dep1 = json['dependencies'] || {} dep2 = json['devDependencies'] || {} found = [] avail = [] rev ||= (prod? ? :patch : :minor) inter = opts.include?('interactive') unless data.empty? JSON.parse(data).each_pair do |key, val| val = val.find { |obj| obj['dependent'] == json['name'] } if val.is_a?(::Array) next unless val && (file = dep1[key] || dep2[key]) && file != '*' latest = val['latest'] ch = file[0] if ch =~ /[~^]/ file = file[1..-1] elsif inter && rev == :major major = true else avail << [key, file, latest, true] next end want = rev == :major && (ver = latest.match(SEM_VER)) && !ver[6] ? latest : val['wanted'] next unless (val['current'] != want || file != want) && (want.match?(SEM_VER) || !file.match?(SEM_VER)) f = semver(semscan(file)) w = semver(semscan(want)) a = f[0] b = f[2] c = w[0] d = w[2] case rev when :major upgrade = a == '0' ? c == '0' : true when :minor upgrade = ch == '^' && (a == '0' ? c == '0' && b == d : a == c) when :patch upgrade = a == c && b == d && f[4] != w[4] end if upgrade && !w[5] next if file == want index = if a != c 1 elsif b != d 3 else 5 end found << [key, file, want, index, major, f, w] elsif !major avail << [key, file, latest, false] end end end pending = 0 modified = 0 size_col = ->(items, i) { items.map { |a| a[i] }.max_by(&:size).size } pad_ord = lambda do |val, ord| ret = val.succ.to_s ord.size > 9 ? ret.rjust(ord.size.to_s.size) : ret end = lambda do |val = 0| next unless verbose msg, hint = if modified == -1 ['Packages were updated', 'more possible'] else ['No packages were updated', 'possible'] end puts (empty_status(msg, hint, pending + val)) end print_item if if !found.empty? col1 = size_col.(found, 0) + 4 col2 = size_col.(found, 1) + 4 found.each_with_index do |item, i| a, b, c, d, e = item if inter && (rev != :major || e || semmajor(item[5], item[6])) && !confirm_outdated(a, c, d, e) cur = -1 else cur = modified doc.sub!(/("#{Regexp.escape(a)}"\s*:\s*)"([~^])#{e ? '?' : ''}#{Regexp.escape(b)}"/) do |capture| if $2 == '~' && rev != :patch cur = -1 pending += 1 capture else modified += 1 "#{$1}\"#{$2 || (d == 1 && e ? '^' : '')}#{c}\"" end end end a = a.ljust(col1) c = if cur == -1 'SKIP' elsif modified == cur 'FAIL' elsif d == 1 a = sub_style(a, styles: theme[:major]) sub_style(c, :bold, styles: color(:green)) else sub_style(c, pat: SEM_VER, styles: color(:green), index: d) end puts "#{pad_ord.(i, found)}. #{a}#{b.ljust(col2)}#{c}" end pending = avail.reduce(pending) { |a, b| a + (b[3] ? 0 : 1) } if dryrun || (modified == 0 && pending > 0) .(modified) elsif modified > 0 File.write(package, doc) modified = -1 .() commit(:add, 'package.json', pass: true) install if opts.include?('prune') end elsif !avail.empty? col1 = size_col.(avail, 0) + 4 col2 = size_col.(avail, 1) col3 = size_col.(avail, 2) + 4 avail.each_with_index do |item, i| a, b, c, d = item a = a.ljust(col1) b = sub_style(b.ljust(col2), styles: color(d ? :red : :yellow)) c = c.ljust(col3) unless d a = sub_style(a, styles: theme[:active]) c = sub_style(c, styles: color(:green)) pending += 1 end puts "#{pad_ord.(i, avail)}. #{a + c + b} (#{d ? 'locked' : 'latest'})" end .() else puts 'No updates were found' end end |
#outdated? ⇒ Boolean
506 507 508 |
# File 'lib/squared/workspace/project/node.rb', line 506 def outdated? package.exist? end |
#pnpm? ⇒ Boolean
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'lib/squared/workspace/project/node.rb', line 530 def pnpm? (@pm[:pnpm] ||= if base_path('pnpm-lock.yaml', ascend: find_package).exist? begin require 'yaml' doc = YAML.load_file(base_path('node_modules/.modules.yaml', ascend: find_package)) @pm[:_] = doc['packageManager'] case doc['nodeLinker'] when 'hoisted' 1 when 'pnp' 3 else 4 end rescue StandardError 4 end else (read_packagemanager || read_install)&.start_with?('pnpm') ? 4 : 0 end) > 0 end |
#populate ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/squared/workspace/project/node.rb', line 61 def populate(*) super return unless outdated? && ref?(Node.ref) namespace name do @@tasks[Node.ref].each do |action, flags| if flags.nil? case action when :run desc format_desc(action, nil, 'command+|^index|#,pattern*') task action, [:command] do |_, args| if args.command == '#' list = read_scripts grep = args.extras.map { |val| Regexp.new(val) } lines = [] if (pad = list.size) > 0 pad = pad.to_s.size list.each_with_index do |val, i| next unless grep.empty? || grep.any? { |pat| pat.match?(val[0]) } lines << "#{(i + 1).to_s.rjust(pad)}. #{val[0]}" end end if lines.empty? lines = ['No scripts were found:', ''] unless args.extras.empty? i = 0 lines += args.extras.to_a.map { |s| "#{i += 1}. #{s}" } lines << '' end lines << package.to_s pat = /^(#{Regexp.escape(lines.last)})(.*)$/ else pat = /^(\s*\d+\.)(.+)$/ end emphasize(lines, title: task_join(name, 'run[^N]'), border: borderstyle, sub: [ headerstyle, { pat: pat, styles: theme[:active] } ]) else cmd = args.to_a guard_params(action, 'command', args: cmd) list = nil cmd.each do |val| if (data = /\A\^(\d+)\z/.match(val)) list ||= read_scripts n = data[1].to_i if (item = list[n - 1]) val = item.first elsif exception raise_error("requested index #{n}", hint: "total #{list.size}") else next log.warn "run script #{n} of #{list.size} (out of range)" end end run compose(val, script: true) end end end end else namespace action do flags.each do |flag| case action when :install desc format_desc(action, flag) task flag do depend(flag) end when :outdated desc format_desc(action, flag, %w[prune interactive dry-run].freeze, arg: 'opts?') task flag, [:opts] do |_, args| outdated(flag, opts: args.to_a) end else desc format_desc(action, flag) task flag do __send__(action, flag) end end end end end end end end |
#prod? ⇒ Boolean
565 566 567 |
# File 'lib/squared/workspace/project/node.rb', line 565 def prod? @prod != false && (Node.prod? || super) end |
#ref ⇒ Object
57 58 59 |
# File 'lib/squared/workspace/project/node.rb', line 57 def ref Node.ref end |
#version ⇒ Object
486 487 488 489 |
# File 'lib/squared/workspace/project/node.rb', line 486 def version read_packagemanager @pm[:version] end |
#workspaces? ⇒ Boolean
552 553 554 555 556 557 558 559 |
# File 'lib/squared/workspace/project/node.rb', line 552 def workspaces? if pnpm? base_path('pnpm-workspace.yaml').exist? else read_packagemanager @pm[:workspaces].is_a?(::Array) end end |
#yarn? ⇒ Boolean
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/squared/workspace/project/node.rb', line 510 def yarn? (@pm[:yarn] ||= if base_path('yarn.lock', ascend: find_package).exist? if (rc = base_path('.yarnrc.yml', ascend: find_package)).exist? begin require 'yaml' doc = YAML.load_file(rc) doc.nodeLinker == 'node-modules' ? 2 : 3 rescue StandardError 3 end else 1 end elsif (ver = read_packagemanager || read_install) && ver.start_with?('yarn') ver == 'yarn' || ver.include?('@1') ? 1 : 3 else 0 end) > 0 end |