yarn-version 8.6.0 → 8.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.
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable complexity */
2
2
 
3
3
  import { Option, program } from "commander";
4
- import { ConsoleHandler, Level, Logger, addConfig } from "nightingale";
4
+ import { LoggerCLI } from "nightingale";
5
5
  import { satisfies } from "semver";
6
6
  import { UsageError } from "../utils/UsageError.ts";
7
7
  import type { BumpType } from "../utils/bumpTypeUtils.ts";
@@ -111,20 +111,8 @@ const todayInYYYYMMDD = (): string => {
111
111
 
112
112
  export const versionCommandAction = async (
113
113
  options: VersionCommandOptions,
114
- { nightingaleHandler = new ConsoleHandler(Level.INFO) } = {},
114
+ { logger = new LoggerCLI("yarn-version", { json: options.json }) } = {},
115
115
  ): Promise<void> => {
116
- // todo nightingale-cli
117
- if (options.json) {
118
- process.env.NIGHTINGALE_CONSOLE_FORMATTER = "json";
119
- }
120
-
121
- addConfig({
122
- pattern: /^yarn-version/,
123
- handler: nightingaleHandler,
124
- });
125
-
126
- const logger = new Logger("yarn-version");
127
-
128
116
  const rootWorkspace = await (options.cwdIsRoot
129
117
  ? createWorkspace(options.cwd)
130
118
  : findRootWorkspace(options.cwd));
@@ -192,50 +180,51 @@ export const versionCommandAction = async (
192
180
  const changedWorkspaces = new Map<Workspace, ChangedWorkspace>();
193
181
  const dependenciesMap = isMonorepo ? buildDependenciesMaps(project) : null;
194
182
 
195
- // todo logger.group
196
- logger.info(
183
+ const bumpableWorkspaces: BumpableWorkspace[] = [];
184
+
185
+ logger.group(
197
186
  options.force
198
187
  ? "Check all workspaces (force option)"
199
188
  : "Finding changed workspaces",
200
- );
189
+ () => {
190
+ // check workspaces and create bumpableWorkspaces
201
191
 
202
- // check workspaces and create bumpableWorkspaces
203
-
204
- const bumpableWorkspaces: BumpableWorkspace[] = [];
205
- for (const workspace of workspaces) {
206
- const workspaceName = getWorkspaceName(workspace);
207
- const isRoot = workspace === rootWorkspace;
208
- if (isRoot && isMonorepo && isMonorepoVersionIndependent) continue;
192
+ for (const workspace of workspaces) {
193
+ const workspaceName = getWorkspaceName(workspace);
194
+ const isRoot = workspace === rootWorkspace;
195
+ if (isRoot && isMonorepo && isMonorepoVersionIndependent) continue;
209
196
 
210
- const version = workspace.pkg.version;
197
+ const version = workspace.pkg.version;
211
198
 
212
- if (!version || version === "0.0.0") {
213
- if (
214
- (isRoot || isMonorepoVersionIndependent) &&
215
- (!isMonorepo || !isMonorepoVersionIndependent)
216
- ) {
217
- throw new UsageError(
218
- 'package.json has no version in its manifest. For the first release, set to "1.0.0-pre" or "0.1.0-pre".',
219
- );
220
- }
199
+ if (!version || version === "0.0.0") {
200
+ if (
201
+ (isRoot || isMonorepoVersionIndependent) &&
202
+ (!isMonorepo || !isMonorepoVersionIndependent)
203
+ ) {
204
+ throw new UsageError(
205
+ 'package.json has no version in its manifest. For the first release, set to "1.0.0-pre" or "0.1.0-pre".',
206
+ );
207
+ }
221
208
 
222
- logger.info(`${workspaceName}: skipped (no version)`);
223
- continue;
224
- }
209
+ logger.info(`${workspaceName}: skipped (no version)`);
210
+ continue;
211
+ }
225
212
 
226
- bumpableWorkspaces.push({
227
- workspace,
228
- workspaceName,
229
- isRoot,
230
- version,
231
- });
232
- }
213
+ bumpableWorkspaces.push({
214
+ workspace,
215
+ workspaceName,
216
+ isRoot,
217
+ version,
218
+ });
219
+ }
233
220
 
234
- if (bumpableWorkspaces.length > 0) {
235
- logger.info("Found bumpable workspaces", {
236
- count: bumpableWorkspaces.length,
237
- });
238
- }
221
+ if (bumpableWorkspaces.length > 0) {
222
+ logger.info("Found bumpable workspaces", {
223
+ count: bumpableWorkspaces.length,
224
+ });
225
+ }
226
+ },
227
+ );
239
228
 
240
229
  const previousTagVersionByWorkspace = new Map<
241
230
  Workspace,
@@ -303,61 +292,58 @@ export const versionCommandAction = async (
303
292
  ),
304
293
  );
305
294
 
306
- // find changed workspaces
307
-
308
- for (const { workspace, workspaceName } of bumpableWorkspaces) {
309
- let bumpType: BumpType | null = null;
310
- let bumpReason: string | undefined;
295
+ await logger.group("Finding changed workspaces", async () => {
296
+ for (const { workspace, workspaceName } of bumpableWorkspaces) {
297
+ let bumpType: BumpType | null = null;
298
+ let bumpReason: string | undefined;
311
299
 
312
- if (options.force) {
313
- bumpType = options.force;
314
- bumpReason = "forced by --force flag";
315
- } else {
316
- const commits = commitsByWorkspace?.get(workspace);
300
+ if (options.force) {
301
+ bumpType = options.force;
302
+ bumpReason = "forced by --force flag";
303
+ } else {
304
+ const commits = commitsByWorkspace?.get(workspace);
317
305
 
318
- // No changes found for this package
319
- if (!commits || commits.length === 0) {
320
- logger.info(`${workspaceName}: skipped (no changes)`);
321
- continue;
322
- }
306
+ // No changes found for this package
307
+ if (!commits || commits.length === 0) {
308
+ logger.info(`${workspaceName}: skipped (no changes)`);
309
+ continue;
310
+ }
323
311
 
324
- const { releaseType, reason } = await recommendBump(
325
- commits,
326
- conventionalCommitConfig,
327
- );
328
- bumpReason = reason;
312
+ const { releaseType, reason } = await recommendBump(
313
+ commits,
314
+ conventionalCommitConfig,
315
+ );
316
+ bumpReason = reason;
329
317
 
330
- if (releaseType) {
331
- bumpType = releaseType as BumpType;
318
+ if (releaseType) {
319
+ bumpType = releaseType as BumpType;
320
+ }
332
321
  }
333
- }
334
322
 
335
- if (bumpType) {
336
- if (isMonorepo && !workspace.pkg.name) {
337
- throw new Error("Workspace name is required");
338
- }
323
+ if (bumpType) {
324
+ if (isMonorepo && !workspace.pkg.name) {
325
+ throw new Error("Workspace name is required");
326
+ }
339
327
 
340
- const currentVersion = workspace.pkg.version;
328
+ const currentVersion = workspace.pkg.version;
341
329
 
342
- if (!currentVersion) {
343
- throw new UsageError(
344
- `Invalid "${getWorkspaceName(workspace)}" version`,
345
- );
346
- }
330
+ if (!currentVersion) {
331
+ throw new UsageError(
332
+ `Invalid "${getWorkspaceName(workspace)}" version`,
333
+ );
334
+ }
347
335
 
348
- changedWorkspaces.set(workspace, {
349
- bumpType,
350
- bumpReason,
351
- });
336
+ changedWorkspaces.set(workspace, {
337
+ bumpType,
338
+ bumpReason,
339
+ });
340
+ }
352
341
  }
353
- }
354
-
355
- if (changedWorkspaces.size === 0) {
356
- logger.info("No changed workspaces");
357
- return;
358
- }
359
342
 
360
- logger.info("Preparing bumping");
343
+ if (changedWorkspaces.size === 0) {
344
+ logger.info("No changed workspaces");
345
+ }
346
+ });
361
347
 
362
348
  const bumpedWorkspaces = new Map<Workspace, BumpedWorkspace>();
363
349
  const noVersionToUpdateWorkspaces = new Map<
@@ -368,147 +354,151 @@ export const versionCommandAction = async (
368
354
  ? buildTopologicalOrderBatches(project, dependenciesMap)
369
355
  : [[rootWorkspace]];
370
356
 
371
- for (const batch of batches) {
372
- for (const workspace of batch) {
373
- const currentVersion = workspace.pkg.version;
374
-
375
- if (!currentVersion && !workspace.pkg.private) {
376
- throw new UsageError(
377
- `Invalid "${getWorkspaceName(workspace)}" version`,
378
- );
379
- }
380
-
381
- const changedWorkspace = changedWorkspaces.get(workspace);
382
- let bumpType: BumpType | null = null;
383
- const bumpReasons: string[] = [];
384
- const dependenciesToBump: BumpedWorkspace["dependenciesToBump"] = [];
385
-
386
- if (changedWorkspace) {
387
- bumpType = changedWorkspace.bumpType;
388
- bumpReasons.push(changedWorkspace.bumpReason || "by commits");
389
- }
390
-
391
- const dependencies = dependenciesMap?.get(workspace);
357
+ logger.group("Preparing bumping", () => {
358
+ for (const batch of batches) {
359
+ for (const workspace of batch) {
360
+ const currentVersion = workspace.pkg.version;
392
361
 
393
- if (dependencies) {
394
- for (const [
395
- dependencyWorkspace,
396
- dependencyType,
397
- dependencyDescriptor,
398
- ] of dependencies) {
399
- const dependencyBumpedWorkspace =
400
- bumpedWorkspaces.get(dependencyWorkspace);
401
-
402
- if (!dependencyBumpedWorkspace) {
403
- continue;
404
- }
362
+ if (!currentVersion && !workspace.pkg.private) {
363
+ throw new UsageError(
364
+ `Invalid "${getWorkspaceName(workspace)}" version`,
365
+ );
366
+ }
405
367
 
406
- if (
407
- dependencyType === "peerDependencies" &&
408
- !options.alwaysBumpPeerDependencies &&
409
- // skip when peerdependency with a new version satisfied by the existing range.
410
- satisfies(
411
- dependencyBumpedWorkspace.newVersion,
412
- dependencyDescriptor.selector,
413
- { includePrerelease: true },
414
- )
415
- ) {
416
- continue;
417
- }
368
+ const changedWorkspace = changedWorkspaces.get(workspace);
369
+ let bumpType: BumpType | null = null;
370
+ const bumpReasons: string[] = [];
371
+ const dependenciesToBump: BumpedWorkspace["dependenciesToBump"] = [];
418
372
 
419
- const newRange = calcBumpRange(
420
- workspace,
421
- dependencyDescriptor.selector,
422
- dependencyBumpedWorkspace.newVersion,
423
- );
373
+ if (changedWorkspace) {
374
+ bumpType = changedWorkspace.bumpType;
375
+ bumpReasons.push(changedWorkspace.bumpReason || "by commits");
376
+ }
424
377
 
425
- if (dependencyDescriptor.selector === newRange) {
426
- continue;
427
- }
378
+ const dependencies = dependenciesMap?.get(workspace);
428
379
 
429
- dependenciesToBump.push([
380
+ if (dependencies) {
381
+ for (const [
382
+ dependencyWorkspace,
430
383
  dependencyType,
431
384
  dependencyDescriptor,
432
- newRange,
433
- ]);
434
-
435
- bumpType = getHighestBumpType([
436
- bumpType ?? "patch",
437
- calcBumpType(
438
- dependencyBumpedWorkspace.bumpType,
439
- options.bumpDependentsHighestAs,
440
- ),
441
- ]);
442
-
443
- bumpReasons.push(
444
- `Version bump for dependency: ${PackageDescriptorNameUtils.stringify(dependencyDescriptor.name)}`,
445
- );
446
- }
447
- }
385
+ ] of dependencies) {
386
+ const dependencyBumpedWorkspace =
387
+ bumpedWorkspaces.get(dependencyWorkspace);
388
+
389
+ if (!dependencyBumpedWorkspace) {
390
+ continue;
391
+ }
392
+
393
+ if (
394
+ dependencyType === "peerDependencies" &&
395
+ !options.alwaysBumpPeerDependencies &&
396
+ // skip when peerdependency with a new version satisfied by the existing range.
397
+ satisfies(
398
+ dependencyBumpedWorkspace.newVersion,
399
+ dependencyDescriptor.selector,
400
+ { includePrerelease: true },
401
+ )
402
+ ) {
403
+ continue;
404
+ }
405
+
406
+ const newRange = calcBumpRange(
407
+ workspace,
408
+ dependencyDescriptor.selector,
409
+ dependencyBumpedWorkspace.newVersion,
410
+ );
448
411
 
449
- const workspaceName = getWorkspaceName(workspace);
450
- if (!currentVersion) {
451
- logger.info(`${workspaceName}: skipped (no version)`);
452
- if (workspace !== rootWorkspace) {
453
- noVersionToUpdateWorkspaces.set(workspace, {
454
- dependenciesToBump,
455
- });
456
- }
457
- } else if (!bumpType) {
458
- logger.info(
459
- `${workspaceName}: skipped (${
460
- changedWorkspace
461
- ? `no bump recommended by ${options.preset}`
462
- : "no changes"
463
- })`,
464
- );
465
- } else {
466
- const newVersion = incrementVersion(
467
- workspace,
468
- currentVersion,
469
- bumpType,
470
- );
412
+ if (dependencyDescriptor.selector === newRange) {
413
+ continue;
414
+ }
471
415
 
472
- const tagName = buildTagName(workspace, newVersion);
416
+ dependenciesToBump.push([
417
+ dependencyType,
418
+ dependencyDescriptor,
419
+ newRange,
420
+ ]);
421
+
422
+ bumpType = getHighestBumpType([
423
+ bumpType ?? "patch",
424
+ calcBumpType(
425
+ dependencyBumpedWorkspace.bumpType,
426
+ options.bumpDependentsHighestAs,
427
+ ),
428
+ ]);
473
429
 
474
- if (workspace === rootWorkspace) {
475
- rootNewVersion = newVersion;
476
- rootNewTag = tagName;
430
+ bumpReasons.push(
431
+ `Version bump for dependency: ${PackageDescriptorNameUtils.stringify(dependencyDescriptor.name)}`,
432
+ );
433
+ }
477
434
  }
478
435
 
479
- if (workspace !== rootWorkspace || !isMonorepo) {
480
- const bumpReason = bumpReasons.join("\n");
481
- bumpedWorkspaces.set(workspace, {
436
+ const workspaceName = getWorkspaceName(workspace);
437
+ if (!currentVersion) {
438
+ logger.info(`${workspaceName}: skipped (no version)`);
439
+ if (workspace !== rootWorkspace) {
440
+ noVersionToUpdateWorkspaces.set(workspace, {
441
+ dependenciesToBump,
442
+ });
443
+ }
444
+ } else if (!bumpType) {
445
+ logger.info(
446
+ `${workspaceName}: skipped (${
447
+ changedWorkspace
448
+ ? `no bump recommended by ${options.preset}`
449
+ : "no changes"
450
+ })`,
451
+ );
452
+ } else {
453
+ const newVersion = incrementVersion(
454
+ workspace,
482
455
  currentVersion,
483
456
  bumpType,
484
- bumpReason,
485
- bumpForDependenciesReasons: changedWorkspace
486
- ? bumpReasons.slice(1)
487
- : bumpReasons,
488
- newVersion,
489
- newTag: tagName,
490
- hasChanged: changedWorkspace !== undefined,
491
- dependenciesToBump,
492
- });
493
-
494
- logger.info(
495
- `${workspaceName}: ${currentVersion} -> ${
496
- !isMonorepo || isMonorepoVersionIndependent ? newVersion : "bump"
497
- } (${bumpReason.replace("\n", " ; ")})`,
498
457
  );
499
- // TODO Json only:
500
- // logger.info({
501
- // cwd: npath.fromPortablePath(workspace.cwd),
502
- // ident: workspaceName,
503
- // oldVersion: currentVersion,
504
- // ...(isMonorepoVersionIndependent ? { newVersion } : {}),
505
- // bumpType,
506
- // bumpReasons,
507
- // });
458
+
459
+ const tagName = buildTagName(workspace, newVersion);
460
+
461
+ if (workspace === rootWorkspace) {
462
+ rootNewVersion = newVersion;
463
+ rootNewTag = tagName;
464
+ }
465
+
466
+ if (workspace !== rootWorkspace || !isMonorepo) {
467
+ const bumpReason = bumpReasons.join("\n");
468
+ bumpedWorkspaces.set(workspace, {
469
+ currentVersion,
470
+ bumpType,
471
+ bumpReason,
472
+ bumpForDependenciesReasons: changedWorkspace
473
+ ? bumpReasons.slice(1)
474
+ : bumpReasons,
475
+ newVersion,
476
+ newTag: tagName,
477
+ hasChanged: changedWorkspace !== undefined,
478
+ dependenciesToBump,
479
+ });
480
+
481
+ logger.info(
482
+ `${workspaceName}: ${currentVersion} -> ${
483
+ !isMonorepo || isMonorepoVersionIndependent
484
+ ? newVersion
485
+ : "bump"
486
+ } (${bumpReason.replace("\n", " ; ")})`,
487
+ );
488
+ // TODO Json only:
489
+ // logger.info({
490
+ // cwd: npath.fromPortablePath(workspace.cwd),
491
+ // ident: workspaceName,
492
+ // oldVersion: currentVersion,
493
+ // ...(isMonorepoVersionIndependent ? { newVersion } : {}),
494
+ // bumpType,
495
+ // bumpReasons,
496
+ // });
497
+ }
508
498
  }
509
499
  }
510
500
  }
511
- }
501
+ });
512
502
 
513
503
  // TODO ask for confirmation
514
504
  // if (!this.yes) {
@@ -569,74 +559,81 @@ export const versionCommandAction = async (
569
559
  );
570
560
  }
571
561
 
572
- // do modifications
562
+ await logger.group("Applying changes", async () => {
563
+ // Update yarn.lock ; must be done to make sure preversion script can be ran
573
564
 
574
- // Update yarn.lock ; must be done to make sure preversion script can be ran
565
+ if (!options.dryRun) {
566
+ logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
567
+ await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
575
568
 
576
- if (!options.dryRun) {
577
- logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
578
- await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
569
+ logger.info("Lifecycle script: preversion");
579
570
 
580
- logger.info("Lifecycle script: preversion");
581
-
582
- if (isMonorepoVersionIndependent && rootWorkspace.pkg.scripts?.preversion) {
583
- await execCommand(
584
- rootWorkspace,
585
- ["yarn", "run", "preversion"],
586
- "inherit",
587
- );
588
- }
571
+ if (
572
+ isMonorepoVersionIndependent &&
573
+ rootWorkspace.pkg.scripts?.preversion
574
+ ) {
575
+ await execCommand(
576
+ rootWorkspace,
577
+ ["yarn", "run", "preversion"],
578
+ "inherit",
579
+ );
580
+ }
589
581
 
590
- // lifecycle: preversion
591
- for (const workspace of bumpedWorkspaces.keys()) {
592
- if (workspace.pkg.scripts?.preversion) {
593
- await execCommand(workspace, ["yarn", "run", "preversion"], "inherit");
582
+ // lifecycle: preversion
583
+ for (const workspace of bumpedWorkspaces.keys()) {
584
+ if (workspace.pkg.scripts?.preversion) {
585
+ await execCommand(
586
+ workspace,
587
+ ["yarn", "run", "preversion"],
588
+ "inherit",
589
+ );
590
+ }
594
591
  }
595
- }
596
592
 
597
- logger.info("Modifying versions in package.json");
598
- // update versions
593
+ logger.info("Modifying versions in package.json");
594
+ // update versions
599
595
 
600
- await Promise.all(
601
- [...bumpedWorkspaces.entries()].map(
602
- ([workspace, { newVersion, dependenciesToBump }]) => {
603
- workspace.pkg.version = newVersion;
596
+ await Promise.all(
597
+ [...bumpedWorkspaces.entries()].map(
598
+ ([workspace, { newVersion, dependenciesToBump }]) => {
599
+ workspace.pkg.version = newVersion;
604
600
 
605
- for (const [
606
- dependencyType,
607
- dependencyDescriptor,
608
- dependencyNewRange,
609
- ] of dependenciesToBump) {
610
- const newDescriptor = PackageDependencyDescriptorUtils.make(
601
+ for (const [
602
+ dependencyType,
611
603
  dependencyDescriptor,
612
604
  dependencyNewRange,
613
- );
614
- const [key, newValue] =
615
- PackageDependencyDescriptorUtils.stringify(newDescriptor);
616
- workspace.pkg[dependencyType]![key] = newValue;
617
- }
618
-
619
- return writePkg(workspace);
620
- },
621
- ),
622
- );
605
+ ] of dependenciesToBump) {
606
+ const newDescriptor = PackageDependencyDescriptorUtils.make(
607
+ dependencyDescriptor,
608
+ dependencyNewRange,
609
+ );
610
+ const [key, newValue] =
611
+ PackageDependencyDescriptorUtils.stringify(newDescriptor);
612
+ workspace.pkg[dependencyType]![key] = newValue;
613
+ }
614
+
615
+ return writePkg(workspace);
616
+ },
617
+ ),
618
+ );
623
619
 
624
- // Update yarn.lock ; must be done before running again lifecycle scripts
625
- logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
626
- await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
620
+ // Update yarn.lock ; must be done before running again lifecycle scripts
621
+ logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
622
+ await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
627
623
 
628
- // lifecycle: version
629
- logger.info("Lifecycle script: version");
630
- for (const workspace of bumpedWorkspaces.keys()) {
631
- if (workspace.pkg.scripts?.version) {
632
- await execCommand(workspace, ["yarn", "run", "version"], "inherit");
624
+ // lifecycle: version
625
+ logger.info("Lifecycle script: version");
626
+ for (const workspace of bumpedWorkspaces.keys()) {
627
+ if (workspace.pkg.scripts?.version) {
628
+ await execCommand(workspace, ["yarn", "run", "version"], "inherit");
629
+ }
633
630
  }
634
631
  }
635
- }
636
632
 
637
- if (isMonorepoVersionIndependent && rootWorkspace.pkg.scripts?.version) {
638
- await execCommand(rootWorkspace, ["yarn", "run", "version"], "inherit");
639
- }
633
+ if (isMonorepoVersionIndependent && rootWorkspace.pkg.scripts?.version) {
634
+ await execCommand(rootWorkspace, ["yarn", "run", "version"], "inherit");
635
+ }
636
+ });
640
637
 
641
638
  const changelogs = new Map<Workspace, string>();
642
639
 
@@ -709,15 +706,13 @@ export const versionCommandAction = async (
709
706
  );
710
707
 
711
708
  if (!options.dryRun) {
712
- // TODO nightingale separator
713
- console.log();
709
+ logger.separator();
714
710
 
715
711
  // install to update versions in lock file
716
712
  logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
717
713
  await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
718
714
 
719
- // TODO nightingale separator
720
- console.log();
715
+ logger.separator();
721
716
 
722
717
  logger.info("Commit, tag and push", {
723
718
  changedFiles: await getDirtyFiles(rootWorkspace),
@@ -903,4 +898,5 @@ export default program
903
898
  'Ignore changes in files matching the glob. Example: "**/*.test.js"',
904
899
  ),
905
900
  )
901
+ .addOption(new Option("--json", "Logger json"))
906
902
  .action((options) => versionCommandAction(options));