mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-18 15:50:27 +08:00
fix(build): Allow _ref path argument to be a _var.
This commit is contained in:
parent
dafa6b3b31
commit
a8bd287176
@ -23,8 +23,13 @@ import YAML from 'js-yaml';
|
||||
import { v1 as uuid } from 'uuid';
|
||||
|
||||
function getRefPath(refDefinition) {
|
||||
if (type.isObject(refDefinition) && refDefinition.path) {
|
||||
return refDefinition.path;
|
||||
if (type.isObject(refDefinition)) {
|
||||
if (refDefinition.path) {
|
||||
return refDefinition.path;
|
||||
}
|
||||
if (refDefinition._var) {
|
||||
return refDefinition;
|
||||
}
|
||||
}
|
||||
if (type.isString(refDefinition)) {
|
||||
return refDefinition;
|
||||
@ -144,16 +149,16 @@ class RefBuilder {
|
||||
for (const refDef of foundRefs.values()) {
|
||||
if (refDef.path === null) {
|
||||
throw new Error(
|
||||
`Invalid _ref definition ${JSON.stringify(refDef.original)} at file ${path}`
|
||||
`Invalid _ref definition ${JSON.stringify({ _ref: refDef.original })} in file ${path}`
|
||||
);
|
||||
}
|
||||
const parsedVars = JSON.parse(
|
||||
JSON.stringify(refDef.vars),
|
||||
const { path: parsedPath, vars: parsedVars } = JSON.parse(
|
||||
JSON.stringify(refDef),
|
||||
refReviver.bind({ parsedFiles, vars })
|
||||
);
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
let parsedFile = await this.recursiveParseFile({
|
||||
path: refDef.path,
|
||||
path: parsedPath,
|
||||
// Parse vars before passing down to parse new file
|
||||
vars: parsedVars,
|
||||
count: count + 1,
|
||||
|
@ -263,7 +263,7 @@ test('buildRefs null ref definition', async () => {
|
||||
];
|
||||
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
|
||||
await expect(buildRefs({ context })).rejects.toThrow(
|
||||
'Invalid _ref definition null at file lowdefy.yaml'
|
||||
'Invalid _ref definition {"_ref":null} in file lowdefy.yaml'
|
||||
);
|
||||
});
|
||||
|
||||
@ -278,7 +278,22 @@ test('buildRefs invalid ref definition', async () => {
|
||||
];
|
||||
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
|
||||
await expect(buildRefs({ context })).rejects.toThrow(
|
||||
'Invalid _ref definition 1 at file lowdefy.yaml'
|
||||
'Invalid _ref definition {"_ref":1} in file lowdefy.yaml'
|
||||
);
|
||||
});
|
||||
|
||||
test('buildRefs invalid ref definition 2', async () => {
|
||||
const files = [
|
||||
{
|
||||
path: 'lowdefy.yaml',
|
||||
content: {
|
||||
invalid: { _ref: { a: 'b' } },
|
||||
},
|
||||
},
|
||||
];
|
||||
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
|
||||
await expect(buildRefs({ context })).rejects.toThrow(
|
||||
'Invalid _ref definition {"_ref":{"a":"b"}} in file lowdefy.yaml'
|
||||
);
|
||||
});
|
||||
|
||||
@ -733,3 +748,75 @@ test('buildRefs _var receives invalid type', async () => {
|
||||
'"_var" operator takes a string or object with name field as arguments. Received "{"_var":[1]}"'
|
||||
);
|
||||
});
|
||||
|
||||
test('buildRefs _ref path is a var, shorthand path', async () => {
|
||||
const files = [
|
||||
{
|
||||
path: 'lowdefy.yaml',
|
||||
content: {
|
||||
_ref: {
|
||||
path: 'file1.yaml',
|
||||
vars: {
|
||||
filePath: 'file2.md',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'file1.yaml',
|
||||
content: {
|
||||
text: {
|
||||
_ref: {
|
||||
_var: 'filePath',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'file2.md',
|
||||
content: 'Hello',
|
||||
},
|
||||
];
|
||||
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
|
||||
const res = await buildRefs({ context });
|
||||
expect(res).toEqual({
|
||||
text: 'Hello',
|
||||
});
|
||||
});
|
||||
|
||||
test('buildRefs _ref path is a var', async () => {
|
||||
const files = [
|
||||
{
|
||||
path: 'lowdefy.yaml',
|
||||
content: {
|
||||
_ref: {
|
||||
path: 'file1.yaml',
|
||||
vars: {
|
||||
filePath: 'file2.md',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'file1.yaml',
|
||||
content: {
|
||||
text: {
|
||||
_ref: {
|
||||
path: {
|
||||
_var: 'filePath',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
path: 'file2.md',
|
||||
content: 'Hello',
|
||||
},
|
||||
];
|
||||
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
|
||||
const res = await buildRefs({ context });
|
||||
expect(res).toEqual({
|
||||
text: 'Hello',
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user