Skip to content

Commit 736994a

Browse files
committed
simplifying operations pattern
1 parent 600bcd2 commit 736994a

18 files changed

Lines changed: 109 additions & 71 deletions

File tree

packages/b2c-cli/src/commands/code/delete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class CodeDelete extends InstanceCommand<typeof CodeDelete> {
5353

5454
protected operations = {
5555
confirm,
56-
deleteCodeVersion: async (codeVersion: string) => deleteCodeVersion(this.instance, codeVersion),
56+
deleteCodeVersion,
5757
};
5858

5959
async run(): Promise<void> {
@@ -85,7 +85,7 @@ export default class CodeDelete extends InstanceCommand<typeof CodeDelete> {
8585
}),
8686
);
8787

88-
await this.operations.deleteCodeVersion(codeVersion);
88+
await this.operations.deleteCodeVersion(this.instance, codeVersion);
8989
this.log(t('commands.code.delete.deleted', 'Code version {{codeVersion}} deleted successfully', {codeVersion}));
9090
}
9191
}

packages/b2c-cli/src/commands/code/deploy.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ export default class CodeDeploy extends CartridgeCommand<typeof CodeDeploy> {
4848
};
4949

5050
protected operations = {
51-
uploadCartridges: async (cartridges: Parameters<typeof uploadCartridges>[1]) =>
52-
uploadCartridges(this.instance, cartridges),
53-
deleteCartridges: async (cartridges: Parameters<typeof deleteCartridges>[1]) =>
54-
deleteCartridges(this.instance, cartridges),
55-
getActiveCodeVersion: async () => getActiveCodeVersion(this.instance),
56-
reloadCodeVersion: async (codeVersion: string) => reloadCodeVersion(this.instance, codeVersion),
51+
uploadCartridges,
52+
deleteCartridges,
53+
getActiveCodeVersion,
54+
reloadCodeVersion,
5755
};
5856

5957
async run(): Promise<DeployResult> {
@@ -68,7 +66,7 @@ export default class CodeDeploy extends CartridgeCommand<typeof CodeDeploy> {
6866
this.warn(
6967
t('commands.code.deploy.noCodeVersion', 'No code version specified, discovering active code version...'),
7068
);
71-
const activeVersion = await this.operations.getActiveCodeVersion();
69+
const activeVersion = await this.operations.getActiveCodeVersion(this.instance);
7270
if (!activeVersion?.id) {
7371
this.error(
7472
t('commands.code.deploy.noActiveVersion', 'No active code version found. Specify one with --code-version.'),
@@ -128,17 +126,17 @@ export default class CodeDeploy extends CartridgeCommand<typeof CodeDeploy> {
128126
try {
129127
// Optionally delete existing cartridges first
130128
if (this.flags.delete) {
131-
await this.operations.deleteCartridges(cartridges);
129+
await this.operations.deleteCartridges(this.instance, cartridges);
132130
}
133131

134132
// Upload cartridges
135-
await this.operations.uploadCartridges(cartridges);
133+
await this.operations.uploadCartridges(this.instance, cartridges);
136134

137135
// Optionally reload code version
138136
let reloaded = false;
139137
if (this.flags.reload) {
140138
try {
141-
await this.operations.reloadCodeVersion(version);
139+
await this.operations.reloadCodeVersion(this.instance, version);
142140
reloaded = true;
143141
} catch (error) {
144142
this.logger?.debug(`Could not reload code version: ${error instanceof Error ? error.message : error}`);

packages/b2c-cli/src/commands/code/watch.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,7 @@ export default class CodeWatch extends CartridgeCommand<typeof CodeWatch> {
2828
};
2929

3030
protected operations = {
31-
watchCartridges: async () =>
32-
watchCartridges(this.instance, this.cartridgePath, {
33-
...this.cartridgeOptions,
34-
onUpload: (files) => {
35-
this.log(t('commands.code.watch.uploaded', '[UPLOAD] {{count}} file(s)', {count: files.length}));
36-
},
37-
onDelete: (files) => {
38-
this.log(t('commands.code.watch.deleted', '[DELETE] {{count}} file(s)', {count: files.length}));
39-
},
40-
onError: (error) => {
41-
this.warn(t('commands.code.watch.error', 'Error: {{message}}', {message: error.message}));
42-
},
43-
}),
31+
watchCartridges,
4432
};
4533

4634
async run(): Promise<void> {
@@ -57,7 +45,18 @@ export default class CodeWatch extends CartridgeCommand<typeof CodeWatch> {
5745
}
5846

5947
try {
60-
const result = await this.operations.watchCartridges();
48+
const result = await this.operations.watchCartridges(this.instance, this.cartridgePath, {
49+
...this.cartridgeOptions,
50+
onUpload: (files) => {
51+
this.log(t('commands.code.watch.uploaded', '[UPLOAD] {{count}} file(s)', {count: files.length}));
52+
},
53+
onDelete: (files) => {
54+
this.log(t('commands.code.watch.deleted', '[DELETE] {{count}} file(s)', {count: files.length}));
55+
},
56+
onError: (error) => {
57+
this.warn(t('commands.code.watch.error', 'Error: {{message}}', {message: error.message}));
58+
},
59+
});
6160

6261
this.log(
6362
t('commands.code.watch.watching', 'Watching {{count}} cartridge(s)...', {count: result.cartridges.length}),

packages/b2c-cli/src/commands/docs/download.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class DocsDownload extends InstanceCommand<typeof DocsDownload> {
3838
};
3939

4040
protected operations = {
41-
downloadDocs: async (input: Parameters<typeof downloadDocs>[1]) => downloadDocs(this.instance, input),
41+
downloadDocs,
4242
};
4343

4444
async run(): Promise<DownloadDocsResult> {
@@ -54,7 +54,7 @@ export default class DocsDownload extends InstanceCommand<typeof DocsDownload> {
5454
}),
5555
);
5656

57-
const result = await this.operations.downloadDocs({
57+
const result = await this.operations.downloadDocs(this.instance, {
5858
outputDir,
5959
keepArchive,
6060
});

packages/b2c-cli/src/commands/job/export.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ export default class JobExport extends JobCommand<typeof JobExport> {
9797
};
9898

9999
protected operations = {
100-
siteArchiveExportToPath: async (
101-
dataUnits: Parameters<typeof siteArchiveExportToPath>[1],
102-
output: Parameters<typeof siteArchiveExportToPath>[2],
103-
options: Parameters<typeof siteArchiveExportToPath>[3],
104-
) => siteArchiveExportToPath(this.instance, dataUnits, output, options),
100+
siteArchiveExportToPath,
105101
};
106102

107103
async run(): Promise<SiteArchiveExportResult & {localPath?: string}> {
@@ -181,7 +177,7 @@ export default class JobExport extends JobCommand<typeof JobExport> {
181177
this.log(t('commands.job.export.dataUnits', 'Data units: {{dataUnits}}', {dataUnits: JSON.stringify(dataUnits)}));
182178

183179
try {
184-
const result = await this.operations.siteArchiveExportToPath(dataUnits, output, {
180+
const result = await this.operations.siteArchiveExportToPath(this.instance, dataUnits, output, {
185181
keepArchive: keepArchive || noDownload,
186182
extractZip: !zipOnly,
187183
waitOptions: {

packages/b2c-cli/src/commands/job/import.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ export default class JobImport extends JobCommand<typeof JobImport> {
5757
};
5858

5959
protected operations = {
60-
siteArchiveImport: async (
61-
target: Parameters<typeof siteArchiveImport>[1],
62-
options: Parameters<typeof siteArchiveImport>[2],
63-
) => siteArchiveImport(this.instance, target, options),
60+
siteArchiveImport,
6461
};
6562

6663
async run(): Promise<SiteArchiveImportResult> {
@@ -114,7 +111,7 @@ export default class JobImport extends JobCommand<typeof JobImport> {
114111
try {
115112
const importTarget = remote ? {remoteFilename: target} : target;
116113

117-
const result = await this.operations.siteArchiveImport(importTarget, {
114+
const result = await this.operations.siteArchiveImport(this.instance, importTarget, {
118115
keepArchive,
119116
waitOptions: {
120117
timeout: timeout ? timeout * 1000 : undefined,

packages/b2c-cli/src/commands/job/run.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ export default class JobRun extends JobCommand<typeof JobRun> {
6767
};
6868

6969
protected operations = {
70-
executeJob: async (jobId: string, options: Parameters<typeof executeJob>[2]) =>
71-
executeJob(this.instance, jobId, options),
72-
waitForJob: async (jobId: string, executionId: string, options: Parameters<typeof waitForJob>[3]) =>
73-
waitForJob(this.instance, jobId, executionId, options),
70+
executeJob,
71+
waitForJob,
7472
};
7573

7674
async run(): Promise<JobExecution> {
@@ -113,7 +111,7 @@ export default class JobRun extends JobCommand<typeof JobRun> {
113111

114112
let execution: JobExecution;
115113
try {
116-
execution = await this.operations.executeJob(jobId, {
114+
execution = await this.operations.executeJob(this.instance, jobId, {
117115
parameters: rawBody ? undefined : parameters,
118116
body: rawBody,
119117
waitForRunning: !noWaitRunning,
@@ -220,7 +218,7 @@ export default class JobRun extends JobCommand<typeof JobRun> {
220218
this.log(t('commands.job.run.waiting', 'Waiting for job to complete...'));
221219

222220
try {
223-
const execution = await this.operations.waitForJob(jobId, executionId, {
221+
const execution = await this.operations.waitForJob(this.instance, jobId, executionId, {
224222
timeout: timeout ? timeout * 1000 : undefined,
225223
onProgress: (exec, elapsed) => {
226224
if (!this.jsonEnabled()) {

packages/b2c-cli/src/commands/job/search.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ export default class JobSearch extends InstanceCommand<typeof JobSearch> {
8080
};
8181

8282
protected operations = {
83-
searchJobExecutions: async (options: Parameters<typeof searchJobExecutions>[1]) =>
84-
searchJobExecutions(this.instance, options),
83+
searchJobExecutions,
8584
};
8685

8786
async run(): Promise<JobExecutionSearchResult> {
@@ -95,7 +94,7 @@ export default class JobSearch extends InstanceCommand<typeof JobSearch> {
9594
}),
9695
);
9796

98-
const results = await this.operations.searchJobExecutions({
97+
const results = await this.operations.searchJobExecutions(this.instance, {
9998
jobId,
10099
status,
101100
count,

packages/b2c-cli/src/commands/job/wait.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ export default class JobWait extends JobCommand<typeof JobWait> {
4747
};
4848

4949
protected operations = {
50-
waitForJob: async (jobId: string, executionId: string, options: Parameters<typeof waitForJob>[3]) =>
51-
waitForJob(this.instance, jobId, executionId, options),
50+
waitForJob,
5251
};
5352

5453
async run(): Promise<JobExecution> {
@@ -65,7 +64,7 @@ export default class JobWait extends JobCommand<typeof JobWait> {
6564
);
6665

6766
try {
68-
const execution = await this.operations.waitForJob(jobId, executionId, {
67+
const execution = await this.operations.waitForJob(this.instance, jobId, executionId, {
6968
timeout: timeout ? timeout * 1000 : undefined,
7069
pollInterval: pollInterval * 1000,
7170
onProgress: (exec, elapsed) => {

packages/b2c-cli/test/commands/code/delete.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,28 @@ describe('code delete', () => {
2424
it('deletes without prompting when --force is set', async () => {
2525
const command: any = await createCommand({force: true}, {codeVersion: 'v1'});
2626

27+
const instance = {config: {hostname: 'example.com'}};
28+
2729
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
2830
sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com'}}));
31+
sinon.stub(command, 'instance').get(() => instance);
2932
sinon.stub(command, 'log').returns(void 0);
3033

3134
const deleteStub = sinon.stub().resolves(void 0);
3235
command.operations = {...command.operations, deleteCodeVersion: deleteStub};
3336

3437
await command.run();
35-
expect(deleteStub.calledOnce).to.equal(true);
38+
expect(deleteStub.calledOnceWithExactly(instance, 'v1')).to.equal(true);
3639
});
3740

3841
it('does not delete when prompt is declined', async () => {
3942
const command: any = await createCommand({}, {codeVersion: 'v1'});
4043

44+
const instance = {config: {hostname: 'example.com'}};
45+
4146
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
4247
sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com'}}));
48+
sinon.stub(command, 'instance').get(() => instance);
4349
sinon.stub(command, 'log').returns(void 0);
4450

4551
const deleteStub = sinon.stub().rejects(new Error('Unexpected delete'));
@@ -55,8 +61,11 @@ describe('code delete', () => {
5561
it('deletes when prompt is accepted', async () => {
5662
const command: any = await createCommand({}, {codeVersion: 'v1'});
5763

64+
const instance = {config: {hostname: 'example.com'}};
65+
5866
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
5967
sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com'}}));
68+
sinon.stub(command, 'instance').get(() => instance);
6069
sinon.stub(command, 'log').returns(void 0);
6170

6271
const deleteStub = sinon.stub().resolves(void 0);
@@ -66,6 +75,6 @@ describe('code delete', () => {
6675
await command.run();
6776

6877
expect(confirmStub.calledOnce).to.equal(true);
69-
expect(deleteStub.calledOnce).to.equal(true);
78+
expect(deleteStub.calledOnceWithExactly(instance, 'v1')).to.equal(true);
7079
});
7180
});

0 commit comments

Comments
 (0)