Class: Squared::Repo::Project::Node
- Defined in:
- lib/squared/repo/project/node.rb
Instance Attribute Summary collapse
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Attributes inherited from Base
#group, #log, #name, #path, #project, #theme, #warning, #workspace
Class Method Summary collapse
Instance Method Summary collapse
- #build? ⇒ Boolean
- #compose(args) ⇒ Object
- #copy(from: 'build', glob: '**/*', subdir: 'node_modules', into: nil, also: [], override: false) ⇒ Object
- #copy? ⇒ Boolean
- #depend(flag = nil, override: false) ⇒ Object
- #depend? ⇒ Boolean
- #dev? ⇒ Boolean
-
#initialize(name, path, workspace, **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
- #run_script(cmd) ⇒ Object
- #yarn? ⇒ Boolean
Methods inherited from Git
#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #stash, #status
Methods inherited from Base
as_path, #base_path, #build, #clean, #clean?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_script, #inspect, #refresh, #refresh?, #test, #test?, to_s, #to_s, to_sym, #to_sym
Methods included from Common::Task
#collect_args, #invoke, #invoked?
Methods included from Common
#__get__, #__set__, #as_a, #finalize!, #message
Constructor Details
#initialize(name, path, workspace, **kwargs) ⇒ Node
Returns a new instance of Node.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/squared/repo/project/node.rb', line 38 def initialize(name, path, workspace, *, **kwargs) super initialize_script(REF) if (opts = env('BUILD', strict: true)) raise ArgumentError, ("BUILD_#{@name.upcase}", opts) if @output[0].is_a?(::Array) @output[1] = opts else @output[1] = (@script && @script[:run]) || workspace.script end @package = base_path('package.json') @dev = workspace.bool_match(env('BUILD', suffix: 'DEV'), kwargs.delete(:dev)) @prod = workspace.bool_match(env('BUILD', suffix: 'PROD'), kwargs.delete(:prod)) @pm = {} end |
Instance Attribute Details
#package ⇒ Object (readonly)
Returns the value of attribute package.
36 37 38 |
# File 'lib/squared/repo/project/node.rb', line 36 def package @package end |
Class Method Details
.is_a?(val) ⇒ Boolean
21 22 23 24 25 26 27 |
# File 'lib/squared/repo/project/node.rb', line 21 def is_a?(val) if (val = as_path(val)) val.join('package.json').exist? else super end end |
.populate ⇒ Object
11 |
# File 'lib/squared/repo/project/node.rb', line 11 def populate(*); end |
.prod? ⇒ Boolean
17 18 19 |
# File 'lib/squared/repo/project/node.rb', line 17 def prod? ENV['NODE_ENV'] == 'production' end |
.tasks ⇒ Object
13 14 15 |
# File 'lib/squared/repo/project/node.rb', line 13 def tasks nil end |
Instance Method Details
#build? ⇒ Boolean
367 368 369 |
# File 'lib/squared/repo/project/node.rb', line 367 def build? @output[0] != false && (!@output[0].nil? || !@output[1].nil?) end |
#compose(args) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/squared/repo/project/node.rb', line 341 def compose(args) args ||= @output[1] cmd = [if yarn? 'yarn' else pnpm? ? 'pnpm' : 'npm' end] cmd << 'run' append_loglevel cmd if args.is_a?(::Array) cmd += args elsif args.is_a?(::String) cmd << args else raise ArgumentError, ("#{cmd.first} script name", hint: args.nil? ? 'missing' : 'invalid') end cmd.join(' ') end |
#copy(from: 'build', glob: '**/*', subdir: 'node_modules', into: nil, also: [], override: false) ⇒ Object
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 |
# File 'lib/squared/repo/project/node.rb', line 92 def copy(from: 'build', glob: '**/*', subdir: 'node_modules', into: nil, also: [], override: false) if @copy && !override return super if @copy.is_a?(::String) 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 = [project == workspace.main ? nil : workspace.home].concat(as_a(also)) items.each_with_index do |dir, i| if i == 0 next unless dev? & !doc? dest = dir elsif dir.is_a?(::String) dest = workspace.root_path(dir) elsif dir.is_a?(::Symbol) dest = Repo.resolve(dir)&.path elsif dir.is_a?(Project) dest = dir.path elsif dir.is_a?(::Hash) into = nil glob = nil dir.each do |key, val| case key.to_sym when :from from = val when :glob glob = val when :subdir subdir = val when :into into = val end 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
375 376 377 |
# File 'lib/squared/repo/project/node.rb', line 375 def copy? !!@copy end |
#depend(flag = nil, override: false) ⇒ Object
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/repo/project/node.rb', line 140 def depend(flag = nil, override: false) if @depend && !override super elsif outdated? frozen = flag == :frozen force = flag == :force dedupe = flag == :dedupe if (yarn = install_type(:yarn)) > 0 cmd = session 'yarn' if yarn > 1 if dedupe cmd << 'dedupe' else cmd << 'install' if force cmd << '--check-cache' elsif frozen cmd << '--immutable' end end else cmd << 'install' if force cmd << '--force' elsif frozen cmd << '--frozen-lockfile' end cmd << '--production' if prod? cmd << '--ignore-engines' unless env('YARN_IGNORE_ENGINES', equals: '0') end elsif pnpm? cmd = session 'pnpm' if dedupe cmd << 'dedupe' else cmd << 'install' if force cmd << '--force' elsif frozen cmd << '--frozen-lockfile' end end cmd << '--prod' if prod? cmd << '--ignore-workspace' if env('NODE_WORKSPACES', equals: '0') append_nocolor else cmd = session 'npm', dedupe ? 'dedupe' : 'install' if force cmd << '--force' elsif frozen cmd << '--package-lock-only' end cmd << '--omit=dev' if 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(exception: workspace.exception) end end |
#depend? ⇒ Boolean
371 372 373 |
# File 'lib/squared/repo/project/node.rb', line 371 def depend? outdated? || !!@depend end |
#dev? ⇒ Boolean
427 428 429 430 431 |
# File 'lib/squared/repo/project/node.rb', line 427 def dev? return false if Node.prod? workspace.dev?(script: @output[1], pat: @dev, global: !@script || @script[:run].nil?) end |
#install_type(prog = nil) ⇒ Object
360 361 362 363 364 365 |
# File 'lib/squared/repo/project/node.rb', line 360 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
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 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 |
# File 'lib/squared/repo/project/node.rb', line 202 def outdated(rev = nil, opts: []) require 'json' rev ||= prod? ? :patch : :minor cmd = pnpm? ? 'pnpm outdated' : 'npm outdated' print_item (("#{cmd}#{opts.include?('dry-run') ? ' --dry-run' : ''}"), multiple: true) pwd = Dir.pwd Dir.chdir(path) log.info cmd data = `#{cmd} --json --loglevel=error` Dir.chdir(pwd) json = JSON.parse(doc = package.read) pat = /^(\d+)(\.)(\d+)(\.)(\d+)$/ dep1 = json['dependencies'] || {} dep2 = json['devDependencies'] || {} found = [] avail = [] if !data.empty? validate = ->(ver) { ver.match?(pat) } JSON.parse(data).each_pair do |key, val| val = val.find { |item| item['dependent'] == json['name'] } if val.is_a?(Array) next unless val && (file = dep1[key] || dep2[key]) ch = file[0] unless ch.match?(/[~^]/) avail << [key, file, val['latest'], true] next end file = file[1..-1] cur = val['current'] want = if rev == :major && validate.(val['latest']) [val['latest'], val['wanted']].max { |a, b| a <=> b } else val['wanted'] end next unless (cur != want || file != want) && (validate.(want) || !validate.(file)) a, b = file.split('.') c, d = want.split('.') upgrade = false 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 end if upgrade next if file == want index = if a != c 1 elsif b != d 3 else 5 end found << [key, file, want, index] else avail << [key, file, val['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 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 = item cur = modified doc.sub!(/("#{Regexp.escape(a)}"\s*:\s*)"([~^])#{Regexp.escape(b)}"/) do |capture| if $2 == '~' && rev != :patch cur = -1 pending += 1 capture else modified += 1 "#{$1}\"#{$2 || ''}#{c}\"" end end a = a.ljust(col1) c = if cur == -1 'SKIP' elsif modified == cur 'FAIL' elsif d == 1 a = sub_style(a, :bold) sub_style(c, :green, :bold) else sub_style(c, :green, :bold, pat: pat, 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 opts.include?('dry-run') || (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 e = sub_style(b.ljust(col2), d ? :red : :yellow) puts "#{pad_ord.(i, avail)}. #{a.ljust(col1)}#{c.ljust(col3)}#{e} (#{d ? 'locked' : 'latest'})" pending += 1 unless d end .() else puts 'No updates were found' end end |
#outdated? ⇒ Boolean
379 380 381 |
# File 'lib/squared/repo/project/node.rb', line 379 def outdated? package.exist? end |
#pnpm? ⇒ Boolean
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/squared/repo/project/node.rb', line 406 def pnpm? (@pm[:pnpm] ||= if File.exist?(base_path('pnpm-lock.yaml')) begin require 'yaml' doc = YAML.load_file(base_path('node_modules/.modules.yaml')) case doc['nodeLinker'] when 'hoisted' 1 when 'pnp' 3 else 4 end rescue StandardError 4 end else env('NODE_INSTALL')&.start_with?('pnpm') ? 4 : 0 end) > 0 end |
#populate ⇒ Object
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 89 90 |
# File 'lib/squared/repo/project/node.rb', line 54 def populate(*) super return unless outdated? namespace name do @@tasks[REF].each do |action, flags| if flags.nil? case action when :run desc format_desc(action, nil, 'command+') task action, [:command] do |_, args| command = collect_args(args, :command) guard_params(action, 'command', args: command) run_script command end end else namespace action do flags.each do |flag| case action when :install desc format_desc(action, flag) task flag do depend(flag, override: true) end when :outdated desc format_desc(action, flag, %w[prune dry-run], req: 'opts?') task flag, [:opts] do |_, args| outdated(flag, opts: collect_args(args, :opts)) end end end end end end end end |
#prod? ⇒ Boolean
433 434 435 436 437 |
# File 'lib/squared/repo/project/node.rb', line 433 def prod? return true if Node.prod? workspace.prod?(script: @output[1], pat: @prod, global: !@script || @script[:run].nil?) end |
#run_script(cmd) ⇒ Object
337 338 339 |
# File 'lib/squared/repo/project/node.rb', line 337 def run_script(cmd) cmd.each { |val| run_s compose(val) } end |
#yarn? ⇒ Boolean
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/squared/repo/project/node.rb', line 383 def yarn? (@pm[:yarn] ||= if File.exist?(base_path('yarn.lock')) if File.exist?(rc = base_path('.yarnrc.yml')) begin require 'yaml' doc = YAML.load_file(rc) doc.nodeLinker == 'node-modules' ? 2 : 3 rescue StandardError 3 end else 1 end else ver = env('NODE_INSTALL') if ver&.start_with?('yarn') ver.include?('@1') ? 1 : 3 else 0 end end) > 0 end |