feat(build): add build context

This commit is contained in:
Sam Tolmay 2020-10-19 18:06:48 +02:00
parent bb0574fb1d
commit 0f13e4114d
3 changed files with 110 additions and 0 deletions

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 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;

View File

@ -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;

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 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;