file-js
Version:
Abstract representation of a pathname
120 lines (83 loc) • 5.08 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Home - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2>
</nav>
<div id="main">
<section class="readme">
<article><h1>file-js</h1><p><a href="https://travis-ci.org/nspragg/file-js"><img src="https://travis-ci.org/nspragg/file-js.svg" alt="Build Status"></a> <a href="https://coveralls.io/github/nspragg/file-js?branch=master"><img src="https://coveralls.io/repos/github/nspragg/file-js/badge.svg?branch=master" alt="Coverage Status"></a></p>
<blockquote>
<p>Abstract representation of a pathname</p>
</blockquote>
<h2>Installation</h2><pre class="prettyprint source"><code>npm install --save file-js</code></pre><h2>Common examples</h2><p>The example below lists all files in <code>myDir</code></p>
<pre class="prettyprint source lang-js"><code>const File = require('file-js');
const files = await new File('myDir').getList();
files.forEach(console.log);</code></pre><h4>Check file types</h4><pre class="prettyprint source lang-js"><code>const pathname = new File('myFile');
if (await pathname.isFile()) {
console.log(`process ${pathname}`)
}</code></pre><h4>List files for a directory</h4><p>Synchronously list files:</p>
<pre class="prettyprint source lang-js"><code>const dir = new File('myDirectory');
const files = dir.getListSync()
console.log(files.forEach(console.log));</code></pre><p>Asynchronously list files:</p>
<pre class="prettyprint source lang-js"><code>const dir = new File('myDirectory');
dir.getList().forEach(console.log);</code></pre><h4>Check permissions</h4><p>Check that a pathname has write permission:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('myFile');
if (await file.isWritable()) {
console.log(`Able to write to ${file.getName()}`);
}</code></pre><p>Check that a pathname is executable:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('myFile');
if (await file.isExecutable()) {
console.log(`Able to execute ${file.getName()}`);
}</code></pre><h4>Pathname changes and access</h4><p>Get the last time a pathname was modified:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('myFile');
const lastModified = file.lastModifiedSync();
console.log(`${file.getName()} was last modified on ${lastModified}`);</code></pre><p>Get the last time a pathname was accessed:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('myFile');
const lastAccessed = file.lastAccessedSync();
console.log(`${file.getName()} was last accessed on ${lastAccessed}`);</code></pre><h4>File size</h4><p>Check a file is less than 1k:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('myFile');
if (file.sizeSync() < 1024) {
console.log(`${file.getName()} < 1k`);
}</code></pre><h4>Recursive delete</h4><p>Deletes a folder and all of its contents:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('myDir/');
file.deleteRecursively()
.then(() => console.log('myDir/ deleted'))
.catch(console.error);</code></pre><h4>Recursive copy</h4><p>Copies a folder and all of its contents. Optionally overwriting an existing destination:</p>
<p>If <code>destinationDir/</code> already exists, you will not be able to copy:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('sourceDir/');
file.copyRecursively('destinationDir/')
.then(() => console.log('sourceDir/ copied to destinationDir/'))
.catch(console.error); // message: 'Directory: "destinationDir/" already exists.'</code></pre><p>If <code>destinationDir/</code> already exists and you want to overwrite it:</p>
<pre class="prettyprint source lang-js"><code>const file = new File('sourceDir/');
file.copyRecursively('destinationDir/', { overwrite: true })
.then(() => console.log('sourceDir/ copied to destinationDir/'))
.catch(console.error);</code></pre></article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Feb 25 2019 09:57:56 GMT+0000 (GMT) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>