fluentnode
Version:
Fluent apis for node (based on the concepts used in C#'s FluentSharp
553 lines (374 loc) • 23.2 kB
HTML
<html>
<head>
<title>fs.litcoffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="Array.html">
Array.litcoffee
</a>
<a class="source" href="Boolean.html">
Boolean.litcoffee
</a>
<a class="source" href="Function.html">
Function.litcoffee
</a>
<a class="source" href="Number.html">
Number.litcoffee
</a>
<a class="source" href="Object.html">
Object.litcoffee
</a>
<a class="source" href="String.html">
String.litcoffee
</a>
<a class="source" href="_register.html">
_register.js
</a>
<a class="source" href="_to_map_and_write_Tests.html">
_to_map_and_write_Tests.litcoffee
</a>
<a class="source" href="assert_Array.html">
assert_Array.litcoffee
</a>
<a class="source" href="assert_Boolean.html">
assert_Boolean.litcoffee
</a>
<a class="source" href="assert_Function.html">
assert_Function.litcoffee
</a>
<a class="source" href="assert_Number.html">
assert_Number.litcoffee
</a>
<a class="source" href="assert_Object.html">
assert_Object.litcoffee
</a>
<a class="source" href="assert_String.html">
assert_String.litcoffee
</a>
<a class="source" href="console.html">
console.litcoffee
</a>
<a class="source" href="fs.html">
fs.litcoffee
</a>
<a class="source" href="globals.html">
globals.litcoffee
</a>
<a class="source" href="http.html">
http.litcoffee
</a>
<a class="source" href="index.html">
index.md
</a>
<a class="source" href="path.html">
path.litcoffee
</a>
<a class="source" href="process.html">
process.litcoffee
</a>
<a class="source" href="Array.test.html">
Array.test.coffee
</a>
<a class="source" href="Boolean.test.html">
Boolean.test.coffee
</a>
<a class="source" href="Function.test.html">
Function.test.coffee
</a>
<a class="source" href="Number.test.html">
Number.test.coffee
</a>
<a class="source" href="Object.test.html">
Object.test.coffee
</a>
<a class="source" href="String.test.html">
String.test.coffee
</a>
<a class="source" href="Array.test.html">
Array.test.coffee
</a>
<a class="source" href="Boolean.test.html">
Boolean.test.coffee
</a>
<a class="source" href="Function.test.html">
Function.test.coffee
</a>
<a class="source" href="Number.test.html">
Number.test.coffee
</a>
<a class="source" href="Object.test.html">
Object.test.coffee
</a>
<a class="source" href="String.test.html">
String.test.coffee
</a>
<a class="source" href="console.test.html">
console.test.coffee
</a>
<a class="source" href="fs.test.html">
fs.test.coffee
</a>
<a class="source" href="globals.test.html">
globals.test.coffee
</a>
<a class="source" href="http.test.html">
http.test.coffee
</a>
<a class="source" href="path.test.html">
path.test.coffee
</a>
<a class="source" href="process.test.html">
process.test.coffee
</a>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>fs.litcoffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>dependencies</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">require</span> <span class="hljs-string">'./path'</span>
fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>)</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>@.<strong>folder_Create</strong></p>
<p>Creates a folder on @, with checks to create parent folders recusively (i.e. it will also create all parents up until it finds a valid directory)</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::folder_Create = <span class="hljs-function">-></span>
target = @.valueOf()
<span class="hljs-keyword">if</span> target.folder_Not_Exists() <span class="hljs-comment"># only do anyhing id the folder doesn't exist</span>
target.parent_Folder() <span class="hljs-comment"># check if the parent folder exists</span>
.folder_Create()
fs.mkdirSync(target) <span class="hljs-comment"># use node fs.mkdirSync to creat the folder</span>
<span class="hljs-keyword">return</span> target.realPath() <span class="hljs-comment"># return full normalized path to target folder</span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>@.<strong>folder_Delete</strong></p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::folder_Delete = <span class="hljs-function">-></span>
folder = @.toString()
<span class="hljs-keyword">if</span> fs.existsSync(folder) <span class="hljs-keyword">then</span> fs.rmdirSync(folder)
<span class="hljs-keyword">return</span> folder.not_Exists()
<span class="hljs-attribute">String</span>::folder_Delete_Recursive = <span class="hljs-function">-></span>
folder = @.toString()
<span class="hljs-keyword">if</span> folder.exists()
<span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> folder.files()
file.file_Delete()
<span class="hljs-keyword">for</span> subFolder <span class="hljs-keyword">in</span> folder.folders()
subFolder.folder_Delete_Recursive()
folder.folder_Delete()
<span class="hljs-keyword">return</span> folder.not_Exists()</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>@.<strong>file_Copy</strong> target</p>
<p>Copy <code>@</code> file to <code>target</code></p>
<p>if target is an existing folder, the file will be copied into that folder with the filename of <code>@</code></p>
<p>returns <code>target</code> path</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Copy = <span class="hljs-function"><span class="hljs-params">(target)</span>-></span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> (target)
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>
<span class="hljs-keyword">if</span> (@.valueOf().file_Not_Exists())
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
<span class="hljs-keyword">if</span> target.folder_Exists()
target = target.path_Combine(@.valueOf().file_Name())
<span class="hljs-keyword">else</span>
target.parent_Folder().folder_Create()
content = fs.readFileSync(@.valueOf());
fs.writeFileSync(target, content)
target</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>@.<strong>file_Create</strong></p>
<p>Creates and empty file at @</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Create = <span class="hljs-function">-></span>
path = @.toString()
<span class="hljs-keyword">if</span> path.not_Exists()
fs.writeFileSync(path,<span class="hljs-string">''</span>)
<span class="hljs-keyword">return</span> path.realPath();
<span class="hljs-attribute">String</span>::file_Delete = <span class="hljs-function">-></span>
file = @.toString().realPath()
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> file
fs.unlinkSync file
<span class="hljs-keyword">return</span> file.file_Not_Exists()
<span class="hljs-attribute">String</span>::file_Contents = <span class="hljs-function">-></span>
file = @.valueOf().realPath()
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> file
<span class="hljs-keyword">return</span> fs.readFileSync(file,<span class="hljs-string">"utf8"</span>)</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>@.<strong>file_Exists</strong></p>
<p>Returns true if @ is a file and it exists</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Exists = <span class="hljs-function">-></span>
<span class="hljs-keyword">return</span> fs.existsSync @.valueOf()</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>@.<strong>file_Write</strong> content</p>
<p>Writes <code>content</code> as file @</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Write = <span class="hljs-function"><span class="hljs-params">(content)</span>-></span>
content.saveAs(@.str())
@</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>@.<strong>file_Not_Exists</strong></p>
<p>Returns true if @ is not a file</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Not_Exists = <span class="hljs-function">-></span>
(fs.existsSync @.valueOf()) == <span class="hljs-literal">false</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>@.<strong>files_and_Folders</strong></p>
<p>returns a list of files and folders from the folder @</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::files_And_Folders = <span class="hljs-function">-></span>
path = @.valueOf()
<span class="hljs-keyword">try</span>
path.path_Combine(item).realPath() <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> fs.readdirSync path
<span class="hljs-keyword">catch</span>
[]
<span class="hljs-attribute">String</span>::files = <span class="hljs-function"><span class="hljs-params">(extension)</span>-></span>
files = (item <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> @.files_And_Folders() <span class="hljs-keyword">when</span> item.is_File())
<span class="hljs-keyword">if</span> extension
<span class="hljs-keyword">return</span> (file <span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> files <span class="hljs-keyword">when</span> file.file_Extension() <span class="hljs-keyword">is</span> extension)
<span class="hljs-keyword">return</span> files
<span class="hljs-attribute">String</span>::files_Recursive = <span class="hljs-function"><span class="hljs-params">(extension)</span>-></span>
files = []
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> @.str().files_And_Folders()
<span class="hljs-keyword">if</span> (item.is_Folder())
files = files.concat(item.files_Recursive(extension))
<span class="hljs-keyword">else</span>
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">not</span> extension <span class="hljs-keyword">or</span> item.file_Extension() <span class="hljs-keyword">is</span> extension)
files.push(item)
<span class="hljs-keyword">return</span> files
<span class="hljs-attribute">String</span>::folders = <span class="hljs-function">-></span> item <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> @.files_And_Folders() <span class="hljs-keyword">when</span> item.is_Folder()</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>@.<strong>is_Folder</strong></p>
<p>Returns true is @ is a folder</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::is_Folder = <span class="hljs-function">-></span>
<span class="hljs-keyword">try</span>
fs.lstatSync(@.valueOf()).isDirectory()
<span class="hljs-keyword">catch</span>
<span class="hljs-literal">false</span></pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>@.<strong>is_Not_Folder</strong></p>
<p>Returns true if @ is not a folder</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::is_Not_Folder = <span class="hljs-function">-></span>
@.is_Folder() <span class="hljs-keyword">is</span> <span class="hljs-literal">false</span>
<span class="hljs-attribute">String</span>::is_File = <span class="hljs-function">-></span> <span class="hljs-keyword">try</span> fs.lstatSync(@.valueOf()).isFile() <span class="hljs-keyword">catch</span> <span class="hljs-keyword">then</span> <span class="hljs-literal">false</span>
<span class="hljs-attribute">String</span>::realPath = <span class="hljs-function">-></span> <span class="hljs-keyword">try</span> fs.realpathSync @.valueOf() <span class="hljs-keyword">catch</span> <span class="hljs-keyword">then</span> <span class="hljs-literal">null</span>
<span class="hljs-attribute">String</span>::temp_Name_In_Folder = <span class="hljs-function">-></span> @.valueOf().realPath().path_Combine(<span class="hljs-string">"_tmp_"</span>.add_Random_String(<span class="hljs-number">10</span>))
<span class="hljs-attribute">String</span>::saveAs = <span class="hljs-function"><span class="hljs-params">(targetFile)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> <span class="hljs-keyword">if</span> targetFile <span class="hljs-keyword">is</span> <span class="hljs-literal">null</span>
contents = @.valueOf()
<span class="hljs-keyword">if</span> (targetFile.exists())
targetFile.file_Delete()
fs.writeFileSync(targetFile,contents)
<span class="hljs-keyword">return</span> targetFile.exists()</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>String::saveAs</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-attribute">String</span>::exists = <span class="hljs-attribute">String</span>::file_Exists
<span class="hljs-attribute">String</span>::create_Dir = <span class="hljs-attribute">String</span>::folder_Create
<span class="hljs-attribute">String</span>::delete_File = <span class="hljs-attribute">String</span>::file_Delete
<span class="hljs-attribute">String</span>::delete_Folder = <span class="hljs-attribute">String</span>::folder_Delete
<span class="hljs-attribute">String</span>::folder_Exists = <span class="hljs-attribute">String</span>::is_Folder
<span class="hljs-attribute">String</span>::folder_Not_Exists = <span class="hljs-attribute">String</span>::is_Not_Folder
<span class="hljs-attribute">String</span>::fullPath = <span class="hljs-attribute">String</span>::realPath
<span class="hljs-attribute">String</span>::is_Directory = <span class="hljs-attribute">String</span>::is_Folder
<span class="hljs-attribute">String</span>::touch = <span class="hljs-attribute">String</span>::file_Create
<span class="hljs-attribute">String</span>::not_Exists = <span class="hljs-attribute">String</span>::file_Not_Exists</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<hr>
</div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>back to <a href="index.html">index</a></p>
</div>
</li>
</ul>
</div>
</body>
</html>