forked from jsdoc/jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.js
167 lines (135 loc) · 4.31 KB
/
fs.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
/**
* Partial Rhino shim for Node.js' `fs` module.
* @see http://nodejs.org/api/fs.html
*/
'use strict';
var path = require('path');
var util = require('util');
var asyncify = path._asyncify;
function checkEncoding(enc, name) {
// we require the `encoding` parameter for Node.js compatibility; on Node.js, if you omit the
// encoding, you get a stream instead of a string
if (!enc || typeof enc === 'function') {
throw new Error(name + ' requires an encoding on Rhino!');
}
// Node.js wants 'utf8', but Java wants 'utf-8'
if (enc === 'utf8') {
enc = 'utf-8';
}
return enc;
}
// provide an error that's consistent with Node.js
function errorFactory(filepath) {
return new Error( util.format("ENOENT, no such file or directory '%s'", filepath) );
}
exports.readFileSync = function readFileSync(filename, encoding) {
encoding = checkEncoding(encoding, 'fs.readFile[Sync]');
return readFile(filename, encoding);
};
exports.readFile = asyncify(exports.readFileSync);
// in node 0.8, path.exists() and path.existsSync() moved to the "fs" module
exports.existsSync = path.existsSync;
exports.exists = path.exists;
var statSync = exports.statSync = function statSync(_path) {
var f = new java.io.File(_path);
if (!f) {
throw errorFactory(_path);
}
return {
isFile: function isFile() {
return f.isFile();
},
isDirectory: function isDirectory() {
return f.isDirectory();
},
isSymlink: function isSymlink() {
// java.io.File resolves symlinks
return false;
}
};
};
exports.stat = asyncify(statSync);
// java.io.File resolves symlinks, so we can alias `lstat` to `stat`
var lstatSync = exports.lstatSync = statSync;
exports.lstat = asyncify(lstatSync);
var readdirSync = exports.readdirSync = function readdirSync(_path) {
var dir;
var files;
dir = new java.io.File(_path);
if (!dir.directory) {
throw errorFactory(_path);
}
files = dir.list()
.map(function(fileName) {
return String(fileName);
});
return files;
};
exports.readdir = asyncify(readdirSync);
// JSDoc extension to `fs` module
var toDir = exports.toDir = function toDir(_path) {
var f;
_path = path.normalize(_path);
f = new java.io.File( path.resolve(global.env.pwd, _path) );
if ( f.isDirectory() ){
return _path;
} else {
return path.dirname(_path);
}
};
var mkdirSync = exports.mkdirSync = function mkdirSync(_path) {
var dir_path = toDir(_path);
( new java.io.File(dir_path) ).mkdir();
};
exports.mkdir = asyncify(mkdirSync);
// JSDoc extension to `fs` module
exports.mkPath = function mkPath(_path) {
if ( Array.isArray(_path) ) {
_path = _path.join('');
}
( new java.io.File(path.resolve(global.env.pwd, _path)) ).mkdirs();
};
// JSDoc extension to `fs` module
exports.copyFileSync = function copyFileSync(inFile, outDir, fileName) {
if (fileName === undefined || fileName === null) {
fileName = path.basename(inFile);
}
outDir = toDir(outDir);
inFile = new java.io.File(inFile);
var outFile = new java.io.File(outDir + '/' + fileName);
var bis = new java.io.BufferedInputStream(new java.io.FileInputStream(inFile), 4096);
var bos = new java.io.BufferedOutputStream(new java.io.FileOutputStream(outFile), 4096);
var theChar = bis.read();
while (theChar !== -1) {
bos.write(theChar);
theChar = bis.read();
}
bos.close();
bis.close();
};
exports.copyFile = asyncify(exports.copyFileSync);
exports.writeFileSync = function writeFileSync(filename, data, encoding) {
encoding = checkEncoding(encoding, 'fs.writeFile[Sync]');
var out = new java.io.PrintWriter(
new java.io.OutputStreamWriter(
new java.io.FileOutputStream(filename),
encoding
)
);
try {
out.write(data);
}
finally {
out.flush();
out.close();
}
};
exports.writeFile = asyncify(exports.writeFileSync);
exports.rmdirSync = function rmdirSync(_path) {
throw new Error('not implemented');
};
exports.rmdir = asyncify(exports.rmdirSync);
exports.unlinkSync = function unlinkSync(_path) {
throw new Error('not implemented');
};
exports.unlink = asyncify(exports.unlinkSync);