fix(build): Refactor writing of build artifact files.

This commit is contained in:
SamTolmay 2021-08-16 17:16:15 +02:00
parent 808f619d19
commit 7162760b18
No known key found for this signature in database
GPG Key ID: 655CB3F5AA745CF8
18 changed files with 60 additions and 144 deletions

View File

@ -17,7 +17,7 @@
*/ */
import createFileLoader from './loaders/fileLoader'; import createFileLoader from './loaders/fileLoader';
import createFileSetter from './loaders/fileSetter'; import createWriteBuildArtifact from './utils/files/writeBuildArtifact';
import createMetaLoader from './loaders/metaLoader'; import createMetaLoader from './loaders/metaLoader';
import addDefaultPages from './build/addDefaultPages/addDefaultPages'; import addDefaultPages from './build/addDefaultPages/addDefaultPages';
@ -41,7 +41,7 @@ import writeRequests from './build/writeRequests';
function createContext(options) { function createContext(options) {
const { blocksServerUrl, cacheDirectory, configDirectory, logger, outputDirectory } = options; const { blocksServerUrl, cacheDirectory, configDirectory, logger, outputDirectory } = options;
const context = { const context = {
artifactSetter: createFileSetter({ baseDirectory: outputDirectory }), writeBuildArtifact: createWriteBuildArtifact({ outputDirectory }),
configLoader: createFileLoader({ baseDirectory: configDirectory }), configLoader: createFileLoader({ baseDirectory: configDirectory }),
blocksServerUrl, blocksServerUrl,
cacheDirectory, cacheDirectory,

View File

@ -15,7 +15,7 @@
*/ */
async function writeApp({ components, context }) { async function writeApp({ components, context }) {
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: 'app.json', filePath: 'app.json',
content: JSON.stringify(components.app || {}, null, 2), content: JSON.stringify(components.app || {}, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writeApp from './writeApp'; import writeApp from './writeApp';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writeApp', async () => { test('writeApp', async () => {
@ -36,7 +32,7 @@ test('writeApp', async () => {
}, },
}; };
await writeApp({ components, context }); await writeApp({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'app.json', filePath: 'app.json',
@ -53,7 +49,7 @@ test('writeApp empty config', async () => {
app: {}, app: {},
}; };
await writeApp({ components, context }); await writeApp({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'app.json', filePath: 'app.json',
@ -66,7 +62,7 @@ test('writeApp empty config', async () => {
test('writeApp config undefined', async () => { test('writeApp config undefined', async () => {
const components = {}; const components = {};
await writeApp({ components, context }); await writeApp({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'app.json', filePath: 'app.json',

View File

@ -15,7 +15,7 @@
*/ */
async function writeConfig({ components, context }) { async function writeConfig({ components, context }) {
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: 'config.json', filePath: 'config.json',
content: JSON.stringify(components.config || {}, null, 2), content: JSON.stringify(components.config || {}, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writeConfig from './writeConfig'; import writeConfig from './writeConfig';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writeConfig', async () => { test('writeConfig', async () => {
@ -36,7 +32,7 @@ test('writeConfig', async () => {
}, },
}; };
await writeConfig({ components, context }); await writeConfig({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'config.json', filePath: 'config.json',
@ -53,7 +49,7 @@ test('writeConfig empty config', async () => {
config: {}, config: {},
}; };
await writeConfig({ components, context }); await writeConfig({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'config.json', filePath: 'config.json',
@ -66,7 +62,7 @@ test('writeConfig empty config', async () => {
test('writeConfig config undefined', async () => { test('writeConfig config undefined', async () => {
const components = {}; const components = {};
await writeConfig({ components, context }); await writeConfig({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'config.json', filePath: 'config.json',

View File

@ -22,7 +22,7 @@ async function writeConnections({ components, context }) {
throw new Error(`Connections is not an array.`); throw new Error(`Connections is not an array.`);
} }
const writePromises = components.connections.map(async (connection) => { const writePromises = components.connections.map(async (connection) => {
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: `connections/${connection.connectionId}.json`, filePath: `connections/${connection.connectionId}.json`,
content: JSON.stringify(connection, null, 2), content: JSON.stringify(connection, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writeConnections from './writeConnections'; import writeConnections from './writeConnections';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writeConnections write connection', async () => { test('writeConnections write connection', async () => {
@ -42,7 +38,7 @@ test('writeConnections write connection', async () => {
], ],
}; };
await writeConnections({ components, context }); await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'connections/connection1.json', filePath: 'connections/connection1.json',
@ -72,7 +68,7 @@ test('writeConnections multiple connection', async () => {
], ],
}; };
await writeConnections({ components, context }); await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'connections/connection1.json', filePath: 'connections/connection1.json',
@ -99,13 +95,13 @@ test('writeConnections no connections', async () => {
connections: [], connections: [],
}; };
await writeConnections({ components, context }); await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([]); expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
}); });
test('writeConnections connections undefined', async () => { test('writeConnections connections undefined', async () => {
const components = {}; const components = {};
await writeConnections({ components, context }); await writeConnections({ components, context });
expect(mockSet.mock.calls).toEqual([]); expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
}); });
test('writeConnections connections not an array', async () => { test('writeConnections connections not an array', async () => {

View File

@ -23,7 +23,7 @@ async function writeGlobal({ components, context }) {
if (!type.isObject(components.global)) { if (!type.isObject(components.global)) {
throw new Error('Global is not an object.'); throw new Error('Global is not an object.');
} }
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: 'global.json', filePath: 'global.json',
content: JSON.stringify(components.global, null, 2), content: JSON.stringify(components.global, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writeGlobal from './writeGlobal'; import writeGlobal from './writeGlobal';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writeGlobal', async () => { test('writeGlobal', async () => {
@ -36,7 +32,7 @@ test('writeGlobal', async () => {
}, },
}; };
await writeGlobal({ components, context }); await writeGlobal({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'global.json', filePath: 'global.json',
@ -53,7 +49,7 @@ test('writeGlobal empty global', async () => {
global: {}, global: {},
}; };
await writeGlobal({ components, context }); await writeGlobal({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'global.json', filePath: 'global.json',
@ -66,7 +62,7 @@ test('writeGlobal empty global', async () => {
test('writeGlobal global undefined', async () => { test('writeGlobal global undefined', async () => {
const components = {}; const components = {};
await writeGlobal({ components, context }); await writeGlobal({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'global.json', filePath: 'global.json',

View File

@ -20,7 +20,7 @@ async function writeMenus({ components, context }) {
if (!type.isArray(components.menus)) { if (!type.isArray(components.menus)) {
throw new Error('Menus is not an array.'); throw new Error('Menus is not an array.');
} }
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: 'menus.json', filePath: 'menus.json',
content: JSON.stringify(components.menus, null, 2), content: JSON.stringify(components.menus, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writeMenus from './writeMenus'; import writeMenus from './writeMenus';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writeMenus', async () => { test('writeMenus', async () => {
@ -40,7 +36,7 @@ test('writeMenus', async () => {
], ],
}; };
await writeMenus({ components, context }); await writeMenus({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'menus.json', filePath: 'menus.json',
@ -61,7 +57,7 @@ test('writeMenus empty menus', async () => {
menus: [], menus: [],
}; };
await writeMenus({ components, context }); await writeMenus({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'menus.json', filePath: 'menus.json',

View File

@ -20,7 +20,7 @@ async function writePage({ page, context }) {
if (!type.isObject(page)) { if (!type.isObject(page)) {
throw new Error(`Page is not an object. Received ${JSON.stringify(page)}`); throw new Error(`Page is not an object. Received ${JSON.stringify(page)}`);
} }
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: `pages/${page.pageId}/${page.pageId}.json`, filePath: `pages/${page.pageId}/${page.pageId}.json`,
content: JSON.stringify(page, null, 2), content: JSON.stringify(page, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writePages from './writePages'; import writePages from './writePages';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writePages write page', async () => { test('writePages write page', async () => {
@ -41,7 +37,7 @@ test('writePages write page', async () => {
], ],
}; };
await writePages({ components, context }); await writePages({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'pages/page1/page1.json', filePath: 'pages/page1/page1.json',
@ -74,7 +70,7 @@ test('writePages multiple pages', async () => {
], ],
}; };
await writePages({ components, context }); await writePages({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'pages/page1/page1.json', filePath: 'pages/page1/page1.json',
@ -105,13 +101,13 @@ test('writePages no pages', async () => {
pages: [], pages: [],
}; };
await writePages({ components, context }); await writePages({ components, context });
expect(mockSet.mock.calls).toEqual([]); expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
}); });
test('writePages pages undefined', async () => { test('writePages pages undefined', async () => {
const components = {}; const components = {};
await writePages({ components, context }); await writePages({ components, context });
expect(mockSet.mock.calls).toEqual([]); expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
}); });
test('writePages pages not an array', async () => { test('writePages pages not an array', async () => {

View File

@ -51,7 +51,7 @@ async function writeRequestsOnPage({ page, context }) {
getRequestsOnBlock({ block: page, requests, pageId: page.pageId }); getRequestsOnBlock({ block: page, requests, pageId: page.pageId });
return requests.map(async (request) => { return requests.map(async (request) => {
await context.artifactSetter.set({ await context.writeBuildArtifact({
filePath: `pages/${page.pageId}/requests/${request.contextId}/${request.requestId}.json`, filePath: `pages/${page.pageId}/requests/${request.contextId}/${request.requestId}.json`,
content: JSON.stringify(request, null, 2), content: JSON.stringify(request, null, 2),
}); });

View File

@ -17,16 +17,12 @@
import writeRequests from './writeRequests'; import writeRequests from './writeRequests';
import testContext from '../test/testContext'; import testContext from '../test/testContext';
const mockSet = jest.fn(); const mockWriteBuildArtifact = jest.fn();
const artifactSetter = { const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
set: mockSet,
};
const context = testContext({ artifactSetter });
beforeEach(() => { beforeEach(() => {
mockSet.mockReset(); mockWriteBuildArtifact.mockReset();
}); });
test('writeRequests write request', async () => { test('writeRequests write request', async () => {
@ -48,7 +44,7 @@ test('writeRequests write request', async () => {
], ],
}; };
await writeRequests({ components, context }); await writeRequests({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'pages/page1/requests/page1/request1.json', filePath: 'pages/page1/requests/page1/request1.json',
@ -95,7 +91,7 @@ test('writeRequests write nested request', async () => {
], ],
}; };
await writeRequests({ components, context }); await writeRequests({ components, context });
expect(mockSet.mock.calls).toEqual([ expect(mockWriteBuildArtifact.mock.calls).toEqual([
[ [
{ {
filePath: 'pages/page1/requests/page1/request1.json', filePath: 'pages/page1/requests/page1/request1.json',
@ -133,13 +129,13 @@ test('writeRequests empty pages array', async () => {
pages: [], pages: [],
}; };
await writeRequests({ components, context }); await writeRequests({ components, context });
expect(mockSet.mock.calls).toEqual([]); expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
}); });
test('writeRequests no pages array', async () => { test('writeRequests no pages array', async () => {
const components = {}; const components = {};
await writeRequests({ components, context }); await writeRequests({ components, context });
expect(mockSet.mock.calls).toEqual([]); expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
}); });
test('writeRequests pages not an array', async () => { test('writeRequests pages not an array', async () => {

View File

@ -1,43 +0,0 @@
/*
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 fs from 'fs';
import path from 'path';
import createFileSetter from './fileSetter';
const baseDirectory = path.resolve(process.cwd(), 'src/test/fileSetter');
test('writeFile', async () => {
const filePath = path.resolve(baseDirectory, 'writeFile.txt');
try {
fs.unlinkSync(filePath);
} catch (error) {
//pass
}
expect(fs.existsSync(filePath)).toBe(false);
const fileSetter = createFileSetter({ baseDirectory });
await fileSetter.set({
filePath: 'writeFile.txt',
content: 'Test fileSetter file',
});
const res = fs.readFileSync(filePath, 'utf8');
expect(res).toEqual('Test fileSetter file');
try {
fs.unlinkSync(filePath);
} catch (error) {
//pass
}
});

View File

@ -15,7 +15,7 @@
*/ */
function testContext({ function testContext({
artifactSetter, writeBuildArtifact,
configDirectory, configDirectory,
configLoader, configLoader,
logger = {}, logger = {},
@ -31,9 +31,7 @@ function testContext({
const context = { const context = {
configDirectory: configDirectory || '', configDirectory: configDirectory || '',
artifactSetter: { writeBuildArtifact: writeBuildArtifact || (() => {}),
set: () => [],
},
configLoader: { configLoader: {
load: () => {}, load: () => {},
}, },
@ -46,10 +44,6 @@ function testContext({
...defaultLogger, ...defaultLogger,
...logger, ...logger,
}; };
if (artifactSetter) {
context.artifactSetter = artifactSetter;
}
if (configLoader) { if (configLoader) {
context.configLoader = configLoader; context.configLoader = configLoader;
} }

View File

@ -17,18 +17,11 @@
import path from 'path'; import path from 'path';
import { writeFile } from '@lowdefy/node-utils'; import { writeFile } from '@lowdefy/node-utils';
class FileSetter { function createWriteBuildArtifact({ outputDirectory }) {
constructor({ baseDirectory }) { async function writeBuildArtifact({ filePath, content }) {
this.baseDirectory = baseDirectory; return writeFile({ filePath: path.resolve(outputDirectory, filePath), content });
}
async set({ filePath, content }) {
return writeFile({ filePath: path.resolve(this.baseDirectory, filePath), content });
} }
return writeBuildArtifact;
} }
function createFileSetter(options) { export default createWriteBuildArtifact;
return new FileSetter(options);
}
export default createFileSetter;