Comments (6)
@focussing The libraries only work asynchronously as they rely on interacting with a headless Chromium instance, which can only be done asynchronously. With async
/await
syntax you can write asynchronous code in a synchronous fashion. If you absolutely must do it synchronously, you may need to get inventive/hacky, like spawning a synchronous child process, however, this could have an impact on performance.
from convert-svg.
Yep, you are right. Sorry for bothering.
How about using existence of the generated SVG file? Would that be an option?
from convert-svg.
Can you please clarify what you mean? Possibly with an example.
from convert-svg.
Basically I want to convert a directory structure consisting of 1100 SVG files to the same structure but then consisting of JPG files.
What I do right now is to walk through the structure generated by readdirp. The readdirp callback is fired when the structure is complete. Next I loop through the files list and convert SVG to JPEG. Now I do that with a timer.
When the conversion is complete I want to delete the SVN file.
Another option would be to walk through the file structure again, and delete the SVN files.
Of course that can be done in a lot of different ways.
Code snippet :)
// https://github.com/thlorenz/readdirp
var readdirp = require('readdirp');
var settings = {
root: outPath,
entryType: 'files',
fileFilter: '*.svg'
};
// In this example, this variable will store all the paths of the files and directories inside the providen path
var allFilePaths = [];
// Iterate recursively through a folder
readdirp(settings,
// This callback is executed everytime a file or directory is found inside the providen path
function(fileInfo) {
// Store the fullPath of the file/directory in our custom array
allFilePaths.push(
fileInfo.fullPath
);
},
// This callback is executed once
function(err, res) {
if (err) {
throw err;
}
// An array with all the fileEntry objects of the folder
// console.log(res);
//console.log(allFilePaths);
// ["c:/file.txt",""]
let i = 0;
// const stopAt = 10;
const stopAt = allFilePaths.length;
let timer = setInterval(function() {
let progress = i / stopAt * 100;
console.log(progress + '% ' + allFilePaths[i]);
const outputFilePath = convertFile(allFilePaths[i], options);
// console.log(outputFilePath);
if (++i == stopAt) {
clearInterval(timer);
console.log('conversion done')
}
}, 250)
// allFilePaths.forEach(function(f) {
// convertFile(f, options);
// });
}
);
from convert-svg.
This appears to be out of scope for this library and involves more of an understanding on managing asynchronous flows within Node.js. I'm not sure that I understand your timer logic, but the following should do what you're asking for:
const { convertFile } = require('convert-svg-to-jpeg');
const fs = require('fs');
const glob = require('glob');
const util = require('util');
const findFiles = util.promisify(glob);
const removeFile = util.promisify(fs.unlink);
(async() => {
const allFilePaths = await findFiles('*.svg', { absolute: true, cwd: outPath, nodir: true });
for (const filePath of allFilePaths) {
await convertFile(filePath, options);
await removeFile(filePath);
}
})();
All written like synchronous code with the benefit that it's fully asynchronous and non-blocking.
I'm not familiar with readdirp so I used glob for demonstration purposes.
This could even allow for parallel conversion and/or deletion if you wanted to, but that's even further out of the scope of this library. Lots of resources out there to help there though.
from convert-svg.
Yep you are right about out-of-scope. I just mentioned it related to checking whether the converted jpg is present and use that as a trigger.
But; thanks for your example code! I will test your solution and come back to that :)
Have a great day!
from convert-svg.
Related Issues (20)
- Support node 16 (update puppeteer) HOT 1
- The Edge Runtime support
- puppeteer params.width and params.height error
- chromium dependency HOT 1
- Arabic characters displayed properly with small svg but not with large one HOT 1
- Support macOS with M1 HOT 3
- Can't run in loop quickly, fails to open chrome eventually HOT 3
- Remote Code Injection vulnerable HOT 11
- Remote Code Injection vulnerable HOT 6
- Remote Code Injection vulnerable HOT 3
- svg:first
- How to disable the sandbox with --no-sandbox? HOT 3
- v0.6.0 fails to convert mermaid SVGs properly
- Typescript support
- 0.6.0 produces dimmer renders than 0.5.0
- Failed converting when doing many in parallel HOT 1
- Fails to convert svg that contains emoji in a <text> tag
- convert-svg-to-png does not process extra parameters in CLI HOT 1
- Doesn't work in Docker Container (convert-svg-to-png) HOT 1
- Puppeteer options setup through CLI won't apply!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from convert-svg.