source: Dev/trunk/src/client/util/build/fileUtils.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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