@teambit/checkout 1.0.108 → 1.0.109

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.
@@ -0,0 +1 @@
1
+ !function(e,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define([],o):"object"==typeof exports?exports["teambit.component/checkout-preview"]=o():e["teambit.component/checkout-preview"]=o()}(self,(()=>(()=>{"use strict";var e={d:(o,t)=>{for(var r in t)e.o(t,r)&&!e.o(o,r)&&Object.defineProperty(o,r,{enumerable:!0,get:t[r]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},o={};e.r(o),e.d(o,{compositions:()=>t,compositions_metadata:()=>n,overview:()=>r});const t=[],r=[],n={compositions:[]};return o})()));
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/checkout",
3
- "version": "1.0.108",
3
+ "version": "1.0.109",
4
4
  "homepage": "https://bit.cloud/teambit/component/checkout",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.component",
8
8
  "name": "checkout",
9
- "version": "1.0.108"
9
+ "version": "1.0.109"
10
10
  },
11
11
  "dependencies": {
12
12
  "chalk": "2.4.2",
@@ -15,20 +15,20 @@
15
15
  "@teambit/bit-error": "0.0.404",
16
16
  "@teambit/component-id": "1.2.0",
17
17
  "@teambit/harmony": "0.4.6",
18
- "@teambit/cli": "0.0.840",
19
- "@teambit/merging": "1.0.108",
20
- "@teambit/component-writer": "1.0.108",
21
- "@teambit/importer": "1.0.108",
22
- "@teambit/logger": "0.0.933",
23
- "@teambit/remove": "1.0.108",
24
- "@teambit/workspace": "1.0.108"
18
+ "@teambit/cli": "0.0.841",
19
+ "@teambit/merging": "1.0.109",
20
+ "@teambit/component-writer": "1.0.109",
21
+ "@teambit/importer": "1.0.109",
22
+ "@teambit/logger": "0.0.934",
23
+ "@teambit/remove": "1.0.109",
24
+ "@teambit/workspace": "1.0.109"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/lodash": "4.14.165",
28
28
  "@types/mocha": "9.1.0",
29
29
  "@types/jest": "^29.2.2",
30
30
  "@types/testing-library__jest-dom": "^5.9.5",
31
- "@teambit/harmony.envs.core-aspect-env": "0.0.13"
31
+ "@teambit/harmony.envs.core-aspect-env": "0.0.14"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "@teambit/legacy": "1.0.624"
package/checkout-cmd.ts DELETED
@@ -1,261 +0,0 @@
1
- import chalk from 'chalk';
2
- import { Command, CommandOptions } from '@teambit/cli';
3
- import {
4
- ApplyVersionResults,
5
- applyVersionReport,
6
- conflictSummaryReport,
7
- installationErrorOutput,
8
- compilationErrorOutput,
9
- getRemovedOutput,
10
- getAddedOutput,
11
- } from '@teambit/merging';
12
- import { COMPONENT_PATTERN_HELP, HEAD, LATEST } from '@teambit/legacy/dist/constants';
13
- import { MergeStrategy } from '@teambit/legacy/dist/consumer/versions-ops/merge-version';
14
- import { ComponentID } from '@teambit/component-id';
15
- import { BitError } from '@teambit/bit-error';
16
- import { CheckoutMain, CheckoutProps } from './checkout.main.runtime';
17
-
18
- export class CheckoutCmd implements Command {
19
- name = 'checkout <to> [component-pattern]';
20
- arguments = [
21
- {
22
- name: 'to',
23
- description:
24
- "permitted values: [head, latest, reset, specific-version]. 'head' - last snap/tag. 'latest' - semver latest tag. 'reset' - removes local changes",
25
- },
26
- {
27
- name: 'component-pattern',
28
- description: COMPONENT_PATTERN_HELP,
29
- },
30
- ];
31
- description = 'switch between component versions or remove local changes';
32
- helpUrl = 'reference/components/merging-changes#checkout-snaps-to-the-working-directory';
33
- group = 'development';
34
- extendedDescription = `
35
- \`bit checkout <version> [component-pattern]\` => checkout the specified ids (or all components when --all is used) to the specified version
36
- \`bit checkout head [component-pattern]\` => checkout to the last snap/tag (use --latest if you only want semver tags), omit [component-pattern] to checkout head for all
37
- \`bit checkout latest [component-pattern]\` => checkout to the latest satisfying semver tag, omit [component-pattern] to checkout latest for all
38
- \`bit checkout reset [component-pattern]\` => remove local modifications from the specified ids (or all components when --all is used)
39
- when on a lane, "checkout head" only checks out components on this lane. to update main components, run "bit lane merge main"`;
40
- alias = 'U';
41
- options = [
42
- [
43
- 'i',
44
- 'interactive-merge',
45
- 'when a component is modified and the merge process found conflicts, display options to resolve them',
46
- ],
47
- ['', 'ours', 'DEPRECATED. use --auto-merge-resolve. In the future, this flag will leave the current code intact'],
48
- [
49
- '',
50
- 'theirs',
51
- 'DEPRECATED. use --auto-merge-resolve. In the future, this flag will override the current code with the incoming code',
52
- ],
53
- ['', 'manual', 'DEPRECATED. use --auto-merge-resolve'],
54
- [
55
- '',
56
- 'auto-merge-resolve <merge-strategy>',
57
- 'in case of merge conflict, resolve according to the provided strategy: [ours, theirs, manual]',
58
- ],
59
- ['r', 'reset', 'revert changes that were not snapped/tagged'],
60
- ['a', 'all', 'all components'],
61
- [
62
- 'e',
63
- 'workspace-only',
64
- "only relevant for 'bit checkout head' when on a lane. don't import components from the remote lane that are not already in the workspace",
65
- ],
66
- ['v', 'verbose', 'showing verbose output for inspection'],
67
- ['x', 'skip-dependency-installation', 'do not auto-install dependencies of the imported components'],
68
- ] as CommandOptions;
69
- loader = true;
70
-
71
- constructor(private checkout: CheckoutMain) {}
72
-
73
- async report(
74
- [to, componentPattern]: [string, string],
75
- {
76
- interactiveMerge = false,
77
- ours = false,
78
- theirs = false,
79
- manual = false,
80
- autoMergeResolve,
81
- all = false,
82
- workspaceOnly = false,
83
- verbose = false,
84
- skipDependencyInstallation = false,
85
- revert = false,
86
- }: {
87
- interactiveMerge?: boolean;
88
- ours?: boolean;
89
- theirs?: boolean;
90
- manual?: boolean;
91
- autoMergeResolve?: MergeStrategy;
92
- all?: boolean;
93
- workspaceOnly?: boolean;
94
- verbose?: boolean;
95
- skipDependencyInstallation?: boolean;
96
- revert?: boolean;
97
- }
98
- ) {
99
- if (ours || theirs || manual) {
100
- throw new BitError(
101
- 'the "--ours", "--theirs" and "--manual" flags are deprecated. use "--auto-merge-resolve" instead.'
102
- );
103
- }
104
- if (
105
- autoMergeResolve &&
106
- autoMergeResolve !== 'ours' &&
107
- autoMergeResolve !== 'theirs' &&
108
- autoMergeResolve !== 'manual'
109
- ) {
110
- throw new BitError('--auto-merge-resolve must be one of the following: [ours, theirs, manual]');
111
- }
112
- if (workspaceOnly && to !== HEAD) {
113
- throw new BitError('--workspace-only is only relevant when running "bit checkout head" on a lane');
114
- }
115
- const checkoutProps: CheckoutProps = {
116
- promptMergeOptions: interactiveMerge,
117
- mergeStrategy: autoMergeResolve,
118
- all,
119
- verbose,
120
- isLane: false,
121
- skipNpmInstall: skipDependencyInstallation,
122
- workspaceOnly,
123
- revert,
124
- };
125
- if (to === HEAD) checkoutProps.head = true;
126
- else if (to === LATEST) checkoutProps.latest = true;
127
- else if (to === 'reset') checkoutProps.reset = true;
128
- else if (to === 'main') checkoutProps.main = true;
129
- else {
130
- if (!ComponentID.isValidVersion(to)) throw new BitError(`the specified version "${to}" is not a valid version`);
131
- checkoutProps.version = to;
132
- }
133
-
134
- const checkoutResults = await this.checkout.checkoutByCLIValues(componentPattern || '', checkoutProps);
135
- return checkoutOutput(checkoutResults, checkoutProps);
136
- }
137
- }
138
-
139
- export function checkoutOutput(checkoutResults: ApplyVersionResults, checkoutProps: CheckoutProps) {
140
- const {
141
- components,
142
- version,
143
- failedComponents,
144
- removedComponents,
145
- addedComponents,
146
- leftUnresolvedConflicts,
147
- newFromLane,
148
- newFromLaneAdded,
149
- installationError,
150
- compilationError,
151
- }: ApplyVersionResults = checkoutResults;
152
-
153
- const { head, reset, latest, main, revert, verbose, all } = checkoutProps;
154
-
155
- // components that failed for no legitimate reason. e.g. merge-conflict.
156
- const realFailedComponents = (failedComponents || []).filter((f) => !f.unchangedLegitimately);
157
- // components that weren't checked out for legitimate reasons, e.g. up-to-date.
158
- const notCheckedOutComponents = (failedComponents || []).filter((f) => f.unchangedLegitimately);
159
-
160
- const getFailureOutput = () => {
161
- if (!realFailedComponents.length) return '';
162
- const title = 'the checkout has been failed on the following component(s)';
163
- const body = realFailedComponents
164
- .map(
165
- (failedComponent) =>
166
- `${chalk.bold(failedComponent.id.toString())} - ${chalk.red(failedComponent.unchangedMessage)}`
167
- )
168
- .join('\n');
169
- return `${chalk.underline(title)}\n${body}\n\n`;
170
- };
171
- const getNotCheckedOutOutput = () => {
172
- if (!notCheckedOutComponents.length) return '';
173
- if (!verbose && all) {
174
- return chalk.green(
175
- `checkout was not needed for ${chalk.bold(
176
- notCheckedOutComponents.length.toString()
177
- )} components (use --verbose to get more details)\n`
178
- );
179
- }
180
- const title = 'checkout was not required for the following component(s)';
181
- const body = notCheckedOutComponents
182
- .map((failedComponent) => `${failedComponent.id.toString()} - ${failedComponent.unchangedMessage}`)
183
- .join('\n');
184
- return `${chalk.underline(title)}\n${body}\n\n`;
185
- };
186
- const getConflictSummary = () => {
187
- if (!components || !components.length || !leftUnresolvedConflicts) return '';
188
- const title = `\n\nfiles with conflicts summary\n`;
189
- const suggestion = `\n\nfix the conflicts above manually and then run "bit install".
190
- once ready, snap/tag the components to persist the changes`;
191
- return chalk.underline(title) + conflictSummaryReport(components) + chalk.yellow(suggestion);
192
- };
193
- const getSuccessfulOutput = () => {
194
- const switchedOrReverted = revert ? 'reverted' : 'switched';
195
- if (!components || !components.length) return '';
196
- if (components.length === 1) {
197
- const component = components[0];
198
- const componentName = reset ? component.id.toString() : component.id.toStringWithoutVersion();
199
- if (reset) return `successfully reset ${chalk.bold(componentName)}\n`;
200
- const title = `successfully ${switchedOrReverted} ${chalk.bold(componentName)} to version ${chalk.bold(
201
- // @ts-ignore version is defined when !reset
202
- head || latest ? component.id.version : version
203
- )}\n`;
204
- return chalk.bold(title) + applyVersionReport(components, false);
205
- }
206
- if (reset) {
207
- const title = 'successfully reset the following components\n\n';
208
- const body = components.map((component) => chalk.bold(component.id.toString())).join('\n');
209
- return chalk.underline(title) + body;
210
- }
211
- const getVerOutput = () => {
212
- if (head) return 'their head version';
213
- if (latest) return 'their latest version';
214
- if (main) return 'their main version';
215
- // @ts-ignore version is defined when !reset
216
- return `version ${chalk.bold(version)}`;
217
- };
218
- const versionOutput = getVerOutput();
219
- const title = `successfully ${switchedOrReverted} ${components.length} components to ${versionOutput}\n`;
220
- const showVersion = head || reset;
221
- return chalk.bold(title) + applyVersionReport(components, true, showVersion);
222
- };
223
- const getNewOnLaneOutput = () => {
224
- if (!newFromLane?.length) return '';
225
- const title = newFromLaneAdded
226
- ? `successfully added the following components from the lane`
227
- : `the following components exist on the lane but were not added to the workspace. omit --workspace-only flag to add them`;
228
- const body = newFromLane.join('\n');
229
- return `\n\n${chalk.underline(title)}\n${body}`;
230
- };
231
- const getSummary = () => {
232
- const checkedOut = components?.length || 0;
233
- const notCheckedOutLegitimately = notCheckedOutComponents.length;
234
- const failedToCheckOut = realFailedComponents.length;
235
- const newLines = '\n\n';
236
- const title = chalk.bold.underline('Summary');
237
- const checkedOutStr = `\nTotal Changed: ${chalk.bold(checkedOut.toString())}`;
238
- const unchangedLegitimatelyStr = `\nTotal Unchanged: ${chalk.bold(notCheckedOutLegitimately.toString())}`;
239
- const failedToCheckOutStr = `\nTotal Failed: ${chalk.bold(failedToCheckOut.toString())}`;
240
- const newOnLaneNum = newFromLane?.length || 0;
241
- const newOnLaneAddedStr = newFromLaneAdded ? ' (added)' : ' (not added)';
242
- const newOnLaneStr = newOnLaneNum
243
- ? `\nNew on lane${newOnLaneAddedStr}: ${chalk.bold(newOnLaneNum.toString())}`
244
- : '';
245
-
246
- return newLines + title + checkedOutStr + unchangedLegitimatelyStr + failedToCheckOutStr + newOnLaneStr;
247
- };
248
-
249
- return (
250
- getFailureOutput() +
251
- getNotCheckedOutOutput() +
252
- getSuccessfulOutput() +
253
- getRemovedOutput(removedComponents) +
254
- getAddedOutput(addedComponents) +
255
- getNewOnLaneOutput() +
256
- getConflictSummary() +
257
- getSummary() +
258
- installationErrorOutput(installationError) +
259
- compilationErrorOutput(compilationError)
260
- );
261
- }
@@ -1,240 +0,0 @@
1
- import * as path from 'path';
2
- import { Consumer } from '@teambit/legacy/dist/consumer';
3
- import { ComponentID } from '@teambit/component-id';
4
- import GeneralError from '@teambit/legacy/dist/error/general-error';
5
- import Version from '@teambit/legacy/dist/scope/models/version';
6
- import { SourceFile } from '@teambit/legacy/dist/consumer/component/sources';
7
- import { pathNormalizeToLinux, PathOsBased } from '@teambit/legacy/dist/utils/path';
8
- import DataToPersist from '@teambit/legacy/dist/consumer/component/sources/data-to-persist';
9
- import RemovePath from '@teambit/legacy/dist/consumer/component/sources/remove-path';
10
- import {
11
- ApplyVersionResult,
12
- FilesStatus,
13
- FileStatus,
14
- MergeOptions,
15
- MergeStrategy,
16
- } from '@teambit/legacy/dist/consumer/versions-ops/merge-version';
17
- import { MergeResultsThreeWay } from '@teambit/legacy/dist/consumer/versions-ops/merge-version/three-way-merge';
18
- import ConsumerComponent from '@teambit/legacy/dist/consumer/component';
19
- import { BitError } from '@teambit/bit-error';
20
- import chalk from 'chalk';
21
-
22
- export type CheckoutProps = {
23
- version?: string; // if reset is true, the version is undefined
24
- ids?: ComponentID[];
25
- latestVersion?: boolean;
26
- promptMergeOptions?: boolean;
27
- mergeStrategy?: MergeStrategy | null;
28
- verbose?: boolean;
29
- skipNpmInstall?: boolean;
30
- ignorePackageJson?: boolean;
31
- writeConfig?: boolean;
32
- reset?: boolean; // remove local changes. if set, the version is undefined.
33
- all?: boolean; // checkout all ids
34
- ignoreDist?: boolean;
35
- isLane?: boolean;
36
- };
37
-
38
- export type ComponentStatusBase = {
39
- currentComponent?: ConsumerComponent;
40
- componentFromModel?: Version;
41
- id: ComponentID;
42
- shouldBeRemoved?: boolean; // in case the component is soft-removed, it should be removed from the workspace
43
- unchangedMessage?: string; // this gets populated either upon skip or failure.
44
- unchangedLegitimately?: boolean; // true for skipped legitimately (e.g. already up to date). false for failure.
45
- };
46
-
47
- export type ComponentStatus = ComponentStatusBase & {
48
- mergeResults?: MergeResultsThreeWay | null | undefined;
49
- };
50
-
51
- export type ApplyVersionWithComps = {
52
- applyVersionResult: ApplyVersionResult;
53
- component?: ConsumerComponent;
54
- // in case the component needs to be written to the filesystem, this is the component to write.
55
- legacyCompToWrite?: ConsumerComponent;
56
- };
57
-
58
- /**
59
- * 1) when the files are modified with conflicts and the strategy is "ours", leave the FS as is
60
- * and update only bitmap id version. (not the componentMap object).
61
- *
62
- * 2) when the files are modified with conflicts and the strategy is "theirs", write the component
63
- * according to id.version.
64
- *
65
- * 3) when files are modified with no conflict or files are modified with conflicts and the
66
- * strategy is manual, load the component according to id.version and update component.files.
67
- * applyModifiedVersion() docs explains what files are updated/added.
68
- *
69
- * 4) when --reset flag is used, write the component according to the bitmap version
70
- *
71
- * Side note:
72
- * Deleted file => if files are in used version but not in the modified one, no need to delete it. (similar to git).
73
- * Added file => if files are not in used version but in the modified one, they'll be under mergeResults.addFiles
74
- */
75
- export async function applyVersion(
76
- consumer: Consumer,
77
- id: ComponentID,
78
- componentFromFS: ConsumerComponent | null | undefined, // it can be null only when isLanes is true
79
- mergeResults: MergeResultsThreeWay | null | undefined,
80
- checkoutProps: CheckoutProps
81
- ): Promise<ApplyVersionWithComps> {
82
- if (!checkoutProps.isLane && !componentFromFS)
83
- throw new Error(`applyVersion expect to get componentFromFS for ${id.toString()}`);
84
- const { mergeStrategy } = checkoutProps;
85
- let filesStatus = {};
86
- if (mergeResults && mergeResults.hasConflicts && mergeStrategy === MergeOptions.ours) {
87
- // even when isLane is true, the mergeResults is possible only when the component is on the filesystem
88
- // otherwise it's impossible to have conflicts
89
- if (!componentFromFS) throw new Error(`applyVersion expect to get componentFromFS for ${id.toString()}`);
90
- componentFromFS.files.forEach((file) => {
91
- filesStatus[pathNormalizeToLinux(file.relative)] = FileStatus.unchanged;
92
- });
93
- consumer.bitMap.updateComponentId(id);
94
- return { applyVersionResult: { id, filesStatus } };
95
- }
96
- const component = await consumer.loadComponentFromModelImportIfNeeded(id);
97
- const componentMap = componentFromFS && componentFromFS.componentMap;
98
- if (componentFromFS && !componentMap) throw new GeneralError('applyVersion: componentMap was not found');
99
-
100
- const files = component.files;
101
- updateFileStatus(files, filesStatus, componentFromFS || undefined);
102
-
103
- await removeFilesIfNeeded(filesStatus, componentFromFS || undefined);
104
-
105
- if (mergeResults) {
106
- // update files according to the merge results
107
- const { filesStatus: modifiedStatus, modifiedFiles } = applyModifiedVersion(files, mergeResults, mergeStrategy);
108
- filesStatus = { ...filesStatus, ...modifiedStatus };
109
- component.files = modifiedFiles;
110
- }
111
-
112
- return {
113
- applyVersionResult: { id, filesStatus },
114
- component,
115
- };
116
- }
117
-
118
- export function updateFileStatus(files: SourceFile[], filesStatus: FilesStatus, componentFromFS?: ConsumerComponent) {
119
- files.forEach((file) => {
120
- const fileFromFs = componentFromFS?.files.find((f) => f.relative === file.relative);
121
- const areFilesEqual = fileFromFs && Buffer.compare(fileFromFs.contents, file.contents) === 0;
122
- // @ts-ignore
123
- filesStatus[pathNormalizeToLinux(file.relative)] = areFilesEqual ? FileStatus.unchanged : FileStatus.updated;
124
- });
125
- }
126
-
127
- /**
128
- * when files exist on the filesystem but not on the checked out versions, they need to be deleted.
129
- * without this function, these files would be left on the filesystem. (we don't delete the comp-dir before writing).
130
- * this needs to be done *before* the component is written to the filesystem, otherwise, it won't work when a file
131
- * has a case change. e.g. from uppercase to lowercase. (see merge-lane.e2e 'renaming files from uppercase to lowercase').
132
- */
133
- export async function removeFilesIfNeeded(filesStatus: FilesStatus, componentFromFS?: ConsumerComponent) {
134
- if (!componentFromFS) return;
135
- const filePathsFromFS = componentFromFS.files || [];
136
- const dataToPersist = new DataToPersist();
137
- filePathsFromFS.forEach((file) => {
138
- const filename = pathNormalizeToLinux(file.relative);
139
- if (!filesStatus[filename]) {
140
- // @ts-ignore todo: typescript has a good point here. it should be the string "removed", not chalk.green(removed).
141
- filesStatus[filename] = FileStatus.removed;
142
- dataToPersist.removePath(new RemovePath(file.path));
143
- }
144
- });
145
- await dataToPersist.persistAllToFS();
146
- }
147
-
148
- /**
149
- * relevant only when
150
- * 1) there is no conflict => add files from mergeResults: addFiles, overrideFiles and modifiedFiles.output.
151
- * 2) there is conflict and mergeStrategy is manual => add files from mergeResults: addFiles, overrideFiles and modifiedFiles.conflict.
152
- *
153
- * this function only updates the files content, it doesn't write the files
154
- */
155
- export function applyModifiedVersion(
156
- componentFiles: SourceFile[],
157
- mergeResults: MergeResultsThreeWay,
158
- mergeStrategy: MergeStrategy | null | undefined
159
- ): { filesStatus: Record<string, any>; modifiedFiles: SourceFile[] } {
160
- let modifiedFiles = componentFiles.map((file) => file.clone());
161
- const filesStatus = {};
162
- if (mergeResults.hasConflicts && mergeStrategy !== MergeOptions.manual) {
163
- return { filesStatus, modifiedFiles };
164
- }
165
- mergeResults.modifiedFiles.forEach((file) => {
166
- const filePath: PathOsBased = path.normalize(file.filePath);
167
- const foundFile = modifiedFiles.find((componentFile) => componentFile.relative === filePath);
168
- if (!foundFile) throw new GeneralError(`file ${filePath} not found`);
169
- if (file.conflict) {
170
- foundFile.contents = Buffer.from(file.conflict);
171
- filesStatus[file.filePath] = FileStatus.manual;
172
- } else if (typeof file.output === 'string') {
173
- foundFile.contents = Buffer.from(file.output);
174
- filesStatus[file.filePath] = FileStatus.merged;
175
- } else if (file.isBinaryConflict) {
176
- // leave the file as is and notify the user later about it.
177
- foundFile.contents = file.fsFile.contents;
178
- filesStatus[file.filePath] = FileStatus.binaryConflict;
179
- } else {
180
- throw new GeneralError(`file ${filePath} does not have output nor conflict`);
181
- }
182
- });
183
-
184
- mergeResults.addFiles.forEach((file) => {
185
- const filePath: PathOsBased = path.normalize(file.filePath);
186
- if (modifiedFiles.find((m) => m.relative === filePath)) return;
187
- modifiedFiles.push(file.fsFile);
188
- filesStatus[file.filePath] = FileStatus.added;
189
- });
190
- mergeResults.deletedConflictFiles.forEach((file) => {
191
- if (!file.fsFile) return;
192
- const filePath: PathOsBased = path.normalize(file.filePath);
193
- if (modifiedFiles.find((m) => m.relative === filePath)) return;
194
- modifiedFiles.push(file.fsFile);
195
- filesStatus[file.filePath] = FileStatus.added;
196
- });
197
- mergeResults.removeFiles.forEach((file) => {
198
- const filePath: PathOsBased = path.normalize(file.filePath);
199
- filesStatus[file.filePath] = FileStatus.removed;
200
- modifiedFiles = modifiedFiles.filter((f) => f.relative !== filePath);
201
- });
202
- mergeResults.remainDeletedFiles.forEach((file) => {
203
- const filePath: PathOsBased = path.normalize(file.filePath);
204
- modifiedFiles = modifiedFiles.filter((f) => f.relative !== filePath);
205
- filesStatus[file.filePath] = FileStatus.remainDeleted;
206
- });
207
- mergeResults.deletedConflictFiles.forEach((file) => {
208
- filesStatus[file.filePath] = FileStatus.deletedConflict;
209
- });
210
-
211
- mergeResults.overrideFiles.forEach((file) => {
212
- const filePath: PathOsBased = path.normalize(file.filePath);
213
- const foundFile = modifiedFiles.find((componentFile) => componentFile.relative === filePath);
214
- if (!foundFile) throw new GeneralError(`file ${filePath} not found`);
215
- foundFile.contents = file.fsFile.contents;
216
- filesStatus[file.filePath] = FileStatus.overridden;
217
- });
218
- mergeResults.updatedFiles.forEach((file) => {
219
- const filePath: PathOsBased = path.normalize(file.filePath);
220
- const foundFile = modifiedFiles.find((componentFile) => componentFile.relative === filePath);
221
- if (!foundFile) throw new GeneralError(`file ${filePath} not found`);
222
- foundFile.contents = file.content;
223
- filesStatus[file.filePath] = FileStatus.updated;
224
- });
225
-
226
- return { filesStatus, modifiedFiles };
227
- }
228
-
229
- export function throwForFailures(allComponentsStatus: ComponentStatusBase[]) {
230
- const failedComponents = allComponentsStatus.filter((c) => c.unchangedMessage && !c.unchangedLegitimately);
231
- if (failedComponents.length) {
232
- const failureMsgs = failedComponents
233
- .map(
234
- (failedComponent) =>
235
- `${chalk.bold(failedComponent.id.toString())} - ${chalk.red(failedComponent.unchangedMessage as string)}`
236
- )
237
- .join('\n');
238
- throw new BitError(`unable to proceed due to the following failures:\n${failureMsgs}`);
239
- }
240
- }
@@ -1,5 +0,0 @@
1
- import { Aspect } from '@teambit/harmony';
2
-
3
- export const CheckoutAspect = Aspect.create({
4
- id: 'teambit.component/checkout',
5
- });
@@ -1,514 +0,0 @@
1
- import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
2
- import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
3
- import WorkspaceAspect, { OutsideWorkspaceError, Workspace } from '@teambit/workspace';
4
- import { BitError } from '@teambit/bit-error';
5
- import { compact } from 'lodash';
6
- import { BEFORE_CHECKOUT } from '@teambit/legacy/dist/cli/loader/loader-messages';
7
- import RemoveAspect, { RemoveMain } from '@teambit/remove';
8
- import { ApplyVersionResults, FailedComponents } from '@teambit/merging';
9
- import ImporterAspect, { ImporterMain } from '@teambit/importer';
10
- import { HEAD, LATEST } from '@teambit/legacy/dist/constants';
11
- import { ComponentWriterAspect, ComponentWriterMain } from '@teambit/component-writer';
12
- import {
13
- getMergeStrategyInteractive,
14
- MergeStrategy,
15
- threeWayMerge,
16
- } from '@teambit/legacy/dist/consumer/versions-ops/merge-version';
17
- import GeneralError from '@teambit/legacy/dist/error/general-error';
18
- import mapSeries from 'p-map-series';
19
- import { ComponentIdList, ComponentID } from '@teambit/component-id';
20
- import { Version, ModelComponent, Lane } from '@teambit/legacy/dist/scope/models';
21
- import { Tmp } from '@teambit/legacy/dist/scope/repositories';
22
- import ComponentNotFoundInPath from '@teambit/legacy/dist/consumer/component/exceptions/component-not-found-in-path';
23
- import { CheckoutCmd } from './checkout-cmd';
24
- import { CheckoutAspect } from './checkout.aspect';
25
- import { applyVersion, ComponentStatus, ComponentStatusBase, throwForFailures } from './checkout-version';
26
- import { RevertCmd } from './revert-cmd';
27
-
28
- export type CheckoutProps = {
29
- version?: string; // if reset/head/latest is true, the version is undefined
30
- ids?: ComponentID[];
31
- head?: boolean;
32
- latest?: boolean;
33
- main?: boolean; // relevant for "revert" only
34
- promptMergeOptions?: boolean;
35
- mergeStrategy?: MergeStrategy | null;
36
- verbose?: boolean;
37
- skipNpmInstall?: boolean;
38
- reset?: boolean; // remove local changes. if set, the version is undefined.
39
- revert?: boolean; // change the files according to the given version, but don't change the bitmap version and don't try to merge
40
- all?: boolean; // checkout all ids
41
- isLane?: boolean;
42
- lane?: Lane; // currently needed for "bit switch" to tell the "fetch" where to fetch from
43
- workspaceOnly?: boolean;
44
- versionPerId?: ComponentID[]; // if given, the ComponentID.version is the version to checkout to.
45
- skipUpdatingBitmap?: boolean; // needed for stash
46
- restoreMissingComponents?: boolean; // in case .bitmap has a component and it's missing from the workspace, restore it (from model)
47
- allowAddingComponentsFromScope?: boolean; // in case the id doesn't exist in .bitmap, add it from the scope (relevant for switch)
48
- includeLocallyDeleted?: boolean; // include components that were deleted locally. currently enabled for "bit checkout reset" only.
49
- };
50
-
51
- export type ComponentStatusBeforeMergeAttempt = ComponentStatusBase & {
52
- propsForMerge?: {
53
- currentlyUsedVersion: string;
54
- componentModel: ModelComponent;
55
- };
56
- };
57
-
58
- export class CheckoutMain {
59
- constructor(
60
- private workspace: Workspace,
61
- private logger: Logger,
62
- private componentWriter: ComponentWriterMain,
63
- private importer: ImporterMain,
64
- private remove: RemoveMain
65
- ) {}
66
-
67
- async checkout(checkoutProps: CheckoutProps): Promise<ApplyVersionResults> {
68
- const consumer = this.workspace.consumer;
69
- const { version, ids, promptMergeOptions } = checkoutProps;
70
- await this.syncNewComponents(checkoutProps);
71
- const addedComponents = await this.restoreMissingComponents(checkoutProps);
72
- const bitIds = ComponentIdList.fromArray(ids?.map((id) => id) || []);
73
- // don't use Promise.all, it loads the components and this operation must be in sequence.
74
- const allComponentStatusBeforeMerge = await mapSeries(bitIds, (id) =>
75
- this.getComponentStatusBeforeMergeAttempt(id, checkoutProps)
76
- );
77
- const compsNeedMerge = allComponentStatusBeforeMerge.filter((c) => c.propsForMerge);
78
- const compsNotNeedMerge = allComponentStatusBeforeMerge.filter((c) => !c.propsForMerge) as ComponentStatus[];
79
-
80
- // in case the requested versions to checkout don't exist locally, import them.
81
- const toImport = allComponentStatusBeforeMerge
82
- .map((compStatus) => {
83
- const idsToImport = [compStatus.id];
84
- if (compStatus.propsForMerge) {
85
- idsToImport.push(compStatus.id.changeVersion(compStatus.propsForMerge.currentlyUsedVersion));
86
- }
87
- return idsToImport;
88
- })
89
- .flat();
90
-
91
- await this.workspace.scope.legacyScope.scopeImporter.importWithoutDeps(ComponentIdList.fromArray(toImport), {
92
- cache: true,
93
- lane: checkoutProps.lane,
94
- });
95
-
96
- const getComponentsStatusOfMergeNeeded = async (): Promise<ComponentStatus[]> => {
97
- const tmp = new Tmp(consumer.scope);
98
- try {
99
- const afterMergeAttempt = await Promise.all(compsNeedMerge.map((c) => this.getMergeStatus(c)));
100
- await tmp.clear();
101
- return afterMergeAttempt;
102
- } catch (err: any) {
103
- await tmp.clear();
104
- throw err;
105
- }
106
- };
107
-
108
- const compStatusMergeNeeded = await getComponentsStatusOfMergeNeeded();
109
-
110
- const allComponentsStatus: ComponentStatus[] = [...compStatusMergeNeeded, ...compsNotNeedMerge];
111
- const componentWithConflict = allComponentsStatus.find(
112
- (component) => component.mergeResults && component.mergeResults.hasConflicts
113
- );
114
- if (componentWithConflict) {
115
- if (!promptMergeOptions && !checkoutProps.mergeStrategy) {
116
- throw new GeneralError(
117
- `automatic merge has failed for component ${componentWithConflict.id.toStringWithoutVersion()}.\nplease use "--auto-merge-resolve" with 'manual', 'ours' or 'theirs' to resolve the conflict/s`
118
- );
119
- }
120
- if (!checkoutProps.mergeStrategy) checkoutProps.mergeStrategy = await getMergeStrategyInteractive();
121
- }
122
-
123
- throwForFailures(allComponentsStatus);
124
-
125
- const failedComponents: FailedComponents[] = allComponentsStatus
126
- .filter((componentStatus) => componentStatus.unchangedMessage)
127
- .filter((componentStatus) => !componentStatus.shouldBeRemoved)
128
- .map((componentStatus) => ({
129
- id: componentStatus.id,
130
- unchangedMessage: componentStatus.unchangedMessage as string,
131
- unchangedLegitimately: componentStatus.unchangedLegitimately,
132
- }));
133
-
134
- const succeededComponents = allComponentsStatus.filter((componentStatus) => !componentStatus.unchangedMessage);
135
- // do not use Promise.all for applyVersion. otherwise, it'll write all components in parallel,
136
- // which can be an issue when some components are also dependencies of others
137
- const checkoutPropsLegacy = { ...checkoutProps, ids: checkoutProps.ids?.map((id) => id) };
138
- const componentsResults = await mapSeries(succeededComponents, ({ id, currentComponent, mergeResults }) => {
139
- return applyVersion(consumer, id, currentComponent, mergeResults, checkoutPropsLegacy);
140
- });
141
-
142
- const componentsLegacy = compact(componentsResults.map((c) => c.component));
143
-
144
- let newFromLane: ComponentID[] | undefined;
145
- let newFromLaneAdded = false;
146
- if (checkoutProps.head) {
147
- newFromLane = await this.getNewComponentsFromLane(checkoutProps.ids || []);
148
- if (!checkoutProps.workspaceOnly) {
149
- const compsNewFromLane = await Promise.all(
150
- newFromLane.map((id) => consumer.loadComponentFromModelImportIfNeeded(id))
151
- );
152
- componentsLegacy.push(...compsNewFromLane);
153
- newFromLaneAdded = true;
154
- }
155
- }
156
-
157
- const leftUnresolvedConflicts = componentWithConflict && checkoutProps.mergeStrategy === 'manual';
158
- let componentWriterResults;
159
- if (componentsLegacy.length) {
160
- const manyComponentsWriterOpts = {
161
- components: componentsLegacy,
162
- skipDependencyInstallation: checkoutProps.skipNpmInstall || leftUnresolvedConflicts,
163
- verbose: checkoutProps.verbose,
164
- resetConfig: checkoutProps.reset,
165
- skipUpdatingBitMap: checkoutProps.skipUpdatingBitmap,
166
- reasonForBitmapChange: 'checkout',
167
- };
168
- componentWriterResults = await this.componentWriter.writeMany(manyComponentsWriterOpts);
169
- }
170
-
171
- const appliedVersionComponents = componentsResults.map((c) => c.applyVersionResult);
172
-
173
- const componentIdsToRemove = allComponentsStatus
174
- .filter((componentStatus) => componentStatus.shouldBeRemoved)
175
- .map((c) => c.id.changeVersion(undefined));
176
-
177
- if (componentIdsToRemove.length) {
178
- await this.remove.removeLocallyByIds(componentIdsToRemove, { force: true });
179
- }
180
-
181
- return {
182
- components: appliedVersionComponents,
183
- removedComponents: componentIdsToRemove,
184
- addedComponents,
185
- version,
186
- failedComponents,
187
- leftUnresolvedConflicts,
188
- newFromLane: newFromLane?.map((n) => n.toString()),
189
- newFromLaneAdded,
190
- installationError: componentWriterResults?.installationError,
191
- compilationError: componentWriterResults?.compilationError,
192
- };
193
- }
194
-
195
- /**
196
- * if .bitmap entry exists but the rootDir is missing from the filesystem, find the component in the scope and restore it.
197
- * returns the restored component ids.
198
- */
199
- async restoreMissingComponents(checkoutProps: CheckoutProps): Promise<ComponentID[] | undefined> {
200
- if (!checkoutProps.restoreMissingComponents) return undefined;
201
- const ids = checkoutProps.ids || [];
202
- const missing: ComponentID[] = [];
203
- await Promise.all(
204
- ids.map(async (id) => {
205
- const bitMapEntry = this.workspace.bitMap.getBitmapEntry(id, { ignoreVersion: true });
206
- if (bitMapEntry.noFilesError && bitMapEntry.noFilesError instanceof ComponentNotFoundInPath) {
207
- delete bitMapEntry.noFilesError;
208
- missing.push(id);
209
- }
210
- })
211
- );
212
- if (!missing.length) return undefined;
213
- const comps = await this.workspace.scope.getMany(missing);
214
- await this.componentWriter.writeMany({
215
- components: comps.map((c) => c.state._consumer),
216
- skipDependencyInstallation: true,
217
- skipUpdatingBitMap: true,
218
- });
219
-
220
- return missing;
221
- }
222
-
223
- async checkoutByCLIValues(componentPattern: string, checkoutProps: CheckoutProps): Promise<ApplyVersionResults> {
224
- const { revert, head } = checkoutProps;
225
- this.logger.setStatusLine(revert ? 'reverting components...' : BEFORE_CHECKOUT);
226
- if (!this.workspace) throw new OutsideWorkspaceError();
227
- const consumer = this.workspace.consumer;
228
- await this.importer.importCurrentObjects(); // important. among others, it fetches the remote lane object and its new components.
229
- if (head) await this.makeLaneComponentsAvailableOnMain();
230
- await this.parseValues(componentPattern, checkoutProps);
231
- const checkoutResults = await this.checkout(checkoutProps);
232
- await consumer.onDestroy(`checkout (${componentPattern})`);
233
- return checkoutResults;
234
- }
235
-
236
- private async syncNewComponents({ ids, head }: CheckoutProps) {
237
- if (!head) return;
238
- const notExported = ids?.filter((id) => !this.workspace.isExported(id)).map((id) => id.changeScope(id.scope));
239
- const scopeComponentsImporter = this.workspace.consumer.scope.scopeImporter;
240
- try {
241
- await scopeComponentsImporter.importWithoutDeps(ComponentIdList.fromArray(notExported || []).toVersionLatest(), {
242
- cache: false,
243
- reason: 'for making sure the new components are really new and are not out-of-sync',
244
- includeUnexported: true,
245
- });
246
- } catch (err) {
247
- // don't stop the process. it's possible that the scope doesn't exist yet because these are new components
248
- this.logger.error(`unable to sync new components, if these components are really new, ignore the error`, err);
249
- }
250
- }
251
-
252
- private async makeLaneComponentsAvailableOnMain() {
253
- const unavailableOnMain = await this.workspace.getUnavailableOnMainComponents();
254
- if (!unavailableOnMain.length) return;
255
- this.workspace.bitMap.makeComponentsAvailableOnMain(unavailableOnMain);
256
- }
257
-
258
- private async parseValues(componentPattern: string, checkoutProps: CheckoutProps) {
259
- if (checkoutProps.head && !componentPattern) {
260
- if (checkoutProps.all) {
261
- this.logger.console(`"--all" is deprecated for "bit checkout ${HEAD}", please omit it.`);
262
- }
263
- checkoutProps.all = true;
264
- }
265
- if (checkoutProps.latest && !componentPattern) {
266
- if (checkoutProps.all) {
267
- this.logger.console(`"--all" is deprecated for "bit checkout ${LATEST}", please omit it.`);
268
- }
269
- checkoutProps.all = true;
270
- }
271
- if (componentPattern && checkoutProps.all) {
272
- throw new GeneralError('please specify either [component-pattern] or --all, not both');
273
- }
274
- if (!componentPattern && !checkoutProps.all) {
275
- throw new GeneralError('please specify [component-pattern] or use --all flag');
276
- }
277
- if (checkoutProps.workspaceOnly && !checkoutProps.head) {
278
- throw new BitError(`--workspace-only flag can only be used with "head" (bit checkout head --workspace-only)`);
279
- }
280
- if (checkoutProps.revert) {
281
- checkoutProps.skipUpdatingBitmap = true;
282
- }
283
- if (checkoutProps.reset || checkoutProps.head) {
284
- checkoutProps.includeLocallyDeleted = true;
285
- }
286
-
287
- const getIds = async () => {
288
- if (componentPattern) {
289
- return this.workspace.idsByPattern(componentPattern, true, {
290
- includeDeleted: checkoutProps.includeLocallyDeleted,
291
- });
292
- }
293
- return checkoutProps.includeLocallyDeleted ? this.workspace.listIdsIncludeRemoved() : this.workspace.listIds();
294
- };
295
-
296
- const idsOnWorkspace = await getIds();
297
-
298
- const currentLane = await this.workspace.consumer.getCurrentLaneObject();
299
- const currentLaneIds = currentLane?.toBitIds();
300
- const ids = currentLaneIds ? idsOnWorkspace.filter((id) => currentLaneIds.hasWithoutVersion(id)) : idsOnWorkspace;
301
- checkoutProps.ids = ids.map((id) => (checkoutProps.head || checkoutProps.latest ? id.changeVersion(LATEST) : id));
302
- }
303
-
304
- private async getNewComponentsFromLane(ids: ComponentID[]): Promise<ComponentID[]> {
305
- // current lane object is up to date due to the previous `importCurrentObjects()` call
306
- const lane = await this.workspace.consumer.getCurrentLaneObject();
307
- if (!lane) {
308
- return [];
309
- }
310
- const laneBitIds = lane.toBitIds();
311
- const newIds = laneBitIds.filter((bitId) => !ids.find((id) => id.isEqualWithoutVersion(bitId)));
312
- const newComponentIds = await this.workspace.resolveMultipleComponentIds(newIds);
313
- const nonRemovedNewIds: ComponentID[] = [];
314
- await Promise.all(
315
- newComponentIds.map(async (id) => {
316
- const isRemoved = await this.workspace.scope.isComponentRemoved(id);
317
- if (!isRemoved) nonRemovedNewIds.push(id);
318
- })
319
- );
320
- return nonRemovedNewIds;
321
- }
322
-
323
- // eslint-disable-next-line complexity
324
- private async getComponentStatusBeforeMergeAttempt(
325
- id: ComponentID,
326
- checkoutProps: CheckoutProps
327
- ): Promise<ComponentStatusBeforeMergeAttempt> {
328
- const consumer = this.workspace.consumer;
329
- const { version, head: headVersion, reset, revert, main, latest: latestVersion, versionPerId } = checkoutProps;
330
- const repo = consumer.scope.objects;
331
-
332
- let existingBitMapId = consumer.bitMap.getComponentIdIfExist(id, { ignoreVersion: true });
333
- const getComponent = async () => {
334
- try {
335
- // TODO: check if we really need the { loadExtensions: true } here
336
- const results = await consumer.loadComponents(ComponentIdList.fromArray([id]), undefined, {
337
- loadExtensions: true,
338
- });
339
- if (results.components[0]) return results.components[0];
340
- if (checkoutProps.includeLocallyDeleted && results.removedComponents[0]) {
341
- return results.removedComponents[0];
342
- }
343
- } catch (err) {
344
- if (checkoutProps.allowAddingComponentsFromScope && !existingBitMapId) return undefined;
345
- throw err;
346
- }
347
- return undefined;
348
- };
349
- const component = await getComponent();
350
- if (component) {
351
- // the component might fix an out-of-sync issue and as a result, the id has changed
352
- id = component.id;
353
- existingBitMapId = consumer.bitMap.getComponentIdIfExist(id, { ignoreVersion: true });
354
- }
355
-
356
- const componentModel = await consumer.scope.getModelComponentIfExist(id);
357
- const componentStatus: ComponentStatusBeforeMergeAttempt = { id };
358
- const returnFailure = (msg: string, unchangedLegitimately = false) => {
359
- componentStatus.unchangedMessage = msg;
360
- componentStatus.unchangedLegitimately = unchangedLegitimately;
361
- return componentStatus;
362
- };
363
- if (!componentModel) {
364
- return returnFailure(`component ${id.toString()} is new, no version to checkout`, true);
365
- }
366
- if (main && !componentModel.head) {
367
- return returnFailure(`component ${id.toString()} is not available on main`);
368
- }
369
- const unmerged = repo.unmergedComponents.getEntry(id.fullName);
370
- if (!reset && unmerged) {
371
- return returnFailure(
372
- `component ${id.toStringWithoutVersion()} is in during-merge state, please snap/tag it first (or use bit merge --resolve/--abort)`
373
- );
374
- }
375
-
376
- const getNewVersion = async (): Promise<string> => {
377
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
378
- if (reset) return component!.id.version as string;
379
- if (headVersion) return componentModel.headIncludeRemote(repo);
380
- // we verified previously that head exists in case of "main"
381
- if (main) return componentModel.head?.toString() as string;
382
- if (latestVersion) {
383
- const latest = componentModel.latestVersionIfExist();
384
- return latest || componentModel.headIncludeRemote(repo);
385
- }
386
- if (versionPerId) {
387
- return versionPerId.find((bitId) => bitId.isEqualWithoutVersion(id))?.version as string;
388
- }
389
-
390
- // if all above are false, the version is defined
391
- return version as string;
392
- };
393
- const newVersion = await getNewVersion();
394
- if (version && !headVersion) {
395
- const hasVersion = await componentModel.hasVersion(version, repo);
396
- if (!hasVersion) return returnFailure(`component ${id.toStringWithoutVersion()} doesn't have version ${version}`);
397
- }
398
- const currentlyUsedVersion = existingBitMapId?.version;
399
- if (existingBitMapId && !currentlyUsedVersion) {
400
- return returnFailure(`component ${id.toStringWithoutVersion()} is new`);
401
- }
402
- if (version && currentlyUsedVersion === version) {
403
- // it won't be relevant for 'reset' as it doesn't have a version
404
- return returnFailure(`component ${id.toStringWithoutVersion()} is already at version ${version}`, true);
405
- }
406
- if (headVersion && currentlyUsedVersion === newVersion) {
407
- return returnFailure(
408
- `component ${id.toStringWithoutVersion()} is already at the latest version, which is ${newVersion}`,
409
- true
410
- );
411
- }
412
- if (!reset) {
413
- const divergeDataForMergePending = await componentModel.getDivergeDataForMergePending(repo);
414
- const isMergePending = divergeDataForMergePending.isDiverged();
415
- if (isMergePending) {
416
- return returnFailure(`component is merge-pending and cannot be checked out, run "bit status" for more info`);
417
- }
418
- }
419
- let isModified = false;
420
- if (currentlyUsedVersion) {
421
- const currentVersionObject: Version = await componentModel.loadVersion(currentlyUsedVersion, repo);
422
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
423
- isModified = await consumer.isComponentModified(currentVersionObject, component!);
424
- const isRemoved = component && component.isRemoved();
425
- if (!isModified && !isRemoved && reset) {
426
- return returnFailure(`component ${id.toStringWithoutVersion()} is not modified`, true);
427
- }
428
- }
429
-
430
- const versionRef = componentModel.getRef(newVersion);
431
- if (!versionRef) throw new Error(`unable to get ref ${newVersion} from ${componentModel.id()}`);
432
- const componentVersion = (await consumer.scope.getObject(versionRef.hash)) as Version | undefined;
433
- if (componentVersion?.isRemoved()) {
434
- if (existingBitMapId) componentStatus.shouldBeRemoved = true;
435
- return returnFailure(`component has been removed`, true);
436
- }
437
-
438
- const newId = id.changeVersion(newVersion);
439
-
440
- if (reset || !isModified || revert || !currentlyUsedVersion) {
441
- // if the component is not modified, no need to try merge the files, they will be written later on according to the
442
- // checked out version. same thing when no version is specified, it'll be reset to the model-version later.
443
-
444
- // if !currentlyUsedVersion it only exists in the model, so just write it. (happening during bit-switch/bit-lane-import)
445
- return { currentComponent: component, componentFromModel: componentVersion, id: newId };
446
- }
447
-
448
- const propsForMerge = {
449
- currentlyUsedVersion,
450
- componentModel,
451
- };
452
-
453
- return { currentComponent: component, componentFromModel: componentVersion, id: newId, propsForMerge };
454
- }
455
-
456
- private async getMergeStatus({
457
- currentComponent: componentFromFS,
458
- componentFromModel,
459
- id,
460
- propsForMerge,
461
- }: ComponentStatusBeforeMergeAttempt): Promise<ComponentStatus> {
462
- if (!propsForMerge) throw new Error(`propsForMerge is missing for ${id.toString()}`);
463
- if (!componentFromFS) throw new Error(`componentFromFS is missing for ${id.toString()}`);
464
- const consumer = this.workspace.consumer;
465
- const repo = consumer.scope.objects;
466
- const { currentlyUsedVersion, componentModel } = propsForMerge;
467
-
468
- // this is tricky. imagine the user is 0.0.2+modification and wants to checkout to 0.0.1.
469
- // the base is 0.0.1, as it's the common version for 0.0.1 and 0.0.2. however, if we let git merge-file use the 0.0.1
470
- // as the base, then, it'll get the changes done since 0.0.1 to 0.0.1, which is nothing, and put them on top of
471
- // 0.0.2+modification. in other words, it won't make any change.
472
- // this scenario of checking out while there are modified files, is forbidden in Git. here, we want to simulate a similar
473
- // experience of "git stash", then "git checkout", then "git stash pop". practically, we want the changes done on 0.0.2
474
- // to be added to 0.0.1
475
- // if there is no modification, it doesn't go the threeWayMerge anyway, so it doesn't matter what the base is.
476
- const baseVersion = currentlyUsedVersion;
477
- const newVersion = id.version as string;
478
- const baseComponent: Version = await componentModel.loadVersion(baseVersion, repo);
479
- const otherComponent: Version = await componentModel.loadVersion(newVersion, repo);
480
- const mergeResults = await threeWayMerge({
481
- consumer,
482
- otherComponent,
483
- otherLabel: newVersion,
484
- currentComponent: componentFromFS,
485
- currentLabel: `${currentlyUsedVersion} modified`,
486
- baseComponent,
487
- });
488
-
489
- return { currentComponent: componentFromFS, componentFromModel, id, mergeResults };
490
- }
491
-
492
- static slots = [];
493
- static dependencies = [CLIAspect, WorkspaceAspect, LoggerAspect, ComponentWriterAspect, ImporterAspect, RemoveAspect];
494
-
495
- static runtime = MainRuntime;
496
-
497
- static async provider([cli, workspace, loggerMain, compWriter, importer, remove]: [
498
- CLIMain,
499
- Workspace,
500
- LoggerMain,
501
- ComponentWriterMain,
502
- ImporterMain,
503
- RemoveMain
504
- ]) {
505
- const logger = loggerMain.createLogger(CheckoutAspect.id);
506
- const checkoutMain = new CheckoutMain(workspace, logger, compWriter, importer, remove);
507
- cli.register(new CheckoutCmd(checkoutMain), new RevertCmd(checkoutMain));
508
- return checkoutMain;
509
- }
510
- }
511
-
512
- CheckoutAspect.addRuntime(CheckoutMain);
513
-
514
- export default CheckoutMain;
package/index.ts DELETED
@@ -1,21 +0,0 @@
1
- import { CheckoutAspect } from './checkout.aspect';
2
-
3
- export type { CheckoutMain, CheckoutProps } from './checkout.main.runtime';
4
- export default CheckoutAspect;
5
- export { CheckoutAspect };
6
-
7
- export {
8
- applyModifiedVersion,
9
- applyVersion,
10
- removeFilesIfNeeded,
11
- updateFileStatus,
12
- throwForFailures,
13
- } from './checkout-version';
14
-
15
- export type {
16
- CheckoutProps as CheckoutPropsLegacy,
17
- ComponentStatus,
18
- ComponentStatusBase,
19
- ApplyVersionWithComps,
20
- } from './checkout-version';
21
- export { checkoutOutput } from './checkout-cmd';
package/revert-cmd.ts DELETED
@@ -1,45 +0,0 @@
1
- import { Command, CommandOptions } from '@teambit/cli';
2
- import { COMPONENT_PATTERN_HELP } from '@teambit/legacy/dist/constants';
3
- import { CheckoutMain } from './checkout.main.runtime';
4
- import { CheckoutCmd } from './checkout-cmd';
5
-
6
- export class RevertCmd implements Command {
7
- name = 'revert <component-pattern> <to>';
8
- arguments = [
9
- {
10
- name: 'component-pattern',
11
- description: COMPONENT_PATTERN_HELP,
12
- },
13
- {
14
- name: 'to',
15
- description: "permitted values: [main, specific-version]. 'main' - head version on main.",
16
- },
17
- ];
18
- description = 'replace the current component files by the specified version, leave the version intact';
19
- group = 'development';
20
- alias = '';
21
- options = [
22
- ['v', 'verbose', 'showing verbose output for inspection'],
23
- ['x', 'skip-dependency-installation', 'do not install packages of the imported components'],
24
- ] as CommandOptions;
25
- loader = true;
26
-
27
- constructor(private checkout: CheckoutMain) {}
28
-
29
- async report(
30
- [componentPattern, to]: [string, string],
31
- {
32
- verbose = false,
33
- skipDependencyInstallation = false,
34
- }: {
35
- verbose?: boolean;
36
- skipDependencyInstallation?: boolean;
37
- }
38
- ) {
39
- return new CheckoutCmd(this.checkout).report([to, componentPattern], {
40
- verbose,
41
- skipDependencyInstallation,
42
- revert: true,
43
- });
44
- }
45
- }