Merge branch 'class'
This commit is contained in:
commit
24287f7f94
248
index.js
248
index.js
@ -14,139 +14,141 @@ const gerberFiles = [
|
|||||||
'CAMOutputs/GerberFiles/profile.gbr',
|
'CAMOutputs/GerberFiles/profile.gbr',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
class ImageGenerator {
|
||||||
* Configures the folders used
|
constructor(folderConfig, imgConfig) {
|
||||||
* @param {Object} folderConfig Object with properties tmpDir and imgDir containing paths to temporary and image output folders
|
// this.folderConfig = folderConfig;
|
||||||
*/
|
this.tmpDir = folderConfig.tmpDir;
|
||||||
function config(folderConfig) {
|
this.imgDir = folderConfig.imgDir;
|
||||||
// Create tmpDir if it does not exist
|
this.imgConfig = imgConfig;
|
||||||
fs.ensureDirSync(folderConfig.tmpDir);
|
|
||||||
// Create imgDir if it does not exist
|
|
||||||
fs.ensureDirSync(folderConfig.imgDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Ensure that the folders exist
|
||||||
* Extracts the passed in zip file
|
try {
|
||||||
* @param {string} fileName Name of the file to be extracted
|
fs.ensureDirSync(this.tmpDir);
|
||||||
* @param {string} tmpDir Temporary directory to extract to
|
fs.ensureDirSync(this.imgDir);
|
||||||
* @returns {Promise} Promise object represents number of files extracted
|
} catch (error) {
|
||||||
*/
|
throw new Error(error);
|
||||||
function extractArchive(fileName, tmpDir) {
|
|
||||||
// Check archive exists
|
|
||||||
try {
|
|
||||||
if (!fs.existsSync(fileName)) {
|
|
||||||
throw Error('Archive does not exist.');
|
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(tmpDir)) {
|
|
||||||
throw Error('Temporary folder does not exist.');
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const zip = new AdmZip(fileName);
|
/**
|
||||||
zip.extractAllTo(path.join(tmpDir, 'archive'));
|
* Extracts the passed in zip file
|
||||||
}
|
* @param {string} fileName Name of the file to be extracted
|
||||||
|
* @param {string} tmpDir Temporary directory to extract to
|
||||||
/**
|
* @returns {Promise} Promise object represents number of files extracted
|
||||||
* Take in a directory of layer files and return an array of the layers files
|
*/
|
||||||
* @param {string} dir Directory containing layer files
|
static extractArchive(fileName, tmpDir) {
|
||||||
* @returns {Array} Array of paths to the layers files
|
// Check archive exists
|
||||||
*/
|
try {
|
||||||
function getLayers(dir) {
|
if (!fs.existsSync(fileName)) {
|
||||||
return new Promise((resolve, reject) => {
|
throw Error('Archive does not exist.');
|
||||||
// Make sure the directory exists
|
}
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(tmpDir)) {
|
||||||
return reject(new Error('Layers folder does not exist.'));
|
throw Error('Temporary folder does not exist.');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e);
|
||||||
}
|
}
|
||||||
// Check that the required layer files exist in source dir
|
|
||||||
let layersValid = true;
|
|
||||||
gerberFiles.forEach((layer) => {
|
|
||||||
if (!fs.existsSync(path.join(dir, layer))) layersValid = false;
|
|
||||||
});
|
|
||||||
if (!layersValid) return reject(new Error('Layer not found.'));
|
|
||||||
// Construct array of layers that match the supplied filenames array
|
|
||||||
const layers = gerberFiles.map((layerName) => ({
|
|
||||||
filename: layerName,
|
|
||||||
gerber: fs.createReadStream(path.join(dir, layerName)),
|
|
||||||
}));
|
|
||||||
return resolve(layers);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
const zip = new AdmZip(fileName);
|
||||||
* Clean up the archive folder in the specified directory
|
zip.extractAllTo(path.join(tmpDir, 'archive'));
|
||||||
* @param {string} dir Path to a directory to clean up
|
|
||||||
*/
|
|
||||||
function cleanupFiles(dir) {
|
|
||||||
try {
|
|
||||||
const folder = path.join(dir, 'archive');
|
|
||||||
fs.emptyDirSync(folder);
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
* @param {Object} config Object containing sharp settings for resizeWidth, compLevel and density
|
|
||||||
* @param {string} tmpDir Temporary directory to extract the archive to
|
|
||||||
* @param {string} outputDir Directory to save the image to
|
|
||||||
*/
|
|
||||||
function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
|
|
||||||
// Create output dir if it doesn't exist
|
|
||||||
try {
|
|
||||||
fs.ensureDirSync(outputDir, 0o644);
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check temp and output dirs exist
|
/**
|
||||||
try {
|
* Take in a directory of layer files and return an array of the layers files
|
||||||
if (!fs.existsSync(gerber)) {
|
* @param {string} dir Directory containing layer files
|
||||||
throw Error('Archive does not exist.');
|
* @returns {Array} Array of paths to the layers files
|
||||||
}
|
*/
|
||||||
if (!fs.existsSync(tmpDir)) {
|
static getLayers(dir) {
|
||||||
throw Error('Temporary folder does not exist.');
|
return new Promise((resolve, reject) => {
|
||||||
}
|
// Make sure the directory exists
|
||||||
if (!fs.existsSync(outputDir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
throw Error('Output folder does not exist.');
|
return reject(new Error('Layers folder does not exist.'));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
// Check that the required layer files exist in source dir
|
||||||
throw new Error(e);
|
let layersValid = true;
|
||||||
}
|
gerberFiles.forEach((layer) => {
|
||||||
|
if (!fs.existsSync(path.join(dir, layer))) layersValid = false;
|
||||||
// Set filenames
|
|
||||||
const imageName = path.basename(gerber, '.zip');
|
|
||||||
const destFile = `${path.join(outputDir, imageName)}.png`;
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
extractArchive(gerber, tmpDir);
|
|
||||||
getLayers(path.join(tmpDir, 'archive'))
|
|
||||||
.then(pcbStackup)
|
|
||||||
.then((stackup) => {
|
|
||||||
sharp(Buffer.from(stackup.top.svg), { density: imgConfig.density })
|
|
||||||
.resize({ width: imgConfig.resizeWidth })
|
|
||||||
.png({ compressionLevel: imgConfig.compLevel })
|
|
||||||
.toFile(destFile);
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
cleanupFiles(tmpDir);
|
|
||||||
resolve(destFile);
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
|
||||||
cleanupFiles(tmpDir);
|
|
||||||
reject(new Error(e));
|
|
||||||
});
|
});
|
||||||
});
|
if (!layersValid) return reject(new Error('Layer not found.'));
|
||||||
|
// Construct array of layers that match the supplied filenames array
|
||||||
|
const layers = gerberFiles.map((layerName) => ({
|
||||||
|
filename: layerName,
|
||||||
|
gerber: fs.createReadStream(path.join(dir, layerName)),
|
||||||
|
}));
|
||||||
|
return resolve(layers);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up the archive folder in the specified directory
|
||||||
|
* @param {string} dir Path to a directory to clean up
|
||||||
|
*/
|
||||||
|
static cleanupFiles(dir) {
|
||||||
|
try {
|
||||||
|
const folder = path.join(dir, 'archive');
|
||||||
|
fs.emptyDirSync(folder);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
||||||
|
fs.ensureDirSync(this.imgDir, 0o644);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check temp and output dirs exist
|
||||||
|
try {
|
||||||
|
if (!fs.existsSync(gerber)) {
|
||||||
|
throw Error('Archive does not exist.');
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(this.tmpDir)) {
|
||||||
|
throw Error('Temporary folder does not exist.');
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(this.imgDir)) {
|
||||||
|
throw Error('Output folder does not exist.');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set filenames
|
||||||
|
const imageName = path.basename(gerber, '.zip');
|
||||||
|
const destFile = `${path.join(this.imgDir, imageName)}.png`;
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
ImageGenerator.extractArchive(gerber, this.tmpDir);
|
||||||
|
ImageGenerator.getLayers(path.join(this.tmpDir, 'archive'))
|
||||||
|
.then(pcbStackup)
|
||||||
|
.then((stackup) => {
|
||||||
|
sharp(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.tmpDir);
|
||||||
|
resolve(destFile);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
ImageGenerator.cleanupFiles(this.tmpDir);
|
||||||
|
reject(new Error(e));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
cleanupFiles,
|
ImageGenerator,
|
||||||
getLayers,
|
|
||||||
extractArchive,
|
|
||||||
config,
|
|
||||||
gerberToImage,
|
|
||||||
};
|
};
|
||||||
|
@ -1,24 +1,45 @@
|
|||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const fileProc = require('../index.js');
|
const { ImageGenerator } = require('../index.js');
|
||||||
|
require('../index.js');
|
||||||
|
|
||||||
const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip');
|
const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip');
|
||||||
const testLayers = path.join(__dirname, 'layers');
|
const testLayers = path.join(__dirname, 'layers');
|
||||||
const emptyFolder = path.join(__dirname, 'layers', 'Empty');
|
const emptyFolder = path.join(__dirname, 'layers', 'Empty');
|
||||||
const tmpDir = path.join(__dirname, 'tmp');
|
// const tmpDir = path.join(__dirname, 'tmp');
|
||||||
const imgDir = path.join(__dirname, 'tmp');
|
// const imgDir = path.join(__dirname, 'tmp');
|
||||||
const nonWritableDir = fs.ensureDirSync(path.join(tmpDir, 'no-write'), 0o400);
|
const folderConfig = {
|
||||||
|
tmpDir: path.join(__dirname, 'tmp'),
|
||||||
|
imgDir: path.join(__dirname, 'tmp'),
|
||||||
|
};
|
||||||
|
const noTempConfig = {
|
||||||
|
tmpDir: emptyFolder,
|
||||||
|
imgDir: path.join(__dirname, 'tmp'),
|
||||||
|
};
|
||||||
|
const noImageConfig = {
|
||||||
|
tmpDir: path.join(__dirname, 'tmp'),
|
||||||
|
imgDir: emptyFolder,
|
||||||
|
};
|
||||||
|
const nonWritableDir = fs.ensureDirSync(path.join(folderConfig.tmpDir, 'no-write'), 0o400);
|
||||||
const imgConfig = {
|
const imgConfig = {
|
||||||
resizeWidth: 600,
|
resizeWidth: 600,
|
||||||
density: 1000,
|
density: 1000,
|
||||||
compLevel: 1,
|
compLevel: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fileProc = new ImageGenerator(folderConfig, imgConfig);
|
||||||
|
const fileProcNoTemp = new ImageGenerator(noTempConfig, imgConfig);
|
||||||
|
const fileProcNoImage = new ImageGenerator(noImageConfig, imgConfig);
|
||||||
|
|
||||||
|
/**************
|
||||||
|
* Tests
|
||||||
|
***************/
|
||||||
|
|
||||||
// getLayers
|
// getLayers
|
||||||
test('Promise of an array of layers from a given folder', () => {
|
test('Promise of an array of layers from a given folder', () => {
|
||||||
return fileProc.getLayers(testLayers).then((data) => {
|
expect.assertions(1);
|
||||||
|
return ImageGenerator.getLayers(testLayers).then((data) => {
|
||||||
expect(data).toEqual(
|
expect(data).toEqual(
|
||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@ -31,59 +52,64 @@ test('Promise of an array of layers from a given folder', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Non-existent folder should reject promise with error', () => {
|
test('Non-existent folder should reject promise with error', () => {
|
||||||
return expect(fileProc.getLayers('./invalid_folder')).rejects.toThrow(
|
expect.assertions(1);
|
||||||
'Layers folder does not exist.'
|
return expect(ImageGenerator.getLayers('./invalid_folder')).rejects.toThrow(
|
||||||
|
new Error('Layers folder does not exist.')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Folder with incorrect number of layers should reject promise with error', () => {
|
test('Folder with incorrect number of layers should reject promise with error', () => {
|
||||||
return expect(fileProc.getLayers(emptyFolder)).rejects.toThrow(
|
expect.assertions(1);
|
||||||
'Layer not found.'
|
return expect(ImageGenerator.getLayers(emptyFolder)).rejects.toThrow(
|
||||||
|
new Error('Layer not found.')
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// extractArchive
|
// extractArchive
|
||||||
test('Non-existent archive should throw an error', () => {
|
test('Non-existent archive should throw an error', () => {
|
||||||
expect(() => fileProc.extractArchive('invalid.zip', tmpDir).toThrow(Error));
|
expect(() =>
|
||||||
|
ImageGenerator.extractArchive('invalid.zip', folderConfig.tmpDir).toThrow(Error)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Temp dir not existing should throw an error', () => {
|
test('Temp dir not existing should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
fileProc.extractArchive(testGerber, './invalid_dir').toThrow(Error)
|
ImageGenerator.extractArchive(testGerber, './invalid_dir').toThrow(Error)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Should extract archive and resolve with the number of files extracted', () => {
|
test('Should extract archive and resolve with the number of files extracted', () => {
|
||||||
expect(() => fileProc.extractArchive(testGerber, tmpDir).toBe(12));
|
expect(() => ImageGenerator.extractArchive(testGerber, folderConfig.tmpDir).toBe(12));
|
||||||
});
|
});
|
||||||
|
|
||||||
// gerberToImage
|
// gerberToImage
|
||||||
test('Temp dir not existing should throw an error', () => {
|
test('Temp dir not existing should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
fileProc
|
fileProcNoTemp
|
||||||
.gerberToImage(testGerber, imgConfig, './invalid_dir', imgDir)
|
.gerberToImage(testGerber)
|
||||||
.toThrow('Temporary folder does not exist.')
|
.toThrow(new Error('Temporary folder does not exist.'))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Output dir not existing should throw an error', () => {
|
test('Output dir not existing should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
fileProc
|
fileProcNoImage
|
||||||
.gerberToImage(testGerber, imgConfig, tmpDir, './invalid_dir')
|
.gerberToImage(testGerber)
|
||||||
.toThrow('Output folder does not exist.')
|
.toThrow(new Error('Output folder does not exist.'))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Invalid archive file should throw an error', () => {
|
test('Invalid archive file should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
fileProc
|
fileProc
|
||||||
.gerberToImage('invalid.zip', imgConfig, tmpDir, imgDir)
|
.gerberToImage('invalid.zip')
|
||||||
.toThrow('Archive does not exist.')
|
.toThrow(new Error('Archive does not exist.'))
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Gerber archive should resolve promise and return a filename of an image', () => {
|
test('Gerber archive should resolve promise and return a filename of an image', () => {
|
||||||
|
expect.assertions(1);
|
||||||
return expect(
|
return expect(
|
||||||
fileProc.gerberToImage(testGerber, imgConfig, tmpDir, imgDir)
|
fileProc.gerberToImage(testGerber)
|
||||||
).resolves.toEqual(expect.stringContaining('Arduino-Pro-Mini.png'));
|
).resolves.toEqual(expect.stringContaining('Arduino-Pro-Mini.png'));
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user