1 | define(["./fs", "./buildControlBase", "dojo/has"], function(fs, bc, has) { |
---|
2 | var |
---|
3 | getFilename= function(filename) { |
---|
4 | if (/\//.test(filename)) { |
---|
5 | return filename.match(/^.*\/([^\/]+)$/)[1]; |
---|
6 | } |
---|
7 | return filename; |
---|
8 | }, |
---|
9 | |
---|
10 | getFilepath= function(filename) { |
---|
11 | if (/\//.test(filename)) { |
---|
12 | var result= filename.match(/^(.*)\/[^\/]+$/)[1]; |
---|
13 | // if result=="", then must have been something like "/someFile" |
---|
14 | return result.length ? result : "/"; |
---|
15 | } |
---|
16 | return ""; |
---|
17 | }, |
---|
18 | |
---|
19 | getFiletype= function(filename, trimDot) { |
---|
20 | var match= filename.match(/(\.([^\/]*))$/); |
---|
21 | return (match && (trimDot ? match[2] : match[1])) || ""; |
---|
22 | }, |
---|
23 | |
---|
24 | cleanupPath= function(path) { |
---|
25 | // change any falsy to "" |
---|
26 | path= path || ""; |
---|
27 | |
---|
28 | // change all backslashes to forward slashes for those with bad habits from windows |
---|
29 | path= path.replace(/\\/g, "/"); |
---|
30 | |
---|
31 | // remove any trailing "/" to be less sensitive to careless user input |
---|
32 | // but remember "/" is not a trailing slash--it's the root |
---|
33 | if (path.length>1 && path.charAt(path.length-1)=="/") { |
---|
34 | path= path.substring(0, path.length-1); |
---|
35 | } |
---|
36 | return path; |
---|
37 | }, |
---|
38 | |
---|
39 | catPath= function(lhs, rhs) { |
---|
40 | if (arguments.length>2) { |
---|
41 | for (var args= [], i= 1; i<arguments.length; args.push(arguments[i++])); |
---|
42 | return catPath(lhs, catPath.apply(this, args)); |
---|
43 | } else if (!rhs || !rhs.length) { |
---|
44 | return lhs; |
---|
45 | } else if (!lhs || !lhs.length) { |
---|
46 | return rhs; |
---|
47 | } else { |
---|
48 | return (lhs + "/" + rhs).replace(/\/\/\/?/g, "/"); |
---|
49 | } |
---|
50 | }, |
---|
51 | |
---|
52 | compactPath = function(path){ |
---|
53 | var result = [], |
---|
54 | segment, lastSegment; |
---|
55 | path = path.replace(/\\/g, '/').split('/'); |
---|
56 | while(path.length){ |
---|
57 | segment = path.shift(); |
---|
58 | if(segment==".." && result.length && lastSegment!=".."){ |
---|
59 | result.pop(); |
---|
60 | lastSegment = result[result.length - 1]; |
---|
61 | }else if(segment!="."){ |
---|
62 | result.push(lastSegment= segment); |
---|
63 | } // else ignore "." |
---|
64 | } |
---|
65 | return result.join("/"); |
---|
66 | }, |
---|
67 | |
---|
68 | isAbsolutePathRe= has("is-windows") ? |
---|
69 | // for windows, starts with "\\" or a drive designator (anything other than "/" or "\" followed by a ":") |
---|
70 | /^((\\\\)|([^\/\\]+\:))/ : |
---|
71 | // for unix, starts with "/" |
---|
72 | /^\//, |
---|
73 | |
---|
74 | isAbsolutePath= function(path) { |
---|
75 | return path && path.length && isAbsolutePathRe.test(path); |
---|
76 | }, |
---|
77 | |
---|
78 | normalize= function(filename){ |
---|
79 | return has("is-windows") ? filename.replace(/\//g, "\\") : filename; |
---|
80 | }, |
---|
81 | |
---|
82 | getAbsolutePath= function(src, base) { |
---|
83 | src= cleanupPath(src); |
---|
84 | if (src.charAt(0)!="/") { |
---|
85 | src= catPath(base, src); |
---|
86 | } |
---|
87 | return compactPath(src); |
---|
88 | }, |
---|
89 | |
---|
90 | computePath= function(path, base) { |
---|
91 | path= cleanupPath(path); |
---|
92 | return compactPath(isAbsolutePath(path) ? path : catPath(base, path)); |
---|
93 | }, |
---|
94 | |
---|
95 | getTimestamp= function(ts) { |
---|
96 | var f= function(i) { return "-" + (i<10 ? "0" + i : i); }; |
---|
97 | return ts.getFullYear() + f(ts.getMonth()+1) + f(ts.getDate()) + f(ts.getHours()) + f(ts.getMinutes()) + f(ts.getSeconds()); |
---|
98 | }, |
---|
99 | |
---|
100 | dirExists= function( |
---|
101 | filename |
---|
102 | ) { |
---|
103 | try { |
---|
104 | return fs.statSync(filename).isDirectory(); |
---|
105 | } catch(e) { |
---|
106 | return false; |
---|
107 | } |
---|
108 | }, |
---|
109 | |
---|
110 | fileExists= function( |
---|
111 | filename |
---|
112 | ) { |
---|
113 | try { |
---|
114 | return fs.statSync(filename).isFile(); |
---|
115 | } catch(e) { |
---|
116 | return false; |
---|
117 | } |
---|
118 | }, |
---|
119 | |
---|
120 | checkedDirectories= {}, |
---|
121 | |
---|
122 | clearCheckedDirectoriesCache= function() { |
---|
123 | checkedDirectories= {}; |
---|
124 | }, |
---|
125 | ensureDirectory= function(path) { |
---|
126 | if (!checkedDirectories[path]) { |
---|
127 | if (!dirExists(path)) { |
---|
128 | ensureDirectory(getFilepath(path)); |
---|
129 | try { |
---|
130 | fs.mkdirSync(path, 0755); |
---|
131 | } catch (e) { |
---|
132 | //squelch |
---|
133 | } |
---|
134 | } |
---|
135 | checkedDirectories[path]= 1; |
---|
136 | } |
---|
137 | }, |
---|
138 | |
---|
139 | ensureDirectoryByFilename= function(filename) { |
---|
140 | ensureDirectory(getFilepath(filename)); |
---|
141 | }, |
---|
142 | |
---|
143 | readAndEval= function(filename, type) { |
---|
144 | try { |
---|
145 | if (fileExists(filename)) { |
---|
146 | return eval("(" + fs.readFileSync(filename, "utf8") + ")"); |
---|
147 | } |
---|
148 | } catch (e) { |
---|
149 | bc.log("failedReadAndEval", ["filename", filename, "type", type, "error", e]); |
---|
150 | } |
---|
151 | return {}; |
---|
152 | }, |
---|
153 | |
---|
154 | maybeRead = function(filename) { |
---|
155 | try { |
---|
156 | if (fileExists(filename)) { |
---|
157 | return fs.readFileSync(filename, "utf8"); |
---|
158 | } |
---|
159 | } catch (e) { |
---|
160 | } |
---|
161 | return 0; |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | return { |
---|
166 | getFilename:getFilename, |
---|
167 | getFilepath:getFilepath, |
---|
168 | getFiletype:getFiletype, |
---|
169 | cleanupPath:cleanupPath, |
---|
170 | isAbsolutePath:isAbsolutePath, |
---|
171 | normalize:normalize, |
---|
172 | getAbsolutePath:getAbsolutePath, |
---|
173 | catPath:catPath, |
---|
174 | compactPath:compactPath, |
---|
175 | computePath:computePath, |
---|
176 | getTimestamp:getTimestamp, |
---|
177 | dirExists:dirExists, |
---|
178 | ensureDirectory:ensureDirectory, |
---|
179 | ensureDirectoryByFilename:ensureDirectoryByFilename, |
---|
180 | clearCheckedDirectoriesCache:clearCheckedDirectoriesCache, |
---|
181 | readAndEval:readAndEval, |
---|
182 | maybeRead:maybeRead, |
---|
183 | fileExists:fileExists |
---|
184 | }; |
---|
185 | }); |
---|