make the fs.statSync shim throw an error (consistent with Node.js)

This commit is contained in:
Jeff Williams 2013-10-23 09:32:20 -07:00
parent e02afd570e
commit 9745685dde
5 changed files with 95 additions and 53 deletions

View File

@ -156,6 +156,7 @@ function main() {
var filter;
var i;
var info;
var isFile;
var l;
var packageDocs;
var packageJson;
@ -178,7 +179,14 @@ function main() {
env.opts = jsdoc.opts.args.parse(env.args);
confPath = env.opts.configure || path.join(__dirname, 'conf.json');
if ( !fs.statSync(confPath).isFile() && !env.opts.configure ) {
try {
isFile = fs.statSync(confPath).isFile();
}
catch(e) {
isFile = false;
}
if ( !isFile && !env.opts.configure ) {
confPath = path.join(__dirname, 'conf.json.EXAMPLE');
}

View File

@ -2,10 +2,68 @@
* Extended version of the standard `fs` module.
* @module jsdoc/fs
*/
var fs = exports.fs = require('fs');
var fs = require('fs');
var path = require('path');
var runtime = require('jsdoc/util/runtime');
var ls = exports.ls = function(dir, recurse, _allFiles, _path) {
var file;
var files;
var isFile;
// first pass
if (_path === undefined) {
_allFiles = [];
_path = [dir];
}
if (!_path.length) {
return _allFiles;
}
if (recurse === undefined) {
recurse = 1;
}
try {
isFile = fs.statSync(dir).isFile();
}
catch (e) {
isFile = false;
}
if (isFile) {
files = [dir];
}
else {
files = fs.readdirSync(dir);
}
for (var i = 0, l = files.length; i < l; i++) {
file = String(files[i]);
// skip dot files
if (file.match(/^\.[^\.\/\\]/)) {
continue;
}
if ( fs.statSync(path.join(_path.join('/'), file)).isDirectory() ) {
// it's a directory
_path.push(file);
if (_path.length - 1 < recurse) {
ls(_path.join('/'), recurse, _allFiles, _path);
}
_path.pop();
}
else {
// it's a file
_allFiles.push( path.normalize(path.join(_path.join('/'), file)) );
}
}
return _allFiles;
};
// export the VM-specific implementations of the extra methods
// TODO: document extra methods here
var extras = require( runtime.getModulePath('fs') );

View File

@ -24,16 +24,26 @@ exports.Scanner.prototype = Object.create( require('events').EventEmitter.protot
@fires sourceFileFound
*/
exports.Scanner.prototype.scan = function(searchPaths, depth, filter) {
var cwd = process.cwd(),
filePaths = [],
self = this;
var isFile;
var cwd = process.cwd();
var filePaths = [];
var self = this;
searchPaths = searchPaths || [];
depth = depth || 1;
searchPaths.forEach(function($) {
var filepath = decodeURIComponent($);
if ( fs.statSync(filepath).isFile() ) {
try {
isFile = fs.statSync(filepath).isFile();
}
catch(e) {
isFile = false;
}
if (isFile) {
filePaths.push( path.resolve(cwd, filepath) );
}
else {

View File

@ -6,6 +6,7 @@
*/
var path = require('path');
var util = require('util');
var asyncify = path._asyncify;
@ -24,6 +25,11 @@ function checkEncoding(enc, name) {
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(filename, encoding) {
encoding = checkEncoding(encoding, 'fs.readFile[Sync]');
@ -37,6 +43,10 @@ exports.exists = path.exists;
var statSync = exports.statSync = function(_path) {
var f = new java.io.File(_path);
if (!f) {
throw errorFactory(_path);
}
return {
isFile: function() {
return f.isFile();
@ -54,7 +64,7 @@ var readdirSync = exports.readdirSync = function(_path) {
dir = new java.io.File(_path);
if (!dir.directory) {
throw new Error("ENOENT, no such file or directory '" + _path + "'");
throw errorFactory(_path);
}
files = dir.list();
@ -68,49 +78,6 @@ var readdirSync = exports.readdirSync = function(_path) {
};
exports.readdir = asyncify(readdirSync);
// JSDoc extension to `fs` module
var ls = exports.ls = function(dir, recurse, _allFiles, _path) {
var files,
file;
if (typeof _path === 'undefined') { // initially
_allFiles = [];
_path = [dir];
}
if (_path.length === 0) { return _allFiles; }
if (typeof recurse === 'undefined') { recurse = 1; }
if ( statSync(dir).isFile(dir) ) {
files = [dir];
}
else {
files = readdirSync(dir);
}
for (var f = 0, lenf = files.length; f < lenf; f++) {
file = String(files[f]);
if (file.match(/^\.[^\.\/\\]/)) { continue; } // skip dot files
if ((new java.io.File(_path.join('/') + '/' + file)).list()) { // it's a directory
_path.push(file);
if (_path.length - 1 < recurse) {
ls(_path.join('/'), recurse, _allFiles, _path);
}
_path.pop();
}
else { // it's a file
_allFiles.push(
path.normalize(_path.join('/') + '/' + file)
);
}
}
return _allFiles;
};
// JSDoc extension to `fs` module
var toDir = exports.toDir = function(_path) {
var f = new java.io.File(_path);

View File

@ -409,8 +409,7 @@ exports.publish = function(taffyData, opts, tutorials) {
var extraStaticFiles = staticFileScanner.scan([filePath], 10, staticFileFilter);
extraStaticFiles.forEach(function(fileName) {
var sourcePath = fs.statSync(filePath).isDirectory() ? filePath :
path.dirname(filePath);
var sourcePath = fs.toDir(filePath);
var toDir = fs.toDir( fileName.replace(sourcePath, outdir) );
fs.mkPath(toDir);
fs.copyFileSync(fileName, toDir);