-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanupComponents.js
More file actions
149 lines (130 loc) · 5.16 KB
/
cleanupComponents.js
File metadata and controls
149 lines (130 loc) · 5.16 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require('fs');
const path = require('path');
const serializeLisp = require('./serializeLisp');
const deserializeLisp = require('./deserializeLisp');
// Spinner frames for loading animation
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
/**
* Creates a loading spinner with message
* @param {string} message - The message to display
* @returns {Object} - Object containing interval and update functions
*/
function createSpinner(message) {
let frameIndex = 0;
process.stdout.write(`\r${spinnerFrames[0]} ${message}`);
const interval = setInterval(() => {
frameIndex = (frameIndex + 1) % spinnerFrames.length;
process.stdout.write(`\r${spinnerFrames[frameIndex]} ${message}`);
}, 80);
return {
interval,
succeed: (component) => {
clearInterval(interval);
process.stdout.write(`\r✅ Removed component: ${component}\n`);
},
fail: (component, error) => {
clearInterval(interval);
process.stdout.write(`\r❌ Failed to remove component ${component} - ${error}\n`);
}
};
}
/**
* Cleans up KiCad components that are no longer needed
* @param {string} projectName - The project name used for symbol libs etc
* @param {string} projectDir - The project directory path relative to baseDir (can be '.')
* @param {string} baseDir - The base directory path
*/
async function cleanupComponents(projectName, projectDir, baseDir) {
const projectPath = path.join(baseDir, projectDir);
const componentsJsonPath = path.join(projectPath, 'components.json');
const componentsTxtPath = path.join(projectPath, 'components.txt');
const symLibPath = path.join(projectPath, `${projectName}.kicad_sym`);
// Check if required files exist
if (!fs.existsSync(componentsJsonPath)) {
// console.error(`components.json not found in ${projectDir}`);
return;
}
if (!fs.existsSync(componentsTxtPath)) {
console.error(`components.txt not found in ${projectDir}`);
return;
}
if (!fs.existsSync(symLibPath)) {
console.error(`Symbol library ${projectName}.kicad_sym not found in ${projectDir}`);
return;
}
// Read and parse the files
let componentsJson;
try {
const rawJson = fs.readFileSync(componentsJsonPath, 'utf8');
if (!rawJson.trim()) {
return;
}
componentsJson = JSON.parse(rawJson);
} catch (error) {
console.error('Error parsing components.json:', error);
return;
}
if (!componentsJson || Object.keys(componentsJson).length === 0 ||
!componentsJson.components || !Array.isArray(componentsJson.components)) {
componentsJson = { components: [] };
}
const activeComponents = new Set(
fs.readFileSync(componentsTxtPath, 'utf8')
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'))
);
// Find components to remove
const componentsToRemove = componentsJson.components.filter(
component => !activeComponents.has(component.lcscId)
);
if (componentsToRemove.length === 0) {
return;
}
// Read and parse the symbol library
const symLibContent = fs.readFileSync(symLibPath, 'utf8');
let symLib = deserializeLisp(symLibContent);
for (const component of componentsToRemove) {
const spinner = createSpinner(`Removing component: ${component.lcscId}...`);
try {
// Remove component files
for (const filePath of component.files || []) {
const fullPath = path.join(projectPath, filePath);
if (fs.existsSync(fullPath)) {
fs.unlinkSync(fullPath);
}
}
// Remove symbols from the library
symLib = {
...symLib,
items: symLib.items.filter(item => {
if (item.type !== 'list' || item.items.length < 2 ||
item.items[0].type !== 'atom' || item.items[0].value !== 'symbol') {
return true;
}
const symbolName = item.items[1].value.replace(/^"|"$/g, '');
return !(component.symbols || []).includes(symbolName);
})
};
spinner.succeed(component.lcscId);
} catch (error) {
spinner.fail(component.lcscId, error.message);
}
}
// Ensure the last item in the list has correct whitespace
if (symLib.items.length > 0) {
const lastItem = symLib.items[symLib.items.length - 1];
if (lastItem.afterWs && lastItem.afterWs.includes('\n')) {
lastItem.afterWs = '\n';
}
}
// Write back the updated files
fs.writeFileSync(symLibPath, serializeLisp(symLib));
componentsJson.components = componentsJson.components.filter(
component => !componentsToRemove.some(
removeComponent => removeComponent.lcscId === component.lcscId
)
);
fs.writeFileSync(componentsJsonPath, JSON.stringify(componentsJson, null, 2));
}
module.exports = cleanupComponents;