n8n/packages/@n8n/node-cli/src/utils/ast.ts
Matsu a2ff55c1f9
chore: Reuse ts-morph Project instance across custom template run (no-changelog) (#28972)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 11:23:18 +00:00

46 lines
1.3 KiB
TypeScript

import {
Project,
SyntaxKind,
type ClassDeclaration,
type ObjectLiteralExpression,
type PropertyAssignment,
type PropertyDeclaration,
} from 'ts-morph';
export { Project };
export const createProject = () => new Project({ skipFileDependencyResolution: true });
export const loadSingleSourceFile = (path: string, project?: Project) => {
const p = project ?? createProject();
return p.addSourceFileAtPath(path);
};
const setStringInitializer = (prop: PropertyAssignment | PropertyDeclaration, value: string) => {
prop.getInitializerIfKindOrThrow(SyntaxKind.StringLiteral).setLiteralValue(value);
};
export const updateStringProperty = ({
obj,
key,
value,
}: { obj: ObjectLiteralExpression | ClassDeclaration; key: string; value: string }) => {
const prop = obj.getPropertyOrThrow(key);
if (prop.isKind(SyntaxKind.PropertyAssignment)) {
setStringInitializer(prop.asKindOrThrow(SyntaxKind.PropertyAssignment), value);
} else if (prop.isKind(SyntaxKind.PropertyDeclaration)) {
setStringInitializer(prop.asKindOrThrow(SyntaxKind.PropertyDeclaration), value);
}
};
export const getChildObjectLiteral = ({
obj,
key,
}: { obj: ObjectLiteralExpression; key: string }) => {
return obj
.getPropertyOrThrow(key)
.asKindOrThrow(SyntaxKind.PropertyAssignment)
.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
};