UNPKG

1.7 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;
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}