feat(build): Add a default 404 page if no page is defined.

This commit is contained in:
SamTolmay 2021-06-07 17:07:01 +02:00
parent 441b150e75
commit b0abb39e10
5 changed files with 315 additions and 3 deletions

View File

@ -22,10 +22,13 @@ async function run() {
const build = await require('./dist/index.js').default.then((module) => module.default);
await build({
logger: console,
cacheDirectory: path.resolve(process.cwd(), '../servers/serverDev/.lowdefy/.cache'),
configDirectory: path.resolve(process.cwd(), '../docs'),
// cacheDirectory: path.resolve(process.cwd(), '../servers/serverDev/.lowdefy/.cache'),
cacheDirectory: path.resolve(process.cwd(), './.lowdefy/.cache'),
// configDirectory: path.resolve(process.cwd(), '../docs'),
// configDirectory: path.resolve(process.cwd(), '../servers/serverDev'),
outputDirectory: path.resolve(process.cwd(), '../servers/serverDev/.lowdefy/build'),
configDirectory: process.cwd(),
// outputDirectory: path.resolve(process.cwd(), '../servers/serverDev/.lowdefy/build'),
outputDirectory: path.resolve(process.cwd(), './.lowdefy/build'),
});
}

View File

@ -20,6 +20,7 @@ import createFileLoader from './loaders/fileLoader';
import createFileSetter from './loaders/fileSetter';
import createMetaLoader from './loaders/metaLoader';
import addDefaultPages from './build/addDefaultPages/addDefaultPages';
import buildAuth from './build/buildAuth/buildAuth';
import buildConnections from './build/buildConnections';
import buildMenu from './build/buildMenu';
@ -60,6 +61,7 @@ async function build(options) {
await validateConfig({ components, context });
await buildAuth({ components, context });
await buildConnections({ components, context });
await addDefaultPages({ components, context });
await buildPages({ components, context });
await buildMenu({ components, context });
await cleanOutputDirectory({ context });

View File

@ -0,0 +1,44 @@
{
"id": "404",
"type": "Context",
"style": {
"minHeight": "100vh"
},
"blocks": [
{
"id": "404_result",
"type": "Result",
"properties": {
"status": 404,
"title": "404",
"subTitle": "Sorry, the page you are visiting does not exist."
},
"areas": {
"extra": {
"blocks": [
{
"id": "home",
"type": "Button",
"properties": {
"title": "Go to home page",
"type": "Link",
"icon": "HomeOutlined"
},
"events": {
"onClick": [
{
"id": "home",
"type": "Link",
"params": {
"home": true
}
}
]
}
}
]
}
}
}
]
}

View File

@ -0,0 +1,39 @@
/*
Copyright 2020-2021 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { type } from '@lowdefy/helpers';
import page404 from './404.json';
const defaultPages = [page404];
async function addDefaultPages({ components }) {
if (type.isNone(components.pages)) {
components.pages = [];
}
if (!type.isArray(components.pages)) {
throw new Error('lowdefy.pages is not an array.');
}
const pageIds = components.pages.map((page) => page.id);
// deep copy to avoid mutating defaultConfig
const filteredDefaultPages = defaultPages.filter(
(defaultPage) => !pageIds.includes(defaultPage.id)
);
components.pages = [...components.pages, ...filteredDefaultPages];
return components;
}
export default addDefaultPages;

View File

@ -0,0 +1,224 @@
/*
Copyright 2020-2021 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import addDefaultPages from './addDefaultPages';
import testContext from '../../test/testContext';
const mockLogWarn = jest.fn();
const logger = {
warn: mockLogWarn,
};
const context = testContext({ logger });
beforeEach(() => {
mockLogWarn.mockReset();
});
test('addDefaultPages, no pages array', async () => {
const components = {};
const res = await addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
blocks: [
{
areas: {
extra: {
blocks: [
{
events: {
onClick: [
{
id: 'home',
params: {
home: true,
},
type: 'Link',
},
],
},
id: 'home',
properties: {
icon: 'HomeOutlined',
title: 'Go to home page',
type: 'Link',
},
type: 'Button',
},
],
},
},
id: '404_result',
properties: {
status: 404,
subTitle: 'Sorry, the page you are visiting does not exist.',
title: '404',
},
type: 'Result',
},
],
id: '404',
style: {
minHeight: '100vh',
},
type: 'Context',
},
],
});
});
test('addDefaultPages, empty pages array', async () => {
const components = { pages: [] };
const res = await addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
blocks: [
{
areas: {
extra: {
blocks: [
{
events: {
onClick: [
{
id: 'home',
params: {
home: true,
},
type: 'Link',
},
],
},
id: 'home',
properties: {
icon: 'HomeOutlined',
title: 'Go to home page',
type: 'Link',
},
type: 'Button',
},
],
},
},
id: '404_result',
properties: {
status: 404,
subTitle: 'Sorry, the page you are visiting does not exist.',
title: '404',
},
type: 'Result',
},
],
id: '404',
style: {
minHeight: '100vh',
},
type: 'Context',
},
],
});
});
test('addDefaultPages, pages without 404 page', async () => {
const components = { pages: [{ id: 'page1', type: 'Context' }] };
const res = await addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
id: 'page1',
type: 'Context',
},
{
blocks: [
{
areas: {
extra: {
blocks: [
{
events: {
onClick: [
{
id: 'home',
params: {
home: true,
},
type: 'Link',
},
],
},
id: 'home',
properties: {
icon: 'HomeOutlined',
title: 'Go to home page',
type: 'Link',
},
type: 'Button',
},
],
},
},
id: '404_result',
properties: {
status: 404,
subTitle: 'Sorry, the page you are visiting does not exist.',
title: '404',
},
type: 'Result',
},
],
id: '404',
style: {
minHeight: '100vh',
},
type: 'Context',
},
],
});
});
test('addDefaultPages, pages with 404 page, should not overwrite', async () => {
const components = {
pages: [
{ id: 'page1', type: 'Context' },
{ id: '404', type: 'Context' },
],
};
const res = await addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
id: 'page1',
type: 'Context',
},
{
id: '404',
type: 'Context',
},
],
});
});
test('addDefaultPages, pages not an ', async () => {
const components = {
pages: { id: 'page1', type: 'Context' },
};
await expect(addDefaultPages({ components, context })).rejects.toThrow(
'lowdefy.pages is not an array.'
);
});