Reformat code

This commit is contained in:
Nick Playfair 2021-01-31 20:37:00 +00:00
parent 8f5408f23a
commit 6a874505a1

View File

@ -1,7 +1,7 @@
const StreamZip = require('node-stream-zip'); const StreamZip = require('node-stream-zip');
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const pcbStackup = require('pcb-stackup'); const pcbStackup = require('pcb-stackup');
const sharp = require('sharp'); const sharp = require('sharp');
// Filenames we need to extract from the archive // Filenames we need to extract from the archive
@ -11,8 +11,8 @@ const gerberFiles = [
'CAMOutputs/GerberFiles/silkscreen_top.gbr', 'CAMOutputs/GerberFiles/silkscreen_top.gbr',
'CAMOutputs/GerberFiles/soldermask_top.gbr', 'CAMOutputs/GerberFiles/soldermask_top.gbr',
'CAMOutputs/GerberFiles/solderpaste_top.gbr', 'CAMOutputs/GerberFiles/solderpaste_top.gbr',
'CAMOutputs/GerberFiles/profile.gbr' 'CAMOutputs/GerberFiles/profile.gbr',
] ];
/** /**
* Configures the folders used * Configures the folders used
@ -45,28 +45,28 @@ function handleError(e) {
*/ */
function extractArchive(fileName, tmpDir) { function extractArchive(fileName, tmpDir) {
// Configure archive to use // Configure archive to use
const archive = new StreamZip({ const archive = new StreamZip({
file: fileName, file: fileName,
storeEntries: true storeEntries: true,
}); });
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Try to extract // Try to extract
archive.on('ready', () => { archive.on('ready', () => {
let extDir = path.join(tmpDir, 'archive'); const extDir = path.join(tmpDir, 'archive');
fs.mkdirSync(extDir, { recursive: true }); fs.mkdirSync(extDir, { recursive: true });
archive.extract(null, extDir, (err, count) => { archive.extract(null, extDir, (err, count) => {
if(!err) { if (!err) {
archive.close(); archive.close();
resolve(count); resolve(count);
} else { } else {
const errMsg = 'Error extracting archive'; const errMsg = 'Error extracting archive';
console.err(errMsg); console.err(errMsg);
archive.close(); archive.close();
reject(errMsg) reject(errMsg);
} }
}) });
}) });
}) });
} }
/** /**
@ -79,13 +79,13 @@ function getLayers(fileName, tmpDir) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const extractDir = path.join(tmpDir, 'archive'); const extractDir = path.join(tmpDir, 'archive');
extractArchive(fileName, tmpDir) extractArchive(fileName, tmpDir)
.then(numfiles => { .then((numfiles) => {
console.log(`${numfiles} files extracted successfully`); console.log(`${numfiles} files extracted successfully`);
const layers = gerberFiles.map(fileName => ({ const layers = gerberFiles.map((layerName) => ({
filename: fileName, filename: layerName,
gerber: fs.createReadStream(path.join(extractDir, fileName)) gerber: fs.createReadStream(path.join(extractDir, layerName)),
})); }));
if(numfiles > 0) { if (numfiles > 0) {
// Some files were extracted // Some files were extracted
resolve(layers); resolve(layers);
} else { } else {
@ -93,10 +93,10 @@ function getLayers(fileName, tmpDir) {
reject(errMsg); reject(errMsg);
} }
}) })
.catch(e => { .catch((e) => {
console.log(e); console.log(e);
}) });
}) });
} }
/** /**
@ -105,7 +105,7 @@ function getLayers(fileName, tmpDir) {
*/ */
function cleanupFiles(dir) { function cleanupFiles(dir) {
try { try {
let folder = path.join(dir, 'archive'); const folder = path.join(dir, 'archive');
fs.emptyDirSync(folder); fs.emptyDirSync(folder);
console.log('Temp files removed.'); console.log('Temp files removed.');
} catch (err) { } catch (err) {
@ -114,49 +114,47 @@ function cleanupFiles(dir) {
} }
/** /**
* Take an archive containing gerber files, config object, temporary dir * 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 * 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 * @param {string} gerber Path to an archive file containing gerber
* @param {Object} config Object containing sharp settings for resizeWidth, compLevel and density * @param {Object} config Object containing sharp settings for resizeWidth, compLevel and density
* @param {string} tmpDir Temporary directory to extract the archive to * @param {string} tmpDir Temporary directory to extract the archive to
* @param {string} outputDir Directory to save the image to * @param {string} outputDir Directory to save the image to
*/ */
function gerberToImage(gerber, config, tmpDir, outputDir) { function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
// Set filenames // Set filenames
const imageName = path.basename(gerber, '.zip'); const imageName = path.basename(gerber, '.zip');
const destFile = path.join(outputDir, imageName) + '.png'; const destFile = `${path.join(outputDir, imageName)}.png`;
// Make sure output dir exists // Make sure output dir exists
try { try {
fs.ensureDirSync(outputDir); fs.ensureDirSync(outputDir);
} } catch (e) {
catch (e) { console.error(e);
console.error(e)
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
getLayers(gerber, tmpDir) getLayers(gerber, tmpDir)
.then(pcbStackup) .then(pcbStackup)
.then(stackup => { .then((stackup) => {
sharp(Buffer.from(stackup.top.svg), { density: config.density }) sharp(Buffer.from(stackup.top.svg), { density: imgConfig.density })
.resize({ width: config.resizeWidth }) .resize({ width: imgConfig.resizeWidth })
.png({ .png({ compressionLevel: imgConfig.compLevel })
compressionLevel: config.compLevel }) .toFile(destFile);
.toFile(destFile)
}) })
.then(() => { .then(() => {
cleanupFiles(tmpDir); cleanupFiles(tmpDir);
resolve(destFile); resolve(destFile);
}) })
.catch((e) => { .catch((e) => {
cleanupFiles(tmpDir) cleanupFiles(tmpDir);
handleError(e); handleError(e);
reject(e); reject(e);
}) });
}); });
} }
module.exports = { module.exports = {
config, config,
gerberToImage gerberToImage,
} };