Skip to content

Commit e7beadb

Browse files
committed
refactor: address feedback, part 1
1 parent 710a6c5 commit e7beadb

10 files changed

Lines changed: 73 additions & 49 deletions

File tree

src/commands/content-item/import-revert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Arguments } from 'yargs';
44
import { FileLog } from '../../common/file-log';
55
import dynamicContentClientFactory from '../../services/dynamic-content-client-factory';
66
import { ContentItem } from 'dc-management-sdk-js';
7-
import { asyncQuestion } from '../../common/log-helpers';
7+
import { asyncQuestion } from '../../common/question-helpers';
88

99
export const revert = async (argv: Arguments<ImportItemBuilderOptions & ConfigurationParameters>): Promise<boolean> => {
1010
let log: FileLog;

src/commands/content-item/import.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import {
2828
} from '../../common/content-item/content-dependancy-tree';
2929

3030
import { AmplienceSchemaValidator, defaultSchemaLookup } from '../../common/content-item/amplience-schema-validator';
31-
import { getDefaultLogPath, asyncQuestion } from '../../common/log-helpers';
31+
import { getDefaultLogPath } from '../../common/log-helpers';
32+
import { asyncQuestion } from '../../common/question-helpers';
3233
import { PublishQueue } from '../../common/import/publish-queue';
3334
import { MediaRewriter } from '../../common/media/media-rewriter';
3435

src/commands/event/archive.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,24 @@ describe('event archive command', () => {
339339
expect(archiveMock).toBeCalledTimes(2);
340340
});
341341

342+
it('should archive events when multiple ids provided', async () => {
343+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
344+
(readline as any).setResponses(['y']);
345+
346+
const { mockGet, mockEditionsList, archiveMock } = mockValues({ status: 'PUBLISHED' });
347+
348+
const argv = {
349+
...yargArgs,
350+
...config,
351+
id: ['1', '2']
352+
};
353+
await handler(argv);
354+
355+
expect(mockGet).toHaveBeenCalledTimes(4);
356+
expect(mockEditionsList).toHaveBeenCalledTimes(2);
357+
expect(archiveMock).toBeCalledTimes(4);
358+
});
359+
342360
it('should delete event with scheduled edition', async () => {
343361
// eslint-disable-next-line @typescript-eslint/no-explicit-any
344362
(readline as any).setResponses(['y']);

src/commands/event/archive.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const getEvents = async ({
7777
hubId,
7878
name
7979
}: {
80-
id?: string;
80+
id?: string | string[];
8181
hubId: string;
8282
name?: string | string[];
8383
client: DynamicContent;
@@ -93,19 +93,23 @@ export const getEvents = async ({
9393
> => {
9494
try {
9595
if (id != null) {
96-
const event = await client.events.get(id);
97-
const editions = await paginator(event.related.editions.list);
98-
99-
return [
100-
{
101-
event,
102-
editions,
103-
command: 'ARCHIVE',
104-
unscheduleEditions: [],
105-
deleteEditions: [],
106-
archiveEditions: []
107-
}
108-
];
96+
const ids = Array.isArray(id) ? id : [id];
97+
98+
return await Promise.all(
99+
ids.map(async id => {
100+
const event = await client.events.get(id);
101+
const editions = await paginator(event.related.editions.list);
102+
103+
return {
104+
event,
105+
editions,
106+
command: 'ARCHIVE',
107+
unscheduleEditions: [],
108+
deleteEditions: [],
109+
archiveEditions: []
110+
};
111+
})
112+
);
109113
}
110114

111115
const hub = await client.hubs.get(hubId);

src/commands/hub/steps/schema-clone-step.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ export class SchemaCloneStep implements CloneHubStep {
5151
const toArchive = revertLog.getData('CREATE', this.getName());
5252
const toUpdate = revertLog.getData('UPDATE', this.getName());
5353

54-
for (let i = 0; i < toArchive.length; i++) {
54+
for (const id of toArchive) {
5555
try {
56-
const schema = await client.contentTypeSchemas.get(toArchive[i]);
56+
const schema = await client.contentTypeSchemas.get(id);
5757
if ((schema as ResourceStatus).status == Status.ACTIVE) {
5858
await schema.related.archive();
5959
}
6060
} catch (e) {
61-
state.logFile.appendLine(`Could not archive ${toArchive[i]}. Continuing...`);
61+
state.logFile.appendLine(`Could not archive ${id}. Continuing...`);
6262
}
6363
}
6464

65-
for (let i = 0; i < toUpdate.length; i++) {
66-
const updateArgs = toUpdate[i].split(' ');
65+
for (const id of toUpdate) {
66+
const updateArgs = id.split(' ');
6767

6868
try {
6969
const schema = await client.contentTypeSchemas.getByVersion(updateArgs[0], Number(updateArgs[1]));
@@ -74,7 +74,7 @@ export class SchemaCloneStep implements CloneHubStep {
7474
typeToSync.related.contentTypeSchema.update();
7575
}
7676
} catch (e) {
77-
state.logFile.appendLine(`Error while updating ${toUpdate[i]}. Continuing...`);
77+
state.logFile.appendLine(`Error while updating ${id}. Continuing...`);
7878
}
7979
}
8080

src/commands/settings/import.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import dynamicContentClientFactory from '../../services/dynamic-content-client-f
55
import { ImportSettingsBuilderOptions } from '../../interfaces/import-settings-builder-options.interface';
66
import { WorkflowStatesMapping } from '../../common/workflowStates/workflowStates-mapping';
77
import { FileLog } from '../../common/file-log';
8-
import { getDefaultLogPath, asyncQuestion } from '../../common/log-helpers';
8+
import { getDefaultLogPath } from '../../common/log-helpers';
9+
import { asyncQuestion } from '../../common/question-helpers';
910
import { join } from 'path';
1011
import { readFile } from 'fs';
1112
import { promisify } from 'util';

src/common/archive/archive-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { asyncQuestion } from '../log-helpers';
1+
import { asyncQuestion } from '../question-helpers';
22

33
export async function confirmArchive(
44
action: string,

src/common/log-helpers.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { join } from 'path';
2-
import readline, { ReadLine } from 'readline';
3-
import { FileLog } from './file-log';
42

53
export function getDefaultLogPath(type: string, action: string, platform: string = process.platform): string {
64
return join(
@@ -9,25 +7,3 @@ export function getDefaultLogPath(type: string, action: string, platform: string
97
`logs/${type}-${action}-<DATE>.log`
108
);
119
}
12-
13-
function asyncQuestionInternal(rl: ReadLine, question: string): Promise<string> {
14-
return new Promise((resolve): void => {
15-
rl.question(question, resolve);
16-
});
17-
}
18-
19-
export async function asyncQuestion(question: string, log?: FileLog): Promise<boolean> {
20-
const rl = readline.createInterface({
21-
input: process.stdin,
22-
output: process.stdout,
23-
terminal: false
24-
});
25-
26-
const answer = await asyncQuestionInternal(rl, question);
27-
rl.close();
28-
29-
if (log != null) {
30-
log.appendLine(question + answer, true);
31-
}
32-
return answer.length > 0 && answer[0].toLowerCase() === 'y';
33-
}

src/common/question-helpers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import readline, { ReadLine } from 'readline';
2+
import { FileLog } from './file-log';
3+
4+
function asyncQuestionInternal(rl: ReadLine, question: string): Promise<string> {
5+
return new Promise((resolve): void => {
6+
rl.question(question, resolve);
7+
});
8+
}
9+
10+
export async function asyncQuestion(question: string, log?: FileLog): Promise<boolean> {
11+
const rl = readline.createInterface({
12+
input: process.stdin,
13+
output: process.stdout,
14+
terminal: false
15+
});
16+
17+
const answer = await asyncQuestionInternal(rl, question);
18+
rl.close();
19+
20+
if (log != null) {
21+
log.appendLine(question + answer, true);
22+
}
23+
return answer.length > 0 && answer[0].toLowerCase() === 'y';
24+
}

src/services/export.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs';
22
import * as path from 'path';
33
import { URL } from 'url';
44
import DataPresenter from '../view/data-presenter';
5-
import { asyncQuestion } from '../common/log-helpers';
5+
import { asyncQuestion } from '../common/question-helpers';
66
import { FileLog } from '../common/file-log';
77

88
export type ExportResult = 'CREATED' | 'UPDATED' | 'UP-TO-DATE';

0 commit comments

Comments
 (0)