forked from smysnk/gulp-rev-all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.js
168 lines (125 loc) · 5.34 KB
/
tool.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var Path = require('path');
var crypto = require('crypto');
module.exports = (function() {
'use strict';
var path_without_ext = function(path) {
var ext = Path.extname(path);
return path.substr(0, path.length - ext.length);
};
var dirname_with_sep = function(path) {
return Path.dirname(path).replace(/\\/g, '/') + '/';
}
var join_path_url = function (prefix, path) {
prefix = prefix.replace(/\/$/, '');
path = path.replace(/^\//, '');
return [ prefix, path ].join('/');
};
/**
* Joins a directory and a filename, replaces Windows forward-slash with a backslash.
*/
var join_path = function (directory, filename) {
return Path.join(directory, filename).replace(/^[a-z]:\\/i, '/').replace(/\\/g, '/');
};
/**
* Given a base path and resource path, will return resource path relative to the base.
* Also replaces Windows forward-slash with a backslash.
*/
var get_relative_path = function (base, path, noStartingSlash) {
if (base === path) {
return '';
}
// Sanitize inputs, convert windows to posix style slashes, ensure trailing slash for base
base = base.replace(/^[a-z]:/i, '').replace(/\\/g, '/').replace(/\/$/g, '') + '/';
path = path.replace(/^[a-z]:/i, '').replace(/\\/g, '/');
// Only truncate paths that overap with the base
if (base === path.substr(0, base.length)) {
path = '/' + path.substr(base.length);
}
var modifyStartingSlash = noStartingSlash !== undefined;
if (modifyStartingSlash) {
if (path[0] === '/' && noStartingSlash) {
path = path.substr(1);
} else if (path[0] !== '/' && !noStartingSlash){
path = '/' + path;
}
}
return path;
};
var md5 = function (str) {
return crypto.createHash('md5').update(str, 'utf8').digest('hex');
};
var is_binary_file = function (file) {
var length = (file.contents.length > 50) ? 50 : file.contents.length;
for (var i = 0; i < length; i++) {
if (file.contents[i] === 0) {
return true;
}
}
return false;
};
/**
* Given a file (context) and a file reference, return all the possible representations of paths to get from
* the context to the reference file.
*
*/
var get_reference_representations_relative = function (fileCurrentReference, file) {
var representations = [];
// Scenario 2: Current file is the same directory or lower than the reference
// (ie. file.path and the reference file.path are the same)
//
// file.base = /user/project
// file.path = /user/project/second/current_file.html
// fileCurrentReference.path = /user/project/second/index.html
if (dirname_with_sep(fileCurrentReference.path).indexOf(dirname_with_sep(file.path)) === 0) {
// index.html
representations.push(get_relative_path(Path.dirname(file.path), fileCurrentReference.revPathOriginal, true));
// ./index.html (reference: relative)
representations.push('.' + get_relative_path(Path.dirname(file.path), fileCurrentReference.revPathOriginal, false));
}
// Scenario 3: Current file is in a different child directory than the reference
// (ie. file.path and the reference file.path are different, not in root directory)
//
// file.base = /user/project
// file.path = /user/project/first/index.html
// fileCurrentReference.path = /user/project/second/index.html
if (dirname_with_sep(file.path) !== dirname_with_sep(fileCurrentReference.path) &&
dirname_with_sep(fileCurrentReference.path).indexOf(dirname_with_sep(file.path)) === -1) {
var pathCurrentReference = dirname_with_sep(get_relative_path(fileCurrentReference.base, fileCurrentReference.revPathOriginal));
var pathFile = dirname_with_sep(get_relative_path(file.base, file.revPathOriginal));
// ../second/index.html
var relPath = Path.relative(pathFile, pathCurrentReference);
relPath = relPath.replace(/\\/g, '/');
representations.push(relPath + '/' + Path.basename(fileCurrentReference.revPathOriginal));
}
return representations;
};
/**
* Given a file (context) and a file reference, return all the possible representations of paths to get from
* the context to the reference file.
*
*/
var get_reference_representations_absolute = function (fileCurrentReference, file) {
var representations = [];
var representation;
// Scenario 1: Current file is anywhere
// /view/index.html (reference: absolute)
representations.push(get_relative_path(fileCurrentReference.base, fileCurrentReference.revPathOriginal, false));
// Without starting slash, only if it contains a directory
// view/index.html (reference: absolute, without slash prefix)
representation = get_relative_path(fileCurrentReference.base, fileCurrentReference.revPathOriginal, true);
if (representation.indexOf('/')) {
representations.push(representation);
}
return representations;
};
return {
get_relative_path: get_relative_path,
md5: md5,
is_binary_file: is_binary_file,
path_without_ext: path_without_ext,
join_path: join_path,
join_path_url: join_path_url,
get_reference_representations_relative: get_reference_representations_relative,
get_reference_representations_absolute: get_reference_representations_absolute
};
})();