-
Notifications
You must be signed in to change notification settings - Fork 6
/
push.ts
192 lines (175 loc) · 6.17 KB
/
push.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
import {CLICommand} from '@coveo/cli-commons/command/cliCommand';
import {Flags} from '@oclif/core';
import {formatOrgId, startSpinner} from '@coveo/cli-commons/utils/ux';
import {
HasNecessaryCoveoPrivileges,
IsAuthenticated,
Preconditions,
} from '@coveo/cli-commons/preconditions/index';
import {Snapshot} from '../../../lib/snapshot/snapshot';
import {SnapshotReporter} from '../../../lib/snapshot/snapshotReporter';
import {
dryRun,
handleReportWithErrors,
cleanupProject,
DryRunOptions,
getMissingVaultEntriesReportHandler,
getErrorReportHandler,
} from '../../../lib/snapshot/snapshotCommon';
import {Config} from '@coveo/cli-commons/config/config';
import {cwd} from 'process';
import {Project} from '../../../lib/project/project';
import {
PreviewLevelValue,
previewLevel,
wait,
} from '../../../lib/flags/snapshotCommonFlags';
import {
writeLinkPrivilege,
writeSnapshotPrivilege,
} from '@coveo/cli-commons/preconditions/platformPrivilege';
import {Trackable} from '@coveo/cli-commons/preconditions/trackable';
import {confirmWithAnalytics} from '../../../lib/utils/cli';
import {SnapshotReportStatus} from '../../../lib/snapshot/reportPreviewer/reportPreviewerDataModels';
import {Example} from '@oclif/core/lib/interfaces';
import {getTargetOrg} from '../../../lib/utils/platform';
import {organization} from '../../../lib/flags/platformCommonFlags';
export default class Push extends CLICommand {
public static description =
'Preview, validate and deploy your changes to the destination org';
public static flags = {
...wait(),
...previewLevel(),
...organization(
'The unique identifier of the organization where to send the changes'
),
deleteMissingResources: Flags.boolean({
description: 'Delete missing resources when enabled',
default: false,
required: false,
}),
};
public static examples: Example[] = [
{
command: 'coveo org:resources:push',
description:
'Preview, validate and deploy resources to the organization in which you are authenticated',
},
{
command: 'coveo org:resources:push --organization myorgid',
description:
'Preview, validate and deploy resources to the organization whose ID is "myorgid"',
},
{
command: 'coveo org:resources:push --previewLevel none',
description: 'Validate and deploy resources without displaying a preview',
},
{
command: 'coveo org:resources:push --deleteMissingResources',
description: `Preview, validate and deploy resources, but also delete from the organization all the resources that are not available inside the "${Project.resourceFolderName}/" directory`,
},
];
@Trackable()
@Preconditions(
IsAuthenticated(),
HasNecessaryCoveoPrivileges(writeSnapshotPrivilege, writeLinkPrivilege)
)
public async run() {
const {flags} = await this.parse(Push);
const target = getTargetOrg(this.configuration, flags.organization);
const cfg = this.configuration.get();
const options = await this.getOptions();
const {reporter, snapshot, project} = await dryRun(
target,
this.projectPath,
options
);
const shouldSkip = await this.shouldSkipPreview();
if (!shouldSkip) {
const display = await this.shouldDisplayExpandedPreview();
const {deleteMissingResources} = await this.getOptions();
await snapshot.preview(project, deleteMissingResources, display);
}
await reporter
.setReportHandler(
SnapshotReportStatus.SUCCESS,
this.getSuccessReportHandler(snapshot)
)
.setReportHandler(
SnapshotReportStatus.MISSING_VAULT_ENTRIES,
getMissingVaultEntriesReportHandler(snapshot, cfg, this.projectPath)
)
.setReportHandler(
SnapshotReportStatus.ERROR,
getErrorReportHandler(snapshot, cfg, this.projectPath)
)
.handleReport();
await this.cleanup(snapshot, project);
}
public catch(err?: Error & {exitCode?: number}) {
cleanupProject(this.projectPath);
return super.catch(err);
}
private async shouldSkipPreview() {
const {flags} = await this.parse(Push);
return flags.previewLevel === PreviewLevelValue.None;
}
private async shouldDisplayExpandedPreview() {
const {flags} = await this.parse(Push);
return flags.previewLevel === PreviewLevelValue.Detailed;
}
private async cleanup(snapshot: Snapshot, project: Project) {
await snapshot.delete();
project.deleteTemporaryZipFile();
}
private getSuccessReportHandler(snapshot: Snapshot) {
const successReportWithChangesHandler = () =>
this.successReportWithChangesHandler(snapshot);
return function (this: SnapshotReporter) {
return successReportWithChangesHandler();
};
}
private async successReportWithChangesHandler(snapshot: Snapshot) {
const canBeApplied =
(await this.shouldSkipPreview()) || (await this.askForConfirmation());
if (canBeApplied) {
await this.applySnapshot(snapshot);
}
}
private async askForConfirmation(): Promise<boolean> {
const {flags} = await this.parse(Push);
const target = getTargetOrg(this.configuration, flags.organization);
const question = `\nWould you like to apply the snapshot to the organization ${formatOrgId(
target
)}? (y/n)`;
return confirmWithAnalytics(question, 'snapshot apply');
}
private async applySnapshot(snapshot: Snapshot) {
startSpinner('Applying snapshot');
const cfg = this.configuration.get();
const {flags} = await this.parse(Push);
const {waitUntilDone} = await this.getOptions();
const reporter = await snapshot.apply(
flags.deleteMissingResources,
waitUntilDone
);
await reporter
.setReportHandler(SnapshotReportStatus.ERROR, async () => {
await handleReportWithErrors(snapshot, cfg, this.projectPath);
})
.handleReport();
}
private async getOptions(): Promise<DryRunOptions> {
const {flags} = await this.parse(Push);
return {
deleteMissingResources: flags.deleteMissingResources,
waitUntilDone: {wait: flags.wait},
};
}
private get configuration() {
return new Config(this.config.configDir);
}
private get projectPath() {
return cwd();
}
}