-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbolLibUtils.js
More file actions
43 lines (39 loc) · 1.67 KB
/
symbolLibUtils.js
File metadata and controls
43 lines (39 loc) · 1.67 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
const fs = require('fs');
const { KicadVersion } = require('./kicad/parametersKicadSymbol');
function idAlreadyInSymbolLib(libPath, componentName, kicadVersion) {
if (!fs.existsSync(libPath)) {
return false;
}
const content = fs.readFileSync(libPath, 'utf8');
const pattern = kicadVersion === KicadVersion.V5 ? `DEF ${componentName}` : `symbol "${componentName}"`;
return content.includes(pattern);
}
function updateComponentInSymbolLibFile(libPath, componentName, componentContent, kicadVersion) {
if (!fs.existsSync(libPath)) {
throw new Error(`Symbol library file ${libPath} not found`);
}
const content = fs.readFileSync(libPath, 'utf8');
const pattern = kicadVersion === KicadVersion.V5 ?
new RegExp(`\\n?# ${componentName}[\\s\\S]*?ENDDEF[\\s]*`, 'm') :
new RegExp(`\\n?#[\\s\\S]*?symbol "${componentName}"[\\s\\S]*?\\)\\n`, 'm');
const updatedContent = content.replace(pattern, '\n' + componentContent);
fs.writeFileSync(libPath, updatedContent);
}
function addComponentInSymbolLibFile(libPath, componentContent, kicadVersion) {
if (!fs.existsSync(libPath)) {
const defaultContent = kicadVersion === KicadVersion.V5 ?
'EESchema-LIBRARY Version 2.4\n#encoding utf-8\n' :
'(kicad_symbol_lib (version 20211014) (generator kicad_symbol_editor)\n)\n';
fs.writeFileSync(libPath, defaultContent);
}
const content = fs.readFileSync(libPath, 'utf8');
const newContent = kicadVersion === KicadVersion.V5 ?
content + componentContent :
content.replace(/\)\s*$/, componentContent + ')');
fs.writeFileSync(libPath, newContent);
}
module.exports = {
idAlreadyInSymbolLib,
updateComponentInSymbolLibFile,
addComponentInSymbolLibFile
};