-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateSymLibTable.js
More file actions
106 lines (99 loc) · 3.3 KB
/
updateSymLibTable.js
File metadata and controls
106 lines (99 loc) · 3.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
const fs = require('fs');
const path = require('path');
const deserializeLisp = require('./deserializeLisp');
const serializeLisp = require('./serializeLisp');
function updateSymLibTable(projectPath, projectName) {
const symLibTablePath = path.join(projectPath, 'sym-lib-table');
let parsed;
// Create the project library entry with proper formatting
const projectLibEntry = {
type: 'list',
beforeWs: ' ',
afterWs: '\n',
items: [
{ type: 'atom', value: 'lib', beforeWs: '', afterWs: ' ' },
{ type: 'list', beforeWs: '', afterWs: '',
items: [
{ type: 'atom', value: 'name', beforeWs: '', afterWs: ' ' },
{ type: 'atom', value: `"${projectName}"`, beforeWs: '', afterWs: '' }
]
},
{ type: 'list', beforeWs: '', afterWs: '',
items: [
{ type: 'atom', value: 'type', beforeWs: '', afterWs: ' ' },
{ type: 'atom', value: '"KiCad"', beforeWs: '', afterWs: '' }
]
},
{ type: 'list', beforeWs: '', afterWs: '',
items: [
{ type: 'atom', value: 'uri', beforeWs: '', afterWs: ' ' },
{ type: 'atom', value: `"\${KIPRJMOD}/${projectName}.kicad_sym"`, beforeWs: '', afterWs: '' }
]
},
{ type: 'list', beforeWs: '', afterWs: '',
items: [
{ type: 'atom', value: 'options', beforeWs: '', afterWs: ' ' },
{ type: 'atom', value: '""', beforeWs: '', afterWs: '' }
]
},
{ type: 'list', beforeWs: '', afterWs: '',
items: [
{ type: 'atom', value: 'descr', beforeWs: '', afterWs: ' ' },
{ type: 'atom', value: '""', beforeWs: '', afterWs: '' }
]
}
]
};
// If file exists, read and parse it
if (fs.existsSync(symLibTablePath)) {
const content = fs.readFileSync(symLibTablePath, 'utf8');
parsed = deserializeLisp(content);
} else {
// Create new sym-lib-table structure with exact formatting
parsed = {
type: 'list',
beforeWs: '',
afterWs: '\n',
items: [
{ type: 'atom', value: 'sym_lib_table', beforeWs: '', afterWs: '\n ' },
{ type: 'list', beforeWs: '', afterWs: '\n',
items: [
{ type: 'atom', value: 'version', beforeWs: '', afterWs: ' ' },
{ type: 'atom', value: '7', beforeWs: '', afterWs: '' }
]
}
]
};
}
// Look for existing lib entry with same name
let found = false;
if (parsed.items.length > 0) {
for (let i = 0; i < parsed.items.length; i++) {
const item = parsed.items[i];
if (item.type === 'list' && item.items[0].value === 'lib') {
// Check the name field
const nameField = item.items.find(subItem =>
subItem.type === 'list' &&
subItem.items[0].value === 'name'
);
if (nameField && nameField.items[1].value === `"${projectName}"`) {
found = true;
break;
}
}
}
}
// Add project library if not found
if (!found) {
parsed.items.push(projectLibEntry);
}
// Write back to file
try {
const updatedContent = serializeLisp(parsed);
fs.writeFileSync(symLibTablePath, updatedContent);
// console.log(`Updated ${symLibTablePath}`);
} catch (err) {
console.error(`Error writing sym-lib-table: ${err}`);
}
}
module.exports = updateSymLibTable;