0.3.0 • Published 7 years ago

fs-funcs v0.3.0

Weekly downloads
8
License
MIT
Repository
github
Last release
7 years ago

fs-funcs

A very limited subset of fs functions I use every day

Install

npm i fs-funcs

Package on npm

API


exec-file(file, args, options)

Execute the file

ArgumentAction
filethe executed file
argsthe list of string arguments
optionsoptional options, default to { maxBuffer: 20971520 }

args can be an Array or a String

The default maxBuffer is 20 Mo instead of 200 ko

result is an object with two properties { stdout, stderr }

The EOF chars \n or \r\n are removed from the returned strings stdout and stderr

const execfile = require('fs-funcs/exec-file')

execfile('echo', ['one', 'two']).then(result => {
  // one two
  console.log(result.stdout)
})

execfile('echo', 'abc def').then(result => {
  // abc def
  console.log(result.stdout)
})

exec(command, options)

Execute the command

ArgumentAction
commandthe executed command
optionsoptional options, default to { maxBuffer: 20971520 }

The default maxBuffer is 20 Mo instead of 200 ko

result is an object with two properties { stdout, stderr }

The EOF chars \n or \r\n are removed from the returned strings stdout and stderr

const exec = require('fs-funcs/exec')

exec('echo one two').then(result => {
  // one two
  console.log(result.stdout)
})

exist(path, nofollow)

Check if path exists

ArgumentAction
paththe tested path
nofollowoptional nofollow, default to false. If true, test the symlink and not is target
const exist = require('fs-funcs/exist')

exist(__filename).then(result => {
  // true
  console.log(result)
})

first-bytes(path, length)

Get a Buffer with the first bytes of a file

ArgumentAction
paththe file path
lengthoptional length, default to 15
const firstbytes = require('fs-funcs/first-bytes')

firstbytes('/path/to/file').then(result => {
  // <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44>
  console.log(result)
})

get-filesize(path)

Get the size of a file

ArgumentAction
paththe file path
const getfilesize = require('fs-funcs/get-filesize')

getfilesize('/path/to/file').then(result => {
  // 54318
  console.log(result)
})

is-directory(path, nothrow)

Check if path is a directory

ArgumentAction
paththe tested path
nothrowoptional nothrow, default to false. If true, resolve to false instead of throw an error
const isdirectory = require('fs-funcs/is-directory')

isdirectory('/path/to/directory').then(result => {
  // true
  console.log(result)
})

isdirectory('/path/to/file')
.then(result => {})
.catch(err => {
  // "path" argument must target a directory
  console.log(err.message)
})

isdirectory('/path/to/file', true).then(result => {
  // false
  console.log(result)
})

is-file(path, nothrow)

Check if path is a file

ArgumentAction
paththe tested path
nothrowoptional nothrow, default to false. If true, resolve to false instead of throw an error
const isfile = require('fs-funcs/is-file')

isfile('/path/to/file').then(result => {
  // true
  console.log(result)
})

isfile('/path/to/directory')
.then(result => {})
.catch(err => {
  // "path" argument must target a file
  console.log(err.message)
})

isfile('/path/to/directory', true).then(result => {
  // false
  console.log(result)
})

is-symlink(path, nothrow)

Check if path is a symlink

ArgumentAction
paththe tested path
nothrowoptional nothrow, default to false. If true, resolve to false instead of throw an error
const issymlink = require('fs-funcs/is-symlink')

issymlink('/path/to/symlink').then(result => {
  // true
  console.log(result)
})

issymlink('/path/to/file')
.then(result => {}).catch(err => {
  // "path" argument must target a symlink
  console.log(err.message)
})

issymlink('/path/to/file', true).then(result => {
  // false
  console.log(result)
})

mkdir(path, pop)

Recursively mkdir

ArgumentAction
paththe created path
popoptional pop, default to false. If true, remove the last part of the path
const mkdir = require('fs-funcs/mkdir')

mkdir('/path/to/directory').then(result => {
  // /path/to/directory
  console.log(result)
})

mkdir('/path/to/file', true).then(result => {
  // /path/to
  console.log(result)
})

read-json(path)

Read and serialize a JSON file

ArgumentAction
paththe file path
const readjson = require('fs-funcs/read-json')

readjson('/path/to/json').then(result => {
  // {a:123, b:"abc"}
  console.log(result)
})

rm(path)

Remove a path

ArgumentAction
paththe removed path
const rm = require('fs-funcs/rm')

rm('/path/to/directory').then(result => {
  // /path/to/directory
  console.log(result)
})

stat(path, nofollow)

Get some data about path

ArgumentAction
paththe tested path
nofollowoptional nofollow, default to false. If true, test the symlink and not is target

The booleans readable, writable and executable are related to the user privileges

const stat = require('fs-funcs/stat')

stat('/path/to/file').then(result => {
  /*
  {
    file: true,
    directory: false,
    symlink: false,
    path: '/path/to/file',
    dirname: '/path/to',
    basename: 'file',
    size: 123,
    readable: true,
    writable: true,
    executable: false
  }
  */
  console.log(result)
})

stat('/path/to/directory').then(result => {
  /*
  {
    file: false,
    directory: true,
    symlink: false,
    path: '/path/to/directory',
    dirname: '/path/to',
    basename: 'directory',
    size: 7,
    readable: true,
    writable: true,
    executable: true
  }
  */
  console.log(result)
})

write-json(path, data, minify)

Write a prettified JSON file. The directory tree is created if needed

ArgumentAction
paththe created path
datathe stringified data
minifyoptional minify, default to false. If true, the JSON will not be beautified
const writejson = require('fs-funcs/write-json')

writejson('/path/to/json', {a:123, b:"abc"}).then(result => {
  // {a:123, b:"abc"}
  console.log(result)
})

License

MIT