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
28 changes: 23 additions & 5 deletions src/commands/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('configure command', function() {
jest.spyOn(fs, 'mkdirSync').mockReturnValueOnce(undefined);
jest.spyOn(fs, 'writeFileSync').mockReturnValueOnce(undefined);

handler({ ...yargArgs, ...configFixture });
handler({ ...yargArgs, ...configFixture, config: CONFIG_FILENAME() });

expect(fs.existsSync).toHaveBeenCalledWith(expect.stringMatching(/\.amplience$/));
expect(fs.mkdirSync).toHaveBeenCalledWith(expect.stringMatching(/\.amplience$/), { recursive: true });
Expand All @@ -48,7 +48,7 @@ describe('configure command', function() {
jest.spyOn(fs, 'mkdirSync');
jest.spyOn(fs, 'writeFileSync').mockReturnValueOnce(undefined);

handler({ ...yargArgs, ...configFixture });
handler({ ...yargArgs, ...configFixture, config: CONFIG_FILENAME() });

expect(fs.existsSync).toHaveBeenCalledWith(expect.stringMatching(/\.amplience$/));
expect(fs.mkdirSync).not.toHaveBeenCalled();
Expand All @@ -58,6 +58,24 @@ describe('configure command', function() {
);
});

it('should write a config file and use the specified file', () => {
jest
.spyOn(fs, 'existsSync')
.mockReturnValueOnce(false)
.mockReturnValueOnce(false);
jest.spyOn(fs, 'mkdirSync').mockReturnValueOnce(undefined);
jest.spyOn(fs, 'writeFileSync').mockReturnValueOnce(undefined);

handler({ ...yargArgs, ...configFixture, config: 'subdirectory/custom-config.json' });

expect(fs.existsSync).toHaveBeenCalledWith(expect.stringMatching(/subdirectory$/));
expect(fs.mkdirSync).toHaveBeenCalledWith(expect.stringMatching(/subdirectory$/), { recursive: true });
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringMatching(new RegExp('subdirectory/custom-config.json$')),
JSON.stringify(configFixture)
);
});

it('should report an error if its not possible to create the .amplience dir', () => {
jest
.spyOn(fs, 'existsSync')
Expand All @@ -69,7 +87,7 @@ describe('configure command', function() {
jest.spyOn(fs, 'writeFileSync').mockReturnValueOnce(undefined);

expect(() => {
handler({ ...yargArgs, ...configFixture });
handler({ ...yargArgs, ...configFixture, config: CONFIG_FILENAME() });
}).toThrowError(/^Unable to create dir ".*". Reason: .*/);

expect(fs.existsSync).toHaveBeenCalledWith(expect.stringMatching(/\.amplience$/));
Expand All @@ -88,7 +106,7 @@ describe('configure command', function() {
});

expect(() => {
handler({ ...yargArgs, ...configFixture });
handler({ ...yargArgs, ...configFixture, config: CONFIG_FILENAME() });
}).toThrowError(/^Unable to write config file ".*". Reason: .*/);

expect(fs.existsSync).toHaveBeenCalledWith(expect.stringMatching(/\.amplience$/));
Expand All @@ -104,7 +122,7 @@ describe('configure command', function() {
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(JSON.stringify(configFixture));
jest.spyOn(fs, 'writeFileSync');

handler({ ...yargArgs, ...configFixture });
handler({ ...yargArgs, ...configFixture, config: CONFIG_FILENAME() });

expect(fs.writeFileSync).not.toHaveBeenCalled();
});
Expand Down
10 changes: 7 additions & 3 deletions src/commands/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export type ConfigurationParameters = {
hubId: string;
};

type ConfigArgument = {
config: string;
};

export const configureCommandOptions: CommandOptions = {
clientId: { type: 'string', demandOption: true },
clientSecret: { type: 'string', demandOption: true },
Expand All @@ -43,14 +47,14 @@ const writeConfigFile = (configFile: string, parameters: ConfigurationParameters
export const readConfigFile = (configFile: string): object =>
fs.existsSync(configFile) ? JSON.parse(fs.readFileSync(configFile, 'utf-8')) : {};

export const handler = (argv: Arguments<ConfigurationParameters>): void => {
export const handler = (argv: Arguments<ConfigurationParameters & ConfigArgument>): void => {
const { clientId, clientSecret, hubId } = argv;
const storedConfig = readConfigFile(CONFIG_FILENAME());
const storedConfig = readConfigFile(argv.config);

if (isEqual(storedConfig, { clientId, clientSecret, hubId })) {
console.log('Config file up-to-date. Please use `--help` for command usage.');
return;
}
writeConfigFile(CONFIG_FILENAME(), { clientId, clientSecret, hubId });
writeConfigFile(argv.config, { clientId, clientSecret, hubId });
console.log('Config file updated.');
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ContentDependancy } from '../../../common/content-item/content-dependan
function dependancy(id: string): ContentDependancy {
return {
_meta: {
schema: 'http://bigcontent.io/cms/schema/v1/core#/definitions/content-link'
schema: 'http://bigcontent.io/cms/schema/v1/core#/definitions/content-link',
name: 'content-link'
},
contentType: 'https://dev-solutions.s3.amazonaws.com/DynamicContentTypes/Accelerators/blog.json',
id: id
Expand Down
86 changes: 86 additions & 0 deletions src/commands/content-item/__snapshots__/tree.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`content-item tree command handler tests should detect and print circular dependencies with a double line indicator 1`] = `
"=== LEVEL 2 (1) ===
item6
└─ item5

=== LEVEL 1 (3) ===
item3

item7

=== CIRCULAR (3) ===
item1 ═════════════════╗
├─ item2 ║
│ └─ item4 ║
│ └─ *** (item1) ══╝
└─ (item3)

Finished. Circular Dependencies printed: 1"
`;

exports[`content-item tree command handler tests should detect intertwined circular dependencies with multiple lines with different position 1`] = `
"=== CIRCULAR (6) ===
item5 ══════════════╗
└─ item6 ║
└─ *** (item5) ══╝

item1 ══════════════════════╗
└─ item2 ═════════════════╗ ║
└─ item3 ║ ║
├─ *** (item2) ═════╝ ║
└─ item4 ║
├─ *** (item1) ════╝
└─ (item5)

Finished. Circular Dependencies printed: 2"
`;

exports[`content-item tree command handler tests should print a single content item by itself 1`] = `
"=== LEVEL 1 (1) ===
item1

Finished. Circular Dependencies printed: 0"
`;

exports[`content-item tree command handler tests should print a tree of content items 1`] = `
"=== LEVEL 4 (1) ===
item1
├─ item2
│ ├─ item4
│ └─ item6
│ └─ item5
└─ item3

=== LEVEL 3 (1) ===
=== LEVEL 2 (1) ===
=== LEVEL 1 (3) ===
Finished. Circular Dependencies printed: 0"
`;

exports[`content-item tree command handler tests should print an error when invalid json is found 1`] = `
"=== LEVEL 1 (1) ===
item1

Finished. Circular Dependencies printed: 0"
`;

exports[`content-item tree command handler tests should print multiple disjoint trees of content items 1`] = `
"=== LEVEL 3 (1) ===
item1
├─ item2
│ └─ item4
└─ item3

=== LEVEL 2 (2) ===
item6
└─ item5

=== LEVEL 1 (4) ===
item7

Finished. Circular Dependencies printed: 0"
`;

exports[`content-item tree command handler tests should print nothing if no content is present 1`] = `"Finished. Circular Dependencies printed: 0"`;
25 changes: 19 additions & 6 deletions src/commands/content-item/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ItemContentDependancies,
ContentDependancyInfo
} from '../../common/content-item/content-dependancy-tree';
import { Body } from '../../common/content-item/body';

import { AmplienceSchemaValidator, defaultSchemaLookup } from '../../common/content-item/amplience-schema-validator';
import { createLog, getDefaultLogPath } from '../../common/log-helpers';
Expand Down Expand Up @@ -675,12 +676,23 @@ const prepareContentForImport = async (
return tree;
};

const rewriteDependancy = (dep: ContentDependancyInfo, mapping: ContentMapping): void => {
const id = mapping.getContentItem(dep.dependancy.id) || dep.dependancy.id;
const rewriteDependancy = (dep: ContentDependancyInfo, mapping: ContentMapping, allowNull: boolean): void => {
let id = mapping.getContentItem(dep.dependancy.id);

if (id == null && !allowNull) {
id = dep.dependancy.id;
}

if (dep.dependancy._meta.schema === '_hierarchy') {
dep.owner.content.body._meta.hierarchy.parentId = id;
} else {
dep.dependancy.id = id;
} else if (dep.parent) {
const parent = dep.parent as Body;
if (id == null) {
delete parent[dep.index];
} else {
parent[dep.index] = dep.dependancy;
dep.dependancy.id = id;
}
}
};

Expand All @@ -706,7 +718,7 @@ const importTree = async (

// Replace any dependancies with the existing mapping.
item.dependancies.forEach(dep => {
rewriteDependancy(dep, mapping);
rewriteDependancy(dep, mapping, false);
});

const originalId = content.id;
Expand Down Expand Up @@ -781,7 +793,7 @@ const importTree = async (
const content = item.owner.content;

item.dependancies.forEach(dep => {
rewriteDependancy(dep, mapping);
rewriteDependancy(dep, mapping, pass === 0);
});

const originalId = content.id;
Expand Down Expand Up @@ -815,6 +827,7 @@ const importTree = async (

newDependants[i] = newItem;
mapping.registerContentItem(originalId as string, newItem.id as string);
mapping.registerContentItem(newItem.id as string, newItem.id as string);
} else {
if (itemShouldPublish(content) && (newItem.version != oldVersion || argv.republish)) {
publishable.push({ item: newItem, node: item });
Expand Down
Loading