Skip to content
This repository was archived by the owner on Jan 27, 2019. It is now read-only.

Commit bfaf14a

Browse files
translate syntax to ES6
1 parent 1baaee1 commit bfaf14a

File tree

8 files changed

+202
-135
lines changed

8 files changed

+202
-135
lines changed

bin/getcss

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#!/usr/bin/env node
22

3-
var fs = require('fs');
4-
var program = require('commander');
5-
var normalizeUrl = require('normalize-url');
6-
var getCss = require('..');
3+
const program = require('commander');
4+
const normalizeUrl = require('normalize-url');
5+
const getCss = require('..');
76

87
program
98
.version('0.0.1')
109
.command('* <url>')
11-
.action(function(url) {
12-
if(url) {
10+
.action((url) => {
11+
if (url) {
1312
url = normalizeUrl(url, { stripWWW: false });
14-
getCss(url, { verbose: true }).then(function(css) {
13+
getCss(url, { verbose: true }).then((css) => {
1514
console.log(css.css);
1615
});
1716
}

index.js

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
'use strict';
2-
3-
var q = require('q');
4-
var isCss = require('is-css');
5-
var isPresent = require('is-present');
6-
var isBlank = require('is-blank');
7-
var isUrl = require('is-url-superb');
8-
var request = require('request');
9-
var cheerio = require('cheerio');
10-
var normalizeUrl = require('normalize-url');
11-
var stripHtmlComments = require('strip-html-comments');
12-
var resolveCssImportUrls = require('resolve-css-import-urls');
13-
var ua = require('ua-string');
14-
15-
var getLinkContents = require('./utils/get-link-contents');
16-
var createLink = require('./utils/create-link');
17-
18-
module.exports = function(url, options){
19-
var deferred = q.defer();
20-
var options = options || {};
1+
const q = require('q');
2+
const isCss = require('is-css');
3+
const isPresent = require('is-present');
4+
const isBlank = require('is-blank');
5+
const isUrl = require('is-url-superb');
6+
const request = require('request');
7+
const cheerio = require('cheerio');
8+
const normalizeUrl = require('normalize-url');
9+
const stripHtmlComments = require('strip-html-comments');
10+
const resolveCssImportUrls = require('resolve-css-import-urls');
11+
const ua = require('ua-string');
12+
13+
const getLinkContents = require('./utils/get-link-contents');
14+
const createLink = require('./utils/create-link');
15+
16+
module.exports = function (url, options) {
17+
const deferred = q.defer();
18+
19+
options = options || {};
20+
2121
options.headers = options.headers || {};
2222
options.headers['User-Agent'] = options.headers['User-Agent'] || ua;
2323
options.timeout = options.timeout || 5000;
2424
options.gzip = true;
2525

2626
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
27-
throw new TypeError('get-css expected a url as a string')
27+
throw new TypeError('get-css expected a url as a string');
2828
}
2929

3030
url = normalizeUrl(url, { stripWWW: false });
@@ -34,15 +34,15 @@ module.exports = function(url, options){
3434
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
3535
}
3636

37-
var status = {
37+
const status = {
3838
parsed: 0,
39-
total: 0
39+
total: 0,
4040
};
4141

42-
var result = {
42+
const result = {
4343
links: [],
4444
styles: [],
45-
css: ''
45+
css: '',
4646
};
4747

4848
function handleResolve() {
@@ -52,19 +52,19 @@ module.exports = function(url, options){
5252
}
5353

5454
function parseHtml(html) {
55-
var $ = cheerio.load(html);
55+
const $ = cheerio.load(html);
5656
result.pageTitle = $('head > title').text();
5757

58-
$('[rel=stylesheet]').each(function() {
59-
var link = $(this).attr('href');
60-
if(isPresent(link)) {
61-
result.links.push(createLink(link, url));
62-
}else{
63-
result.styles.push(stripHtmlComments($(this).text()));
64-
}
58+
$('[rel=stylesheet]').each(function () {
59+
const link = $(this).attr('href');
60+
if (isPresent(link)) {
61+
result.links.push(createLink(link, url));
62+
} else {
63+
result.styles.push(stripHtmlComments($(this).text()));
64+
}
6565
});
6666

67-
$('style').each(function() {
67+
$('style').each(function () {
6868
result.styles.push(stripHtmlComments($(this).text()));
6969
});
7070

@@ -73,19 +73,19 @@ module.exports = function(url, options){
7373
deferred.resolve(false);
7474
}
7575

76-
result.links.forEach(function(link) {
76+
result.links.forEach((link) => {
7777
getLinkContents(link.url, options)
78-
.then(function(css) {
78+
.then((css) => {
7979
handleCssFromLink(link, css);
8080
})
81-
.catch(function(error) {
81+
.catch((error) => {
8282
link.error = error;
8383
status.parsed++;
8484
handleResolve();
8585
});
8686
});
8787

88-
result.styles.forEach(function(css) {
88+
result.styles.forEach((css) => {
8989
result.css += css;
9090
status.parsed++;
9191
handleResolve();
@@ -107,37 +107,45 @@ module.exports = function(url, options){
107107
status.total += link.imports.length;
108108
result.css += css;
109109

110-
link.imports.forEach(function(importUrl) {
111-
var importLink = createLink(importUrl, importUrl);
110+
link.imports.forEach((importUrl) => {
111+
const importLink = createLink(importUrl, importUrl);
112112
result.links.push(importLink);
113113

114114
getLinkContents(importLink.url, options)
115-
.then(function(css) {
115+
.then((css) => {
116116
handleCssFromLink(importLink, css);
117117
})
118-
.catch(function(error) {
118+
.catch((error) => {
119119
link.error = error;
120120
status.parsed++;
121121
handleResolve();
122122
});
123123
});
124124
}
125125

126-
request(options, function(error, response, body) {
126+
request(options, (error, response, body) => {
127127
if (error) {
128-
if (options.verbose) console.log('Error from ' + url + ' ' + error);
128+
if (options.verbose) {
129+
console.log(`Error from ${url} ${error}`);
130+
}
131+
129132
deferred.reject(error);
133+
130134
return;
131135
}
132136

133-
if (response && response.statusCode != 200) {
134-
if (options.verbose) console.log('Received a ' + response.statusCode + ' from: ' + url);
135-
deferred.reject({ url: url, statusCode: response.code });
137+
if (response && response.statusCode !== 200) {
138+
if (options.verbose) {
139+
console.log(`Received a ${response.statusCode} from: ${url}`);
140+
}
141+
142+
deferred.reject({ url, statusCode: response.code });
143+
136144
return;
137145
}
138146

139147
if (isCss(url)) {
140-
var link = createLink(url, url);
148+
const link = createLink(url, url);
141149
result.links.push(link);
142150
handleCssFromLink(link, body);
143151
} else {

test/results.json

Lines changed: 97 additions & 36 deletions
Large diffs are not rendered by default.

test/utils/create-link-test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
var assert = require('assert');
2-
var createLink = require('../../utils/create-link');
1+
const assert = require('assert');
2+
const createLink = require('../../utils/create-link');
33

4-
describe('create-link', function() {
5-
6-
it('should create the correct link object', function() {
4+
describe('create-link', () => {
5+
it('should create the correct link object', () => {
76
assert.deepEqual(
87
createLink('../bar.css', 'http://foo.com/css/my-css.css'),
98
{ link: '../bar.css', url: 'http://foo.com/bar.css', css: '' });
109
});
1110

12-
it('should correctly resolve full url links', function() {
11+
it('should correctly resolve full url links', () => {
1312
assert.deepEqual(
1413
createLink('http://foo.com/bar.css', 'http://foo.com/css/my-css.css'),
1514
{ link: 'http://foo.com/bar.css', url: 'http://foo.com/bar.css', css: '' });

test/utils/resolve-url-test.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
var assert = require('assert');
2-
var resolveUrl = require('../../utils/resolve-url');
1+
const assert = require('assert');
2+
const resolveUrl = require('../../utils/resolve-url');
33

4-
describe('resolve-url', function() {
5-
6-
it('should correctly resolve a .. relative link', function() {
4+
describe('resolve-url', () => {
5+
it('should correctly resolve a .. relative link', () => {
76
assert.equal(resolveUrl('http://foo.com/some/path', '../bar.css'), 'http://foo.com/some/bar.css');
87
});
98

10-
it('should correctly resolve a .. relative link when the url has a trailing /', function() {
9+
it('should correctly resolve a .. relative link when the url has a trailing /', () => {
1110
assert.equal(resolveUrl('http://foo.com/some/path/', '../bar.css'), 'http://foo.com/some/bar.css');
1211
});
1312

14-
it('should correctly resolve a relative link', function() {
13+
it('should correctly resolve a relative link', () => {
1514
assert.equal(resolveUrl('http://foo.com/some/path', 'bar.css'), 'http://foo.com/some/path/bar.css');
1615
});
1716

18-
it('should correctly return a full link', function() {
17+
it('should correctly return a full link', () => {
1918
assert.equal(
2019
resolveUrl('http://foo.com', 'http://foo.com/some/path/bar.css'),
2120
'http://foo.com/some/path/bar.css');
2221
});
2322

24-
it('should correctly resolve an absolute link', function() {
23+
it('should correctly resolve an absolute link', () => {
2524
assert.equal(resolveUrl('http://foo.com/some/path', '/bar.css'), 'http://foo.com/bar.css');
2625
});
2726

28-
it('should correctly resolve a relative url from an html file', function() {
27+
it('should correctly resolve a relative url from an html file', () => {
2928
assert.equal(resolveUrl('http://foo.bar/awesome/baz.html', 'baz.css'), 'http://foo.bar/awesome/baz.css');
3029
});
3130

32-
it('should correctly resolve an absolute url from an html file', function() {
31+
it('should correctly resolve an absolute url from an html file', () => {
3332
assert.equal(resolveUrl('http://foo.bar/awesome/baz.html', '/baz.css'), 'http://foo.bar/baz.css');
3433
});
3534
});

utils/create-link.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var resolveUrl = require('./resolve-url');
1+
const resolveUrl = require('./resolve-url');
22

33
module.exports = function createLink(link, url) {
44
return {
5-
link: link,
5+
link,
66
url: resolveUrl(url, link),
7-
css: ''
7+
css: '',
88
};
99
};

utils/get-link-contents.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var q = require('q');
2-
var request = require('request');
1+
const q = require('q');
2+
const request = require('request');
33

44
module.exports = function getLinkContents(linkUrl, options) {
5-
var d = q.defer();
5+
const d = q.defer();
66

7-
request({ url: linkUrl, timeout: options.timeout, gzip: true }, function(error, response, body) {
7+
request({ url: linkUrl, timeout: options.timeout, gzip: true }, (error, response, body) => {
88
if (error || response.statusCode !== 200) {
99
d.reject(error);
1010
}

utils/resolve-url.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
var urlResolver = require('url').resolve;
2-
3-
module.exports = function resolveUrl(url, link) {
4-
if(link.match(/^(http|https)/g)) {
5-
return link;
6-
} else {
7-
if (isCssFile(url)) {
8-
removeExtension(url);
9-
} else if(!endsInForwardSlash(url)) {
10-
if (!isHtmlUrl(url)) {
11-
url += '/';
12-
}
13-
}
14-
15-
return urlResolver(url, link);
16-
}
17-
};
1+
const urlResolver = require('url').resolve;
182

193
function endsInForwardSlash(url) {
20-
return url.indexOf('/', url.length - 1) != -1;
4+
return url.indexOf('/', url.length - 1) !== -1;
215
}
226

237
function isCssFile(url) {
24-
return url.indexOf('.css', url.length - 4) != -1;
8+
return url.indexOf('.css', url.length - 4) !== -1;
259
}
2610

2711
function isHtmlUrl(url) {
28-
return url.indexOf('.html', url.length - 5) != -1;
12+
return url.indexOf('.html', url.length - 5) !== -1;
2913
}
3014

3115
function removeExtension(url) {
3216
url.replace(/\.[^/.]+$/, '');
3317
}
18+
19+
20+
module.exports = function resolveUrl(url, link) {
21+
if (link.match(/^(http|https)/g)) {
22+
return link;
23+
}
24+
25+
if (isCssFile(url)) {
26+
removeExtension(url);
27+
} else if (!endsInForwardSlash(url)) {
28+
if (!isHtmlUrl(url)) {
29+
url += '/';
30+
}
31+
}
32+
33+
return urlResolver(url, link);
34+
};

0 commit comments

Comments
 (0)