Export class instead of several functions
This commit is contained in:
parent
6cbbef212d
commit
444e8a6b2a
66
index.js
66
index.js
@ -14,15 +14,20 @@ const gerberFiles = [
|
||||
'CAMOutputs/GerberFiles/profile.gbr',
|
||||
];
|
||||
|
||||
/**
|
||||
* Configures the folders used
|
||||
* @param {Object} folderConfig Object with properties tmpDir and imgDir containing paths to temporary and image output folders
|
||||
*/
|
||||
function config(folderConfig) {
|
||||
// Create tmpDir if it does not exist
|
||||
fs.ensureDirSync(folderConfig.tmpDir);
|
||||
// Create imgDir if it does not exist
|
||||
fs.ensureDirSync(folderConfig.imgDir);
|
||||
class ImageGenerator {
|
||||
constructor(folderConfig, imgConfig) {
|
||||
// this.folderConfig = folderConfig;
|
||||
this.tmpDir = folderConfig.tmpDir;
|
||||
this.imgDir = folderConfig.imgDir;
|
||||
this.imgConfig = imgConfig;
|
||||
|
||||
// Ensure that the folders exist
|
||||
try {
|
||||
fs.ensureDirSync(this.tmpDir);
|
||||
fs.ensureDirSync(this.imgDir);
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,7 +36,7 @@ function config(folderConfig) {
|
||||
* @param {string} tmpDir Temporary directory to extract to
|
||||
* @returns {Promise} Promise object represents number of files extracted
|
||||
*/
|
||||
function extractArchive(fileName, tmpDir) {
|
||||
static extractArchive(fileName, tmpDir) {
|
||||
// Check archive exists
|
||||
try {
|
||||
if (!fs.existsSync(fileName)) {
|
||||
@ -53,7 +58,7 @@ function extractArchive(fileName, tmpDir) {
|
||||
* @param {string} dir Directory containing layer files
|
||||
* @returns {Array} Array of paths to the layers files
|
||||
*/
|
||||
function getLayers(dir) {
|
||||
static getLayers(dir) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Make sure the directory exists
|
||||
if (!fs.existsSync(dir)) {
|
||||
@ -78,7 +83,7 @@ function getLayers(dir) {
|
||||
* Clean up the archive folder in the specified directory
|
||||
* @param {string} dir Path to a directory to clean up
|
||||
*/
|
||||
function cleanupFiles(dir) {
|
||||
static cleanupFiles(dir) {
|
||||
try {
|
||||
const folder = path.join(dir, 'archive');
|
||||
fs.emptyDirSync(folder);
|
||||
@ -91,14 +96,12 @@ function cleanupFiles(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
|
||||
* @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
|
||||
* @returns {Promise.<string>} Promise to return path to image
|
||||
*/
|
||||
function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
|
||||
gerberToImage(gerber) {
|
||||
// Create output dir if it doesn't exist
|
||||
try {
|
||||
fs.ensureDirSync(outputDir, 0o644);
|
||||
fs.ensureDirSync(this.imgDir, 0o644);
|
||||
} catch (e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
@ -108,10 +111,10 @@ function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
|
||||
if (!fs.existsSync(gerber)) {
|
||||
throw Error('Archive does not exist.');
|
||||
}
|
||||
if (!fs.existsSync(tmpDir)) {
|
||||
if (!fs.existsSync(this.tmpDir)) {
|
||||
throw Error('Temporary folder does not exist.');
|
||||
}
|
||||
if (!fs.existsSync(outputDir)) {
|
||||
if (!fs.existsSync(this.imgDir)) {
|
||||
throw Error('Output folder does not exist.');
|
||||
}
|
||||
} catch (e) {
|
||||
@ -120,33 +123,32 @@ function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
|
||||
|
||||
// Set filenames
|
||||
const imageName = path.basename(gerber, '.zip');
|
||||
const destFile = `${path.join(outputDir, imageName)}.png`;
|
||||
const destFile = `${path.join(this.imgDir, imageName)}.png`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
extractArchive(gerber, tmpDir);
|
||||
getLayers(path.join(tmpDir, 'archive'))
|
||||
ImageGenerator.extractArchive(gerber, this.tmpDir);
|
||||
ImageGenerator.getLayers(path.join(this.tmpDir, 'archive'))
|
||||
.then(pcbStackup)
|
||||
.then((stackup) => {
|
||||
sharp(Buffer.from(stackup.top.svg), { density: imgConfig.density })
|
||||
.resize({ width: imgConfig.resizeWidth })
|
||||
.png({ compressionLevel: imgConfig.compLevel })
|
||||
sharp(Buffer.from(stackup.top.svg), {
|
||||
density: this.imgConfig.density,
|
||||
})
|
||||
.resize({ width: this.imgConfig.resizeWidth })
|
||||
.png({ compressionLevel: this.imgConfig.compLevel })
|
||||
.toFile(destFile);
|
||||
})
|
||||
.then(() => {
|
||||
cleanupFiles(tmpDir);
|
||||
ImageGenerator.cleanupFiles(this.tmpDir);
|
||||
resolve(destFile);
|
||||
})
|
||||
.catch((e) => {
|
||||
cleanupFiles(tmpDir);
|
||||
ImageGenerator.cleanupFiles(this.tmpDir);
|
||||
reject(new Error(e));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
cleanupFiles,
|
||||
getLayers,
|
||||
extractArchive,
|
||||
config,
|
||||
gerberToImage,
|
||||
ImageGenerator,
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user