mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-02-23 14:39:32 +08:00
fix(build): refactor buildRefs function.
This commit is contained in:
parent
d1591a2a05
commit
b66cc5a38d
@ -15,12 +15,12 @@
|
||||
*/
|
||||
|
||||
import recursiveBuild from './recursiveBuild';
|
||||
import makeRefDefinition from './makeRefDefinition';
|
||||
|
||||
async function buildRefs({ context }) {
|
||||
return recursiveBuild({
|
||||
context,
|
||||
path: 'lowdefy.yaml',
|
||||
vars: {},
|
||||
refDef: makeRefDefinition('lowdefy.yaml'),
|
||||
count: 0,
|
||||
});
|
||||
}
|
||||
|
@ -21,12 +21,16 @@ import YAML from 'js-yaml';
|
||||
|
||||
import parseNunjucks from './parseNunjucks';
|
||||
|
||||
async function getRefContent({ context, path, vars }) {
|
||||
if (!type.isString(path)) {
|
||||
async function getRefContent({ context, refDef, referencedFrom }) {
|
||||
if (!type.isString(refDef.path)) {
|
||||
throw new Error(
|
||||
`Tried to get file with file path ${JSON.stringify(path)}, but file path should be a string`
|
||||
`Invalid _ref definition ${JSON.stringify({
|
||||
_ref: refDef.original,
|
||||
})} in file ${referencedFrom}`
|
||||
);
|
||||
}
|
||||
|
||||
const { path, vars } = refDef;
|
||||
let content;
|
||||
content = await context.readConfigFile(path);
|
||||
|
||||
|
@ -14,21 +14,19 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import * as nodePath from 'path';
|
||||
|
||||
import { readFile } from '@lowdefy/node-utils';
|
||||
|
||||
import getRefContent from './getRefContent';
|
||||
import getRefsFromFile from './getRefsFromFile';
|
||||
import refReviver from './refReviver';
|
||||
import runTransformer from './runTransformer';
|
||||
|
||||
async function recursiveParseFile({ context, path, count, vars }) {
|
||||
async function recursiveParseFile({ context, refDef, count, referencedFrom }) {
|
||||
// TODO: Maybe it would be better to detect a cycle, since this is the real issue here?
|
||||
if (count > 20) {
|
||||
throw new Error(`Maximum recursion depth of references exceeded.`);
|
||||
}
|
||||
let fileContent = await getRefContent({ context, path, vars });
|
||||
let fileContent = await getRefContent({ context, refDef, referencedFrom });
|
||||
const { foundRefs, fileContentBuiltRefs } = getRefsFromFile(fileContent);
|
||||
|
||||
const parsedFiles = {};
|
||||
|
||||
// Since we can have references in the variables of a reference, we need to first parse
|
||||
@ -37,40 +35,30 @@ async function recursiveParseFile({ context, path, count, vars }) {
|
||||
// deepest nodes first we for loop over over foundRefs one by one, awaiting each result.
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const refDef of foundRefs.values()) {
|
||||
// TODO: This will now be valid if resolver is set
|
||||
if (refDef.path === null) {
|
||||
throw new Error(
|
||||
`Invalid _ref definition ${JSON.stringify({ _ref: refDef.original })} in file ${path}`
|
||||
);
|
||||
}
|
||||
|
||||
for (const newRefDef of foundRefs.values()) {
|
||||
// Parse vars and path before passing down to parse new file
|
||||
const { path: parsedPath, vars: parsedVars } = JSON.parse(
|
||||
JSON.stringify(refDef),
|
||||
refReviver.bind({ parsedFiles, vars })
|
||||
const parsedRefDef = JSON.parse(
|
||||
JSON.stringify(newRefDef),
|
||||
refReviver.bind({ parsedFiles, vars: refDef.vars })
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
let parsedFile = await recursiveParseFile({
|
||||
const parsedFile = await recursiveParseFile({
|
||||
context,
|
||||
path: parsedPath,
|
||||
vars: parsedVars,
|
||||
refDef: parsedRefDef,
|
||||
count: count + 1,
|
||||
referencedFrom: refDef.path,
|
||||
});
|
||||
const transformedFile = await runTransformer({
|
||||
context,
|
||||
parsedFile,
|
||||
refDef: newRefDef,
|
||||
});
|
||||
|
||||
if (refDef.transformer) {
|
||||
// TODO: create a helper fn to handle reading and executing JS
|
||||
const transformerFile = await readFile(
|
||||
nodePath.resolve(context.configDirectory, refDef.transformer)
|
||||
);
|
||||
const transformerFn = eval(transformerFile);
|
||||
parsedFile = transformerFn(parsedFile, parsedVars);
|
||||
}
|
||||
|
||||
parsedFiles[refDef.id] = parsedFile;
|
||||
parsedFiles[newRefDef.id] = transformedFile;
|
||||
}
|
||||
return JSON.parse(JSON.stringify(fileContentBuiltRefs), refReviver.bind({ parsedFiles, vars }));
|
||||
return JSON.parse(
|
||||
JSON.stringify(fileContentBuiltRefs),
|
||||
refReviver.bind({ parsedFiles, vars: refDef.vars })
|
||||
);
|
||||
}
|
||||
|
||||
export default recursiveParseFile;
|
||||
|
31
packages/build/src/build/buildRefs/runTransformer.js
Normal file
31
packages/build/src/build/buildRefs/runTransformer.js
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
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 * as nodePath from 'path';
|
||||
import { readFile } from '@lowdefy/node-utils';
|
||||
|
||||
async function runTransformer({ context, parsedFile, refDef }) {
|
||||
if (refDef.transformer) {
|
||||
// TODO: create a helper fn to handle reading and executing JS
|
||||
const transformerFile = await readFile(
|
||||
nodePath.resolve(context.configDirectory, refDef.transformer)
|
||||
);
|
||||
const transformerFn = eval(transformerFile);
|
||||
return transformerFn(parsedFile, refDef.vars);
|
||||
}
|
||||
return parsedFile;
|
||||
}
|
||||
|
||||
export default runTransformer;
|
Loading…
Reference in New Issue
Block a user