Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve relative paths as urls #209

Merged
merged 4 commits into from
Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require("path"),
url = require("url"),
fs = require("fs"),
_ = require("underscore"),
color = require("tinycolor2"),
Expand All @@ -23,8 +24,12 @@ const path = require("path"),
ROTATE_DEGREES = 90;

function helpers(options) {
function relative(directory) {
return path.join(options.path, directory).replace(/\\/g, "/");
function directory(path) {
return path.substr(-1) === "/" ? path : `${path}/`;
}

function relative(path) {
return url.resolve(options.path && directory(options.path), path);
}

function print(context, message) {
Expand Down
29 changes: 12 additions & 17 deletions test/default.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
const favicons = require('../src');
const test = require('ava');
const path = require('path');
const favicons = require("../src");
const test = require("ava");

test.cb('should generate the expected default result', t => {
t.plan(1);
const { logo, normalize } = require("./util");

favicons(path.join(__dirname, 'logo.png'), {}, (error, response) => {
test.cb("should generate the expected default result", t => {
t.plan(1);

if (error) {
throw error;
}
favicons(logo, {}, (error, result) => {
if (error) {
throw error;
}

const result = [...response.files, ...response.images].reduce(
(obj, {name, contents}) => Object.assign(obj, {[name]: contents}),
{'index.html': response.html}
);

t.snapshot(result);
t.end();
});
t.snapshot(normalize(result));
t.end();
});
});
14 changes: 14 additions & 0 deletions test/prefixed.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const favicons = require('../src');
const test = require('ava');

const {logo, normalize} = require('./util');

test('should allow setting an URL prefix', async t => {
t.plan(1);

const result = await favicons(logo, {
path: 'https://domain/subdomain',
});

t.snapshot(normalize(result));
});
20 changes: 8 additions & 12 deletions test/promise.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
const favicons = require('../src');
const test = require('ava');
const path = require('path');
const favicons = require("../src");
const test = require("ava");

test('should return a promise if callback is undefined', t => {
t.plan(1);
const { logo, normalize } = require("./util");

return favicons(path.join(__dirname, 'logo.png'), {}).then(response => {
const result = [...response.files, ...response.images].reduce(
(obj, {name, contents}) => Object.assign(obj, {[name]: contents}),
{'index.html': response.html}
);
test("should return a promise if callback is undefined", t => {
t.plan(1);

t.snapshot(result);
});
return favicons(logo, {}).then(result => {
t.snapshot(normalize(result));
});
});
12,624 changes: 12,624 additions & 0 deletions test/snapshots/prefixed.test.js.md

Large diffs are not rendered by default.

Binary file added test/snapshots/prefixed.test.js.snap
Binary file not shown.
30 changes: 16 additions & 14 deletions test/stream.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const favicons = require('../src').stream;
const test = require('ava');
const path = require('path');
const gulp = require('gulp');
const favicons = require("../src").stream;
const test = require("ava");
const gulp = require("gulp");

test.cb('should provide stream interface', t => {
t.plan(1);
const { logo } = require("./util");

const result = {};
test.cb("should provide stream interface", t => {
t.plan(1);

gulp.src(path.join(__dirname, 'logo.png'))
.pipe(favicons({ html: 'index.html', pipeHTML: true }))
.on('data', chunk => result[chunk.path] = chunk.contents)
.on('end', () => {
t.snapshot(result);
t.end();
});
const result = {};

gulp
.src(logo)
.pipe(favicons({ html: "index.html", pipeHTML: true }))
.on("data", chunk => (result[chunk.path] = chunk.contents))
.on("end", () => {
t.snapshot(result);
t.end();
});
});
9 changes: 9 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require("path");

module.exports.logo = path.resolve(__dirname, "logo.png");

module.exports.normalize = ({ files, images, html }) =>
[...files, ...images].reduce(
(obj, { name, contents }) => Object.assign(obj, { [name]: contents }),
{ "index.html": html }
);