1 | define([], 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 | // the item+"" is necessary because item is a java object that doesn't have the substring method |
---|
49 | return (new java.io.File(path)).listFiles().map(function(item){ return (item.name+""); }); |
---|
50 | }, |
---|
51 | |
---|
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 | }); |
---|