1+ import yaml from 'js-yaml' ;
2+ import fs from 'fs' ;
3+ import path from 'path' ;
4+ import { fileURLToPath } from 'url' ;
5+
6+ const __filename = fileURLToPath ( import . meta. url ) ;
7+ const __dirname = path . dirname ( __filename ) ;
8+
9+ const SPACING_X = 320 ;
10+ const SPACING_Y = 240 ;
11+ const MIN_GAP = 280 ;
12+
13+ function calculateNodeSpacing ( nodes , edges ) {
14+ const graph = { } ;
15+
16+ nodes . forEach ( node => {
17+ graph [ node . id ] = {
18+ x : node . position . x ,
19+ y : node . position . y ,
20+ width : 200 ,
21+ height : 100 ,
22+ } ;
23+ } ) ;
24+
25+ return graph ;
26+ }
27+
28+ function adjustPositions ( nodePositions ) {
29+ const entries = Object . entries ( nodePositions ) ;
30+
31+ if ( entries . length === 0 ) return nodePositions ;
32+
33+ const nodes = entries . map ( ( [ id , pos ] ) => ( {
34+ id,
35+ x : typeof pos . x === 'number' ? pos . x : parseFloat ( pos . x ) || 0 ,
36+ y : typeof pos . y === 'number' ? pos . y : parseFloat ( pos . y ) || 0 ,
37+ } ) ) ;
38+
39+ nodes . sort ( ( a , b ) => a . x - b . x || a . y - b . y ) ;
40+
41+ const adjustedNodes = nodes . map ( ( node , index ) => {
42+ const col = index % 5 ;
43+ const row = Math . floor ( index / 5 ) ;
44+
45+ return {
46+ id : node . id ,
47+ x : col * SPACING_X + ( row % 2 === 1 ? SPACING_X / 2 : 0 ) ,
48+ y : row * SPACING_Y ,
49+ } ;
50+ } ) ;
51+
52+ const result = { } ;
53+ adjustedNodes . forEach ( node => {
54+ result [ node . id ] = {
55+ x : node . x ,
56+ y : node . y ,
57+ } ;
58+ } ) ;
59+
60+ return result ;
61+ }
62+
63+ function processFlowYaml ( filePath ) {
64+ const content = fs . readFileSync ( filePath , 'utf-8' ) ;
65+ const flow = yaml . load ( content ) ;
66+
67+ if ( ! flow . ui || ! flow . ui . nodePositions ) {
68+ console . log ( `No node positions found in ${ filePath } ` ) ;
69+ return ;
70+ }
71+
72+ console . log ( `\nProcessing: ${ filePath } ` ) ;
73+ console . log ( `Nodes count: ${ Object . keys ( flow . ui . nodePositions ) . length } ` ) ;
74+
75+ const oldPositions = { ...flow . ui . nodePositions } ;
76+ flow . ui . nodePositions = adjustPositions ( flow . ui . nodePositions ) ;
77+
78+ const backupPath = filePath + '.backup' ;
79+ fs . writeFileSync ( backupPath , content ) ;
80+ console . log ( `Backup saved to: ${ backupPath } ` ) ;
81+
82+ const newContent = yaml . dump ( flow , {
83+ indent : 2 ,
84+ lineWidth : - 1 ,
85+ noRefs : true ,
86+ quotingType : '"' ,
87+ forceQuotes : false ,
88+ } ) ;
89+
90+ fs . writeFileSync ( filePath , newContent ) ;
91+ console . log ( `Updated: ${ filePath } ` ) ;
92+ }
93+
94+ function main ( ) {
95+ const pipelinesDir = path . join ( __dirname , '..' , '..' , 'builtin' , 'pipelines' ) ;
96+
97+ const flows = [
98+ path . join ( pipelinesDir , 'new' , 'flow.yaml' ) ,
99+ path . join ( pipelinesDir , 'module-migrate' , 'flow.yaml' ) ,
100+ ] ;
101+
102+ flows . forEach ( flowPath => {
103+ if ( fs . existsSync ( flowPath ) ) {
104+ processFlowYaml ( flowPath ) ;
105+ } else {
106+ console . log ( `File not found: ${ flowPath } ` ) ;
107+ }
108+ } ) ;
109+
110+ console . log ( '\n✅ Node positions adjusted successfully!' ) ;
111+ }
112+
113+ main ( ) ;
0 commit comments