-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
version-command.ts
941 lines (808 loc) · 32.7 KB
/
version-command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
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
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
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
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
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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
import chalk from 'chalk';
import dedent from 'dedent';
import { minimatch } from 'minimatch';
import { EOL as OS_EOL } from 'node:os';
import pLimit from 'p-limit';
import pMap from 'p-map';
import pPipe from 'p-pipe';
import pReduce from 'p-reduce';
import semver from 'semver';
import {
EOL,
type ChangelogPresetOptions,
checkWorkingTree,
collectPackages,
collectUpdates,
Command,
type CommandType,
createRunner,
logOutput,
type Package,
type PackageGraphNode,
type ProjectConfig,
promptConfirmation,
runTopologically,
throwIfUncommitted,
type UpdateCollectorOptions,
ValidationError,
type VersionCommandOption,
} from '@lerna-lite/core';
import { getCurrentBranch } from './lib/get-current-branch.js';
import { createRelease, createReleaseClient } from './lib/create-release.js';
import { isAnythingCommitted } from './lib/is-anything-committed.js';
import { remoteBranchExists } from './lib/remote-branch-exists.js';
import { isBehindUpstream } from './lib/is-behind-upstream.js';
import { isBreakingChange } from './lib/is-breaking-change.js';
import { gitAdd } from './lib/git-add.js';
import { gitCommit } from './lib/git-commit.js';
import { gitTag } from './lib/git-tag.js';
import { gitPush, gitPushSingleTag } from './lib/git-push.js';
import { makePromptVersion } from './lib/prompt-version.js';
import {
loadPackageLockFileWhenExists,
updateClassicLockfileVersion,
updateTempModernLockfileVersion,
runInstallLockFileOnly,
saveUpdatedLockJsonFile,
} from './lib/update-lockfile-version.js';
import { GitCreateReleaseClientOutput, ReleaseNote, RemoteCommit } from './models/index.js';
import {
applyBuildMetadata,
getCommitsSinceLastRelease,
recommendVersion,
updateChangelog,
} from './conventional-commits/index.js';
export function factory(argv: VersionCommandOption) {
return new VersionCommand(argv);
}
export class VersionCommand extends Command<VersionCommandOption> {
/** command name */
name = 'version' as CommandType;
globalVersion = '';
changelogIncludeCommitsClientLogin?: boolean | string;
changelogIncludeCommitsGitAuthor?: boolean | string;
commitsSinceLastRelease?: RemoteCommit[];
currentBranch = '';
gitRemote = '';
tagPrefix = '';
commitAndTag = true;
pushToRemote = true;
hasRootedLeaf = false;
releaseClient?: GitCreateReleaseClientOutput;
releaseNotes: ReleaseNote[] = [];
gitOpts: any;
runPackageLifecycle: any;
runRootLifecycle!: (stage: string) => Promise<void> | void;
savePrefix = '';
tags: string[] = [];
packagesToVersion: Package[] = [];
updates: PackageGraphNode[] = [];
updatesVersions?: Map<string, string>;
premajorVersionBump?: 'default' | 'force-patch';
get otherCommandConfigs(): string[] {
// back-compat
return ['publish'];
}
get requiresGit(): boolean {
// prettier-ignore
return (
this.commitAndTag ||
this.pushToRemote ||
this.options.allowBranch ||
this.options.conventionalCommits
) as boolean;
}
constructor(argv: VersionCommandOption | ProjectConfig) {
super(argv);
}
async configureProperties() {
super.configureProperties();
// Defaults are necessary here because yargs defaults
// override durable options provided by a config file
const {
amend,
changelogIncludeCommitsClientLogin,
changelogIncludeCommitsGitAuthor,
commitHooks = true,
gitRemote = 'origin',
gitTagVersion = true,
granularPathspec = true,
push = true,
signGitCommit,
signoffGitCommit,
signGitTag,
forceGitTag,
tagVersionPrefix = 'v',
premajorVersionBump = 'default',
} = this.options;
this.gitRemote = gitRemote;
this.tagPrefix = tagVersionPrefix;
this.commitAndTag = gitTagVersion;
this.pushToRemote = gitTagVersion && amend !== true && push;
this.changelogIncludeCommitsClientLogin =
changelogIncludeCommitsClientLogin === '' ? true : changelogIncludeCommitsClientLogin;
this.changelogIncludeCommitsGitAuthor = changelogIncludeCommitsGitAuthor === '' ? true : changelogIncludeCommitsGitAuthor;
this.premajorVersionBump = premajorVersionBump;
// never automatically push to remote when amending a commit
if (this.pushToRemote && this.options.createRelease) {
this.releaseClient = await createReleaseClient(this.options.createRelease);
}
this.releaseNotes = [];
if (this.releaseClient && this.options.conventionalCommits !== true) {
throw new ValidationError('ERELEASE', 'To create a release, you must enable --conventional-commits');
}
if (this.options.generateReleaseNotes && !this.options.createRelease) {
throw new ValidationError('ERELEASE', 'To generate release notes, you must define --create-release');
}
if (this.options.createReleaseDiscussion && !this.options.createRelease) {
throw new ValidationError('ERELEASE', 'To create a release discussion, you must define --create-release');
}
if (this.options.createReleaseDiscussion !== undefined && !this.options.createReleaseDiscussion) {
throw new ValidationError(
'ERELEASE',
'A discussion category name must be provided to the --create-release-discussion option.'
);
}
if (this.releaseClient && this.options.changelog === false && this.options.generateReleaseNotes !== true) {
throw new ValidationError('ERELEASE', 'To create a release, you cannot pass --no-changelog');
}
if (
this.options.conventionalCommits &&
typeof this.options.changelogPreset === 'object' &&
(this.options.changelogPreset as ChangelogPresetOptions)?.releaseCommitMessageFormat
) {
throw new ValidationError(
'ENOTSUPPORTED',
'The Lerna config "changelogPreset.releaseCommitMessageFormat" is not supported, you should simply use "version.message" instead which will give you the same end result.'
);
}
this.gitOpts = {
amend,
commitHooks,
granularPathspec,
signGitCommit,
signoffGitCommit,
signGitTag,
forceGitTag,
};
// https://docs.npmjs.com/misc/config#save-prefix
this.savePrefix = this.options.exact ? '' : '^';
}
async initialize() {
const isIndependent = this.project.isIndependent();
const describeTag = this.options.describeTag;
if (!isIndependent) {
this.logger.info('current project version', this.project.version ?? '');
}
if (this.requiresGit) {
// git validation, if enabled, should happen before updates are calculated and versions picked
if (!isAnythingCommitted(this.execOpts, this.options.dryRun)) {
throw new ValidationError('ENOCOMMIT', 'No commits in this repository. Please commit something before using version.');
}
this.currentBranch = getCurrentBranch(this.execOpts, false);
if (this.currentBranch === 'HEAD') {
throw new ValidationError('ENOGIT', 'Detached git HEAD, please checkout a branch to choose versions.');
}
if (this.pushToRemote && !remoteBranchExists(this.gitRemote, this.currentBranch, this.execOpts, this.options.dryRun)) {
throw new ValidationError(
'ENOREMOTEBRANCH',
dedent`
Branch "${this.currentBranch}" doesn't exist in remote "${this.gitRemote}".
If this is a new branch, please make sure you push it to the remote first.
`
);
}
if (
this.options.allowBranch &&
!([] as string[]).concat(this.options.allowBranch).some((x) => minimatch(this.currentBranch, x))
) {
throw new ValidationError(
'ENOTALLOWED',
dedent`
Branch "${this.currentBranch}" is restricted from versioning due to allowBranch config.
Please consider the reasons for this restriction before overriding the option.
`
);
}
if (
this.commitAndTag &&
this.pushToRemote &&
isBehindUpstream(this.gitRemote, this.currentBranch, this.execOpts, this.options.dryRun)
) {
const message = `Local branch "${this.currentBranch}" is behind remote upstream ${this.gitRemote}/${this.currentBranch}`;
if (!this.options.ci) {
// interrupt interactive execution
throw new ValidationError(
'EBEHIND',
dedent`
${message}
Please merge remote changes into "${this.currentBranch}" with "git pull"
`
);
}
// CI execution should not error, but warn & exit
this.logger.warn('EBEHIND', `${message}, exiting`);
// still exits zero, aka "ok"
return false;
}
} else {
this.logger.notice('FYI', 'git repository validation has been skipped, please ensure your version bumps are correct');
}
if (this.options.conventionalPrerelease && this.options.conventionalGraduate) {
throw new ValidationError(
'ENOTALLOWED',
dedent`
--conventional-prerelease cannot be combined with --conventional-graduate.
`
);
}
if (this.options.manuallyUpdateRootLockfile && this.options.syncWorkspaceLock) {
throw new ValidationError(
'ENOTALLOWED',
dedent`
--manually-update-root-lockfile cannot be combined with --sync-workspace-lock.
`
);
}
if (this.changelogIncludeCommitsClientLogin && this.changelogIncludeCommitsGitAuthor) {
throw new ValidationError(
'ENOTALLOWED',
dedent`
--changelog-include-commits-client-login cannot be combined with --changelog-include-commits-git-author.
`
);
}
// fetch all commits from remote server of the last release when user wants to include client login associated to each commits
const remoteClient = this.options.createRelease || this.options.remoteClient;
if (this.options.conventionalCommits && this.changelogIncludeCommitsClientLogin) {
if (!remoteClient) {
throw new ValidationError(
'EMISSINGCLIENT',
dedent`
--changelog-include-commits-client-login requires one of these two option --remote-client or --create-release to be defined.
`
);
}
this.commitsSinceLastRelease = await getCommitsSinceLastRelease(
remoteClient,
this.options.gitRemote,
this.currentBranch,
isIndependent,
this.execOpts
);
}
this.updates = collectUpdates(this.packageGraph.rawPackageList, this.packageGraph, this.execOpts, {
...this.options,
isIndependent,
describeTag,
} as UpdateCollectorOptions).filter((node) => {
// --no-private completely removes private packages from consideration
if (node.pkg.private && this.options.private === false) {
// TODO: (major) make --no-private the default
return false;
}
if (!node.version) {
// a package may be unversioned only if it is private
if (node.pkg.private) {
this.logger.info('version', 'Skipping unversioned private package %j', node.name);
} else {
throw new ValidationError(
'ENOVERSION',
dedent`
A version field is required in ${node.name}'s package.json file.
If you wish to keep the package unversioned, it must be made private.
`
);
}
}
return !!node.version;
});
if (!this.updates.length) {
this.logger.success(`No changed packages to ${this.composed ? 'publish' : 'version'}`);
// still exits zero, aka "ok"
return false;
}
// a "rooted leaf" is the regrettable pattern of adding "." to the "packages" config in lerna.json
this.hasRootedLeaf = this.packageGraph.has(this.project.manifest.name);
if (this.hasRootedLeaf && !this.composed) {
this.logger.info('version', 'rooted leaf detected, skipping synthetic root lifecycles');
}
this.runPackageLifecycle = createRunner({ ...this.options, stdio: 'inherit' });
// don't execute recursively if run from a poorly-named script
this.runRootLifecycle = /^(pre|post)?version$/.test(process.env.npm_lifecycle_event as string)
? (stage) => this.logger.warn('lifecycle', 'Skipping root %j because it has already been called', stage)
: (stage) => this.runPackageLifecycle(this.project.manifest, stage);
// amending a commit probably means the working tree is dirty
if (this.commitAndTag && this.gitOpts.amend !== true) {
const { forcePublish, conventionalCommits, conventionalGraduate } = this.options;
const checkUncommittedOnly = forcePublish || (conventionalCommits && conventionalGraduate);
const check = checkUncommittedOnly ? throwIfUncommitted : checkWorkingTree;
await check(this.execOpts, this.options.dryRun);
} else {
this.logger.warn('version', 'Skipping working tree validation, proceed at your own risk');
}
const versionsForUpdate = await this.getVersionsForUpdates();
await this.setUpdatesForVersions(versionsForUpdate);
return this.confirmVersions();
}
async execute() {
await this.updatePackageVersions();
if (this.commitAndTag) {
await this.commitAndTagUpdates();
} else {
this.logger.info('execute', 'Skipping git tag/commit');
}
if (this.pushToRemote) {
await this.gitPushToRemote();
} else {
this.logger.info('execute', 'Skipping git push');
}
if (this.releaseClient) {
this.logger.info('execute', 'Creating releases...');
try {
await createRelease(
{
client: this.releaseClient,
type: this.options.createRelease!,
generateReleaseNotes: this.options.generateReleaseNotes,
releaseDiscussion: this.options.createReleaseDiscussion,
},
{
tags: this.tags,
tagVersionSeparator: this.options.tagVersionSeparator || '@',
releaseNotes: this.releaseNotes,
},
{
gitRemote: this.options.gitRemote,
execOpts: this.execOpts,
skipBumpOnlyReleases: this.options.skipBumpOnlyReleases,
},
this.options.dryRun
);
} catch (err: any) {
this.logger.error(
'ERELEASE',
`Something went wrong when creating the ${this.options.createRelease} release. Error:: ${err?.message ?? err}`
);
}
} else {
this.logger.info('execute', 'Skipping releases');
}
if (!this.composed) {
this.logger.success('version', 'finished');
}
return {
updates: this.updates,
updatesVersions: this.updatesVersions,
};
}
getVersionsForUpdates() {
const independentVersions = this.project.isIndependent();
const { bump, conventionalCommits, preid } = this.options;
const repoVersion = (bump ? semver.clean(bump) : '') as string;
const increment = (bump && !semver.valid(bump) ? bump : '') as semver.ReleaseType;
const resolvePrereleaseId = (existingPreid?: string) => preid || existingPreid || 'alpha';
const makeGlobalVersionPredicate = (nextVersion: string) => {
this.globalVersion = nextVersion;
return () => nextVersion;
};
// decide the predicate in the conditionals below
let predicate: (s: any) => any;
if (repoVersion) {
predicate = makeGlobalVersionPredicate(applyBuildMetadata(repoVersion, this.options.buildMetadata));
} else if (increment && independentVersions) {
// compute potential prerelease ID for each independent update
predicate = (node: { version: string; prereleaseId: string }) =>
applyBuildMetadata(
semver.inc(node.version, increment, resolvePrereleaseId(node.prereleaseId)),
this.options.buildMetadata
);
} else if (increment) {
// compute potential prerelease ID once for all fixed updates
const prereleaseId = prereleaseIdFromVersion(this.project.version);
const nextVersion = applyBuildMetadata(
semver.inc(this.project.version, increment, resolvePrereleaseId(prereleaseId)),
this.options.buildMetadata
);
predicate = makeGlobalVersionPredicate(nextVersion as string);
} else if (conventionalCommits) {
// it's a bit weird to have a return here, true
return this.recommendVersions(resolvePrereleaseId);
} else if (independentVersions) {
// prompt for each independent update with potential prerelease ID
predicate = makePromptVersion(resolvePrereleaseId, this.options.buildMetadata);
} else {
// prompt once with potential prerelease ID
const prereleaseId = prereleaseIdFromVersion(this.project.version);
const node = { version: this.project.version, prereleaseId };
predicate = makePromptVersion(resolvePrereleaseId, this.options.buildMetadata);
predicate = predicate(node).then(makeGlobalVersionPredicate);
}
return Promise.resolve(predicate).then((getVersion: (s: PackageGraphNode) => string) => this.reduceVersions(getVersion));
}
reduceVersions(getVersion: (s: PackageGraphNode) => string) {
const iterator = (versionMap: Map<string, string>, node: PackageGraphNode) =>
Promise.resolve(getVersion(node)).then((version: string) => versionMap.set(node.name, version));
return pReduce(this.updates, iterator, new Map());
}
setUpdatesForVersions(versions: Map<string, string>) {
if (this.project.isIndependent()) {
// only partial fixed versions need to be checked
this.updatesVersions = versions;
} else {
let hasBreakingChange = false;
for (const [name, bump] of versions) {
hasBreakingChange = hasBreakingChange || isBreakingChange(this.packageGraph?.get(name)!.version, bump);
}
if (hasBreakingChange) {
// _all_ packages need a major version bump whenever _any_ package does
this.updates = Array.from(this.packageGraph?.values() ?? []);
// --no-private completely removes private packages from consideration
if (this.options.private === false) {
// TODO: (major) make --no-private the default
this.updates = this.updates.filter((node) => !node.pkg.private);
}
this.updatesVersions = new Map(this.updates.map((node) => [node.name, this.globalVersion]));
} else {
this.updatesVersions = versions;
}
}
this.packagesToVersion = this.updates.map((node) => node.pkg);
}
getPrereleasePackageNames() {
const prereleasePackageNames = this.getPackagesForOption(this.options.conventionalPrerelease);
// prettier-ignore
const isCandidate = prereleasePackageNames.has('*')
? () => true
: (_node, name) => prereleasePackageNames.has(name);
const prePkgs = collectPackages(this.packageGraph, { isCandidate }).map((pkg) => pkg?.name ?? '');
if (
this.options.conventionalPrerelease !== undefined &&
typeof this.options.conventionalPrerelease !== 'boolean' &&
prePkgs.length === 0
) {
throw new ValidationError(
'ENOPRERELEASE',
`No packages found to prerelease when using "--conventional-prerelease ${this.options.conventionalPrerelease}".`
);
}
return prePkgs;
}
/**
* @param {boolean|string|string[]} option
* @returns {Set<string>} A set of package names (or wildcard) derived from option value.
*/
getPackagesForOption(option?: boolean | string | string[]) {
// new Set(null) is equivalent to new Set([])
// i.e., an empty Set
let inputs: string[] | null = null;
if (option === true) {
// option passed without specific packages, eg. --force-publish
inputs = ['*'];
} else if (typeof option === 'string') {
// option passed with one or more comma separated package names, eg.:
// --force-publish=*
// --force-publish=foo
// --force-publish=foo,bar
inputs = option.split(',');
} else if (Array.isArray(option)) {
// option passed multiple times with individual package names
// --force-publish foo --force-publish baz
inputs = [...option];
}
return new Set(inputs);
}
async recommendVersions(resolvePrereleaseId) {
const independentVersions = this.project.isIndependent();
const { buildMetadata, changelogPreset, conventionalGraduate, conventionalBumpPrerelease } = this.options;
const rootPath = this.project.manifest.location;
const type = independentVersions ? 'independent' : 'fixed';
const prereleasePackageNames = this.getPrereleasePackageNames();
const graduatePackageNames = Array.from(this.getPackagesForOption(conventionalGraduate));
const shouldPrerelease = (name) => prereleasePackageNames?.includes(name);
const shouldGraduate = (name) => graduatePackageNames.includes('*') || graduatePackageNames.includes(name);
const getPrereleaseId = (node) => {
if (!shouldGraduate(node.name) && (shouldPrerelease(node.name) || node.prereleaseId)) {
return resolvePrereleaseId(node.prereleaseId);
}
};
if (type === 'fixed') {
this.setGlobalVersionFloor();
}
const versions: Map<string, string> = await this.reduceVersions(
(node) =>
recommendVersion(
node,
type,
{
changelogPreset,
rootPath,
tagPrefix: this.tagPrefix,
prereleaseId: getPrereleaseId(node),
conventionalBumpPrerelease,
buildMetadata,
},
this.premajorVersionBump
) as any
);
if (type === 'fixed') {
this.globalVersion = this.setGlobalVersionCeiling(versions) ?? '';
}
return versions;
}
confirmVersions(): Promise<boolean> | boolean {
const changes = this.packagesToVersion.map((pkg) => {
let line = ` - ${pkg.name ?? '[n/a]'}: ${pkg.version} => ${chalk.cyan(this.updatesVersions?.get(pkg?.name ?? ''))}`;
if (pkg.private) {
line += ` (${chalk.red('private')})`;
}
return line;
});
logOutput('');
logOutput(`Changes (${changes.length} packages):`);
logOutput(changes.join(EOL));
logOutput('');
if (this.options.yes) {
this.logger.info('auto-confirmed', '');
return true;
}
// When composed from `lerna publish`, use this opportunity to confirm publishing
let confirmMessage = this.options.dryRun ? chalk.bgMagenta('[dry-run]') : '';
confirmMessage += this.composed
? ' Are you sure you want to publish these packages?'
: ' Are you sure you want to create these versions?';
return promptConfirmation(confirmMessage.trim());
}
updatePackageVersions() {
const { conventionalCommits, changelogPreset, changelogHeaderMessage, changelog = true } = this.options;
const independentVersions = this.project.isIndependent();
const rootPath = this.project.manifest.location;
const changedFiles = new Set<string>();
const npmClient = this.options.npmClient || 'npm';
let chain: Promise<any> = Promise.resolve();
// preversion: Run BEFORE bumping the package version.
// version: Run AFTER bumping the package version, but BEFORE commit.
// postversion: Run AFTER bumping the package version, and AFTER commit.
// @see https://docs.npmjs.com/misc/scripts
if (!this.hasRootedLeaf) {
// exec preversion lifecycle in root (before all updates)
chain = chain.then(() => this.runRootLifecycle('preversion'));
}
const actions = [
(pkg: Package) => this.runPackageLifecycle(pkg, 'preversion').then(() => pkg),
// manifest may be mutated by any previous lifecycle
(pkg: Package) => pkg.refresh(),
(pkg: Package) => {
// set new version
pkg.set('version', this.updatesVersions?.get(pkg?.name ?? ''));
// update pkg dependencies
for (const [depName, resolved] of this.packageGraph?.get(pkg.name)!.localDependencies) {
const depVersion = this.updatesVersions?.get(depName);
if (depVersion && resolved.type !== 'directory') {
// don't overwrite local file: specifiers, they only change during publish
pkg.updateLocalDependency(
resolved,
depVersion,
this.savePrefix,
this.options.allowPeerDependenciesUpdate,
this.commandName
);
}
}
return Promise.all([updateClassicLockfileVersion(pkg), pkg.serialize()]).then(([lockfilePath]) => {
// commit the updated manifest
changedFiles.add(pkg.manifestLocation);
if (lockfilePath) {
changedFiles.add(lockfilePath);
}
return pkg;
});
},
(pkg: Package) => this.runPackageLifecycle(pkg, 'version').then(() => pkg),
];
if (conventionalCommits && changelog) {
// we can now generate the Changelog, based on the
// the updated version that we're about to release.
const type = independentVersions ? 'independent' : 'fixed';
actions.push((pkg: Package) =>
updateChangelog(pkg, type, {
changelogPreset,
rootPath,
tagPrefix: this.tagPrefix,
changelogIncludeCommitsGitAuthor: this.changelogIncludeCommitsGitAuthor,
changelogIncludeCommitsClientLogin: this.changelogIncludeCommitsClientLogin,
changelogHeaderMessage,
commitsSinceLastRelease: this.commitsSinceLastRelease,
}).then(({ logPath, newEntry }) => {
// commit the updated changelog
changedFiles.add(logPath);
// add release notes
if (independentVersions) {
this.releaseNotes.push({
name: pkg.name,
notes: newEntry,
pkg,
});
}
return pkg;
})
);
} else if (this.options.generateReleaseNotes && !changelog) {
actions.push((pkg: Package) => {
this.releaseNotes.push({
name: pkg.name,
pkg,
});
});
}
const mapUpdate = pPipe(...actions);
chain = chain.then(() =>
runTopologically(this.packagesToVersion, mapUpdate, {
concurrency: this.concurrency,
rejectCycles: this.options.rejectCycles,
graphType: 'allDependencies',
})
);
// update the project root npm lock file, we will read and write back to the lock file
// this is currently the default update and if none of the flag are enabled (or all undefined) then we'll consider this as enabled
if (
npmClient === 'npm' &&
(this.options.manuallyUpdateRootLockfile ||
(this.options.manuallyUpdateRootLockfile === undefined && !this.options.syncWorkspaceLock))
) {
this.logger.warn(
'npm',
'we recommend using --sync-workspace-lock which will sync your lock file via your favorite npm client instead of relying on Lerna-Lite itself to update it.'
);
chain = chain.then(() =>
// update modern lockfile (version 2 or higher) when exist in the project root
loadPackageLockFileWhenExists(rootPath).then((lockFileResponse) => {
if (lockFileResponse && lockFileResponse.lockfileVersion >= 2) {
this.logger.verbose(`lock`, `start process loop of manually updating npm lock file`);
for (const pkg of this.packagesToVersion) {
this.logger.verbose(`lock`, `updating root "package-lock-json" for package "${pkg.name}"`);
updateTempModernLockfileVersion(pkg, lockFileResponse.json);
}
// save the lockfile, only once, after all package versions were updated
return saveUpdatedLockJsonFile(lockFileResponse.path, lockFileResponse.json).then((lockfilePath) => {
if (lockfilePath) {
changedFiles.add(lockfilePath);
}
});
}
})
);
} else if (this.options.syncWorkspaceLock) {
// update lock file, with npm client defined when `--sync-workspace-lock` is enabled
chain = chain.then(() =>
runInstallLockFileOnly(npmClient, this.project.manifest.location, {
npmClientArgs: this.options.npmClientArgs || [],
runScriptsOnLockfileUpdate: this.options.runScriptsOnLockfileUpdate,
}).then((lockfilePath) => {
if (lockfilePath) {
changedFiles.add(lockfilePath);
}
})
);
}
if (!independentVersions) {
this.project.version = this.globalVersion;
if (conventionalCommits && changelog) {
chain = chain.then(() =>
updateChangelog(this.project.manifest, 'root', {
changelogPreset,
rootPath,
tagPrefix: this.tagPrefix,
version: this.globalVersion,
changelogIncludeCommitsGitAuthor: this.changelogIncludeCommitsGitAuthor,
changelogIncludeCommitsClientLogin: this.changelogIncludeCommitsClientLogin,
changelogHeaderMessage,
commitsSinceLastRelease: this.commitsSinceLastRelease,
}).then(({ logPath, newEntry }) => {
// commit the updated changelog
changedFiles.add(logPath);
// add release notes
this.releaseNotes.push({
name: 'fixed',
notes: newEntry,
});
})
);
} else if (this.options.generateReleaseNotes && !changelog) {
chain.then(() => {
this.releaseNotes.push({
name: 'fixed',
});
});
}
chain = chain.then(() =>
this.project.serializeConfig().then((lernaConfigLocation) => {
// commit the version update
changedFiles.add(lernaConfigLocation);
})
);
}
if (!this.hasRootedLeaf) {
// exec version lifecycle in root (after all updates)
chain = chain.then(() => this.runRootLifecycle('version'));
}
if (this.commitAndTag) {
chain = chain.then(() => gitAdd(Array.from(changedFiles), this.gitOpts, this.execOpts, this.options.dryRun));
}
return chain;
}
async commitAndTagUpdates() {
if (this.project.isIndependent()) {
this.tags = await this.gitCommitAndTagVersionForUpdates();
} else {
this.tags = await this.gitCommitAndTagVersion();
}
// run the postversion script for each update
await pMap(this.packagesToVersion, (pkg) => this.runPackageLifecycle(pkg, 'postversion'));
if (!this.hasRootedLeaf) {
// run postversion, if set, in the root directory
await this.runRootLifecycle('postversion');
}
}
async gitCommitAndTagVersionForUpdates() {
const tagVersionSeparator = this.options.tagVersionSeparator || '@';
const tags = this.packagesToVersion.map((pkg) => `${pkg.name}${tagVersionSeparator}${this.updatesVersions?.get(pkg.name)}`);
const subject = this.options.message || 'chore: Publish new release';
const message = tags.reduce((msg, tag) => `${msg}${OS_EOL} - ${tag}`, `${subject}${OS_EOL}`);
await gitCommit(message, this.gitOpts, this.execOpts, this.options.dryRun);
for (const tag of tags) {
await gitTag(tag, this.gitOpts, this.execOpts, this.options.gitTagCommand, this.options.dryRun);
}
return tags;
}
async gitCommitAndTagVersion() {
const version = this.globalVersion;
const tag = `${this.tagPrefix}${version}`;
const message = this.options.message ? this.options.message.replace(/%s/g, tag).replace(/%v/g, version) : tag;
await gitCommit(message, this.gitOpts, this.execOpts, this.options.dryRun);
await gitTag(tag, this.gitOpts, this.execOpts, this.options.gitTagCommand, this.options.dryRun);
return [tag];
}
gitPushToRemote() {
// we could push tags one by one (to avoid GitHub limit)
if (this.options.pushTagsOneByOne) {
this.logger.info('git', 'Pushing tags one by one...');
const promises: Promise<void>[] = [];
const limit = pLimit(1);
this.tags.forEach((tag) => {
this.logger.verbose('git', `Pushing tag: ${tag}`);
promises.push(limit(() => gitPushSingleTag(this.gitRemote, this.currentBranch, tag, this.execOpts, this.options.dryRun)));
});
return Promise.allSettled(promises);
}
// or push all tags by using followTags
this.logger.info('git', 'Pushing tags...');
return gitPush(this.gitRemote, this.currentBranch, this.execOpts, this.options.dryRun);
}
setGlobalVersionFloor() {
const globalVersion = this.project.version;
for (const node of this.updates) {
if (semver.lt(node.version, globalVersion)) {
this.logger.verbose('version', `Overriding version of ${node.name} from ${node.version} to ${globalVersion}`);
node.pkg.version = globalVersion;
}
}
}
setGlobalVersionCeiling(versions: Map<string, string>) {
let highestVersion = this.project.version;
versions.forEach((bump: string) => {
if (bump && semver.gt(bump, highestVersion)) {
highestVersion = bump;
}
});
versions.forEach((_, name) => versions.set(name, highestVersion));
return highestVersion;
}
}
/**
* @param {string} version
* @returns {string|undefined}
*/
function prereleaseIdFromVersion(version) {
return ((semver.prerelease(version) || []) as string[]).shift();
}