Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

580 lines (474 sloc) 16.957 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>lib&#x2F;files.js - YUIDoc</title>
<link rel="stylesheet" href="http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.8.0&#x2F;build&#x2F;cssgrids&#x2F;cssgrids-min.css">
<link rel="stylesheet" href="..&#x2F;assets/vendor/prettify/prettify-min.css">
<link rel="stylesheet" href="..&#x2F;assets/css/main.css" id="site_styles">
<link rel="shortcut icon" type="image/png" href="..&#x2F;assets/favicon.png">
<script src="http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;combo?3.8.0&#x2F;build&#x2F;yui&#x2F;yui-min.js"></script>
</head>
<body class="yui3-skin-sam">
<div id="doc">
<div id="hd" class="yui3-g header">
<div class="yui3-u-3-4">
<h1><img src="http:&#x2F;&#x2F;yuilibrary.com&#x2F;img&#x2F;yui-logo.png" title="YUIDoc"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 0.3.36</em>
</div>
</div>
<div id="bd" class="yui3-g">
<div class="yui3-u-1-4">
<div id="docs-sidebar" class="sidebar apidocs">
<div id="api-list">
<h2 class="off-left">APIs</h2>
<div id="api-tabview" class="tabview">
<ul class="tabs">
<li><a href="#api-classes">Classes</a></li>
<li><a href="#api-modules">Modules</a></li>
</ul>
<div id="api-tabview-filter">
<input type="search" id="api-filter" placeholder="Type to filter APIs">
</div>
<div id="api-tabview-panel">
<ul id="api-classes" class="apis classes">
<li><a href="..&#x2F;classes/CLI.html">CLI</a></li>
<li><a href="..&#x2F;classes/DocBuilder.html">DocBuilder</a></li>
<li><a href="..&#x2F;classes/DocParser.html">DocParser</a></li>
<li><a href="..&#x2F;classes/DocView.html">DocView</a></li>
<li><a href="..&#x2F;classes/Files.html">Files</a></li>
<li><a href="..&#x2F;classes/Help.html">Help</a></li>
<li><a href="..&#x2F;classes/Main.html">Main</a></li>
<li><a href="..&#x2F;classes/Options.html">Options</a></li>
<li><a href="..&#x2F;classes/Server.html">Server</a></li>
<li><a href="..&#x2F;classes/Utils.html">Utils</a></li>
<li><a href="..&#x2F;classes/YUIDoc.html">YUIDoc</a></li>
</ul>
<ul id="api-modules" class="apis modules">
<li><a href="..&#x2F;modules/yuidoc.html">yuidoc</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="yui3-u-3-4">
<div id="api-options">
Show:
<label for="api-show-inherited">
<input type="checkbox" id="api-show-inherited" checked>
Inherited
</label>
<label for="api-show-protected">
<input type="checkbox" id="api-show-protected">
Protected
</label>
<label for="api-show-private">
<input type="checkbox" id="api-show-private">
Private
</label>
<label for="api-show-deprecated">
<input type="checkbox" id="api-show-deprecated">
Deprecated
</label>
</div>
<div class="apidocs">
<div id="docs-main">
<div class="content">
<h1 class="file-heading">File: lib&#x2F;files.js</h1>
<div class="file">
<pre class="code prettyprint linenums">
YUI.add(&#x27;files&#x27;, function(Y) {
&#x2F;**
* Ported fileutils methods from [Selleck](http:&#x2F;&#x2F;github.com&#x2F;rgrove&#x2F;selleck)
* @class Files
* @module yuidoc
*&#x2F;
Y.Files = {};
&#x2F;*
Selleck
Copyright (c) 2011 Yahoo! Inc.
Licensed under the BSD License.
*&#x2F;
var fs = require(&#x27;graceful-fs&#x27;);
fsPath = require(&#x27;path&#x27;),
nodeUtil = require(&#x27;util&#x27;),
useFS = (fs.exists) ? fs : fsPath;
var exists = function(file, cb) {
if (cb) {
useFS.exists(file, cb);
} else {
return useFS.existsSync(file);
}
};
Y.Files.exists = exists;
&#x2F;**
* Copy a directory from one location to another
* @method copyDirectory
* @param {Path} source The source directory
* @param {Path} dest The destination directory
* @param {Boolean} [overwrite=false] Whether or not to overwrite destination files
if they already exist.
* @param {Function} callback The callback to be executed when complete.
**&#x2F;
function copyDirectory(source, dest, overwrite, callback) {
&#x2F;&#x2F; Allow callback as third arg.
if (typeof overwrite === &#x27;function&#x27;) {
callback = overwrite;
overwrite = null;
}
fs.stat(source, afterSourceStat);
function afterSourceStat(err, stats) {
if (err) { return callback(err); }
if (!stats.isDirectory()) {
return callback(new Error(&quot;Source is not a directory: &quot; + source));
}
fs.lstat(dest, afterDestStat);
}
function afterDestStat(err, stats) {
if (err &amp;&amp; err.code !== &#x27;ENOENT&#x27;) { return callback(err); }
if (stats) {
&#x2F;&#x2F; If the destination is a file or a link, either delete it or
&#x2F;&#x2F; bubble an error if overwrite isn&#x27;t true.
if (stats.isFile() || stats.isSymbolicLink()) {
if (overwrite) {
deletePath(dest); &#x2F;&#x2F; TODO: make this async
} else {
callback(new Error(&quot;Destination already exists: &quot; + dest));
return;
}
}
afterMkDir();
} else {
fs.mkdir(dest, 0755, afterMkDir);
}
}
function afterMkDir(err) {
if (err &amp;&amp; err.code !== &#x27;EEXIST&#x27;) { return callback(err); }
fs.readdir(source, afterReadDir);
}
function afterReadDir(err, files) {
if (err) { return callback(err); }
var pending = files.length,
filename;
if (!pending) { return callback(); }
while ((filename = files.shift())) {
copyPath(fsPath.join(source, filename), fsPath.join(dest, filename), overwrite, function (err) {
if (err) { return callback(err); }
pending -= 1;
if (!pending) {
callback();
}
});
}
}
}
Y.Files.copyDirectory = copyDirectory;
&#x2F;**
* Copy a file from one location to another
* @method copyFile
* @param {Path} source The source file
* @param {Path} dest The destination file
* @param {Boolean} [overwrite=false] Whether or not to overwrite destination files
if they already exist.
* @param {Callback} callback The callback to be executed when complete.
* @param {Error} callback.err The Error returned from Node
**&#x2F;
function copyFile(source, dest, overwrite, callback) {
&#x2F;&#x2F; Allow callback as third arg.
if (typeof overwrite === &#x27;function&#x27;) {
callback = overwrite;
overwrite = null;
}
fs.lstat(source, function (err, sourceStats) {
if (err) { return callback(err); }
if (!sourceStats.isFile()) {
return callback(new Error(&quot;Source is not a file: &quot; + source));
}
fs.lstat(dest, function (err, destStats) {
if (err &amp;&amp; err.code !== &#x27;ENOENT&#x27;) { return callback(err); }
if (destStats) {
if (overwrite) {
deletePath(dest); &#x2F;&#x2F; TODO: make this async
} else {
callback(new Error(&quot;Destination already exists: &quot; + dest));
return;
}
}
nodeUtil.pump(fs.createReadStream(source),
fs.createWriteStream(dest, {mode: 0655}), callback);
});
});
}
Y.Files.copyFile = copyFile;
&#x2F;**
If _source_ is a file, copies it to _dest_. If it&#x27;s a directory, recursively
copies it and all files and directories it contains to _dest_.
Note that when attempting to copy a file into a directory, you should specify
the full path to the new file (including the new filename). Otherwise, it will
be interpreted as an attempt to copy the _source_ file *over* the _dest_
directory instead of *into* it.
Known issues:
- Doesn&#x27;t preserve ownership or permissions on copied files&#x2F;directories.
@method copyPath
@param {String} source Source path.
@param {String} dest Destination path.
@param {Boolean} [overwrite=false] Whether or not to overwrite destination files
if they already exist.
@param {Callback} callback The callback to execute when completed.
@param {Error} callback.err
**&#x2F;
function copyPath(source, dest, overwrite, callback) {
var destStats = statSync(dest),
sourceStats = statSync(source);
&#x2F;&#x2F; Allow callback as third arg.
if (typeof overwrite === &#x27;function&#x27;) {
callback = overwrite;
overwrite = null;
}
if (!sourceStats) {
callback(new Error(&quot;Source not found: &quot; + source));
return;
}
if (sourceStats.isFile()) {
copyFile(source, dest, overwrite, callback);
} else if (sourceStats.isDirectory()) {
copyDirectory(source, dest, overwrite, callback);
} else {
callback(new Error(&quot;Source is neither a file nor a directory: &quot; + source));
}
}
Y.Files.copyPath = copyPath;
&#x2F;&#x2F; TODO: copySymbolicLink()?
&#x2F;**
If _path_ is a file, deletes it. If _path_ is a directory, recursively deletes
it and all files and directories it contains.
This method is synchronous.
@method deletePath
@param {String} path File or directory to delete.
**&#x2F;
function deletePath(path) {
var stats = fs.lstatSync(path);
if (stats.isFile() || stats.isSymbolicLink()) {
fs.unlinkSync(path);
} else if (stats.isDirectory()) {
fs.readdirSync(path).forEach(function (filename) {
deletePath(fsPath.join(path, filename));
});
fs.rmdirSync(path);
}
}
Y.Files.deletePath = deletePath;
&#x2F;**
Check to see if this is a directory
@method isDirectory
@param {Path} path The path to check
@param {Boolean} [link=false] Also validate a symlink
@return {Boolean} True if it is a directory
**&#x2F;
function isDirectory(path, link) {
var i = false;
link = (link === false) ? false : true;
try {
var stat = fs.lstatSync(path);
if (stat) {
if (stat.isSymbolicLink() &amp;&amp; link) {
stat = fs.statSync(path);
}
i = stat.isDirectory();
}
} catch (e) {
i = false;
}
return i;
};
Y.Files.isDirectory = isDirectory;
&#x2F;**
Check to see if this is a File
@method isFile
@param {Path} path The path to check
@param {Boolean} [link=false] Also validate a symlink
@return {Boolean} True if it is a file
**&#x2F;
function isFile(path, link) {
var i = false;
try {
var stat = fs.lstatSync(path);
if (stat) {
if (stat.isSymbolicLink() &amp;&amp; link) {
stat = fs.statSync(path);
}
i = stat.isFile();
}
} catch (e) {
i = false;
}
return i;
}
Y.Files.isFile = isFile;
&#x2F;**
Check to see if this is a SymLink
@method isSymbolicLink
@param {Path} path The path to check
@return {Boolean} True if it is a link
**&#x2F;
function isSymbolicLink(path) {
var stats = lstatSync(path);
return stats ? stats.isSymbolicLink() : false;
}
Y.Files.isSymbolicLink = isSymbolicLink;
&#x2F;**
Like &#x60;fs.lstatSync()&#x60;, but returns &#x60;null&#x60; instead of throwing when _path_
doesn&#x27;t exist. Will still throw on other types of errors.
@method lstatSync
@param {String} path Path to stat.
@return {fs.Stats|null} &#x60;fs.Stats&#x60; object, or &#x60;null&#x60; if _path_ doesn&#x27;t exist.
**&#x2F;
function lstatSync(path) {
try {
return fs.lstatSync(path);
} catch (ex) {
if (ex.code === &#x27;ENOENT&#x27;) {
return null;
}
throw ex;
}
}
Y.Files.lstatSync = lstatSync;
&#x2F;**
Like &#x60;fs.statSync()&#x60;, but returns &#x60;null&#x60; instead of throwing when _path_
doesn&#x27;t exist. Will still throw on other types of errors.
@method statSync
@param {String} path Path to stat.
@return {fs.Stats|null} &#x60;fs.Stats&#x60; object, or &#x60;null&#x60; if _path_ doesn&#x27;t exist.
**&#x2F;
function statSync(path) {
try {
return fs.statSync(path);
} catch (ex) {
if (ex.code === &#x27;ENOENT&#x27;) {
return null;
}
throw ex;
}
}
Y.Files.statSync = statSync;
&#x2F;**
Copy the theme assets directory
@method copyAssets
@param {Path} from The source directory
@param {Path} dest The destination directory
@param {Boolean} deleteFirst Should the directory be deleted if it exists
@param {Function} callback The callback to be executed
*&#x2F;
function copyAssets() {
var args = Array.prototype.slice.call(arguments),
callback = args.pop(),
from = args.shift(),
to = args.shift(),
deleteFirst = args.shift();
if (from[0] === from[1]) {
if (isDirectory(from[0])) {
if (deleteFirst &amp;&amp; isDirectory(to)) {
deletePath(to);
}
copyPath(from[0], to, true, callback);
} else {
callback();
}
} else {
if (isDirectory(from[0])) {
if (deleteFirst &amp;&amp; isDirectory(to)) {
deletePath(to);
}
copyPath(from[0], to, true, function() {
if (isDirectory(from[1])) {
copyPath(from[1], to, true, callback)
} else {
callback();
}
});
} else {
callback();
}
}
}
Y.Files.copyAssets = copyAssets;
&#x2F;**
* Helper method for getting JSON data from a local file
* @method getJSON
* @param {Path} filename The filename to parse JSON from
* @return {Object} The JSON data
*&#x2F;
Y.Files.getJSON = function(filename) {
var data = {};
if (exists(filename)) {
data = JSON.parse(fs.readFileSync(filename, &#x27;utf8&#x27;));
};
return data;
};
&#x2F;**
* Helper method for writing files to disk. It wraps the NodeJS file API
* @method writeFile
* @param {Path} file The filename to write to
* @param {String} data The data to write
* @param {Callback} callback*
*&#x2F;
var writeFileTimer = 100,
readFileTimer = 100;
var writeFile = function(file, data, cb) {
var flags = {
flags: &quot;w&quot;, encoding: Y.charset, mode: 0644
}
var args = arguments;
if (cb) {
fs.writeFile(file, data, flags, function(err) {
if (err &amp;&amp; err.message.match(&#x2F;^EMFILE, Too many open files&#x2F;)) {
Y.log(&#x27;Writefile failed, too many open files (&#x27; + args[0] + &#x27;). Trying again.&#x27;, &#x27;warn&#x27;, &#x27;files&#x27;);
writeFileTimer++;
Y.later(writeFileTimer, Y, writeFile, args);
return;
}
cb();
});
} else {
var out = fs.createWriteStream(file, flags);
out.write(data);
out.end();
}
};
Y.Files.writeFile = writeFile;
var readFile = function(file, enc, cb) {
var args = arguments;
fs.readFile(file, enc, function(err, data) {
if (err &amp;&amp; err.message.match(&#x2F;^EMFILE, Too many open files&#x2F;)) {
Y.log(&#x27;Readfile failed, too many open files (&#x27; + args[0] + &#x27;). Trying again.&#x27;, &#x27;warn&#x27;, &#x27;files&#x27;);
readFileTimer++;
Y.later(readFileTimer, Y, readFile, args);
return;
}
cb(err, data);
});
};
Y.Files.readFile = readFile;
});
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="..&#x2F;assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="..&#x2F;assets/js/yui-prettify.js"></script>
<script src="..&#x2F;assets/../api.js"></script>
<script src="..&#x2F;assets/js/api-filter.js"></script>
<script src="..&#x2F;assets/js/api-list.js"></script>
<script src="..&#x2F;assets/js/api-search.js"></script>
<script src="..&#x2F;assets/js/apidocs.js"></script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.