@compas/code-gen
Advanced tools
Comparing version
{ | ||
"name": "@compas/code-gen", | ||
"version": "0.0.171", | ||
"version": "0.0.172", | ||
"description": "Generate various boring parts of your server", | ||
@@ -18,4 +18,4 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@compas/cli": "0.0.171", | ||
"@compas/stdlib": "0.0.171" | ||
"@compas/cli": "0.0.172", | ||
"@compas/stdlib": "0.0.172" | ||
}, | ||
@@ -22,0 +22,0 @@ "maintainers": [ |
@@ -138,2 +138,3 @@ // @ts-nocheck | ||
type FileCache = store.FileCache; | ||
type SessionStoreSettings = store.SessionStoreSettings; | ||
`; | ||
@@ -140,0 +141,0 @@ } else if (generator === "server") { |
@@ -36,14 +36,2 @@ /** | ||
/** | ||
* Find nested references and add to generatorInput in the correct group | ||
* | ||
* @param rootData | ||
* @param generatorInput | ||
* @param value | ||
*/ | ||
export function includeReferenceTypes( | ||
rootData: any, | ||
generatorInput: any, | ||
value: any, | ||
): any; | ||
/** | ||
* @param root | ||
@@ -50,0 +38,0 @@ * @param structure |
@@ -19,5 +19,48 @@ // @ts-nocheck | ||
const error = includeReferenceTypes(structure, input, input); | ||
if (error) { | ||
throw error; | ||
includeReferenceTypes(structure, input); | ||
} | ||
/** | ||
* Find nested references and add to generatorInput in the correct group | ||
* | ||
* @param {CodeGenStructure} structure | ||
* @param {CodeGenStructure} input | ||
* @returns {void} | ||
*/ | ||
function includeReferenceTypes(structure, input) { | ||
const stack = [input]; | ||
while (stack.length) { | ||
const currentObject = stack.shift(); | ||
// handle values | ||
if (currentObject?.type === "reference") { | ||
const { group, name, uniqueName } = currentObject.reference; | ||
// ensure ref does not already exits | ||
if (!isNil(structure[group]?.[name]) && isNil(input[group]?.[name])) { | ||
addToData(input, structure[group][name]); | ||
// Note that we need the full referenced object here, since | ||
// currentObject.reference only contains { group, name, uniqueName } | ||
stack.push(input[group][name]); | ||
continue; | ||
} else if (isNil(structure[group]?.[name])) { | ||
throw new AppError("codeGen.app.followReferences", 500, { | ||
message: `Could not resolve reference '${uniqueName}'`, | ||
}); | ||
} | ||
} | ||
// extend stack | ||
if (Array.isArray(currentObject)) { | ||
for (const it of currentObject) { | ||
stack.push(it); | ||
} | ||
} else if (isPlainObject(currentObject)) { | ||
for (const key of Object.keys(currentObject)) { | ||
stack.push(currentObject[key]); | ||
} | ||
} | ||
} | ||
@@ -71,71 +114,2 @@ } | ||
/** | ||
* Find nested references and add to generatorInput in the correct group | ||
* | ||
* @param rootData | ||
* @param generatorInput | ||
* @param value | ||
*/ | ||
export function includeReferenceTypes(rootData, generatorInput, value) { | ||
if (isNil(value) || (!isPlainObject(value) && !Array.isArray(value))) { | ||
// Skip primitives & null / undefined | ||
return; | ||
} | ||
if ( | ||
isPlainObject(value) && | ||
value.type && | ||
value.type === "reference" && | ||
isPlainObject(value.reference) | ||
) { | ||
const { group, name } = value.reference; | ||
if ( | ||
!isNil(rootData[group]?.[name]) && | ||
isNil(generatorInput[group]?.[name]) | ||
) { | ||
if (isNil(generatorInput[group])) { | ||
generatorInput[group] = {}; | ||
} | ||
const refValue = rootData[group][name]; | ||
generatorInput[group][name] = refValue; | ||
const err = includeReferenceTypes(rootData, generatorInput, refValue); | ||
if (err) { | ||
if (value.uniqueName) { | ||
err.info.foundAt = value.uniqueName; | ||
} | ||
return err; | ||
} | ||
} else if (isNil(rootData[group]?.[name])) { | ||
return new AppError("codeGen.app.followReferences", 500, { | ||
message: `Could not resolve reference '${value.reference.uniqueName}'.`, | ||
foundAt: "unknown", | ||
}); | ||
} | ||
} | ||
if (isPlainObject(value)) { | ||
for (const key of Object.keys(value)) { | ||
const err = includeReferenceTypes(rootData, generatorInput, value[key]); | ||
if (err) { | ||
if (value.uniqueName) { | ||
err.info.foundAt = value.uniqueName; | ||
} | ||
return err; | ||
} | ||
} | ||
} else if (Array.isArray(value)) { | ||
for (let i = 0; i < value.length; ++i) { | ||
const err = includeReferenceTypes(rootData, generatorInput, value[i]); | ||
if (err) { | ||
if (value.uniqueName) { | ||
err.info.foundAt = value.uniqueName; | ||
} | ||
return err; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* @param root | ||
@@ -142,0 +116,0 @@ * @param structure |
@@ -1,2 +0,2 @@ | ||
import { addToData, includeReferenceTypes } from "../generate.js"; | ||
import { addGroupsToGeneratorInput, addToData } from "../generate.js"; | ||
import { js } from "./tag/index.js"; | ||
@@ -25,5 +25,5 @@ | ||
structureSource += js` | ||
export const ${group}StructureString = '${string}'; | ||
export const ${group}Structure = JSON.parse(${group}StructureString); | ||
`; | ||
export const ${group}StructureString = '${string}'; | ||
export const ${group}Structure = JSON.parse(${group}StructureString); | ||
`; | ||
} | ||
@@ -36,5 +36,5 @@ | ||
structureSource += js` | ||
export const structure = Object.assign({}, ${groups}); | ||
export const structureString = JSON.stringify(structure); | ||
`; | ||
export const structure = Object.assign({}, ${groups}); | ||
export const structureString = JSON.stringify(structure); | ||
`; | ||
} | ||
@@ -56,10 +56,7 @@ | ||
// Include recursive references that are used in route types | ||
const error = includeReferenceTypes( | ||
addGroupsToGeneratorInput( | ||
apiStructure, | ||
context.structure, | ||
apiStructure, | ||
apiStructure, | ||
Object.keys(apiStructure), | ||
); | ||
if (error) { | ||
throw error; | ||
} | ||
@@ -73,4 +70,4 @@ const string = JSON.stringify(apiStructure) | ||
structureSource += js` | ||
export const compasApiStructureString = '${string}'; | ||
`; | ||
export const compasApiStructureString = '${string}'; | ||
`; | ||
} | ||
@@ -77,0 +74,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
740667
-0.11%20353
-0.21%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated