Use new getLayers function and adm-zip
New extractArchive function uses adm-zip instead of node-stream-zip, fixes bug where it wouldn’t wait for all files to be extracted before creating layers. Replace old getLayers function with the new one that takes in a folder of layers that have already been extracted from a zip archive
This commit is contained in:
parent
dae607c891
commit
c0b71f4ee9
83
index.js
83
index.js
@ -1,4 +1,4 @@
|
||||
const StreamZip = require('node-stream-zip');
|
||||
const AdmZip = require('adm-zip');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const pcbStackup = require('pcb-stackup');
|
||||
@ -44,73 +44,28 @@ function handleError(e) {
|
||||
* @returns {Promise} Promise object represents number of files extracted
|
||||
*/
|
||||
function extractArchive(fileName, tmpDir) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Check archive exists
|
||||
try {
|
||||
if (!fs.existsSync(fileName)) {
|
||||
return reject(new Error('Archive does not exist.'));
|
||||
}
|
||||
if (!fs.existsSync(tmpDir)) {
|
||||
return reject(new Error('Temporary folder does not exist.'));
|
||||
}
|
||||
} catch (e) {
|
||||
throw new Error(e);
|
||||
// Check archive exists
|
||||
try {
|
||||
if (!fs.existsSync(fileName)) {
|
||||
throw Error('Archive does not exist.');
|
||||
}
|
||||
// Configure archive to use
|
||||
const archive = new StreamZip({
|
||||
file: fileName,
|
||||
storeEntries: true,
|
||||
});
|
||||
// Handle errors
|
||||
archive.on('error', (err) =>
|
||||
reject(new Error(`Error extracting archive: ${err}`))
|
||||
);
|
||||
// Extract
|
||||
archive.on('ready', () => {
|
||||
const extDir = path.join(tmpDir, 'archive');
|
||||
fs.mkdirSync(extDir, { recursive: true });
|
||||
archive.extract(null, extDir, (err, count) => {
|
||||
if (err) throw new Error(err);
|
||||
archive.close();
|
||||
console.log('Extraction complete');
|
||||
return resolve(count);
|
||||
});
|
||||
});
|
||||
return true;
|
||||
});
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Take in a zip file and return an array of the layers files
|
||||
* @param {string} fileName Name of the file to be extracted
|
||||
* @param {string} tmpDir Temporary directory to extract to
|
||||
* Take in a directory of layer files and return an array of the layers files
|
||||
* @param {string} dir Directory containing layer files
|
||||
* @returns {Array} Array of paths to the layers files
|
||||
*/
|
||||
function getLayers(fileName, tmpDir) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const extractDir = path.join(tmpDir, 'archive');
|
||||
extractArchive(fileName, tmpDir)
|
||||
.then((numfiles) => {
|
||||
console.log(`${numfiles} files extracted successfully`);
|
||||
const layers = gerberFiles.map((layerName) => ({
|
||||
filename: layerName,
|
||||
gerber: fs.createReadStream(path.join(extractDir, layerName)),
|
||||
}));
|
||||
if (numfiles > 0) {
|
||||
// Some files were extracted
|
||||
resolve(layers);
|
||||
} else {
|
||||
const errMsg = 'No files were extracted';
|
||||
reject(errMsg);
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getLayers2(dir) {
|
||||
function getLayers(dir) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Make sure the directory exists
|
||||
if (!fs.existsSync(dir)) {
|
||||
@ -166,7 +121,8 @@ function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getLayers(gerber, tmpDir)
|
||||
extractArchive(gerber, tmpDir);
|
||||
getLayers(path.join(tmpDir, 'archive'))
|
||||
.then(pcbStackup)
|
||||
.then((stackup) => {
|
||||
sharp(Buffer.from(stackup.top.svg), { density: imgConfig.density })
|
||||
@ -189,7 +145,6 @@ function gerberToImage(gerber, imgConfig, tmpDir, outputDir) {
|
||||
module.exports = {
|
||||
cleanupFiles,
|
||||
getLayers,
|
||||
getLayers2,
|
||||
extractArchive,
|
||||
config,
|
||||
gerberToImage,
|
||||
|
10
package-lock.json
generated
10
package-lock.json
generated
@ -892,6 +892,11 @@
|
||||
"integrity": "sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==",
|
||||
"dev": true
|
||||
},
|
||||
"adm-zip": {
|
||||
"version": "0.5.2",
|
||||
"resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.5.2.tgz",
|
||||
"integrity": "sha512-lUI3ZSNsfQXNYNzGjt68MdxzCs0eW29lgL74y/Y2h4nARgHmH3poFWuK3LonvFbNHFt4dTb2X/QQ4c1ZUWWsJw=="
|
||||
},
|
||||
"ajv": {
|
||||
"version": "6.12.6",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||
@ -5426,11 +5431,6 @@
|
||||
"which": "^2.0.2"
|
||||
}
|
||||
},
|
||||
"node-stream-zip": {
|
||||
"version": "1.12.0",
|
||||
"resolved": "https://registry.npmjs.org/node-stream-zip/-/node-stream-zip-1.12.0.tgz",
|
||||
"integrity": "sha512-HZ3XehqShTFj9gHauRJ3Bri9eiCTOII7/crtXzURtT14NdnOFs9Ia5E82W7z3izVBNx760tqwddxrBJVG52Y1Q=="
|
||||
},
|
||||
"noop-logger": {
|
||||
"version": "0.1.1",
|
||||
"resolved": "https://registry.npmjs.org/noop-logger/-/noop-logger-0.1.1.tgz",
|
||||
|
@ -22,9 +22,9 @@
|
||||
"gerber"
|
||||
],
|
||||
"dependencies": {
|
||||
"adm-zip": "^0.5.2",
|
||||
"fs-extra": "^9.1.0",
|
||||
"jszip": "^3.5.0",
|
||||
"node-stream-zip": "^1.12.0",
|
||||
"pcb-stackup": "^4.2.5",
|
||||
"sharp": "^0.27.0"
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user