@typecad/kicad-symbols
Version:
Intelligent fuzzy search for KiCad symbols with CLI interface
93 lines • 3.28 kB
JavaScript
import fs from 'node:fs';
import path from 'node:path';
import { platform } from 'node:os';
import { lookpathSync } from 'find-bin';
export let kicad_cli_path;
export let kicad_symbols_path;
export class KiCAD {
#possible_paths = [
'C:/Program Files/KiCad/9.0/',
'C:/Program Files/KiCad/8.0/',
'C:/Program Files/KiCad/7.0/', // windows
'/usr/share/kicad/', // linux
'/Applications/KiCad/KiCad.app/Contents/SharedSupport/', // mac
'/Applications/KiCad/KiCad.app/Contents/MacOS' // mac fallback
];
#symbols_subpaths = [
'share/kicad/symbols', // Standard KiCad structure
'symbols', // Direct symbols folder
'share/symbols' // Alternative structure
];
constructor() {
// push typecad.json#kicad_path into the list to check
let kicad_installation_path;
for (const installPath of this.#possible_paths) {
if (fs.existsSync(installPath)) {
kicad_installation_path = installPath;
break;
}
}
// Find the symbols directory
if (kicad_installation_path) {
kicad_symbols_path = this.findSymbolsDirectory(kicad_installation_path);
}
// if it found a path, figure out where 'kicad-cli' is
if (platform() == 'win32') {
if (kicad_installation_path) {
kicad_cli_path = path.join(kicad_installation_path, 'bin', 'kicad-cli.exe');
}
else {
// Fallback to PATH search if installation directory wasn't found
kicad_cli_path = lookpathSync('kicad-cli');
}
}
else if (platform() == 'linux' || platform() == 'darwin') {
kicad_cli_path = lookpathSync('kicad-cli');
}
}
/**
* Find the symbols directory within a KiCad installation
* @param installationPath - The base KiCad installation path
* @returns The path to the symbols directory, or undefined if not found
*/
findSymbolsDirectory(installationPath) {
for (const subpath of this.#symbols_subpaths) {
const symbolsPath = path.join(installationPath, subpath);
if (fs.existsSync(symbolsPath) && fs.statSync(symbolsPath).isDirectory()) {
return symbolsPath;
}
}
return undefined;
}
/**
* Get the path to the KiCad symbols directory
* @returns The symbols directory path, or undefined if not found
*/
getSymbolsPath() {
return kicad_symbols_path;
}
/**
* Get the path to the kicad-cli executable
* @returns The kicad-cli path, or undefined if not found
*/
getCliPath() {
return kicad_cli_path;
}
/**
* Check if KiCad symbols are available locally
* @returns True if symbols directory exists and contains .kicad_sym files
*/
async hasLocalSymbols() {
if (!kicad_symbols_path) {
return false;
}
try {
const files = await fs.promises.readdir(kicad_symbols_path);
return files.some(file => file.endsWith('.kicad_sym'));
}
catch (error) {
return false;
}
}
}
//# sourceMappingURL=kicad.js.map