Skip to content

Commit

Permalink
fix: set default public_folder on files and fields (decaporg#3519)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah authored and vladdu committed Jan 26, 2021
1 parent 52e54fd commit bba6be6
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 38 deletions.
162 changes: 155 additions & 7 deletions packages/netlify-cms-core/src/actions/__tests__/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('config', () => {
expect(
applyDefaults(
fromJS({
collections: [{ folder: '/foo' }],
collections: [{ folder: '/foo', fields: [{ name: 'title', widget: 'string' }] }],
}),
).getIn(['collections', 0, 'folder']),
).toEqual('foo');
Expand All @@ -120,18 +120,26 @@ describe('config', () => {
expect(
applyDefaults(
fromJS({
collections: [{ files: [{ file: '/foo' }] }],
collections: [
{ files: [{ file: '/foo', fields: [{ name: 'title', widget: 'string' }] }] },
],
}),
).getIn(['collections', 0, 'files', 0, 'file']),
).toEqual('foo');
});

describe('public_folder and media_folder', () => {
it('should set collection public_folder collection based on media_folder if not set', () => {
it('should set collection public_folder based on media_folder if not set', () => {
expect(
applyDefaults(
fromJS({
collections: [{ folder: 'foo', media_folder: 'static/images/docs' }],
collections: [
{
folder: 'foo',
media_folder: 'static/images/docs',
fields: [{ name: 'title', widget: 'string' }],
},
],
}),
).getIn(['collections', 0, 'public_folder']),
).toEqual('static/images/docs');
Expand All @@ -146,6 +154,7 @@ describe('config', () => {
folder: 'foo',
media_folder: 'static/images/docs',
public_folder: 'images/docs',
fields: [{ name: 'title', widget: 'string' }],
},
],
}),
Expand All @@ -156,20 +165,154 @@ describe('config', () => {
it("should set collection media_folder and public_folder to an empty string when collection path exists, but collection media_folder doesn't", () => {
const result = applyDefaults(
fromJS({
collections: [{ folder: 'foo', path: '{{slug}}/index' }],
collections: [
{
folder: 'foo',
path: '{{slug}}/index',
fields: [{ name: 'title', widget: 'string' }],
},
],
}),
);
expect(result.getIn(['collections', 0, 'media_folder'])).toEqual('');
expect(result.getIn(['collections', 0, 'public_folder'])).toEqual('');
});

it('should set file public_folder based on media_folder if not set', () => {
expect(
applyDefaults(
fromJS({
collections: [
{
files: [
{
file: 'foo',
media_folder: 'static/images/docs',
fields: [{ name: 'title', widget: 'string' }],
},
],
},
],
}),
).getIn(['collections', 0, 'files', 0, 'public_folder']),
).toEqual('static/images/docs');
});

it('should not overwrite file public_folder if set', () => {
expect(
applyDefaults(
fromJS({
collections: [
{
files: [
{
file: 'foo',
media_folder: 'static/images/docs',
public_folder: 'images/docs',
fields: [{ name: 'title', widget: 'string' }],
},
],
},
],
}),
).getIn(['collections', 0, 'files', 0, 'public_folder']),
).toEqual('images/docs');
});

it('should set nested field public_folder based on media_folder if not set', () => {
const config = applyDefaults(
fromJS({
collections: [
{
folder: 'foo',
path: '{{slug}}/index',
fields: [
{
name: 'title',
widget: 'string',
media_folder: 'collection/static/images/docs',
},
],
},
{
files: [
{
file: 'foo',
fields: [
{
name: 'title',
widget: 'string',
media_folder: 'file/static/images/docs',
},
],
},
],
},
],
}),
);
expect(config.getIn(['collections', 0, 'fields', 0, 'public_folder'])).toEqual(
'collection/static/images/docs',
);
expect(
config.getIn(['collections', 1, 'files', 0, 'fields', 0, 'public_folder']),
).toEqual('file/static/images/docs');
});

it('should not overwrite nested field public_folder if set', () => {
const config = applyDefaults(
fromJS({
collections: [
{
folder: 'foo',
path: '{{slug}}/index',
fields: [
{
name: 'title',
widget: 'string',
media_folder: 'collection/static/images/docs',
public_folder: 'collection/public_folder',
},
],
},
{
files: [
{
file: 'foo',
fields: [
{
name: 'title',
widget: 'string',
public_folder: 'file/public_folder',
},
],
},
],
},
],
}),
);
expect(config.getIn(['collections', 0, 'fields', 0, 'public_folder'])).toEqual(
'collection/public_folder',
);
expect(
config.getIn(['collections', 1, 'files', 0, 'fields', 0, 'public_folder']),
).toEqual('file/public_folder');
});
});

describe('publish', () => {
it('should set publish to true if not set', () => {
expect(
applyDefaults(
fromJS({
collections: [{ folder: 'foo', media_folder: 'static/images/docs' }],
collections: [
{
folder: 'foo',
media_folder: 'static/images/docs',
fields: [{ name: 'title', widget: 'string' }],
},
],
}),
).getIn(['collections', 0, 'publish']),
).toEqual(true);
Expand All @@ -180,7 +323,12 @@ describe('config', () => {
applyDefaults(
fromJS({
collections: [
{ folder: 'foo', media_folder: 'static/images/docs', publish: false },
{
folder: 'foo',
media_folder: 'static/images/docs',
publish: false,
fields: [{ name: 'title', widget: 'string' }],
},
],
}),
).getIn(['collections', 0, 'publish']),
Expand Down
25 changes: 20 additions & 5 deletions packages/netlify-cms-core/src/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { trimStart, get, isPlainObject } from 'lodash';
import { authenticateUser } from 'Actions/auth';
import * as publishModes from 'Constants/publishModes';
import { validateConfig } from 'Constants/configSchema';
import { selectDefaultSortableFields } from '../reducers/collections';
import { selectDefaultSortableFields, traverseFields } from '../reducers/collections';
import { resolveBackend } from 'coreSrc/backend';

export const CONFIG_REQUEST = 'CONFIG_REQUEST';
Expand All @@ -24,6 +24,13 @@ const getConfigUrl = () => {
return 'config.yml';
};

const setDefaultPublicFolder = map => {
if (map.has('media_folder') && !map.has('public_folder')) {
map = map.set('public_folder', map.get('media_folder'));
}
return map;
};

const defaults = {
publish_mode: publishModes.SIMPLE,
};
Expand Down Expand Up @@ -70,9 +77,11 @@ export function applyDefaults(config) {
// default value for media folder when using the path config
collection = collection.set('media_folder', '');
}
if (collection.has('media_folder') && !collection.has('public_folder')) {
collection = collection.set('public_folder', collection.get('media_folder'));
}
collection = setDefaultPublicFolder(collection);
collection = collection.set(
'fields',
traverseFields(collection.get('fields'), setDefaultPublicFolder),
);
collection = collection.set('folder', trimStart(folder, '/'));
}

Expand All @@ -81,7 +90,13 @@ export function applyDefaults(config) {
collection = collection.set(
'files',
files.map(file => {
return file.set('file', trimStart(file.get('file'), '/'));
file = file.set('file', trimStart(file.get('file'), '/'));
file = setDefaultPublicFolder(file);
file = file.set(
'fields',
traverseFields(file.get('fields'), setDefaultPublicFolder),
);
return file;
}),
);
}
Expand Down
65 changes: 39 additions & 26 deletions packages/netlify-cms-core/src/reducers/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,37 @@ export const selectField = (collection: Collection, key: string) => {
return field;
};

export const traverseFields = (
fields: List<EntryField>,
updater: (field: EntryField) => EntryField,
done = () => false,
) => {
if (done()) {
return fields;
}
fields = fields
.map(f => {
const field = updater(f as EntryField);
if (done()) {
return field;
} else if (field.has('fields')) {
return field.set('fields', traverseFields(field.get('fields')!, updater, done));
} else if (field.has('field')) {
return field.set(
'field',
traverseFields(List([field.get('field')!]), updater, done).get(0),
);
} else if (field.has('types')) {
return field.set('types', traverseFields(field.get('types')!, updater, done));
} else {
return field;
}
})
.toList() as List<EntryField>;

return fields;
};

export const updateFieldByKey = (
collection: Collection,
key: string,
Expand All @@ -245,37 +276,19 @@ export const updateFieldByKey = (
}

let updated = false;

const traverseFields = (fields: List<EntryField>) => {
if (updated) {
// we can stop once the field is found
return fields;
const updateAndBreak = (f: EntryField) => {
const field = f as EntryField;
if (field === selected) {
updated = true;
return updater(field);
} else {
return field;
}

fields = fields
.map(f => {
const field = f as EntryField;
if (field === selected) {
updated = true;
return updater(field);
} else if (field.has('fields')) {
return field.set('fields', traverseFields(field.get('fields')!));
} else if (field.has('field')) {
return field.set('field', traverseFields(List([field.get('field')!])).get(0));
} else if (field.has('types')) {
return field.set('types', traverseFields(field.get('types')!));
} else {
return field;
}
})
.toList() as List<EntryField>;

return fields;
};

collection = collection.set(
'fields',
traverseFields(collection.get('fields', List<EntryField>())),
traverseFields(collection.get('fields', List<EntryField>()), updateAndBreak, () => updated),
);

return collection;
Expand Down

0 comments on commit bba6be6

Please sign in to comment.