UNPKG

3.4 kBJavaScriptView Raw
1var fs = require('graceful-fs')
2
3/**
4 * Check if a file exists. Throws if it does not.
5 * Mostly just for a nicer error message.
6 *
7 * @param {String} filename
8 * @return {Object}
9 * @api public
10 */
11
12exports.exists = function* (filename) {
13 try {
14 return yield fs.stat.bind(null, filename);
15 } catch (err) {
16 if (err.code === 'ENOENT') throw new Error('file "' + filename + '" does not exist.');
17 throw err;
18 }
19}
20
21/**
22 * Unlink a file. Ignores errors incase it doesn't exist.
23 *
24 * @param {String} filename
25 * @api public
26 */
27
28exports.unlink = function* (filename) {
29 try {
30 yield fs.unlink.bind(null, filename);
31 } catch (err) {
32 if (err.code === 'ENOENT') return;
33 throw err;
34 }
35}
36
37/**
38 * This is how the url rewriter and file copy/symlink will rewrite the file names.
39 * This will create names like github's with `/`s.
40 * i.e. fortawesome/fontawesome/v4.0.3/fonts/font.woff
41 * and, for local components, lib/my-local-component/image.png
42 *
43 * @param {Object} branch
44 * @return {String}
45 * @api public
46 */
47
48exports.rewriteUrl = function (branch) {
49 if (branch.type === 'local') return (branch.relativePath || branch.name).replace(/\\/g, '/');
50 if (branch.type === 'dependency') return branch.name + '/' + branch.ref;
51}
52
53/**
54 * Strip leading `./` from filenames.
55 *
56 * @param {String} filename
57 * @return {String}
58 * @api public
59 */
60
61exports.stripLeading = function (x) {
62 if (x.slice(0, 2) === './') return x.slice(2);
63 return x;
64}
65
66/**
67 * Check if an object is a Generator Function.
68 *
69 * @param {Object} obj
70 * @return {Boolean}
71 * @api private
72 */
73
74exports.isGeneratorFunction = function (obj) {
75 return obj
76 && obj.constructor
77 && 'GeneratorFunction' === obj.constructor.name;
78}
79
80
81/*
82 * css-url-rewriter
83 * https://github.com/callumlocke/css-url-rewriter
84 *
85 * Copyright (c) 2014 Callum Locke
86 * Licensed under the MIT license.
87 */
88
89// Regex to find CSS properties that contain URLs
90// Fiddle: http://refiddle.com/refiddles/css-url-matcher
91// Railroad: http://goo.gl/LXpk52
92var cssPropertyMatcher = /@import[^;]*|[;\s]?\*?[a-zA-Z\-]+\s*\:\#?[^;}]*url\(\s*['"]?[^'"\)\s]+['"]?\s*\)[^;}]*/g;
93
94// Regex to find the URLs within a CSS property value
95// Fiddle: http://refiddle.com/refiddles/match-multiple-urls-within-a-css-property-value
96// Railroad: http://goo.gl/vQzMcg
97var urlMatcher = /url\(\s*['"]?([^)'"]+)['"]?\s*\)/g;
98
99var defaults = {
100 excludeProperties: ['behavior', '*behavior']
101};
102
103exports.rewriteCSSURLs = function rewriteCSSURLs(css, settings, rewriterFn) {
104 // Normalise arguments and settings
105 if (typeof settings === 'function') {
106 rewriterFn = settings;
107 settings = defaults;
108 }
109
110 // Return the modified CSS
111 var result = css.toString().replace(cssPropertyMatcher, function(property) {
112 // This function deals with an individual CSS property.
113
114 // If this property is excluded, return it unchanged
115 if (settings.excludeProperties.length) {
116 var propertyName = property.split(':')[0].replace(/^\s+|\s+$/g, '');
117
118 for (var i = settings.excludeProperties.length - 1; i >= 0; i--) {
119 if (propertyName.indexOf(settings.excludeProperties[i]) === 0) {
120 return property;
121 }
122 }
123 }
124
125 // Return the property with the URL rewritten
126 return property.replace(urlMatcher, function(urlFunc, justURL) {
127 return urlFunc.replace(justURL, rewriterFn(justURL));
128 });
129 });
130
131 return result;
132};