oslllo/svg-fixer

does not work on multiple files

Closed this issue · 11 comments

It just works with 1 specified file.
Not working with whole folder

@dnamyslak Hi, what is the full error message you are getting and what does your code look like?

i am using node express with yarp and sharp resolution ( to exclude the other issue I mentioned )
package.json

{
"name": "nodejs",
"version": "0.0.0",
"private": false,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"morgan": "~1.9.1",
"oslllo-svg-fixer": "^0.4.3",
"yarn": "^1.22.4"
},
"resolutions": {
"sharp": "^0.23.1"
}
}

and using your function in bin/www.js

svgfixerFixExample();

async function svgfixerFixExample() {
// You can use a path that points to a directory with SVGs.
await svgfixer.fix('svg/example.svg', 'svg/fixed-svgs');
}

works fine but just witn explicit ONE file so I needet to change for each filename and run this several times

does not work when tried to use => await svgfixer.fix('svg', 'svg/fixed-svgs');

@dnamyslak

  • await only works inside async functions
  • You can try this.
svgfixer.fix('path/to/folder/with/multiple/svgs', 'path/to/store/fixed/svgs')
.then(() => {
    console.log('finished')
})
.catch((error) => {
    console.log(error)
})
  • If you want to fix all svg files in a folder do not include the .svg at the end of the path, just pass in the directory/folder path like this: path/to/folder not path/to/folder.svg

  • My paths use / because i'm on ubuntu, if you are on windows use \ or just copy the folder path from explorer.

@dnamyslak could I also see what www.js looks like?

`#!/usr/bin/env node

/**

  • Module dependencies.
    */

var app = require('../app');
var debug = require('debug')('nodejs:server');
var http = require('http');
var svgfixer = require('oslllo-svg-fixer');

/**

  • Get port from environment and store in Express.
    */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**

  • Create HTTP server.
    */

var server = http.createServer(app);

/**

  • Listen on provided port, on all network interfaces.
    */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**

  • Normalize a port into a number, string, or false.
    */

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**

  • Event listener for HTTP server "error" event.
    */

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**

  • Event listener for HTTP server "listening" event.
    */

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

svgfixerFixExample();

async function svgfixerFixExample() {
// You can use a path that points to a directory with SVGs.
await svgfixer.fix('svg', 'svg/fixed-svgs2');
}
`

@dnamyslak is the svg folder in the same location as the www.js file? Also do you just want to fix icons or you really want to run the package on a web server?

the svg folder is 1 level up
just want to fixed the icons which I did doing one by one :)
thanks for your app - its great !
I use iconmoon constantly and always had problem with stroked icons there

@dnamyslak Glad to hear that is solved that issue for you too, but I really don't want you do have to do the icons one by one, especially if the app supports doing a whole folder. If you only want to fix icons you don't need node server to do it, try doing this.

  1. Create a new folder somewhere and call it icon-fixer
  2. cd into the folder and run npm init
  3. Press enter until its done.
  4. create a file inside this folder and call it index.js
  5. Open the package.json file inside the folder.
  6. Inside the scripts object add this key value pair "fixer": "node index.js", so it looks something like this:
  "scripts": {
    "fixer": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  1. run npm i oslllo-svg-fixer --save inside the folder

  2. Open the index.js file and type this inside of it:

const svgfixer = require("oslllo-svg-fixer");

var source = "path/to/folder/with/multiple/svgs";
var destination = "path/to/store/fixed/svgs";

var options = {
    throwIfPathDoesNotExist: true,
    showProgressBar: true,
};

svgfixer.fix(source, destination, options)
.then(() => {
    console.log("Finished fixing svgs")
})
.catch((err) => {
    console.log("Error fixing svgs")
    console.log(err)
})
  1. change source to the path pointing to the svgs you want to fix.
  2. change destination to the path where you want to store the fixed svgs.
  3. save the file and then run npm run fixer

@dnamyslak fixed the multiple bugs with the issue, update and try the latest version, 0.5.0. You also no more need/require sharp anymore.

Just a small correction on the above wrong code:

SVGfixer(source, destination, options).fix()
.then(() => {
    console.log("Finished fixing svgs");
})
.catch((err) => {
    console.log("Error fixing svgs");
    console.log(err);
});