-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow switching between macros in the same namespace easily (#172)
- Loading branch information
Showing
18 changed files
with
379 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
64 changes: 64 additions & 0 deletions
64
dev-server/src/service/resolve-dependent-packages-macros.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
MacrosDefCollection, | ||
NodesDefCollection, | ||
isMacroNode, | ||
} from "@flyde/core"; | ||
import { | ||
deserializeFlow, | ||
isCodeNodePath, | ||
macroNodeToDefinition, | ||
resolveCodeNodeDependencies, | ||
resolveImportablePaths, | ||
} from "@flyde/resolver"; | ||
import { readFileSync } from "fs"; | ||
|
||
export async function resolveDependentPackagesMacros( | ||
rootPath: string, | ||
flydeDependencies: string[] | ||
) { | ||
return flydeDependencies.reduce<Record<string, MacrosDefCollection>>( | ||
(acc, dep) => { | ||
try { | ||
const paths = resolveImportablePaths(rootPath, dep); | ||
const nodes = paths.reduce((acc, filePath) => { | ||
if (isCodeNodePath(filePath)) { | ||
const obj = resolveCodeNodeDependencies(filePath).nodes.reduce( | ||
(obj, { node }) => { | ||
// Only include macro nodes | ||
if (isMacroNode(node)) { | ||
return { | ||
...obj, | ||
[node.id]: macroNodeToDefinition(node, filePath), | ||
}; | ||
} | ||
return obj; | ||
}, | ||
{} | ||
); | ||
|
||
return { ...acc, ...obj }; | ||
} | ||
try { | ||
const flow = deserializeFlow( | ||
readFileSync(filePath, "utf8"), | ||
filePath | ||
); | ||
// Only include macro nodes | ||
if (isMacroNode(flow.node)) { | ||
return { ...acc, [flow.node.id]: flow.node }; | ||
} | ||
return acc; | ||
} catch (e) { | ||
console.error(`Skipping corrupt flow at ${filePath}, error: ${e}`); | ||
return acc; | ||
} | ||
}, {}); | ||
return { ...acc, [dep]: nodes }; | ||
} catch (e) { | ||
console.log(`skipping invalid dependency ${dep}`); | ||
return acc; | ||
} | ||
}, | ||
{} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { join, relative } from "path"; | ||
import { | ||
isCodeNodePath, | ||
resolveCodeNodeDependencies, | ||
deserializeFlow, | ||
macroNodeToDefinition, | ||
} from "@flyde/resolver"; | ||
|
||
import { | ||
debugLogger, | ||
isMacroNode, | ||
ImportablesResult, | ||
ImportableMacrosResult, | ||
MacrosDefCollection, | ||
MacroNode, | ||
} from "@flyde/core"; | ||
import { scanFolderStructure } from "./scan-folders-structure"; | ||
import { FlydeFile } from "../fs-helper/shared"; | ||
import { getFlydeDependencies } from "./get-flyde-dependencies"; | ||
import * as StdLib from "@flyde/stdlib/dist/all"; | ||
import { resolveDependentPackagesMacros } from "./resolve-dependent-packages-macros"; | ||
|
||
export interface CorruptScannedNode { | ||
type: "corrupt"; | ||
error: string; | ||
} | ||
|
||
export async function scanImportableMacros( | ||
rootPath: string, | ||
filename: string | ||
): Promise<ImportableMacrosResult> { | ||
const fileRoot = join(rootPath, filename); | ||
|
||
const localFiles = getLocalFlydeFiles(rootPath); | ||
|
||
const depsNames = await getFlydeDependencies(rootPath); | ||
|
||
const depsNodes = await resolveDependentPackagesMacros(rootPath, depsNames); | ||
|
||
let builtInStdLib: Record<string, MacrosDefCollection> = {}; | ||
if (!depsNames.includes("@flyde/stdlib")) { | ||
debugLogger("Using built-in stdlib"); | ||
|
||
const nodes = Object.fromEntries( | ||
Object.entries(StdLib) | ||
.filter((pair) => isMacroNode(pair[1])) | ||
.map(([id, node]) => [ | ||
id, | ||
macroNodeToDefinition(node as MacroNode<any>, ""), | ||
]) | ||
) as MacrosDefCollection; | ||
builtInStdLib = { | ||
"@flyde/stdlib": nodes, | ||
}; | ||
} | ||
|
||
let allErrors: ImportablesResult["errors"] = []; | ||
|
||
const localMacros = localFiles | ||
.filter((file) => !file.relativePath.endsWith(filename)) | ||
.reduce<Record<string, MacrosDefCollection>>((acc, file) => { | ||
if (isCodeNodePath(file.fullPath)) { | ||
const { errors, nodes } = resolveCodeNodeDependencies(file.fullPath); | ||
allErrors.push( | ||
...errors.map((err) => ({ path: file.fullPath, message: err })) | ||
); | ||
|
||
const nodesObj = nodes.reduce((obj, { node }) => { | ||
if (isMacroNode(node)) { | ||
obj[node.id] = macroNodeToDefinition(node, file.fullPath); | ||
} | ||
return obj; | ||
}, {}); | ||
const relativePath = relative(join(fileRoot, ".."), file.fullPath); | ||
acc[relativePath] ??= {}; | ||
acc[relativePath] = { | ||
...acc[relativePath], | ||
...nodesObj, | ||
}; | ||
|
||
return acc; | ||
} | ||
|
||
return acc; // Skip non-code nodes | ||
}, {}); | ||
|
||
return { | ||
importableMacros: { ...builtInStdLib, ...depsNodes, ...localMacros }, | ||
errors: allErrors, | ||
}; | ||
} | ||
|
||
function getLocalFlydeFiles(rootPath: string) { | ||
const structure = scanFolderStructure(rootPath); | ||
|
||
const localFlydeFiles: FlydeFile[] = []; | ||
const queue = [...structure]; | ||
while (queue.length) { | ||
const item = queue.pop(); | ||
if (item.isFolder === true) { | ||
queue.push(...item.children); | ||
} else if (item.isFlyde || item.isFlydeCode) { | ||
localFlydeFiles.push(item as FlydeFile); | ||
} | ||
} | ||
|
||
return localFlydeFiles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.