-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathutils.js
More file actions
75 lines (61 loc) · 1.66 KB
/
Copy pathutils.js
File metadata and controls
75 lines (61 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fse = require("fs-extra");
const fs = require("fs");
const path = require("path");
// Make a locally bound path joiner, (bound to generate).
var local = path.join.bind(null, __dirname, "../");
var util = {
pointerRegex: /\s*\*\s*/,
doublePointerRegex: /\s*\*\*\s*/,
readFile: function(file) {
try {
return fs.readFileSync(local(file)).toString();
}
catch (unhandledException) {
return "";
}
},
writeFile: function(file, content, header) {
try {
var file = local(file);
if (typeof content == "object") {
content = JSON.stringify(content, null, 2)
}
if (header) {
var commentPrefix = ~header.indexOf('.gyp') ? '#' : '//'
content = commentPrefix +
" This is a generated file, modify: generate/templates/" +
header +
"\n\n" +
content;
}
fse.ensureFileSync(file);
fse.writeFileSync(file, content);
return true;
}
catch (exception) {
return false;
}
},
titleCase: function(str) {
return str.split(/_|\//).map(function(val, index) {
if (val.length) {
return val[0].toUpperCase() + val.slice(1);
}
return val;
}).join("");
},
camelCase: function(str) {
return str.split(/_|\//).map(function(val, index) {
return (index >= 1
? val[0].toUpperCase() + val.slice(1)
: val[0].toLowerCase() + val.slice(1));
}).join("");
},
isPointer: function(type) {
return util.pointerRegex.test(type) || util.doublePointerRegex.test(type);
},
isDoublePointer: function(type) {
return util.doublePointerRegex.test(type);
}
};
module.exports = util;