feat(build): add write functions

This commit is contained in:
Sam Tolmay 2020-10-20 18:21:33 +02:00
parent 01e892fad5
commit 701b583a6d
6 changed files with 413 additions and 0 deletions

View File

@ -0,0 +1,34 @@
/*
Copyright 2020 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';
async function writeConnections({ components, context }) {
let writePromises = [];
if (type.isArray(components.connections)) {
writePromises = components.connections.map(async (connection) => {
await context.artifactSetter.set({
filePath: `connections/${connection.connectionId}.json`,
content: JSON.stringify(connection, null, 2),
});
await context.logger.info(`Updated connection: ${connection.connectionId}`);
});
}
await Promise.all(writePromises);
}
export default writeConnections;

View File

@ -0,0 +1,131 @@
/*
Copyright 2020 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 writeConnections from './writeConnections';
import testContext from '../test/testContext';
const mockLogInfo = jest.fn();
const mockSet = jest.fn();
const logger = {
info: mockLogInfo,
};
const artifactSetter = {
set: mockSet,
};
const context = testContext({ logger, artifactSetter });
beforeEach(() => {
mockLogInfo.mockReset();
mockSet.mockReset();
});
test('writeConnections add connection', async () => {
const components = {
connections: [
{
id: 'connection:connection1',
connectionId: 'connection1',
properties: {
prop: 'val',
},
},
],
};
await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'connections/connection1.json',
content: `{
"id": "connection:connection1",
"connectionId": "connection1",
"properties": {
"prop": "val"
}
}`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([['Updated connection: connection1']]);
});
test('writeConnections multiple connection', async () => {
const components = {
connections: [
{
id: 'connection:connection1',
connectionId: 'connection1',
},
{
id: 'connection:connection2',
connectionId: 'connection2',
},
],
};
await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'connections/connection1.json',
content: `{
"id": "connection:connection1",
"connectionId": "connection1"
}`,
},
],
[
{
filePath: 'connections/connection2.json',
content: `{
"id": "connection:connection2",
"connectionId": "connection2"
}`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([
['Updated connection: connection1'],
['Updated connection: connection2'],
]);
});
test('writeConnections no connections', async () => {
const components = {
connections: [],
};
await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([]);
expect(mockLogInfo.mock.calls).toEqual([]);
});
test('writeConnections connections undefined', async () => {
const components = {};
await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([]);
expect(mockLogInfo.mock.calls).toEqual([]);
});
test('writeConnections connections not an array', async () => {
const components = {
connections: 'connections',
};
await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([]);
expect(mockLogInfo.mock.calls).toEqual([]);
});

View File

@ -0,0 +1,33 @@
/*
Copyright 2020 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';
async function writeGlobal({ components, context }) {
if (type.isNone(components.global)) {
components.global = {};
}
if (!type.isObject(components.global)) {
throw new Error('Global is not an object.');
}
await context.artifactSetter.set({
filePath: 'global.json',
content: JSON.stringify(components.global, null, 2),
});
await context.logger.info('Updated global');
}
export default writeGlobal;

View File

@ -0,0 +1,93 @@
/*
Copyright 2020 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 writeGlobal from './writeGlobal';
import testContext from '../test/testContext';
const mockLogInfo = jest.fn();
const mockSet = jest.fn();
const logger = {
info: mockLogInfo,
};
const artifactSetter = {
set: mockSet,
};
const context = testContext({ logger, artifactSetter });
beforeEach(() => {
mockLogInfo.mockReset();
mockSet.mockReset();
});
test('writeGlobal', async () => {
const components = {
global: {
key: 'value',
},
};
await writeGlobal({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'global.json',
content: `{
"key": "value"
}`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([['Updated global']]);
});
test('writeGlobal empty global', async () => {
const components = {
global: {},
};
await writeGlobal({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'global.json',
content: `{}`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([['Updated global']]);
});
test('writeGlobal global undefined', async () => {
const components = {};
await writeGlobal({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'global.json',
content: `{}`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([['Updated global']]);
});
test('writeGlobal global not an object', async () => {
const components = {
global: 'global',
};
await expect(writeGlobal({ components, context })).rejects.toThrow('Global is not an object.');
});

View File

@ -0,0 +1,30 @@
/*
Copyright 2020 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';
async function writeMenus({ components, context }) {
if (!type.isArray(components.menus)) {
throw new Error('Menus is not an array.');
}
await context.artifactSetter.set({
filePath: 'menus.json',
content: JSON.stringify(components.menus, null, 2),
});
await context.logger.info('Updated menus');
}
export default writeMenus;

View File

@ -0,0 +1,92 @@
/*
Copyright 2020 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 writeMenus from './writeMenus';
import testContext from '../test/testContext';
const mockLogInfo = jest.fn();
const mockSet = jest.fn();
const logger = {
info: mockLogInfo,
};
const artifactSetter = {
set: mockSet,
};
const context = testContext({ logger, artifactSetter });
beforeEach(() => {
mockLogInfo.mockReset();
mockSet.mockReset();
});
test('writeMenus', async () => {
const components = {
menus: [
{
id: 'menu:default',
menuId: 'default',
links: [],
},
],
};
await writeMenus({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'menus.json',
content: `[
{
"id": "menu:default",
"menuId": "default",
"links": []
}
]`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([['Updated menus']]);
});
test('writeMenus empty menus', async () => {
const components = {
menus: [],
};
await writeMenus({ components, context });
expect(mockSet.mock.calls).toEqual([
[
{
filePath: 'menus.json',
content: `[]`,
},
],
]);
expect(mockLogInfo.mock.calls).toEqual([['Updated menus']]);
});
test('writeMenus menus undefined', async () => {
const components = {};
await expect(writeMenus({ components, context })).rejects.toThrow('Menus is not an array.');
});
test('writeMenus menus not an array', async () => {
const components = {
menus: 'menus',
};
await expect(writeMenus({ components, context })).rejects.toThrow('Menus is not an array.');
});