UNPKG

fluentnode

Version:

Fluent apis for node (based on the concepts used in C#'s FluentSharp

770 lines (521 loc) 31 kB
<!DOCTYPE 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 &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page"> <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="fluentnode.html"> fluentnode.coffee </a> <a class="source" href="index.html"> index.md </a> <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="console.html"> console.litcoffee </a> <a class="source" href="crypto.html"> crypto.litcoffee </a> <a class="source" href="fs.html"> fs.litcoffee </a> <a class="source" href="http.GET.html"> http.GET.litcoffee </a> <a class="source" href="http.POST.html"> http.POST.litcoffee </a> <a class="source" href="http.Server.html"> http.Server.litcoffee </a> <a class="source" href="http.html"> http.litcoffee </a> <a class="source" href="path.html"> path.litcoffee </a> <a class="source" href="process.html"> process.litcoffee </a> <a class="source" href="encoding.html"> encoding.litcoffee </a> <a class="source" href="globals.html"> globals.litcoffee </a> <a class="source" href="assert_Array.test.html"> assert_Array.test.coffee </a> <a class="source" href="assert_Boolean.test.html"> assert_Boolean.test.coffee </a> <a class="source" href="assert_Function.test.html"> assert_Function.test.coffee </a> <a class="source" href="assert_Number.test.html"> assert_Number.test.coffee </a> <a class="source" href="assert_Object.test.html"> assert_Object.test.coffee </a> <a class="source" href="assert_String.test.html"> assert_String.test.coffee </a> <a class="source" href="fluentnode.test.html"> fluentnode.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="crypto.test.html"> crypto.test.coffee </a> <a class="source" href="fs.test.html"> fs.test.coffee </a> <a class="source" href="http.GET.test.html"> http.GET.test.coffee </a> <a class="source" href="http.POST.test.html"> http.POST.test.coffee </a> <a class="source" href="http.Server.test.html"> http.Server.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> <a class="source" href="encoding.test.html"> encoding.test.coffee </a> <a class="source" href="globals.test.html"> globals.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">&#182;</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">&#182;</a> </div> <p>@.<strong>create_Parent_Folder</strong></p> <p>Creates the parent folder of the current @</p> <p>Useful when creating a file in a folder that might not exist</p> <p>Note that <code>folder_Create()</code> will create multiple parent folders (if needed)</p> <p>Returns @</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::create_Parent_Folder = <span class="hljs-function">-&gt;</span> @.valueOf().parent_Folder() .folder_Create() @</pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</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">-&gt;</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-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>@.<strong>folder_Delete</strong></p> <p>Deletes the @ folder</p> <p>twin methods: delete_Folder</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::folder_Delete = <span class="hljs-function">-&gt;</span> folder = @.toString() <span class="hljs-keyword">if</span> fs.existsSync(folder) fs.rmdirSync(folder) <span class="hljs-keyword">return</span> folder.not_Exists() <span class="hljs-attribute">String</span>::delete_Folder = <span class="hljs-attribute">String</span>::folder_Delete</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>@.<strong>folder_Delete_Recursive</strong></p> <p>Deletes the folder @ recursively (ie. all files and all folders inside it)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::folder_Delete_Recursive = <span class="hljs-function">-&gt;</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-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>@.<strong>file_Append</strong> target contents, callback</p> <p>Appends <code>contents</code> to the <code>@</code> file.</p> <p>If callback exists file_Append is async if it doesn’t it is sync</p> <p><code>@</code> value (or error) is used for the return value and callback param</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Append = <span class="hljs-function"><span class="hljs-params">(contents, callback)</span>-&gt;</span> file_Path = @.real_Path() <span class="hljs-keyword">if</span> file_Path.file_Exists() <span class="hljs-keyword">and</span> contents <span class="hljs-keyword">if</span> callback fs.appendFile file_Path, contents, <span class="hljs-function"><span class="hljs-params">(err)</span>=&gt;</span> callback err || file_Path <span class="hljs-keyword">else</span> fs.appendFileSync file_Path, contents <span class="hljs-keyword">return</span> @</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</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>-&gt;</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-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>@.<strong>file_Create</strong></p> <p>Creates an file at @ with contents</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Create = <span class="hljs-function"><span class="hljs-params">(contents)</span>-&gt;</span> path = @.toString() <span class="hljs-keyword">if</span> path.not_Exists() fs.writeFileSync(path,contents || <span class="hljs-string">''</span>) <span class="hljs-keyword">return</span> path.realPath();</pre></div></div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>@.<strong>File_Delete</strong></p> <p>Deletes @ file</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Delete = <span class="hljs-function">-&gt;</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()</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>@.<strong>file_Contents</strong></p> <p>Returns the ascii contents (utf8) of the @ file or null if there is an error loading the file (for example if the file does not exist or is a directory</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Contents = <span class="hljs-function">-&gt;</span> file = @.valueOf().realPath() <span class="hljs-keyword">try</span> <span class="hljs-keyword">return</span> fs.readFileSync(file,<span class="hljs-string">"utf8"</span>) <span class="hljs-keyword">catch</span> <span class="hljs-literal">null</span></pre></div></div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>@.<strong>file_Exists</strong></p> <p>Returns true if @ is a file and it exists</p> <p>twin methods: exists</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Exists = <span class="hljs-function">-&gt;</span> <span class="hljs-keyword">return</span> fs.existsSync @.valueOf() <span class="hljs-attribute">String</span>::exists = <span class="hljs-attribute">String</span>::file_Exists</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>@.<strong>file_Lines</strong></p> <p>Returns an array of the file contents splitted by lines (or empty if there was no content)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::file_Lines = <span class="hljs-function">-&gt;</span> file_Contents = @.file_Contents() <span class="hljs-keyword">if</span> file_Contents <span class="hljs-keyword">isnt</span> <span class="hljs-literal">null</span> file_Contents.lines() <span class="hljs-keyword">else</span> []</pre></div></div> </li> <li id="section-13"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-13">&#182;</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>-&gt;</span> content.saveAs(@.str()) @</pre></div></div> </li> <li id="section-14"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-14">&#182;</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">-&gt;</span> (fs.existsSync @.valueOf()) == <span class="hljs-literal">false</span></pre></div></div> </li> <li id="section-15"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-15">&#182;</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">-&gt;</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>-&gt;</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>-&gt;</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">-&gt;</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-16"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-16">&#182;</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">-&gt;</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-17"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-17">&#182;</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">-&gt;</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">-&gt;</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">-&gt;</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></pre></div></div> </li> <li id="section-18"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>@.<strong>temp_File</strong> contents</p> <p>Creates a file in <code>@</code> folder with a temp name and with <code>contents</code></p> <p>If <code>contents</code> is empty, the file will be created with ‘’ If <code>@</code> is not a folder, no file will be created and null is returned</p> <p>If all goes well, the retuned value is the full path to the temp file created</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::temp_File = <span class="hljs-function"><span class="hljs-params">(contents)</span>-&gt;</span> <span class="hljs-keyword">if</span> @.is_Folder() @.temp_Name_In_Folder() .file_Write(contents || <span class="hljs-string">''</span>) .valueOf() <span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span> <span class="hljs-attribute">String</span>::temp_Name_In_Folder = <span class="hljs-function">-&gt;</span> @.valueOf().realPath().path_Combine(<span class="hljs-string">"_tmp_"</span>.add_Random_String(<span class="hljs-number">10</span>))</pre></div></div> </li> <li id="section-19"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>@.<strong>save_As</strong> targetFile</p> <p>Sync saves @ into <code>targetFile</code>. If <code>targetFile</code> exists, it is deleted first, before save is done</p> <p>Returns full path to saved file</p> <p>twin method: saveAs (legacy)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::save_As = <span class="hljs-function"><span class="hljs-params">(targetFile)</span> -&gt;</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> <span class="hljs-keyword">or</span> targetFile.length &gt; <span class="hljs-number">255</span> contents = @.valueOf() <span class="hljs-keyword">if</span> (targetFile.exists()) targetFile.file_Delete() fs.writeFileSync(targetFile,contents) <span class="hljs-keyword">if</span> targetFile.real_Path().exists() <span class="hljs-keyword">return</span> targetFile.real_Path() <span class="hljs-attribute">String</span>::saveAs = <span class="hljs-attribute">String</span>::save_As</pre></div></div> </li> <li id="section-20"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Other twin methods to add to correct location</p> </div> <div class="content"><div class='highlight'><pre><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>::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>::real_Path = <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-21"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-21">&#182;</a> </div> <hr> </div> </li> <li id="section-22"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>back to <a href="index.html">index</a></p> </div> </li> </ul> </div> </body> </html>