Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"ajv": "^6.12.3",
"axios": "^0.18.1",
"chalk": "^2.4.2",
"dc-management-sdk-js": "^1.9.0",
"dc-management-sdk-js": "^1.13.0",
"lodash": "^4.17.15",
"node-fetch": "^2.6.0",
"promise-retry": "^2.0.1",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/content-item/__mocks__/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const setForceFail = (fail: boolean): void => {
export const handler = async (argv: Arguments<CopyItemBuilderOptions & ConfigurationParameters>): Promise<boolean> => {
calls.push(argv);
const idOut = argv.exportedIds as string[];
idOut.push(...outputIds);

if (idOut) {
idOut.push(...outputIds);
}

return !forceFail;
};
10 changes: 6 additions & 4 deletions src/commands/content-item/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ArchiveLog } from '../../common/archive/archive-log';
import paginator from '../../common/dc-management-sdk-js/paginator';
import { confirmArchive } from '../../common/archive/archive-helpers';
import ArchiveOptions from '../../common/archive/archive-options';
import { ContentItem, DynamicContent } from 'dc-management-sdk-js';
import { ContentItem, DynamicContent, Status } from 'dc-management-sdk-js';
import { equalsOrRegex } from '../../common/filter/filter';
import { getDefaultLogPath, createLog } from '../../common/log-helpers';
import { FileLog } from '../../common/file-log';
Expand Down Expand Up @@ -153,7 +153,7 @@ export const getContentItems = async ({
contentType
}: {
client: DynamicContent;
id?: string;
id?: string | string[];
hubId: string;
repoId?: string | string[];
folderId?: string | string[];
Expand All @@ -165,7 +165,9 @@ export const getContentItems = async ({
const contentItems: ContentItem[] = [];

if (id != null) {
contentItems.push(await client.contentItems.get(id));
const itemIds = Array.isArray(id) ? id : [id];
const items = await Promise.all(itemIds.map(id => client.contentItems.get(id)));
contentItems.push(...items);

return {
contentItems,
Expand All @@ -192,7 +194,7 @@ export const getContentItems = async ({
)
: await Promise.all(
contentRepositories.map(async source => {
const items = await paginator(source.related.contentItems.list, { status: 'ACTIVE' });
const items = await paginator(source.related.contentItems.list, { status: Status.ACTIVE });
contentItems.push(...items);
})
);
Expand Down
41 changes: 36 additions & 5 deletions src/commands/content-item/copy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Arguments } from 'yargs';
import { ExportItemBuilderOptions } from '../../interfaces/export-item-builder-options.interface';
import { ConfigurationParameters } from '../configure';
import { ImportItemBuilderOptions } from '../../interfaces/import-item-builder-options.interface';
import { createLog, getDefaultLogPath } from '../../common/log-helpers';
import { createLog, getDefaultLogPath, openRevertLog } from '../../common/log-helpers';
import * as copyConfig from '../../common/content-item/copy-config';
import { FileLog } from '../../common/file-log';

Expand All @@ -22,7 +22,10 @@ jest.mock('../../services/dynamic-content-client-factory');
jest.mock('./export');
jest.mock('./import');
jest.mock('./import-revert');
jest.mock('../../common/log-helpers');
jest.mock('../../common/log-helpers', () => ({
...jest.requireActual('../../common/log-helpers'),
getDefaultLogPath: jest.fn()
}));

function rimraf(dir: string): Promise<Error> {
return new Promise((resolve): void => {
Expand All @@ -49,7 +52,8 @@ describe('content-item copy command', () => {
expect(spyOption).toHaveBeenCalledWith('revertLog', {
type: 'string',
describe:
'Path to a log file to revert a copy for. This will archive the most recently copied resources, and revert updated ones.'
'Path to a log file to revert a copy for. This will archive the most recently copied resources, and revert updated ones.',
coerce: openRevertLog
});

expect(spyOption).toHaveBeenCalledWith('srcRepo', {
Expand Down Expand Up @@ -173,7 +177,9 @@ describe('content-item copy command', () => {
clientId: 'client-id',
clientSecret: 'client-id',
hubId: 'hub-id',
logFile: new FileLog()

logFile: new FileLog(),
revertLog: Promise.resolve(undefined)
};

beforeAll(async () => {
Expand Down Expand Up @@ -285,7 +291,7 @@ describe('content-item copy command', () => {
dstClientId: 'acc2-id',
dstSecret: 'acc2-secret',

revertLog: 'revertTest.txt'
revertLog: Promise.resolve(new FileLog())
};
await handler(argv);

Expand All @@ -299,6 +305,31 @@ describe('content-item copy command', () => {
expect(revertCalls[0].revertLog).toEqual(argv.revertLog);
});

it('should exit early when revertLog is not present.', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const exportCalls: Arguments<ExportItemBuilderOptions & ConfigurationParameters>[] = (exporter as any).calls;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const importCalls: Arguments<ImportItemBuilderOptions & ConfigurationParameters>[] = (importer as any).calls;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const revertCalls: Arguments<ImportItemBuilderOptions & ConfigurationParameters>[] = (reverter as any).calls;

const argv = {
...yargArgs,
...config,

dstHubId: 'hub2-id',
dstClientId: 'acc2-id',
dstSecret: 'acc2-secret',

revertLog: openRevertLog('temp/copy/revertMissing.txt')
};
await handler(argv);

expect(exportCalls.length).toEqual(0);
expect(importCalls.length).toEqual(0);
expect(revertCalls.length).toEqual(0);
});

it('should return false and remove temp folder when import fails or throws.', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const exportCalls: Arguments<ExportItemBuilderOptions & ConfigurationParameters>[] = (exporter as any).calls;
Expand Down
21 changes: 17 additions & 4 deletions src/commands/content-item/copy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createLog, getDefaultLogPath } from '../../common/log-helpers';
import { createLog, getDefaultLogPath, openRevertLog } from '../../common/log-helpers';
import { Argv, Arguments } from 'yargs';
import { join } from 'path';
import { CopyItemBuilderOptions } from '../../interfaces/copy-item-builder-options.interface';
Expand All @@ -10,6 +10,8 @@ import { handler as importer } from './import';
import { ensureDirectoryExists } from '../../common/import/directory-utils';
import { revert } from './import-revert';
import { loadCopyConfig } from '../../common/content-item/copy-config';
import { FileLog } from '../../common/file-log';
import { LogErrorLevel } from '../../common/archive/archive-log';

export function getTempFolder(name: string, platform: string = process.platform): string {
return join(process.env[platform == 'win32' ? 'USERPROFILE' : 'HOME'] || __dirname, '.amplience', `copy-${name}/`);
Expand All @@ -27,7 +29,8 @@ export const builder = (yargs: Argv): void => {
.option('revertLog', {
type: 'string',
describe:
'Path to a log file to revert a copy for. This will archive the most recently copied resources, and revert updated ones.'
'Path to a log file to revert a copy for. This will archive the most recently copied resources, and revert updated ones.',
coerce: openRevertLog
})

.option('srcRepo', {
Expand Down Expand Up @@ -167,7 +170,15 @@ export const handler = async (argv: Arguments<CopyItemBuilderOptions & Configura

const { srcHubId, srcClientId, srcSecret, dstHubId, dstClientId, dstSecret } = copyConfig;

if (argv.revertLog) {
const revertLog = await argv.revertLog;

if (revertLog) {
if (revertLog.errorLevel === LogErrorLevel.INVALID) {
log.error('Could not read the revert log.');
await log.close();
return false;
}

result = await revert({
...yargArgs,

Expand All @@ -176,6 +187,7 @@ export const handler = async (argv: Arguments<CopyItemBuilderOptions & Configura
clientSecret: dstSecret,

dir: tempFolder, // unused
logFile: new FileLog(),

revertLog: argv.revertLog
});
Expand Down Expand Up @@ -226,7 +238,8 @@ export const handler = async (argv: Arguments<CopyItemBuilderOptions & Configura
excludeKeys: argv.excludeKeys,

media: argv.media,
logFile: log
logFile: log,
revertLog: Promise.resolve(undefined)
});

if (importResult) {
Expand Down
9 changes: 6 additions & 3 deletions src/commands/content-item/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { dependsOn } from './__mocks__/dependant-content-helper';
import dynamicContentClientFactory from '../../services/dynamic-content-client-factory';
import Yargs from 'yargs/yargs';
import { ItemTemplate, getItemInfo, getItemName, MockContent } from '../../common/dc-management-sdk-js/mock-content';
import { getDefaultLogPath } from '../../common/log-helpers';
import { createLog, getDefaultLogPath } from '../../common/log-helpers';
import { exists } from 'fs';
import { promisify } from 'util';
import readline from 'readline';

import rmdir from 'rimraf';
import { FileLog } from '../../common/file-log';

jest.mock('readline');
jest.mock('../../services/dynamic-content-client-factory');
Expand Down Expand Up @@ -82,7 +83,8 @@ describe('content-item export command', () => {
expect(spyOption).toHaveBeenCalledWith('logFile', {
type: 'string',
default: LOG_FILENAME,
describe: 'Path to a log file to write to.'
describe: 'Path to a log file to write to.',
coerce: createLog
});
});
});
Expand Down Expand Up @@ -116,7 +118,8 @@ describe('content-item export command', () => {
const config = {
clientId: 'client-id',
clientSecret: 'client-id',
hubId: 'hub-id'
hubId: 'hub-id',
logFile: new FileLog()
};

beforeAll(async () => {
Expand Down
15 changes: 7 additions & 8 deletions src/commands/content-item/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { uniqueFilenamePath, writeJsonToFile } from '../../services/export.servi

import { ExportItemBuilderOptions } from '../../interfaces/export-item-builder-options.interface';
import paginator from '../../common/dc-management-sdk-js/paginator';
import { ContentItem, Folder, DynamicContent, Hub, ContentRepository } from 'dc-management-sdk-js';
import { ContentItem, Folder, DynamicContent, Hub, ContentRepository, Status } from 'dc-management-sdk-js';

import { ensureDirectoryExists } from '../../common/import/directory-utils';
import { ContentDependancyTree, RepositoryContentItem } from '../../common/content-item/content-dependancy-tree';
import { ContentMapping } from '../../common/content-item/content-mapping';
import { getDefaultLogPath } from '../../common/log-helpers';
import { createLog, getDefaultLogPath } from '../../common/log-helpers';
import { AmplienceSchemaValidator, defaultSchemaLookup } from '../../common/content-item/amplience-schema-validator';

interface PublishedContentItem {
Expand Down Expand Up @@ -64,7 +64,8 @@ export const builder = (yargs: Argv): void => {
.option('logFile', {
type: 'string',
default: LOG_FILENAME,
describe: 'Path to a log file to write to.'
describe: 'Path to a log file to write to.',
coerce: createLog
});
};

Expand Down Expand Up @@ -136,7 +137,7 @@ const getContentItems = async (
// Add content items in repo base folder. Cache the other items so we don't have to request them again.
let newItems: ContentItem[];
try {
const allItems = await paginator(repository.related.contentItems.list, { status: 'ACTIVE' });
const allItems = await paginator(repository.related.contentItems.list, { status: Status.ACTIVE });

Array.prototype.push.apply(repoItems, allItems);
newItems = allItems.filter(item => item.folderId == null);
Expand Down Expand Up @@ -221,7 +222,7 @@ export const handler = async (argv: Arguments<ExportItemBuilderOptions & Configu

const folderToPathMap: Map<string, string> = new Map();
const client = dynamicContentClientFactory(argv);
const log = typeof logFile === 'string' || logFile == null ? new FileLog(logFile) : logFile;
const log = logFile.open();
const hub = await client.hubs.get(argv.hubId);

log.appendLine('Retrieving content items, please wait.');
Expand Down Expand Up @@ -345,7 +346,5 @@ export const handler = async (argv: Arguments<ExportItemBuilderOptions & Configu
writeJsonToFile(resolvedPath, item);
}

if (typeof logFile !== 'object') {
await log.close();
}
await log.close();
};
Loading