ack-path
v1.8.0
Published
Robust operating system directory functionality
Downloads
336
Readme
Robust operating system directory functionality
- Install
- import
- CLI Commands
- Directory Functionality
- String Manipulations
- Directory Sync Examples
- File Functionality
Install
npm install ack-path
Import
How to import this package
var Path = require('ack-path')(__dirname)
console.log( Path.path === __dirname )//true test
//Now, you can do a whole lot more! Continue reading...
CLI Commands
Timesaver script commands
How to Use
Using the most basic command as an example, you can invoke the copy command, using any of the following methods:
package.json script example
"scripts":{
"copy": "ack-path copy ./relativeFrom ./relativeTo"
}
from command terminal example
./node_modules/bin/ack-path copy ./relativeFrom ./relativeTo
Copy
ack-path copy ./relativeFrom ./relativeTo
Move
ack-path move ./relativeFrom ./relativeTo
Delete
ack-path delete ./relativePath
Directory Functionality
.join()
write file promise
Path.join('file-name.js').writeFile(string).then().catch()
.param()
Create directory if not existant. Does not take into condsideration if path is actually a file (file path will be created as a folder)
Path.param().then()
.paramDir()
Create directory if not existant. Takes condsideration if path is actually a file and only creates folder pathing
Returns promise with context of this Path
Path.paramDir().then()
.copyTo()
Path.copyTo(__dirname+'-copy').then()
.moveTo()
move entire directory or single file
Path.moveTo( newPath:string, overwrite:boolean ).then()
Path.moveTo(__dirname+'-moved').then()
.rename()
in-place rename directory or single file
Path.rename( newName:string, overwrite:boolean ).then()
Path.rename('new-item-name', true).then()
.delete()
path delete promise
Path.delete().then()
.each()
file and folder looper
- Based on options, you can recursively read directories and/or files. returns promise
- Runs using npm package readdir. See npm readdir for more usage instructions.
- Arguments
- eachCall function(String:path, Number:index)
- options
- recursive : true
- INCLUDE_DIRECTORIES : true
- INCLUDE_HIDDEN : true
- filter : ['/.js','/.jade']
- excludeByName : name=>yesNo
Path.each( itemStringPath=>itemStringPath ).then( itemPathArray=>console.log(itemPathArray) )
.eachFilePath()
Loop folder to fire callback for each file found. Only produces file results. see eachPath function
Path.eachFilePath( fileStringPath=>fileStringPath ).then( filePathArray=>console.log(filePathArray) )
recur
Recursively loop folder to fire callback for each item found. See eachPath function
Path.recur( ItemPath=>ItemPath.path ).then( pathStringArray=>console.log(pathStringArray) )
.recurFiles()
Recursively loop folder to fire callback for each file found. See eachPath function
Path.recurFiles( filePath=>console.log('file', filePath) )
String Manipulations
var PathTest = require('ack-path')('/test/file.js')
PathTest.removeExt().path == "/test/file"
PathTest.removeFile().path == "/test/"
isDirectory
hard-checks file system if item is a folder
require('ack-path')('/test/file.js').isDirectory().then(res=>res==false)
isFile
hard-checks file system if item is a file
require('ack-path')('/test/file.js').isFile().then(res=>res==true)
isLikeFile
Checks string for a file extension
require('ack-path')('/test/file.js').isLikeFile() == true
getLastName
Returns item after last slash
require('ack-path')('/test/file.js').getLastName() == 'file.js'
require('ack-path')('/test/folder/').getLastName() == 'folder'
SYNC Examples
.sync().dirExists()
considers if path is actually a file
PathSync.dirExists()
.sync().exists()
PathSync.exists()
.sync().delete()
PathSync.delete()
.sync().copyTo()
PathSync.copyTo()
.sync().moveTo()
PathSync.copyTo()
File Functionality
A more file specific set of objective functionality
require.file
var File = require('ack-path')(__dirname).file('file-name.js')
var filePath = File.path
.file().copyTo()
File.copyTo(__filename+'.copy').then()
.file().moveTo()
File.moveTo(__filename+'.newname').then()
.file().delete()
File.delete().then()
.file().getMimeType()
File.getMimeType()//Ex: application/javascript
.file().stat()
File.stat().then(stats=>stats.size)
.file().write()
File.write(string).then()
.file().param()
just like write but if file already exists, no error will be thrown
File.param(string).then()
.file().append()
File.append(string).then()
.file().readJson()
File.readJson().then()