mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-01-06 13:15:24 +08:00
feat(build): add build context
This commit is contained in:
parent
bb0574fb1d
commit
0f13e4114d
30
packages/build/src/context/context.js
Normal file
30
packages/build/src/context/context.js
Normal 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;
|
46
packages/build/src/context/fileLoader.js
Normal file
46
packages/build/src/context/fileLoader.js
Normal 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;
|
34
packages/build/src/context/fileSetter.js
Normal file
34
packages/build/src/context/fileSetter.js
Normal 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;
|
Loading…
Reference in New Issue
Block a user