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 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
Instance Method Summary collapse
- #compose(opts) ⇒ Object
- #copy(from: 'build', glob: '**/*', subdir: 'node_modules', into: nil, also: nil, override: false) ⇒ Object
- #copy? ⇒ Boolean
- #depend(flag = nil) ⇒ Object
- #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
- #yarn? ⇒ Boolean
Methods inherited from Git
#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #stash, #status
Methods inherited from Base
#add, as_path, #base_path, #build, #build?, #clean, #clean?, #color, #depend?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, ref, #ref?, #refresh, #refresh?, #script?, #test, #test?, to_s, #to_s, #to_sym, #variable_all, #variable_set
Constructor Details
#initialize(**kwargs) ⇒ Node
Returns a new instance of Node.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/squared/workspace/project/node.rb', line 35 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.
33 34 35 |
# File 'lib/squared/workspace/project/node.rb', line 33 def package @package end |
Class Method Details
.is_a?(val) ⇒ Boolean
18 19 20 21 22 23 24 |
# File 'lib/squared/workspace/project/node.rb', line 18 def is_a?(val) if (val = as_path(val)) val.join('package.json').exist? else super end end |
.populate ⇒ Object
8 |
# File 'lib/squared/workspace/project/node.rb', line 8 def populate(*); end |
.prod? ⇒ Boolean
14 15 16 |
# File 'lib/squared/workspace/project/node.rb', line 14 def prod? ENV['NODE_ENV'] == 'production' end |
.tasks ⇒ Object
10 11 12 |
# File 'lib/squared/workspace/project/node.rb', line 10 def tasks nil end |
Instance Method Details
#compose(opts) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/squared/workspace/project/node.rb', line 357 def compose(opts) ret = session (if yarn? 'yarn' else pnpm? ? 'pnpm' : 'npm' end), 'run' 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: '**/*', subdir: 'node_modules', into: nil, also: nil, override: false) ⇒ Object
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 |
# File 'lib/squared/workspace/project/node.rb', line 90 def copy(from: 'build', glob: '**/*', subdir: 'node_modules', into: 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) subdir = @copy[:subdir] if @copy.key?(:subdir) into = @copy[:into] if @copy.key?(:into) also = @copy[:also] if @copy.key?(:also) end items = [path == workspace.home ? nil : workspace.home] 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 into = nil dir.each do |key, val| case key.to_sym when :target dest = val when :from from = val when :glob glob = val when :subdir subdir = val when :into into = val end end when Project::Base dest = dir.path end next unless into end next unless dest&.directory? from = base_path(from) dest = dest.join(subdir, into || project) log.warn "cp #{from.join(glob)} #{dest}" copy_d(from, dest, glob: glob, verbose: verbose) end end |
#copy? ⇒ Boolean
382 383 384 |
# File 'lib/squared/workspace/project/node.rb', line 382 def copy? super || @copy.is_a?(::Hash) end |
#depend(flag = nil) ⇒ Object
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 201 202 203 204 205 206 |
# File 'lib/squared/workspace/project/node.rb', line 145 def depend(flag = nil) 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 env('YARN_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 = env('PNPM_PUBLIC_HOIST_PATTERN')) 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 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 env('NPM_PACKAGE_LOCK', equals: '0') append_nocolor end append_loglevel run(sync: invoked_sync?('depend', flag)) end end |
#dev? ⇒ Boolean
432 433 434 |
# File 'lib/squared/workspace/project/node.rb', line 432 def dev? !Node.prod? && super end |
#install_type(prog = nil) ⇒ Object
375 376 377 378 379 380 |
# File 'lib/squared/workspace/project/node.rb', line 375 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
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 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 |
# File 'lib/squared/workspace/project/node.rb', line 208 def outdated(rev = nil, opts: []) dryrun = opts.include?('dry-run') || !env('NODE_DRY_RUN').nil? cmd = pnpm? && read_packagemanager(ver: '7.15') ? 'pnpm outdated' : 'npm outdated' 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]) 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 && latest.match(SEM_VER) && !Regexp.last_match(6) ? latest : val['wanted'] next unless (val['current'] != want || file != want) && (want.match?(SEM_VER) || !file.match?(SEM_VER)) f = semver(file.scan(SEM_VER)).first w = semver(want.scan(SEM_VER)).first 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) 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
386 387 388 |
# File 'lib/squared/workspace/project/node.rb', line 386 def outdated? package.exist? end |
#pnpm? ⇒ Boolean
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/squared/workspace/project/node.rb', line 410 def pnpm? (@pm[:pnpm] ||= if base_path('pnpm-lock.yaml').exist? begin require 'yaml' doc = YAML.load_file(base_path('node_modules/.modules.yaml')) @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
52 53 54 55 56 57 58 59 60 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 |
# File 'lib/squared/workspace/project/node.rb', line 52 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+') task action, [:command] do |_, args| cmd = args.to_a guard_params(action, 'command', args: cmd) cmd.each { |val| run_s compose(val) } 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], arg: 'opts?') task flag, [:opts] do |_, args| outdated(flag, opts: args.to_a) end end end end end end end end |
#prod? ⇒ Boolean
436 437 438 |
# File 'lib/squared/workspace/project/node.rb', line 436 def prod? @prod != false && (Node.prod? || super) end |
#ref ⇒ Object
48 49 50 |
# File 'lib/squared/workspace/project/node.rb', line 48 def ref Node.ref end |
#yarn? ⇒ Boolean
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/squared/workspace/project/node.rb', line 390 def yarn? (@pm[:yarn] ||= if base_path('yarn.lock').exist? if (rc = base_path('.yarnrc.yml')).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 |