Skip to content

Commit

Permalink
feat: add callback feature to SVGFixer.fix()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghustavh97 committed Oct 17, 2020
1 parent 1d5a823 commit 975d658
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 17 deletions.
11 changes: 8 additions & 3 deletions src/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Option = require("./option");
const Location = require("./location");
const is = require("oslllo-validator");
const Processor = require("./processor");

const SVGFixer = function (source, destination, options = {}) {
Expand All @@ -17,11 +18,15 @@ const SVGFixer = function (source, destination, options = {}) {
};

SVGFixer.prototype = {
fix: async function () {
fix: function (callback) {
var processor = new Processor(this);
await processor.start();
if (is.fn(callback)) {
processor.start(callback);

return this;
return this;
}

return processor.start();
},
};

Expand Down
46 changes: 33 additions & 13 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require("fs");
const path = require("path");
const Svg = require("./svg");
const colors = require("colors");
const is = require("oslllo-validator");
const cliprogress = require("cli-progress");

const Processor = function (fixer) {
Expand All @@ -18,21 +19,40 @@ const Processor = function (fixer) {
};

Processor.prototype = {
start: async function () {
this.setup();
var svgs = this.source;
svgs = svgs.map((source) => {
var destination = path.join(
this.destination,
path.basename(source)
);
start: function (callback) {
if (is.fn(callback)) {
this.pipeline()
.then((fixer) => {
callback(null, fixer);
})
.catch((err) => {
callback(err);
});

return { source, destination };
});
for (var i = 0; i < svgs.length; i++) {
await this.instance(svgs[i]);
return this.fixer;
}
this.teardown();

return this.pipeline();
},
pipeline: function () {
return new Promise(async (resolve, reject) => {
try {
this.setup();
var svgs = this.source;
svgs = svgs.map((source) => {
var destination = path.join(this.destination, path.basename(source));

return { source, destination };
});
for (var i = 0; i < svgs.length; i++) {
await this.instance(svgs[i]);
}
this.teardown();
resolve(this.fixer);
} catch (err) {
reject(err);
}
});
},
setup: function () {
if (this.progress.show) {
Expand Down
1 change: 1 addition & 0 deletions test/assets/broken-icons/invalid/invalid.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ prepare();
require("./src/test.parameters");
require("./src/test.pathing");
require("./src/test.exceptions");
require("./src/test.async");
require("./src/test.svgfixer");
37 changes: 37 additions & 0 deletions test/src/test.async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

const { emptyDir, isEmptyDir } = require("./");
const { SVGFixer, assert, path2 } = require("./helper");

describe("test.async", () => {
describe("it exports the svg(s) and", () => {
it("returns a promise when no callback is provided", async () => {
var source = path2.single.relative;
var destination = path2.fixed.relative;
emptyDir(destination);
assert.isTrue(isEmptyDir(destination));
var fixer = SVGFixer(source, destination).fix();
assert.isTrue(fixer instanceof Promise);
await fixer;
assert.isFalse(isEmptyDir(destination));
});
it("returns an instance of SVGFixer when a callback function is provided", (done) => {
var source = path2.single.relative;
var destination = path2.fixed.relative;
emptyDir(destination);
assert.isTrue(isEmptyDir(destination));
/**
* @ignore
* @description - Test callback function
*/
function callback (err, fixer) {
assert.isFalse(fixer instanceof Promise);
assert.isTrue(fixer instanceof SVGFixer);
assert.isFalse(isEmptyDir(destination));
done(err);
}
var fixer = SVGFixer(source, destination).fix(callback);
assert.isTrue(fixer instanceof SVGFixer);
});
});
});
24 changes: 23 additions & 1 deletion test/src/test.exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require("fs-extra");
const { isEmptyDir } = require("./");
const is = require("oslllo-validator");
const error = require("../../src/error");
const { SVGFixer, assert, path2 } = require("./helper");
const { SVGFixer, assert, path2, expect } = require("./helper");

describe("test.exceptions", () => {
/**
Expand Down Expand Up @@ -45,6 +45,28 @@ describe("test.exceptions", () => {
`one of the source file paths does not point to a .svg file. ${source}`
);
});
describe("if source svg is invalid", () => {
var source = path
.resolve("test/assets/broken-icons/invalid/invalid.svg")
.replace(/\\/gu, "/");
var destination = path2.fixed.relative;
it("rejects if (promise)", function () {
return expect(SVGFixer(source, destination).fix()).to.be.rejectedWith(
Error
);
});
it("returns an err if (callback)", function (done) {
/**
* @ignore
* @description - Test callback function
*/
function callback (err) {
assert.isTrue(err instanceof Error);
done();
}
SVGFixer(source, destination).fix(callback);
});
});
});

describe("invalid destination arguments", () => {
Expand Down

0 comments on commit 975d658

Please sign in to comment.