mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-02-23 14:39:32 +08:00
fix(build): Refactor writing of build artifact files.
This commit is contained in:
parent
808f619d19
commit
7162760b18
@ -17,7 +17,7 @@
|
||||
*/
|
||||
|
||||
import createFileLoader from './loaders/fileLoader';
|
||||
import createFileSetter from './loaders/fileSetter';
|
||||
import createWriteBuildArtifact from './utils/files/writeBuildArtifact';
|
||||
import createMetaLoader from './loaders/metaLoader';
|
||||
|
||||
import addDefaultPages from './build/addDefaultPages/addDefaultPages';
|
||||
@ -41,7 +41,7 @@ import writeRequests from './build/writeRequests';
|
||||
function createContext(options) {
|
||||
const { blocksServerUrl, cacheDirectory, configDirectory, logger, outputDirectory } = options;
|
||||
const context = {
|
||||
artifactSetter: createFileSetter({ baseDirectory: outputDirectory }),
|
||||
writeBuildArtifact: createWriteBuildArtifact({ outputDirectory }),
|
||||
configLoader: createFileLoader({ baseDirectory: configDirectory }),
|
||||
blocksServerUrl,
|
||||
cacheDirectory,
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
async function writeApp({ components, context }) {
|
||||
await context.artifactSetter.set({
|
||||
await context.writeBuildArtifact({
|
||||
filePath: 'app.json',
|
||||
content: JSON.stringify(components.app || {}, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writeApp from './writeApp';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writeApp', async () => {
|
||||
@ -36,7 +32,7 @@ test('writeApp', async () => {
|
||||
},
|
||||
};
|
||||
await writeApp({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'app.json',
|
||||
@ -53,7 +49,7 @@ test('writeApp empty config', async () => {
|
||||
app: {},
|
||||
};
|
||||
await writeApp({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'app.json',
|
||||
@ -66,7 +62,7 @@ test('writeApp empty config', async () => {
|
||||
test('writeApp config undefined', async () => {
|
||||
const components = {};
|
||||
await writeApp({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'app.json',
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
async function writeConfig({ components, context }) {
|
||||
await context.artifactSetter.set({
|
||||
await context.writeBuildArtifact({
|
||||
filePath: 'config.json',
|
||||
content: JSON.stringify(components.config || {}, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writeConfig from './writeConfig';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writeConfig', async () => {
|
||||
@ -36,7 +32,7 @@ test('writeConfig', async () => {
|
||||
},
|
||||
};
|
||||
await writeConfig({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'config.json',
|
||||
@ -53,7 +49,7 @@ test('writeConfig empty config', async () => {
|
||||
config: {},
|
||||
};
|
||||
await writeConfig({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'config.json',
|
||||
@ -66,7 +62,7 @@ test('writeConfig empty config', async () => {
|
||||
test('writeConfig config undefined', async () => {
|
||||
const components = {};
|
||||
await writeConfig({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'config.json',
|
||||
|
@ -22,7 +22,7 @@ async function writeConnections({ components, context }) {
|
||||
throw new Error(`Connections is not an array.`);
|
||||
}
|
||||
const writePromises = components.connections.map(async (connection) => {
|
||||
await context.artifactSetter.set({
|
||||
await context.writeBuildArtifact({
|
||||
filePath: `connections/${connection.connectionId}.json`,
|
||||
content: JSON.stringify(connection, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writeConnections from './writeConnections';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writeConnections write connection', async () => {
|
||||
@ -42,7 +38,7 @@ test('writeConnections write connection', async () => {
|
||||
],
|
||||
};
|
||||
await writeConnections({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'connections/connection1.json',
|
||||
@ -72,7 +68,7 @@ test('writeConnections multiple connection', async () => {
|
||||
],
|
||||
};
|
||||
await writeConnections({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'connections/connection1.json',
|
||||
@ -99,13 +95,13 @@ test('writeConnections no connections', async () => {
|
||||
connections: [],
|
||||
};
|
||||
await writeConnections({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([]);
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
|
||||
});
|
||||
|
||||
test('writeConnections connections undefined', async () => {
|
||||
const components = {};
|
||||
await writeConnections({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([]);
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
|
||||
});
|
||||
|
||||
test('writeConnections connections not an array', async () => {
|
||||
|
@ -23,7 +23,7 @@ async function writeGlobal({ components, context }) {
|
||||
if (!type.isObject(components.global)) {
|
||||
throw new Error('Global is not an object.');
|
||||
}
|
||||
await context.artifactSetter.set({
|
||||
await context.writeBuildArtifact({
|
||||
filePath: 'global.json',
|
||||
content: JSON.stringify(components.global, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writeGlobal from './writeGlobal';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writeGlobal', async () => {
|
||||
@ -36,7 +32,7 @@ test('writeGlobal', async () => {
|
||||
},
|
||||
};
|
||||
await writeGlobal({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'global.json',
|
||||
@ -53,7 +49,7 @@ test('writeGlobal empty global', async () => {
|
||||
global: {},
|
||||
};
|
||||
await writeGlobal({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'global.json',
|
||||
@ -66,7 +62,7 @@ test('writeGlobal empty global', async () => {
|
||||
test('writeGlobal global undefined', async () => {
|
||||
const components = {};
|
||||
await writeGlobal({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'global.json',
|
||||
|
@ -20,7 +20,7 @@ async function writeMenus({ components, context }) {
|
||||
if (!type.isArray(components.menus)) {
|
||||
throw new Error('Menus is not an array.');
|
||||
}
|
||||
await context.artifactSetter.set({
|
||||
await context.writeBuildArtifact({
|
||||
filePath: 'menus.json',
|
||||
content: JSON.stringify(components.menus, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writeMenus from './writeMenus';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writeMenus', async () => {
|
||||
@ -40,7 +36,7 @@ test('writeMenus', async () => {
|
||||
],
|
||||
};
|
||||
await writeMenus({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'menus.json',
|
||||
@ -61,7 +57,7 @@ test('writeMenus empty menus', async () => {
|
||||
menus: [],
|
||||
};
|
||||
await writeMenus({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'menus.json',
|
||||
|
@ -20,7 +20,7 @@ async function writePage({ page, context }) {
|
||||
if (!type.isObject(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`,
|
||||
content: JSON.stringify(page, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writePages from './writePages';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writePages write page', async () => {
|
||||
@ -41,7 +37,7 @@ test('writePages write page', async () => {
|
||||
],
|
||||
};
|
||||
await writePages({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'pages/page1/page1.json',
|
||||
@ -74,7 +70,7 @@ test('writePages multiple pages', async () => {
|
||||
],
|
||||
};
|
||||
await writePages({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'pages/page1/page1.json',
|
||||
@ -105,13 +101,13 @@ test('writePages no pages', async () => {
|
||||
pages: [],
|
||||
};
|
||||
await writePages({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([]);
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
|
||||
});
|
||||
|
||||
test('writePages pages undefined', async () => {
|
||||
const components = {};
|
||||
await writePages({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([]);
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
|
||||
});
|
||||
|
||||
test('writePages pages not an array', async () => {
|
||||
|
@ -51,7 +51,7 @@ async function writeRequestsOnPage({ page, context }) {
|
||||
getRequestsOnBlock({ block: page, requests, pageId: page.pageId });
|
||||
|
||||
return requests.map(async (request) => {
|
||||
await context.artifactSetter.set({
|
||||
await context.writeBuildArtifact({
|
||||
filePath: `pages/${page.pageId}/requests/${request.contextId}/${request.requestId}.json`,
|
||||
content: JSON.stringify(request, null, 2),
|
||||
});
|
||||
|
@ -17,16 +17,12 @@
|
||||
import writeRequests from './writeRequests';
|
||||
import testContext from '../test/testContext';
|
||||
|
||||
const mockSet = jest.fn();
|
||||
const mockWriteBuildArtifact = jest.fn();
|
||||
|
||||
const artifactSetter = {
|
||||
set: mockSet,
|
||||
};
|
||||
|
||||
const context = testContext({ artifactSetter });
|
||||
const context = testContext({ writeBuildArtifact: mockWriteBuildArtifact });
|
||||
|
||||
beforeEach(() => {
|
||||
mockSet.mockReset();
|
||||
mockWriteBuildArtifact.mockReset();
|
||||
});
|
||||
|
||||
test('writeRequests write request', async () => {
|
||||
@ -48,7 +44,7 @@ test('writeRequests write request', async () => {
|
||||
],
|
||||
};
|
||||
await writeRequests({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'pages/page1/requests/page1/request1.json',
|
||||
@ -95,7 +91,7 @@ test('writeRequests write nested request', async () => {
|
||||
],
|
||||
};
|
||||
await writeRequests({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([
|
||||
[
|
||||
{
|
||||
filePath: 'pages/page1/requests/page1/request1.json',
|
||||
@ -133,13 +129,13 @@ test('writeRequests empty pages array', async () => {
|
||||
pages: [],
|
||||
};
|
||||
await writeRequests({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([]);
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
|
||||
});
|
||||
|
||||
test('writeRequests no pages array', async () => {
|
||||
const components = {};
|
||||
await writeRequests({ components, context });
|
||||
expect(mockSet.mock.calls).toEqual([]);
|
||||
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
|
||||
});
|
||||
|
||||
test('writeRequests pages not an array', async () => {
|
||||
|
@ -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
|
||||
}
|
||||
});
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
function testContext({
|
||||
artifactSetter,
|
||||
writeBuildArtifact,
|
||||
configDirectory,
|
||||
configLoader,
|
||||
logger = {},
|
||||
@ -31,9 +31,7 @@ function testContext({
|
||||
|
||||
const context = {
|
||||
configDirectory: configDirectory || '',
|
||||
artifactSetter: {
|
||||
set: () => [],
|
||||
},
|
||||
writeBuildArtifact: writeBuildArtifact || (() => {}),
|
||||
configLoader: {
|
||||
load: () => {},
|
||||
},
|
||||
@ -46,10 +44,6 @@ function testContext({
|
||||
...defaultLogger,
|
||||
...logger,
|
||||
};
|
||||
|
||||
if (artifactSetter) {
|
||||
context.artifactSetter = artifactSetter;
|
||||
}
|
||||
if (configLoader) {
|
||||
context.configLoader = configLoader;
|
||||
}
|
||||
|
@ -17,18 +17,11 @@
|
||||
import path from 'path';
|
||||
import { writeFile } from '@lowdefy/node-utils';
|
||||
|
||||
class FileSetter {
|
||||
constructor({ baseDirectory }) {
|
||||
this.baseDirectory = baseDirectory;
|
||||
}
|
||||
|
||||
async set({ filePath, content }) {
|
||||
return writeFile({ filePath: path.resolve(this.baseDirectory, filePath), content });
|
||||
function createWriteBuildArtifact({ outputDirectory }) {
|
||||
async function writeBuildArtifact({ filePath, content }) {
|
||||
return writeFile({ filePath: path.resolve(outputDirectory, filePath), content });
|
||||
}
|
||||
return writeBuildArtifact;
|
||||
}
|
||||
|
||||
function createFileSetter(options) {
|
||||
return new FileSetter(options);
|
||||
}
|
||||
|
||||
export default createFileSetter;
|
||||
export default createWriteBuildArtifact;
|
Loading…
Reference in New Issue
Block a user