From 0f13e4114dc96a9af918452918614cd13d8930a1 Mon Sep 17 00:00:00 2001 From: Sam Tolmay Date: Mon, 19 Oct 2020 18:06:48 +0200 Subject: [PATCH] feat(build): add build context --- packages/build/src/context/context.js | 30 ++++++++++++++++ packages/build/src/context/fileLoader.js | 46 ++++++++++++++++++++++++ packages/build/src/context/fileSetter.js | 34 ++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 packages/build/src/context/context.js create mode 100644 packages/build/src/context/fileLoader.js create mode 100644 packages/build/src/context/fileSetter.js diff --git a/packages/build/src/context/context.js b/packages/build/src/context/context.js new file mode 100644 index 000000000..cbd76dcca --- /dev/null +++ b/packages/build/src/context/context.js @@ -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 createFileLoader from './fileLoader'; +import createFileSetter from './fileSetter'; + +function createContext(bootstrapContext) { + const context = { + logger: bootstrapContext.logger, + configLoader: createFileLoader({ baseDir: bootstrapContext.configBaseDir }), + artifactSetter: createFileSetter({ baseDir: bootstrapContext.outputBaseDir }), + }; + + return context; +} + +export default createContext; diff --git a/packages/build/src/context/fileLoader.js b/packages/build/src/context/fileLoader.js new file mode 100644 index 000000000..075cc5e67 --- /dev/null +++ b/packages/build/src/context/fileLoader.js @@ -0,0 +1,46 @@ +/* + 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 { get } from '@lowdefy/helpers'; +import path from 'path'; +import Dataloader from 'dataloader'; +import getFile from '../utils/files/getFile'; + +function createFileBatchLoader({ baseDir }) { + async function loader(keys) { + const fetched = []; + const promises = keys.map(async (filePath) => { + const item = await getFile(path.resolve(baseDir, filePath)); + fetched.push(item); + }); + await Promise.all(promises); + const returned = keys + .map((filePath) => + fetched.find((item) => { + return get(item, 'filePath') === filePath; + }) + ) + .map((obj) => obj.content); + return returned; + } + return loader; +} + +function createFileLoader(options) { + return new Dataloader(createFileBatchLoader(options)); +} + +export default createFileLoader; diff --git a/packages/build/src/context/fileSetter.js b/packages/build/src/context/fileSetter.js new file mode 100644 index 000000000..a1877f5e6 --- /dev/null +++ b/packages/build/src/context/fileSetter.js @@ -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 path from 'path'; +import writeFile from '../utils/files/writeFile'; + +class FileSetter { + constructor({ baseDir }) { + this.baseDir = baseDir; + } + + async set({ filePath, content }) { + return writeFile({ filePath: path.resolve(this.baseDir, filePath), content }); + } +} + +function createFileSetter(options) { + return new FileSetter(options); +} + +export default createFileSetter;