source: Dev/branches/rest-dojo-ui/client/util/build/rhino/fs.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 1.8 KB
Line 
1define([], function() {
2        var
3                readFileSync= function(filename, encoding) {
4                        if (encoding=="utf8") {
5                                // convert node.js idiom to rhino idiom
6                                encoding= "utf-8";
7                        }
8                        return readFile(filename, encoding || "utf-8");
9                },
10
11                writeFileSync= function(filename, contents, encoding){
12                        var
13                                outFile = new java.io.File(filename),
14                                outWriter;
15                        if (encoding=="utf8") {
16                                // convert node.js idiom to java idiom
17                                encoding= "UTF-8";
18                        }
19                        if(encoding){
20                                outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile), encoding);
21                        }else{
22                                outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile));
23                        }
24
25                        var os = new java.io.BufferedWriter(outWriter);
26                        try{
27                                os.write(contents);
28                        }finally{
29                                os.close();
30                        }
31                };
32
33        return {
34                statSync: function(filename) {
35                        return new java.io.File(filename);
36                },
37
38                mkdirSync: function(filename) {
39                        var dir= new java.io.File(filename);
40                        if (!dir.exists()) {
41                                dir.mkdirs();
42                        }
43                },
44
45                readFileSync: readFileSync,
46
47                readdirSync: function(path) {
48                        // java returns the complete path with each filename in listFiles; node returns just the filename
49                        // the item+"" is necessary because item is a java object that doesn't have the substring method
50                        var l= path.length + 1;
51                        return (new java.io.File(path)).listFiles().map(function(item){ return (item+"").substring(l); });
52                },
53
54                readFile: function(filename, encoding, cb) {
55                        var result= readFileSync(filename, encoding);
56                        if (cb) {
57                                cb(0, result);
58                        }
59                },
60
61                writeFileSync:writeFileSync,
62
63                writeFile: function(filename, contents, encoding, cb) {
64                        if (arguments.length==3 && typeof encoding!="string") {
65                                cb= encoding;
66                                encoding= 0;
67                        }
68                        writeFileSync(filename, contents, encoding);
69                        if (cb) {
70                                cb(0);
71                        };
72                }
73        };
74});
Note: See TracBrowser for help on using the repository browser.