fix(build): refactor buildRefs function.

This commit is contained in:
SamTolmay 2021-08-18 23:48:07 +02:00
parent d1591a2a05
commit b66cc5a38d
No known key found for this signature in database
GPG Key ID: 655CB3F5AA745CF8
4 changed files with 61 additions and 38 deletions

View File

@ -15,12 +15,12 @@
*/ */
import recursiveBuild from './recursiveBuild'; import recursiveBuild from './recursiveBuild';
import makeRefDefinition from './makeRefDefinition';
async function buildRefs({ context }) { async function buildRefs({ context }) {
return recursiveBuild({ return recursiveBuild({
context, context,
path: 'lowdefy.yaml', refDef: makeRefDefinition('lowdefy.yaml'),
vars: {},
count: 0, count: 0,
}); });
} }

View File

@ -21,12 +21,16 @@ import YAML from 'js-yaml';
import parseNunjucks from './parseNunjucks'; import parseNunjucks from './parseNunjucks';
async function getRefContent({ context, path, vars }) { async function getRefContent({ context, refDef, referencedFrom }) {
if (!type.isString(path)) { if (!type.isString(refDef.path)) {
throw new Error( 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; let content;
content = await context.readConfigFile(path); content = await context.readConfigFile(path);

View File

@ -14,21 +14,19 @@
limitations under the License. limitations under the License.
*/ */
import * as nodePath from 'path';
import { readFile } from '@lowdefy/node-utils';
import getRefContent from './getRefContent'; import getRefContent from './getRefContent';
import getRefsFromFile from './getRefsFromFile'; import getRefsFromFile from './getRefsFromFile';
import refReviver from './refReviver'; 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? // TODO: Maybe it would be better to detect a cycle, since this is the real issue here?
if (count > 20) { if (count > 20) {
throw new Error(`Maximum recursion depth of references exceeded.`); 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 { foundRefs, fileContentBuiltRefs } = getRefsFromFile(fileContent);
const parsedFiles = {}; const parsedFiles = {};
// Since we can have references in the variables of a reference, we need to first parse // 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. // deepest nodes first we for loop over over foundRefs one by one, awaiting each result.
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
for (const refDef of foundRefs.values()) { for (const newRefDef 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}`
);
}
// Parse vars and path before passing down to parse new file // Parse vars and path before passing down to parse new file
const { path: parsedPath, vars: parsedVars } = JSON.parse( const parsedRefDef = JSON.parse(
JSON.stringify(refDef), JSON.stringify(newRefDef),
refReviver.bind({ parsedFiles, vars }) refReviver.bind({ parsedFiles, vars: refDef.vars })
); );
const parsedFile = await recursiveParseFile({
// eslint-disable-next-line no-await-in-loop
let parsedFile = await recursiveParseFile({
context, context,
path: parsedPath, refDef: parsedRefDef,
vars: parsedVars,
count: count + 1, count: count + 1,
referencedFrom: refDef.path,
});
const transformedFile = await runTransformer({
context,
parsedFile,
refDef: newRefDef,
}); });
if (refDef.transformer) { parsedFiles[newRefDef.id] = transformedFile;
// 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;
} }
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; export default recursiveParseFile;

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