UNPKG

1.28 kBJavaScriptView Raw
1var fs = require('fs');
2var mkdirp = require('mkdirp');
3var accessFile = fs.accessSync || fs.existsSync;
4
5exports.generateCompiledPath = function(filePath) {
6 if (process.env.DEBUG) {
7 return filePath.replace(/\W+/g, '_');
8 }
9 else {
10 return 's-' + hashString(filePath);
11 }
12};
13
14exports.isReadable = function(filePath) {
15 try {
16 var ret = accessFile(filePath, fs.R_OK);
17 if (ret !== undefined) {
18 return ret;
19 }
20 return true;
21 }
22 catch(e) {
23 return false;
24 }
25};
26
27exports.isWritable = function(filePath) {
28 try {
29 var ret = accessFile(filePath, fs.W_OK);
30 if (ret !== undefined) {
31 return ret;
32 }
33 return true;
34 }
35 catch(e) {
36 return false;
37 }
38};
39
40
41exports.mkdirSync = function(dirPath) {
42 try {
43 mkdirp.sync(dirPath);
44 }
45 catch (e) {
46 if (!e.message.match('EEXIST')) {
47 throw e;
48 }
49 }
50};
51
52// Courtesy of http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
53function hashString(string) {
54 var hash = 0;
55 var i, chr, len;
56
57 if (string.length === 0) return hash;
58
59 for (i = 0, len = string.length; i < len; i++) {
60 chr = string.charCodeAt(i);
61 hash = ((hash << 5) - hash) + chr;
62 hash |= 0; // Convert to 32bit integer
63 }
64
65 return String(hash);
66}