-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
88 lines (84 loc) · 2.27 KB
/
utils.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const path = require('path');
const fs = require('fs');
module.exports = {
cmdMap: {
"s":"start", "start":"start",
"b":"build", "build":"build",
"d":"deploy", "deploy":"deploy",
"r":"release", "release":"release",
"c":"clear", "clear":"clear"
},
getProcessor: function(filename) {
if (this.isJS(filename)) {
return 'uglify';
} else if (this.isSass(filename)) {
return 'sass';
} else if (this.isTemplate(filename)) {
return 'nunjucks';
} else if (this.isImage(filename)) {
return 'imagemin';
} else {
return 'copy';
}
},
isJS: function(str) {
return /\.js$/.test(str);
},
isSass: function(str) {
return /\.scss|sass$/.test(str);
},
isTemplate: function(str) {
return /\.html|htm$/.test(str);
},
isImage: function(str) {
return /\.png|gif|jpg$/.test(str);
},
hasContents: function (file) {
if (this.exists(file)) {
return fs.readFileSync(file, 'utf8') !== '';
} else {
return false;
}
},
exists: function(file) {
return fs.existsSync(file);
},
isFile: function(filename) {
try {
return fs.lstatSync(filename).isFile();
} catch (err) {
return false;
}
},
isDir: function(filename) {
try {
return fs.lstatSync(filename).isDirectory();
} catch (err) {
return false;
}
},
relativeDir: function (dir) {
return path.relative(process.cwd(), dir);
},
dirToPath: function(dir) {
dir = path.normalize(dir);
return dir.replace(/\\/g, '/');
},
isAbsUrl: function (url) {
return /^http:|https:|\/\//.test(url);
},
isDataUri: function (url) {
return /^data:image/.test(url);
},
getTag: function (tagname) {
switch (tagname) {
case 'link':
return '<link type="text/css" rel="stylesheet" href="{{source}}" />';
case 'script':
return '<script src="{{source}}"></script>';
default:
return '<'+ tagname +'></'+ tagname +'>'
}
},
};