-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-layers.js
More file actions
109 lines (90 loc) · 3 KB
/
Copy pathtest-layers.js
File metadata and controls
109 lines (90 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Simple test script to verify layer creation
const fs = require('fs');
const path = require('path');
// Simulate the layer creation logic
function mantaDir() {
return path.join(process.cwd(), 'manta');
}
function layersRootDir() {
return path.join(mantaDir(), 'graphs');
}
function ensureLayersRoot() {
fs.mkdirSync(layersRootDir(), { recursive: true });
}
function listLayers() {
try {
ensureLayersRoot();
const entries = fs.readdirSync(layersRootDir())
.filter(file => file.endsWith('.json') && file !== 'active-layer.json')
.map(file => file.replace('.json', ''));
// Always include C4 layers first
const c4Layers = ['system', 'container', 'component', 'code'];
const userLayers = entries.filter(layer => !c4Layers.includes(layer));
// Sort user layers by creation time if available, otherwise lexicographically
const sortedUserLayers = userLayers.sort((a, b) => a.localeCompare(b));
return [...c4Layers, ...sortedUserLayers];
} catch {
// Return C4 layers even if directory doesn't exist
return ['system', 'container', 'component', 'code'];
}
}
function createLayer(desiredName) {
ensureLayersRoot();
let name = desiredName?.trim();
if (!name) {
// Find next graphN name
const existing = new Set(listLayers());
let i = 1;
while (existing.has(`graph${i}`)) i++;
name = `graph${i}`;
}
// Check if layer already exists
const existingLayers = listLayers();
if (existingLayers.includes(name)) {
throw new Error(`Layer '${name}' already exists`);
}
console.log(`Creating layer: ${name}`);
console.log(`Layers root: ${layersRootDir()}`);
// Create layer definition
const layerDef = {
name,
nodeIds: [],
positions: {},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
const layerDefPath = path.join(layersRootDir(), `${name}.json`);
fs.writeFileSync(layerDefPath, JSON.stringify(layerDef, null, 2));
console.log(`Created layer definition: ${layerDefPath}`);
// Initialize graph files
const currentGraphPath = path.join(layersRootDir(), 'current-graph.xml');
const baseGraphPath = path.join(layersRootDir(), 'base-graph.xml');
const initialGraphXml = `<?xml version="1.0" encoding="UTF-8"?>
<graph xmlns="urn:app:graph" version="1.0" directed="true">
<nodes>
</nodes>
<edges>
</edges>
</graph>`;
fs.writeFileSync(currentGraphPath, initialGraphXml);
fs.writeFileSync(baseGraphPath, initialGraphXml);
console.log(`Created graph files: ${currentGraphPath}, ${baseGraphPath}`);
return name;
}
// Test the functions
console.log('Current layers:', listLayers());
try {
const layerName = createLayer();
console.log('Created layer:', layerName);
console.log('Updated layers:', listLayers());
} catch (e) {
console.error('Error:', e.message);
}
// Check files
console.log('\nFiles in manta directory:');
try {
const files = fs.readdirSync('manta', { recursive: true });
console.log(files);
} catch (e) {
console.log('No manta directory or error:', e.message);
}