squared 0.6.15 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ module Squared
5
5
  module Project
6
6
  class Node < Git
7
7
  OPT_NPM = {
8
- common: %w[dry-run=!? force=!? loglevel=b include-workspace-root=!? workspaces=!? w|workspace=v].freeze,
8
+ common: %w[dry-run=!? loglevel=b include-workspace-root=!? workspaces=!? w|workspace=v].freeze,
9
9
  install: %w[package-lock-only=!? prefer-dedupe=!? E|save-exact=!? before=q cpu=b libc=b os=b].freeze,
10
10
  install_a: %w[audit=! bin-links=! foreground-scripts=!? fund=! ignore-scripts=!? install-links=!?
11
11
  package-lock=! strict-peer-deps=!? include=b install-strategy=b omit=b].freeze,
@@ -149,11 +149,11 @@ module Squared
149
149
  'pack' => nil
150
150
  })
151
151
 
152
- def initialize(*, init: nil, ts: 'tsconfig.json', asdf: 'nodejs', **kwargs)
152
+ def initialize(*, ts: 'tsconfig.json', asdf: 'nodejs', **kwargs)
153
153
  super
154
154
  if @pass.include?(Node.ref)
155
155
  initialize_ref Node.ref
156
- initialize_logger(**kwargs)
156
+ initialize_logger kwargs[:log]
157
157
  else
158
158
  initialize_build(Node.ref, prod: prod?, **kwargs)
159
159
  initialize_env(**kwargs)
@@ -161,7 +161,7 @@ module Squared
161
161
  @dependname = 'package.json'
162
162
  dependfile_set [@dependname]
163
163
  @tsfile = basepath! ts
164
- @pm = { __: init }
164
+ @pm = { __: kwargs[:init] }
165
165
  end
166
166
 
167
167
  def ref
@@ -181,7 +181,7 @@ module Squared
181
181
  when 'add'
182
182
  format_desc action, nil, 'save?=[=-]prod|dev|optional|peer|bundle,(-)name+'
183
183
  task action, [:save] do |_, args|
184
- packages = if args.save =~ /^([=-]*)(prod|dev|optional|peer|bundle)$/
184
+ packages = if args.save =~ /\A([=-]*)?(prod|dev|optional|peer|bundle)\z/
185
185
  save = [$2, $1.include?('='), $1.include?('-')]
186
186
  args.extras
187
187
  else
@@ -201,23 +201,24 @@ module Squared
201
201
  format_list(list, "run[#{indexchar}N]", 'scripts', grep: args.extras, from: dependfile)
202
202
  else
203
203
  args = param_guard(action, 'script', args: args.to_a)
204
- opts = []
205
- args.each do |val|
204
+ opts = args.each_with_object([]) do |val, out|
206
205
  if (n, extra = indexitem(val))
207
206
  if (item = list[n.pred])
208
207
  run compose([item.first, extra].compact.join(' '), script: true)
209
208
  elsif exception
210
209
  indexerror n, list
211
210
  else
212
- next log.warn "run script #{n} of #{list.size} (out of range)"
211
+ log.warn "run script #{n} of #{list.size}".subhint('out of range')
212
+ next
213
213
  end
214
214
  else
215
- opts << val
215
+ out << val
216
216
  end
217
217
  end
218
218
  next if opts.empty?
219
219
 
220
- list = if (yarn = dependtype(:yarn)) > 0
220
+ yarn = dependtype(:yarn)
221
+ list = if yarn > 0
221
222
  yarn == 1 ? OPT_YARN[:run] + OPT_YARN[:common] : OPT_BERRY[:run]
222
223
  elsif pnpm?
223
224
  pnpmopts :run, :common_filter
@@ -260,14 +261,20 @@ module Squared
260
261
  run(from: :exec)
261
262
  end
262
263
  when 'nvm'
263
- next unless ENV['NVM_DIR']
264
+ next unless ENV['NVM_DIR'] && !workspace.windows?
264
265
 
265
- format_desc action, nil, 'version,args*'
266
+ format_desc action, nil, 'version?,args*'
266
267
  task action, [:version] do |_, args|
267
- version = param_guard(action, 'version', args: args, key: :version)
268
+ path = ->(s) { File.join(ENV['NVM_DIR'], s) }
269
+ unless (version = args.version)
270
+ cmd = ". #{path.call('nvm.sh')} && nvm list --no-colors --no-alias"
271
+ version = pwd_set(from: :nvm) do
272
+ choice_index('Select a version', IO.popen(cmd).map { |line| line[/\s(v\S+)/, 1] }, series: true)
273
+ end
274
+ end
268
275
  args = args.extras
269
276
  args << readline('Enter command', force: true) if args.empty?
270
- args.unshift(File.join(ENV['NVM_DIR'], 'nvm-exec'))
277
+ args.unshift(path.call('nvm-exec'))
271
278
  run(args.join(' '), { 'NODE_VERSION' => version }, banner: false, from: :nvm)
272
279
  end
273
280
  when 'pack'
@@ -423,11 +430,12 @@ module Squared
423
430
  begin
424
431
  unless packed
425
432
  require 'open3'
426
- files = pwd_set do
427
- Open3.capture2e(session_output('npm', 'pack --dry-run --no-color', npmname).to_s)
433
+ cmd = session_output 'npm', 'pack --dry-run --no-color', npmname
434
+ files = pwd_set(cmd, from: :'npm:pack') do
435
+ Open3.capture2e(cmd.to_s)
428
436
  .first
429
437
  .scan(/^npm notice \d+(?:\.\d+)?[a-z]+ (.+)$/i)
430
- .map! { |item| Pathname.new(item.first) }
438
+ .map { |item| Pathname.new(item.first) }
431
439
  .select(&:exist?)
432
440
  end.concat(Array(files))
433
441
  packed = true
@@ -435,21 +443,21 @@ module Squared
435
443
  base = dest.join(into, npmname)
436
444
  base.mkpath
437
445
  log.info "cp npm:#{npmname} #{base}"
438
- subdir = []
439
446
  errors = 0
440
- files.each do |file|
441
- s, d = file.is_a?(Array) ? file : [file, file]
447
+ subdir = files.each_with_object([]) do |file, out|
448
+ s, d = file.is_a?(Array) ? file : [file] * 2
442
449
  dest = base + d
443
- unless subdir.include?((target = dest.dirname).to_s)
450
+ target = dest.dirname
451
+ unless out.include?(dirname = target.to_s)
444
452
  target.mkpath
445
- subdir << target.to_s
453
+ out << dirname
446
454
  end
447
455
  FileUtils.cp(basepath(s), dest, verbose: !silent?)
448
- rescue StandardError => e
449
- print_error e
456
+ rescue => e
457
+ print_error(Logger::ERROR, e, subject: name)
450
458
  errors += 1
451
459
  end
452
- rescue StandardError => e
460
+ rescue => e
453
461
  on_error e, :copy
454
462
  else
455
463
  puts message(base, subdir.size, files.size - errors) unless silent?
@@ -469,9 +477,8 @@ module Squared
469
477
  begin
470
478
  doc = JSON.parse(file.read)
471
479
  doc['name']
472
- rescue StandardError => e
473
- log.error e
474
- raise if exception
480
+ rescue => e
481
+ print_error(Logger::ERROR, e, subject: proj.name)
475
482
  end
476
483
  end
477
484
  if sub
@@ -486,8 +493,8 @@ module Squared
486
493
  target.each do |src, to|
487
494
  glob.each { |val| log.info "cp #{from + val} #{to}" }
488
495
  copy_dir(src, to, glob, create: create, link: link, force: force, pass: pass, verbose: !silent?)
489
- rescue StandardError => e
490
- on_error e, :copy
496
+ rescue => e
497
+ on_error e
491
498
  end
492
499
  end
493
500
  on :last, :copy
@@ -499,7 +506,7 @@ module Squared
499
506
  super
500
507
  elsif outdated?
501
508
  workspace.rev_clear(name, sync: sync)
502
- return update if !flag && env('NODE_UPDATE')
509
+ return update if !flag && (env('UPDATE') || env('NODE_UPDATE'))
503
510
 
504
511
  add = flag == :add
505
512
  if add
@@ -526,39 +533,35 @@ module Squared
526
533
  rm = lambda do |target|
527
534
  return if remove.empty?
528
535
 
529
- run(target.temp(*remove).sub!(/ (?:add|install) /, ' remove '), from: :remove, sync: sync)
536
+ run(target.temp(*remove).sub!(/ (?:add|install) /, ' remove '), sync: sync, from: :remove)
530
537
  end
531
- if (yarn = dependtype(:yarn)) > 0
532
- if !flag && yarn > 1 && prod?
533
- cmd = session 'yarn', 'workspaces focus --all --production'
538
+ yarn = dependtype(:yarn)
539
+ if yarn > 0
540
+ cmd = session('yarn', flag || 'install')
541
+ append_loglevel
542
+ if yarn == 1
543
+ cmd << '--ignore-engines' if option('ignore-engines')
544
+ cmd << '--ignore-scripts' if option('ignore-scripts')
545
+ cmd << '--force' if option('force')
534
546
  else
535
- cmd = session('yarn', flag || 'install')
547
+ cmd << '--mode=skip-build' if option('ignore-scripts')
548
+ cmd << '--check-cache' if !flag && option('force')
549
+ end
550
+ if nolockfile?('yarn')
551
+ cmd << '--no-lockfile'
552
+ elsif option('ci')
536
553
  if yarn == 1
537
- cmd << '--production' if prod?
538
- cmd << '--ignore-engines' unless option('ignore-engines', equals: '0')
539
- cmd << '--ignore-scripts' if option('ignore-scripts')
540
- cmd << '--force' if option('force')
541
- else
542
- cmd << '--mode=skip-build' if option('ignore-scripts')
543
- cmd << '--check-cache' if !flag && option('force')
544
- end
545
- if nolockfile?('yarn')
546
- cmd << '--no-lockfile'
547
- elsif option('ci')
548
- if yarn == 1
549
- cmd << '--frozen-lockfile'
550
- elsif !flag
551
- cmd << '--immutable' << '--refresh-lockfile'
552
- end
553
- end
554
- if add
555
- cmd << '-W' if yarn == 1 && !option('w', 'ignore-workspace-root-check', equals: '0')
556
- rm.call(cmd)
557
- om.call(cmd)
558
- cmd << '--exact' if exact
554
+ cmd << '--frozen-lockfile'
555
+ elsif !flag
556
+ cmd << '--immutable' << '--refresh-lockfile'
559
557
  end
560
558
  end
561
- append_loglevel
559
+ if add
560
+ cmd << '-W' if yarn == 1 && option('w', 'ignore-workspace-root-check', notequals: '0')
561
+ rm.call(cmd)
562
+ om.call(cmd)
563
+ cmd << '--exact' if exact
564
+ end
562
565
  elsif pnpm?
563
566
  cmd = session('pnpm', flag || 'install')
564
567
  append_nocolor
@@ -569,7 +572,6 @@ module Squared
569
572
  cmd << '--save-exact' if exact
570
573
  option('allow-build') { |val| cmd << quote_option('allow-build', val) }
571
574
  else
572
- cmd << '--prod' if prod?
573
575
  append_platform
574
576
  end
575
577
  option('public-hoist-pattern') do |val|
@@ -588,28 +590,12 @@ module Squared
588
590
  else
589
591
  cmd = session 'npm'
590
592
  cmd << (ci = option('ci') ? 'ci' : 'install')
591
- option('approve-scripts') do |val|
592
- cmd = npm_output 'approve-scripts'
593
- cmd << case val
594
- when '1'
595
- '--all --no-allow-scripts-pin'
596
- when 'true', 'all'
597
- '--all'
598
- else
599
- val.split(/\s+/).quote!.join(' ')
600
- end
601
- print_run cmd, silent?
602
- run(cmd, banner: false)
603
- end
604
593
  cmd << '--workspaces=false' if ws
605
- cmd << '--force' if option('force')
606
594
  append_nocolor
607
595
  append_loglevel
608
596
  if omit
609
597
  cmd << "--omit=#{save || omit}"
610
598
  save = nil
611
- elsif !add && prod?
612
- cmd << '--include=prod'
613
599
  end
614
600
  unless ci
615
601
  if add
@@ -628,7 +614,7 @@ module Squared
628
614
 
629
615
  cmd.merge(packages.quote!)
630
616
  end
631
- run(from: flag || :depend, sync: sync)
617
+ run(sync: sync, from: flag || :depend)
632
618
  end
633
619
  end
634
620
 
@@ -642,14 +628,14 @@ module Squared
642
628
  banner = format_banner(cmd.temp(('--dry-run' if dryrun)))
643
629
  print_item banner if sync
644
630
  begin
645
- data = pwd_set(dryrun: dryrun) { `#{cmd.temp('--json --loglevel=error')}` }
631
+ data = pwd_set(cmd, dryrun: dryrun) { `#{cmd.temp('--json --loglevel=error')}` }
646
632
  doc = dependfile.read
647
633
  json = JSON.parse(doc)
648
634
  dep1 = json['dependencies'] || {}
649
635
  dep2 = json['devDependencies'] || {}
650
636
  target = json['name']
651
- rescue StandardError => e
652
- on_error(e, :outdated, dryrun: dryrun)
637
+ rescue => e
638
+ on_error(e, dryrun: dryrun)
653
639
  return
654
640
  end
655
641
  found = []
@@ -714,7 +700,7 @@ module Squared
714
700
  when :patch
715
701
  a == c && b == d && f[4] != w[4]
716
702
  end
717
- if upgrade && !w[6]
703
+ if upgrade && !w[5]
718
704
  next if file == want
719
705
 
720
706
  found << [key, file, want, if a != c
@@ -870,22 +856,17 @@ module Squared
870
856
  package(:update, from: :update)
871
857
  end
872
858
 
873
- def publish(flag = nil, *, sync: invoked_sync?('publish', flag), otp: nil, tag: nil, access: nil, dryrun: nil,
874
- workspace: false)
859
+ def publish(flag = nil, *, sync: invoked_sync?('publish', flag), otp: nil, tag: nil, access: nil, dryrun: nil)
875
860
  if read_package('private')
876
- return if workspace
877
-
878
861
  ws = children.select { |proj| proj.ref?(Node.ref) }
879
862
  if ws.empty?
880
863
  print_error('nothing to publish', subject: name, hint: 'private')
881
864
  elsif confirm_basic('Publish workspace?', ws.map(&:name).join(', '), 'N')
882
- ws.each do |proj|
883
- proj.publish(flag, sync: sync, otp: otp, tag: tag, access: access, dryrun: dryrun, workspace: true)
884
- end
865
+ ws.each { |proj| proj.publish(flag, sync: sync, otp: otp, tag: tag, access: access, dryrun: dryrun) }
885
866
  end
886
867
  return
887
868
  end
888
- return print_error('version not found', subject: name, hint: dependname) unless version
869
+ return print_error("version: #{dependname}", subject: name, hint: 'not found') unless version
889
870
 
890
871
  cmd = session 'npm', 'publish'
891
872
  cmd << basic_option('otp', otp) if otp ||= option('otp')
@@ -908,8 +889,9 @@ module Squared
908
889
  else
909
890
  require 'open3'
910
891
  on :first, from
911
- pwd_set(from: from, dryrun: dryrun) do
912
- Open3.popen2e(cmd = session_done(cmd)) do |_, out|
892
+ pwd_set(cmd, dryrun: dryrun) do
893
+ cmd = session_done cmd
894
+ Open3.popen2e(cmd) do |_, out|
913
895
  write_lines(out, banner: format_banner(cmd),
914
896
  sub: npmnotice(opt_style(color(:bright_blue), /^(.+)(Tarball .+)$/, 2)))
915
897
  end
@@ -954,7 +936,7 @@ module Squared
954
936
  end, cmd, project: self)
955
937
  if yarn == 1 && flag != :reinstall
956
938
  op << '--no-lockfile' if nolockfile?('yarn')
957
- op << '--ignore-engines' unless option('ignore-engines', equals: '0')
939
+ op << '--ignore-engines' if option('ignore-engines')
958
940
  end
959
941
  else
960
942
  args = if pnpm?
@@ -969,22 +951,18 @@ module Squared
969
951
  flag = :install
970
952
  end
971
953
  flags = [flag]
972
- unless flag == :rebuild
973
- flags << :install_a
974
- no = OPT_PNPM[:no][flag]
954
+ flags << :install_a unless flag == :rebuild
955
+ unless flag == :dedupe
956
+ flags << :common_filter
957
+ unless flag == :add
958
+ flags << :install_b
959
+ flags << :common_cpu unless flag == :update
960
+ end
975
961
  end
962
+ no = OPT_PNPM[:no][flag]
976
963
  [
977
964
  opts,
978
- flags.yield_self do |out|
979
- unless flag == :dedupe
980
- out << :common_filter
981
- unless flag == :add
982
- out << :install_b
983
- out << :common_cpu unless flag == :update
984
- end
985
- end
986
- pnpmopts(*out)
987
- end,
965
+ pnpmopts(*flags),
988
966
  session('pnpm', flag)
989
967
  ]
990
968
  else
@@ -1006,12 +984,12 @@ module Squared
1006
984
  unless flag == :dedupe
1007
985
  %w[save ignore-scripts strict-peer-deps].each do |key|
1008
986
  option(key, prefix: 'npm', ignore: false) do |val|
1009
- opts << case val
1010
- when '0', 'false'
1011
- "#{key}=false"
1012
- else
1013
- "#{key}=true"
1014
- end
987
+ opts << basic_option(key, case val
988
+ when '0', 'false'
989
+ false
990
+ else
991
+ true
992
+ end)
1015
993
  end
1016
994
  end
1017
995
  flags << :install_b
@@ -1043,15 +1021,14 @@ module Squared
1043
1021
  op.found << (spec == 2 && (n = opt.index('@')) ? opt[0, n] : opt)
1044
1022
  end
1045
1023
  end
1046
- op.swap
1047
- .concat(packages)
1024
+ op.swap.concat(packages)
1048
1025
  raise_error ArgumentError, 'no packages to add' if op.empty? && spec == 1
1049
1026
  op.append(quote: true)
1050
1027
  .clear(errors: true)
1051
1028
  else
1052
1029
  op.clear
1053
1030
  end
1054
- run(from: from || :"package:#{flag}")
1031
+ run(from: from || symjoin('package', flag))
1055
1032
  end
1056
1033
 
1057
1034
  def bump(flag, val = nil)
@@ -1081,8 +1058,8 @@ module Squared
1081
1058
  else
1082
1059
  raise_error 'version not found', hint: dependfile
1083
1060
  end
1084
- rescue StandardError => e
1085
- on_error(e, :bump, dryrun: dryrun?)
1061
+ rescue => e
1062
+ on_error(e, dryrun: dryrun?)
1086
1063
  end
1087
1064
 
1088
1065
  def pack(opts = [])
@@ -1142,11 +1119,9 @@ module Squared
1142
1119
  print_run(cmd, banner, **kwargs)
1143
1120
  session 'npx', cmd
1144
1121
  start = time_epoch if kwargs.fetch(:verbose, verbose? && !stdin?)
1145
- run(sync: sync, banner: banner, exception: kwargs.fetch(:exception, exception), from: from).tap do |ret|
1146
- next unless success?(ret, banner, start.nil?) && start
1147
-
1148
- print_status(name, subject: 'tsc', start: start, from: :completed)
1149
- end
1122
+ ret = run(sync: sync, banner: banner, exception: kwargs.fetch(:exception, exception?), from: from)
1123
+ print_status(name, subject: 'tsc', start: start) if success?(ret, banner, start.nil?) && start
1124
+ ret
1150
1125
  end
1151
1126
 
1152
1127
  def compose(target, opts = nil, script: false, args: nil, from: nil, **)
@@ -1265,12 +1240,8 @@ module Squared
1265
1240
 
1266
1241
  private
1267
1242
 
1268
- def npm_output(*cmd, **kwargs)
1269
- session_output('npm', *cmd, **kwargs)
1270
- end
1271
-
1272
1243
  def read_package(key = 'packageManager', update: false)
1273
- if !@pm.key?(key) || update
1244
+ unless !update && @pm.key?(key)
1274
1245
  doc = JSON.parse(dependfile.read)
1275
1246
  @pm[key] = case key
1276
1247
  when 'packageManager'
@@ -1287,7 +1258,7 @@ module Squared
1287
1258
  @pm[:_] = true
1288
1259
  end
1289
1260
  end
1290
- rescue StandardError => e
1261
+ rescue => e
1291
1262
  log.debug e
1292
1263
  @pm[key] = nil
1293
1264
  else
@@ -1330,7 +1301,7 @@ module Squared
1330
1301
  else
1331
1302
  1
1332
1303
  end
1333
- rescue StandardError => e
1304
+ rescue => e
1334
1305
  return 0 if exist
1335
1306
 
1336
1307
  log.debug e
@@ -1349,7 +1320,7 @@ module Squared
1349
1320
  else
1350
1321
  4
1351
1322
  end
1352
- rescue StandardError => e
1323
+ rescue => e
1353
1324
  if exist
1354
1325
  %w[pnpm-lock.yaml pnpm-workspace.yaml].any? { |val| exist?(val) } ? 4 : 0
1355
1326
  else
@@ -1360,14 +1331,14 @@ module Squared
1360
1331
 
1361
1332
  def remove_modules(prefix = dependbin)
1362
1333
  modules = basepath 'node_modules'
1363
- return false unless modules.directory? && confirm_basic('Remove?', modules, prefix: prefix)
1334
+ return false unless modules.directory? && (option('y', prefix: prefix) || confirm_basic('Remove?', modules))
1364
1335
 
1365
1336
  modules.rmtree
1366
1337
  rescue Timeout::Error => e
1367
1338
  puts
1368
1339
  print_error(e, hint: modules, pass: true)
1369
1340
  exit 1
1370
- rescue StandardError => e
1341
+ rescue => e
1371
1342
  print_error(e, pass: true)
1372
1343
  false
1373
1344
  else
@@ -1448,23 +1419,23 @@ module Squared
1448
1419
  end
1449
1420
 
1450
1421
  def lockfile(delete = false)
1451
- file = basepath(if yarn?
1452
- 'yarn.lock'
1453
- else
1454
- pnpm? ? 'pnpm-lock.yaml' : 'package-lock.json'
1455
- end)
1456
- if file.exist?
1422
+ ret = basepath(if yarn?
1423
+ 'yarn.lock'
1424
+ else
1425
+ pnpm? ? 'pnpm-lock.yaml' : 'package-lock.json'
1426
+ end)
1427
+ if ret.exist?
1457
1428
  if delete
1458
- file.delete
1429
+ ret.delete
1459
1430
  return
1460
1431
  end
1461
- file
1462
- elsif (file = rootpath(file.basename, ascend: dependroot)).exist?
1463
- file
1432
+ ret
1433
+ elsif (ret = rootpath(ret.basename, ascend: dependroot)).exist?
1434
+ ret
1464
1435
  end
1465
- rescue StandardError => e
1436
+ rescue => e
1466
1437
  log.debug e
1467
- file
1438
+ ret
1468
1439
  end
1469
1440
 
1470
1441
  def nolockfile?(prefix = dependbin)