Class: Squared::Workspace::Project::Node
- Defined in:
- lib/squared/workspace/project/node.rb
Constant Summary
Constants included from Common
Instance Attribute Summary
Attributes inherited from Base
#dependfile, #exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace
Class Method Summary collapse
- .aliasargs ⇒ Object
- .bannerargs ⇒ Object
- .batchargs ⇒ Object
- .config?(val) ⇒ Boolean
- .populate ⇒ Object
- .prod? ⇒ Boolean
- .tasks ⇒ Object
Instance Method Summary collapse
- #bump(flag = nil, sync: invoked_sync?('bump', flag)) ⇒ Object
- #compose(opts, flags = nil, script: false) ⇒ Object
- #copy(from: 'build', into: 'node_modules', workspace: false, include: nil, exclude: nil, scope: nil, also: nil, create: nil, override: false) ⇒ Object
- #copy? ⇒ Boolean
- #depend(flag = nil, sync: invoked_sync?('depend', flag)) ⇒ Object
- #depend? ⇒ Boolean
- #dependtype(prog) ⇒ Object
- #dev? ⇒ Boolean
-
#initialize(**kwargs) ⇒ Node
constructor
A new instance of Node.
- #outdated(rev = nil, opts: []) ⇒ Object
- #outdated? ⇒ Boolean
- #packagename ⇒ Object
- #pnpm? ⇒ Boolean
- #populate ⇒ Object
- #prod? ⇒ Boolean
- #ref ⇒ Object
- #version ⇒ Object
- #workspaces? ⇒ Boolean
- #yarn? ⇒ Boolean
Methods inherited from Git
#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #show, #stash, #status
Methods inherited from Base
#add, #allref, as_path, #basepath, #build, #build?, #clean, #clean?, #doc, #doc?, #enabled?, #graph, #graph?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, ref, #ref?, #script?, #test, #test?, to_s, #to_s, #to_sym, #variable_set, #with
Methods included from Common::Format
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 = {} @dependfile = basepath('package.json') end |
Class Method Details
.aliasargs ⇒ Object
18 19 20 |
# File 'lib/squared/workspace/project/node.rb', line 18 def aliasargs [ref, { refresh: :build }] end |
.bannerargs ⇒ Object
22 23 24 |
# File 'lib/squared/workspace/project/node.rb', line 22 def %i[version dependfile].freeze end |
.batchargs ⇒ Object
14 15 16 |
# File 'lib/squared/workspace/project/node.rb', line 14 def batchargs [ref, { refresh: %i[build copy] }] end |
.config?(val) ⇒ Boolean
30 31 32 33 34 |
# File 'lib/squared/workspace/project/node.rb', line 30 def config?(val) return false unless (val = as_path(val)) val.join('package.json').exist? end |
.populate ⇒ Object
8 |
# File 'lib/squared/workspace/project/node.rb', line 8 def populate(*); end |
.prod? ⇒ Boolean
26 27 28 |
# File 'lib/squared/workspace/project/node.rb', line 26 def prod? ENV['NODE_ENV'] == 'production' end |
.tasks ⇒ Object
10 11 12 |
# File 'lib/squared/workspace/project/node.rb', line 10 def tasks %i[outdated].freeze end |
Instance Method Details
#bump(flag = nil, sync: invoked_sync?('bump', flag)) ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 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 |
# File 'lib/squared/workspace/project/node.rb', line 420 def bump(flag = nil, sync: invoked_sync?('bump', flag)) return unless (ver = version) seg = semscan(ver, fill: false) 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 else seg[4] &&= seg[4].succ end return if (out = seg.join) == ver begin doc = dependfile.read if doc.sub!(/"version"\s*:\s*"#{ver}"/, "\"version\": \"#{out}\"") unless dryrun? dependfile.write(doc) log.info "bump version #{ver} to #{out} (#{flag})" end 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 if sync end end |
#compose(opts, flags = nil, script: false) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/squared/workspace/project/node.rb', line 468 def compose(opts, flags = nil, script: false) return unless opts && script ret = session dependbin, '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', into: 'node_modules', workspace: false, include: nil, exclude: nil, scope: nil, also: nil, create: nil, override: false) ⇒ Object
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 147 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 120 def copy(from: 'build', into: 'node_modules', workspace: false, include: nil, exclude: nil, scope: nil, also: nil, create: nil, override: false) return if @copy == false if @copy && !override return super if runnable?(@copy) from = @copy[:from] if @copy.key?(:from) into = @copy[:into] if @copy.key?(:into) workspace = @copy[:workspace] if @copy.key?(:workspace) glob = @copy[:include] pass = @copy[:exclude] scope = @copy[:scope] also = @copy[:also] create = @copy[:create] end items = [] items << @workspace.home if build? && path != @workspace.home && @workspace.home? items += as_a(also) if also return if items.empty? print_item unless @output[0] || !verbose || task_invoked?(/^copy(?::#{Node.ref}|$)/) items.each do |dir| case dir when Pathname dest = dir when String dest = @workspace.rootpath(dir) when Symbol dest = @workspace.find(name: dir)&.path log.warn ("copy project :#{dir}", hint: 'not found') unless dest when Hash glob = dir[:include] pass = dir[:exclude] from = dir[:from] if dir.key?(:from) into = dir[:into] if dir.key?(:into) scope = dir[:scope] if dir.key?(:scope) dest = dir[:target] create = dir[:create] workspace = dir[:workspace] dest = items.first unless dest && dest != true when Project::Base dest = dir.path else raise_error("given: #{dir}", hint: 'unknown') end next unless from && dest&.directory? from = basepath(from) glob = as_a(glob || '**/*') target = [] if workspace Dir.glob(from.join('*')).each do |path| next unless (path = Pathname.new(path)).directory? sub = if (proj = @workspace.find(path)) proj.packagename elsif (file = path.join('package.json')).exist? begin doc = JSON.parse(file.read) doc['name'] rescue StandardError => e log.warn e raise if exception end end if sub target << [path, dest.join(into, sub)] else log.debug ("package.json in \"#{path}\"", hint: 'not found') end end else target << [from, dest.join(into, scope || project)] end target.each do |src, to| glob.each { |val| log.info "cp #{from.join(val)} #{to}" } copy_d(src, to, glob: glob, create: create, pass: pass, verbose: verbose) end end end |
#copy? ⇒ Boolean
488 489 490 |
# File 'lib/squared/workspace/project/node.rb', line 488 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 = dependtype(: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
484 485 486 |
# File 'lib/squared/workspace/project/node.rb', line 484 def depend? @depend != false && (!@depend.nil? || outdated?) end |
#dependtype(prog) ⇒ Object
555 556 557 558 559 560 |
# File 'lib/squared/workspace/project/node.rb', line 555 def dependtype(prog) return @pm[prog] if @pm.key?(prog) meth = :"#{prog}?" respond_to?(meth) && __send__(meth) ? @pm[prog] : 0 end |
#dev? ⇒ Boolean
547 548 549 |
# File 'lib/squared/workspace/project/node.rb', line 547 def dev? super && (!Node.prod? || (@dev == true && !prod?)) 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 ||= dryrun?('pnpm') else cmd = 'npm outdated' dryrun ||= dryrun?('npm') end log.info cmd = ("#{cmd}#{dryrun ? ' --dry-run' : ''}") if invoked_sync?('outdated', rev) print_item = nil end data = nil pwd_set { data = `#{cmd} --json --loglevel=error` } json = JSON.parse(doc = dependfile.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 = semscan(file) w = semscan(want) a = f[0] b = f[2] c = w[0] d = w[2] case rev when :major upgrade = a == '0' ? c == '0' || c == '1' : 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 modified = -1 .() File.write(dependfile, doc) 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
492 493 494 |
# File 'lib/squared/workspace/project/node.rb', line 492 def outdated? dependfile.exist? end |
#packagename ⇒ Object
567 568 569 570 |
# File 'lib/squared/workspace/project/node.rb', line 567 def packagename read_packagemanager @pm[:name] end |
#pnpm? ⇒ Boolean
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/squared/workspace/project/node.rb', line 516 def pnpm? (@pm[:pnpm] ||= if basepath('pnpm-lock.yaml', ascend: dependext).exist? begin require 'yaml' doc = YAML.load_file(basepath('node_modules/.modules.yaml', ascend: dependext)) @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 |
# 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 == '#' format_list(read_scripts, 'run[^N]', 'scripts', grep: args.extras, from: dependfile.to_s) else cmd = guard_params(action, 'command', args: args.to_a) cmd.each do |val| if (data = indexitem(val)) n, opts = data list = read_scripts if (item = list[n - 1]) val = opts ? "#{item.first} #{opts}" : item.first elsif exception indexerror n, list 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
551 552 553 |
# File 'lib/squared/workspace/project/node.rb', line 551 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
562 563 564 565 |
# File 'lib/squared/workspace/project/node.rb', line 562 def version read_packagemanager @pm[:version] end |
#workspaces? ⇒ Boolean
538 539 540 541 542 543 544 545 |
# File 'lib/squared/workspace/project/node.rb', line 538 def workspaces? if pnpm? basepath('pnpm-workspace.yaml').exist? else read_packagemanager @pm[:workspaces].is_a?(Array) end end |
#yarn? ⇒ Boolean
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/squared/workspace/project/node.rb', line 496 def yarn? (@pm[:yarn] ||= if basepath('yarn.lock', ascend: dependext).exist? if (rc = basepath('.yarnrc.yml', ascend: dependext)).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 |