Skip to content

Commit

Permalink
custom node related fixes (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabiGrin authored Dec 23, 2024
1 parent b868555 commit f04df34
Show file tree
Hide file tree
Showing 28 changed files with 328 additions and 627 deletions.
3 changes: 2 additions & 1 deletion core/knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"project": [
"**/*.{js,ts,tsx}",
"!dist/**/*",
"!src/misc/**/*"
"!src/misc",
"!src/improved-macros"
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
MacroConfigurableValue,
InputPin,
nodeInput,
MacroNode,
MacroConfigurableValue,
MacroEditorFieldDefinition,
} from "../..";
MacroNode,
nodeInput,
} from "..";

function extractInputNameAndPath(match: string): {
inputName: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { assert } from "chai";
import { spiedOutput } from "../../test-utils";
import { spiedOutput } from "../test-utils";

import { improvedMacroToOldMacro, ImprovedMacroNode } from "./improved-macros";
import { processImprovedMacro, ImprovedMacroNode } from "./improved-macros";
import {
extractInputsFromValue,
replaceInputsInValue,
} from "./improved-macro-utils";
import { eventually } from "../../";
import { eventually } from "..";
import {
MacroConfigurableValue,
macroConfigurableValue,
nodeOutput,
dynamicNodeInput,
} from "../..";
} from "..";

describe("ImprovedMacros", () => {
describe("SimpleMacro with dot notation", () => {
Expand Down Expand Up @@ -43,7 +43,7 @@ describe("ImprovedMacros", () => {
},
};

const macro = improvedMacroToOldMacro(SimpleMacro);
const macro = processImprovedMacro(SimpleMacro);

const definition = macro.definitionBuilder(macro.defaultData);
assert.deepEqual(Object.keys(definition.inputs), ["person"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
MacroConfigurableValue,
macroConfigurableValue,
MacroEditorFieldDefinition,
} from "../..";
} from "..";
import {
extractInputsFromValue,
generateConfigEditor,
renderDerivedString,
replaceInputsInValue,
} from "./improved-macro-utils";

export * from "./improved-macro-utils";

export type StaticOrDerived<T, Config> = T | ((config: Config) => T);

export interface BaseMacroNodeData<Config = any> {
Expand Down Expand Up @@ -118,9 +120,7 @@ export function isAdvancedMacroNode<Config>(
return (node as AdvancedMacroNode<Config>).defaultConfig !== undefined;
}

export function improvedMacroToOldMacro(
node: ImprovedMacroNode
): MacroNode<any> {
export function processImprovedMacro(node: ImprovedMacroNode): MacroNode<any> {
const isAdvanced = isAdvancedMacroNode(node);

const displayName =
Expand Down
2 changes: 2 additions & 0 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export * from "./node";
export * from "./node/get-node-with-dependencies";
export * from "./flow-schema";

export * from "./improved-macros/improved-macros";

export interface InstanceViewData {
id: string;
nodeIdOrGroup: string | VisualNode;
Expand Down
16 changes: 7 additions & 9 deletions core/src/misc/custom-code-node-from-code.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { isCodeNode, isMacroNode, MacroNode, Node } from "../";
import { transpileFile } from "./transpile-file/transpile-file";
import { improvedMacroToOldMacro } from "./improved-macros.ts/improved-macros";
import { processImprovedMacro } from "../improved-macros/improved-macros";

export function customCodeNodeFromCode(
code: string,
suffixId?: string
suffixId?: string,
imports?: { [key: string]: any }
): Node | MacroNode<any> {
const transpiledCode = transpileFile("", code);

// Wrap the transpiled code to handle default export
const wrappedCode = `(function () {
const wrappedCode = `
const __imports = arguments[0];
const __exports = {};
let __exports = {};
${transpiledCode}
return __exports;
})(arguments)
`;

const result = new Function(wrappedCode)({});
const result = new Function(wrappedCode)(imports);

if (isCodeNode(result.default) || isMacroNode(result.default)) {
if (result.default.icon) {
const macro = improvedMacroToOldMacro(result.default) as MacroNode<any>;
const macro = processImprovedMacro(result.default) as MacroNode<any>;
macro.id = `${macro.id}${suffixId ? `-${suffixId}` : ""}`;
return macro;
}
Expand Down
49 changes: 18 additions & 31 deletions core/src/misc/transpile-file/exports-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,33 @@ export function exportToGlobalTransformer(
) {
const declarations = node.declarationList.declarations;

return ts.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
[
ts.factory.createVariableDeclaration(
ts.factory.createIdentifier("__exports"),
undefined,
undefined,
ts.factory.createObjectLiteralExpression(
declarations.map((declaration) => {
const name = (declaration.name as ts.Identifier).text;
const initializer = declaration.initializer;
return declarations.map((declaration) => {
const name = (declaration.name as ts.Identifier).text;
const initializer = declaration.initializer;

if (!initializer) {
console.warn(
`Initializer is undefined for declaration: ${name}`
);
return ts.factory.createShorthandPropertyAssignment(name);
}
if (!initializer) {
console.warn(`Initializer is undefined for declaration: ${name}`);
return node;
}

return ts.factory.createPropertyAssignment(
name,
initializer
);
})
)
return ts.factory.createExpressionStatement(
ts.factory.createAssignment(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("__exports"),
ts.factory.createIdentifier(name)
),
],
ts.NodeFlags.Const
)
);
initializer
)
);
});
}

// Handle default export assignments
if (ts.isExportAssignment(node)) {
const expression = node.expression;

if (!expression) {
console.warn("Expression is undefined for export assignment");
return node; // Skip transformation if expression is undefined
return node;
}

return ts.factory.createExpressionStatement(
Expand All @@ -71,9 +58,9 @@ export function exportToGlobalTransformer(
return node;
}
};

return (node: ts.SourceFile) => {
const transformedNode = ts.visitNode(node, visit) as ts.SourceFile;
// Add a return statement for the __exports object at the end of the file
const returnStatement = ts.factory.createReturnStatement(
ts.factory.createIdentifier("__exports")
);
Expand Down
2 changes: 1 addition & 1 deletion core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"outDir": "dist",
"types": ["node", "mocha"]
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"include": ["src/**/*.ts", "src/**/*.tsx", "src/misc/improved-macros"],

"exclude": ["node_modules", "dist"]
}
78 changes: 78 additions & 0 deletions flow-editor/src/types/@flyde-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare module '@flyde/core' {
export * from "@flyde/core/node";
export * from "@flyde/core/node/get-node-with-dependencies";
export * from "@flyde/core/flow-schema";
export * from "@flyde/core/improved-macros/improved-macros";
export interface InstanceViewData {
id: string;
nodeIdOrGroup: string | VisualNode;
Expand Down Expand Up @@ -217,6 +218,73 @@ declare module '@flyde/core/node/get-node-with-dependencies' {
export const getNodeWithDependencies: (node: CustomNode, resolvedDeps: CustomNodeCollection, existingIds?: string[]) => CustomNode[];
}

declare module '@flyde/core/improved-macros/improved-macros' {
import { CodeNode, InputPin, OutputPin, MacroNode, NodeStyle, InputMode } from "@flyde/core/";
export * from "@flyde/core/improved-macros/improved-macro-utils";
export type StaticOrDerived<T, Config> = T | ((config: Config) => T);
export interface BaseMacroNodeData<Config = any> {
id: string;
namespace?: string;
menuDisplayName?: string;
menuDescription?: string;
displayName?: StaticOrDerived<string, Config>;
description?: StaticOrDerived<string, Config>;
icon?: string;
completionOutputs?: StaticOrDerived<string[], Config>;
run: CodeNode["run"];
}
export interface SimplifiedMacroNode<Config> extends BaseMacroNodeData<Config> {
inputs: Record<string, InputConfig>;
outputs: Record<string, {
description?: string;
}>;
}
export interface AdvancedMacroNode<Config> extends BaseMacroNodeData<Config> {
inputs: StaticOrDerived<Record<string, InputPin>, Config>;
outputs: StaticOrDerived<Record<string, OutputPin>, Config>;
reactiveInputs?: StaticOrDerived<string[], Config>;
defaultConfig: Config;
editorConfig?: MacroNode<Config>["editorConfig"];
defaultStyle?: NodeStyle;
}
export type ImprovedMacroNode<Config = any> = SimplifiedMacroNode<Config> | AdvancedMacroNode<Config>;
type InputConfig = {
defaultValue?: any;
description?: string;
mode?: InputMode | "reactive";
} & EditorTypeConfig;
type EditorTypeConfig = {
[K in EditorType]: {
editorType?: K;
editorTypeData?: EditorTypeDataMap[K];
};
}[EditorType];
type EditorType = "string" | "number" | "boolean" | "json" | "select" | "longtext" | "enum";
type EditorTypeDataMap = {
string: undefined;
number: {
min?: number;
max?: number;
};
boolean: undefined;
json: undefined;
select: {
options: string[] | {
value: string | number;
label: string;
}[];
};
longtext: {
rows?: number;
};
enum: {
options: string[];
};
};
export function isAdvancedMacroNode<Config>(node: ImprovedMacroNode<Config>): node is AdvancedMacroNode<Config>;
export function processImprovedMacro(node: ImprovedMacroNode): MacroNode<any>;
}

declare module '@flyde/core/common/test-data-creator' {
export type TestDataCreator<T> = (partial?: Partial<T>) => T;
export type ObjOrObjCreator<T> = T | (() => T);
Expand Down Expand Up @@ -679,6 +747,7 @@ declare module '@flyde/core/' {
export * from "@flyde/core/node";
export * from "@flyde/core/node/get-node-with-dependencies";
export * from "@flyde/core/flow-schema";
export * from "@flyde/core/improved-macros/improved-macros";
export interface InstanceViewData {
id: string;
nodeIdOrGroup: string | VisualNode;
Expand Down Expand Up @@ -733,6 +802,15 @@ declare module '@flyde/core/execute/debugger' {
};
}

declare module '@flyde/core/improved-macros/improved-macro-utils' {
import { InputPin, MacroConfigurableValue, MacroNode } from "@flyde/core/";
export function extractInputsFromValue(val: MacroConfigurableValue, key: string): Record<string, InputPin>;
export function replaceInputsInValue(inputs: Record<string, any>, value: MacroConfigurableValue, fieldName: string, ignoreMissingInputs?: boolean): MacroConfigurableValue["value"];
export function renderConfigurableValue(value: MacroConfigurableValue, fieldName: string): string;
export function generateConfigEditor<Config>(config: Config, overrides?: Partial<Record<keyof Config, any>>): MacroNode<Config>["editorConfig"];
export function renderDerivedString(displayName: string, config: any): string;
}

declare module '@flyde/core/node/macro-node' {
import { CodeNode, CodeNodeDefinition, NodeMetadata } from "@flyde/core/node/node";
import type React from "react";
Expand Down
Loading

0 comments on commit f04df34

Please sign in to comment.