Change dir structure

This commit is contained in:
Nick Playfair 2025-06-15 21:37:08 +01:00
parent 1be82e0451
commit d3546313a3
5 changed files with 222 additions and 12 deletions

10
.gitignore vendored
View File

@ -75,22 +75,12 @@ typings/
# parcel-bundler cache (https://parceljs.org/) # parcel-bundler cache (https://parceljs.org/)
.cache .cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files # Gatsby files
.cache/ .cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js # Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support # https://nextjs.org/blog/next-9-1#public-directory-support
# public # public
# vuepress build output
.vuepress/dist
# Serverless directories # Serverless directories
.serverless/ .serverless/

219
dist/index.js vendored Normal file
View File

@ -0,0 +1,219 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
//Modules
// const AdmZip = require('adm-zip');
const adm_zip_1 = __importDefault(require("adm-zip"));
const fs_extra_1 = require("fs-extra");
const path_1 = __importDefault(require("path"));
const pcb_stackup_1 = __importDefault(require("pcb-stackup"));
const sharp_1 = __importDefault(require("sharp"));
const node_stream_1 = require("node:stream");
const node_fs_1 = require("node:fs");
//Class definition
class ImageGenerator {
constructor(folderConfig, imgConfig, layerNames) {
this.folderConfig = folderConfig;
this.imgConfig = imgConfig;
this.layerNames = layerNames;
//Ensure folders exist
if (!(0, node_fs_1.existsSync)(folderConfig.tmpDir))
throw new Error('Temp dir does not exist');
if (!(0, node_fs_1.existsSync)(folderConfig.imgDir))
throw new Error('Image dir does not exist');
//Check folder permissions
(0, node_fs_1.accessSync)(folderConfig.tmpDir, node_fs_1.constants.R_OK | node_fs_1.constants.W_OK);
(0, node_fs_1.accessSync)(folderConfig.imgDir, node_fs_1.constants.R_OK | node_fs_1.constants.W_OK);
}
/**
* Extracts the passed in zip file
*/
extractArchive(fileName, tmpDir) {
// Check archive exists
if (!(0, node_fs_1.existsSync)(fileName)) {
throw Error('Archive does not exist.');
}
//Check temp folder exists
if (!(0, node_fs_1.existsSync)(tmpDir)) {
throw Error('Temporary folder does not exist.');
}
const zip = new adm_zip_1.default(fileName);
zip.extractAllTo(path_1.default.join(tmpDir, 'archive'));
return zip.getEntries().length;
}
/**
* Temporary test method zip file
*/
testArchive(fileName, tmpDir) {
// Check archive exists
try {
if (!(0, node_fs_1.existsSync)(fileName)) {
throw Error('Archive does not exist.');
}
if (!(0, node_fs_1.existsSync)(tmpDir)) {
throw Error('Temporary folder does not exist.');
}
}
catch (e) {
console.error(e);
}
const zip = new adm_zip_1.default(fileName);
return zip.getEntries().length;
}
//Layer promise
getLayers(dir, layerNames) {
//Check correct number of layers and folder exists
layerNames.forEach((layerName) => {
if (!(0, node_fs_1.existsSync)(path_1.default.join(dir, layerName))) {
throw `Missing layer: ${layerName}`;
}
});
if (!(0, node_fs_1.existsSync)(dir)) {
throw new Error('Folder not there');
}
//Return layer promise
const layersPromise = new Promise(function (resolve, reject) {
const layers = layerNames.map((layerName) => ({
filename: layerName,
gerber: (0, node_fs_1.createReadStream)(path_1.default.join(dir, layerName)),
}));
if (layers.length === layerNames.length) {
resolve(layers);
}
else {
reject('Invalid layer count');
}
});
return layersPromise;
}
//Clean up the archive folder in the specified directory
static cleanupFiles(dir) {
try {
const folder = path_1.default.join(dir, 'archive');
(0, fs_extra_1.emptyDirSync)(folder);
}
catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}
}
// * Take an archive containing gerber files, config object, temporary dir
// * and output dir and create a PNG image from the gerber in the output dir
// * @param {string} gerber Path to an archive file containing gerber
// * @returns {Promise.<string>} Promise to return path to image
gerberToImage(gerber) {
// Create output dir if it doesn't exist
try {
(0, fs_extra_1.ensureDirSync)(this.folderConfig.imgDir);
}
catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}
// Check temp and output dirs exist
try {
if (!(0, node_fs_1.existsSync)(gerber)) {
throw Error('Archive does not exist.');
}
if (!(0, node_fs_1.existsSync)(this.folderConfig.tmpDir)) {
throw Error('Temporary folder does not exist.');
}
if (!(0, node_fs_1.existsSync)(this.folderConfig.imgDir)) {
throw Error('Output folder does not exist.');
}
}
catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}
// Set filenames
//Use the filename of the gerber zip to determine the output png filename
const imageName = path_1.default.basename(gerber, '.zip');
const destFile = `${path_1.default.join(this.folderConfig.imgDir, imageName)}.png`;
return new Promise((resolve, reject) => {
if (!this.layerNames) {
throw new Error('You must supply an array of layer names.');
}
this.extractArchive(gerber, this.folderConfig.tmpDir);
this.getLayers(path_1.default.join(this.folderConfig.tmpDir, 'archive'), this.layerNames)
.then(pcb_stackup_1.default)
.then((stackup) => {
(0, sharp_1.default)(Buffer.from(stackup.top.svg), {
density: this.imgConfig.density,
})
.resize({ width: this.imgConfig.resizeWidth })
.png({ compressionLevel: this.imgConfig.compLevel })
.toFile(destFile);
})
.then(() => {
ImageGenerator.cleanupFiles(this.folderConfig.tmpDir);
resolve(destFile);
})
.catch((e) => {
ImageGenerator.cleanupFiles(this.folderConfig.tmpDir);
reject(new Error(e));
});
});
}
/**
* Take an archive containing gerber files and return a stream containing
* a PNG image from the gerber
* @param {string} gerber Path to an archive file containing gerber
* @returns {Promise.<stream.Readable>} Promise that resolves to a PNG stream
*/
gerberToStream(gerber) {
// Check temp and output dirs exist
try {
if (!(0, node_fs_1.existsSync)(gerber)) {
throw Error('Archive does not exist.');
}
if (!(0, node_fs_1.existsSync)(this.folderConfig.tmpDir)) {
throw Error('Temporary folder does not exist.');
}
if (!(0, node_fs_1.existsSync)(this.folderConfig.imgDir)) {
throw Error('Output folder does not exist.');
}
}
catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}
return new Promise((resolve, reject) => {
this.extractArchive(gerber, this.folderConfig.tmpDir);
if (!this.layerNames)
throw new Error('No layers provided');
this.getLayers(path_1.default.join(this.folderConfig.tmpDir, 'archive'), this.layerNames)
.then(pcb_stackup_1.default)
.then((stackup) => {
(0, sharp_1.default)(Buffer.from(stackup.top.svg), {
density: this.imgConfig.density,
})
.resize({ width: this.imgConfig.resizeWidth })
.png({ compressionLevel: this.imgConfig.compLevel })
.toBuffer()
.then((buffer) => {
ImageGenerator.cleanupFiles(this.folderConfig.tmpDir);
const stream = new node_stream_1.Readable();
stream.push(buffer);
stream.push(null);
resolve(stream);
});
})
.catch((e) => {
ImageGenerator.cleanupFiles(this.folderConfig.tmpDir);
reject(new Error(e));
});
});
}
}
module.exports = {
ImageGenerator,
};

View File

@ -2,7 +2,8 @@
"name": "@nplayfair/npe_gerber", "name": "@nplayfair/npe_gerber",
"version": "0.3.0", "version": "0.3.0",
"description": "Create a PCB image from gerber files", "description": "Create a PCB image from gerber files",
"main": "index.js", "main": "dist/index.js",
"types": "types/npe_gerber.d.ts",
"scripts": { "scripts": {
"test": "jest", "test": "jest",
"lint": "eslint ." "lint": "eslint ."

View File

@ -110,6 +110,6 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
}, },
"include": ["src"], "include": ["src", "types/npe_gerber.d.ts"],
"exclude": ["node_modules", "dist/**/*", "test/**/*"] "exclude": ["node_modules", "dist/**/*", "test/**/*"]
} }