Class: Squared::Workspace::Project::Node
- Defined in:
- lib/squared/workspace/project/node.rb
Constant Summary
Constants inherited from Base
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) ⇒ Object
- #compose(opts, flags = nil, script: false) ⇒ Object
- #copy(from: 'build', glob: nil, into: 'node_modules', scope: nil, also: 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
- #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, #stash, #status
Methods inherited from Base
#add, #allref, as_path, #basepath, #build, #build?, #clean, #clean?, #color, #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) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 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 |
# File 'lib/squared/workspace/project/node.rb', line 407 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 = dependfile.read if doc.sub!(/"version"\s*:\s*"#{ver}"/, "\"version\": \"#{out}\"") dependfile.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
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/squared/workspace/project/node.rb', line 387 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: nil, into: 'node_modules', scope: nil, also: 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 |
# File 'lib/squared/workspace/project/node.rb', line 120 def copy(from: 'build', glob: nil, into: 'node_modules', scope: nil, also: nil, override: false) return if @copy == 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.rootpath(dir) when ::Symbol dest = workspace.find(name: dir)&.path log.warn ("copy project :#{dir}", hint: 'not found') unless dest when ::Hash missing = ->(val) { log.warn ("copy attr :#{val}", hint: 'missing') } glob = dir[:glob] into = dir[:into] if dir.key?(:into) scope = dir[:scope] missing.('target') unless (dest = dir[:target]) missing.('from') unless (from = dir[:from]) when Project::Base dest = dir.path else raise_error("given: #{dir}", hint: 'unknown') end end next unless from && dest&.directory? from = basepath(from) dest = dest.join(into, scope || project) glob = as_a(glob || '**/*') glob.each { |val| log.info "cp #{from.join(val)} #{dest}" } copy_d(from, dest, glob: glob, verbose: verbose) end end |
#copy? ⇒ Boolean
469 470 471 |
# File 'lib/squared/workspace/project/node.rb', line 469 def copy? super || @copy.is_a?(::Hash) end |
#depend(flag = nil, sync: invoked_sync?('depend', flag)) ⇒ Object
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 201 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 |
# File 'lib/squared/workspace/project/node.rb', line 169 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
465 466 467 |
# File 'lib/squared/workspace/project/node.rb', line 465 def depend? @depend != false && (!@depend.nil? || outdated?) end |
#dependtype(prog) ⇒ Object
458 459 460 461 462 463 |
# File 'lib/squared/workspace/project/node.rb', line 458 def dependtype(prog) return @pm[prog] if @pm.key?(prog) meth = :"#{prog}?" respond_to?(meth) && __send__(meth) ? @pm[prog] : 0 end |
#dev? ⇒ Boolean
528 529 530 |
# File 'lib/squared/workspace/project/node.rb', line 528 def dev? !Node.prod? && super end |
#outdated(rev = nil, opts: []) ⇒ Object
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 264 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 |
# File 'lib/squared/workspace/project/node.rb', line 232 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' : ''}") 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 = 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' || 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 File.write(dependfile, 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
473 474 475 |
# File 'lib/squared/workspace/project/node.rb', line 473 def outdated? dependfile.exist? end |
#pnpm? ⇒ Boolean
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/squared/workspace/project/node.rb', line 497 def pnpm? (@pm[:pnpm] ||= if basepath('pnpm-lock.yaml', ascend: find_package).exist? begin require 'yaml' doc = YAML.load_file(basepath('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 |
# 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 = indexdata(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
532 533 534 |
# File 'lib/squared/workspace/project/node.rb', line 532 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
453 454 455 456 |
# File 'lib/squared/workspace/project/node.rb', line 453 def version read_packagemanager @pm[:version] end |
#workspaces? ⇒ Boolean
519 520 521 522 523 524 525 526 |
# File 'lib/squared/workspace/project/node.rb', line 519 def workspaces? if pnpm? basepath('pnpm-workspace.yaml').exist? else read_packagemanager @pm[:workspaces].is_a?(::Array) end end |
#yarn? ⇒ Boolean
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/squared/workspace/project/node.rb', line 477 def yarn? (@pm[:yarn] ||= if basepath('yarn.lock', ascend: find_package).exist? if (rc = basepath('.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 |