fsify
Convert an array of objects into a persistent or temporary directory structure
Last updated 2 months ago by electerious .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install fsify 
SYNC missed versions from official npm registry.

fsify

Travis Build Status Coverage Status Dependencies Greenkeeper badge

Convert an array of objects into a persistent or temporary directory structure.

Contents

Description

fsify creates a persistent or temporary directory structure from an array of objects. It's like the opposite of the Linux and Unix tree command.

Install

npm install fsify

Usage

Structure with content

.
├── dirname
│   └── filename
└── filename
const fsify = require('fsify')()

const structure = [
	{
		type: fsify.DIRECTORY,
		name: 'dirname',
		contents: [
			{
				type: fsify.FILE,
				name: 'filename',
				contents: 'data'
			}
		]
	},
	{
		type: fsify.FILE,
		name: 'filename',
		contents: 'data'
	}
]

fsify(structure)
	.then((structure) => console.log(structure))
	.catch((err) => console.error(err))

Deeply nested structure

.
└── dirname
    └── dirname
        └── filename
const fsify = require('fsify')()

const structure = [
	{
		type: fsify.DIRECTORY,
		name: 'dirname',
		contents: [
			{
				type: fsify.DIRECTORY,
				name: 'dirname',
				contents: [
					{
						type: fsify.FILE,
						name: 'filename'
					}
				]
			}
		]
	}
]

fsify(structure)
	.then((structure) => console.log(structure))
	.catch((err) => console.error(err))

Temporary file in existing directory

dirname/
└── filename
const fsify = require('fsify')({
	cwd: 'dirname/',
	persistent: false
})

const structure = [
	{
		type: fsify.FILE,
		name: 'filename'
	}
]

fsify(structure)
	.then((structure) => console.log(structure))
	.catch((err) => console.error(err))

Structure from tree -J

tree is a Linux and Unix command that lists the contents of directories in a tree-like format. It's a helpful CLI to view the structure of your file system. The flag -J prints out an JSON representation of the tree. The output can be used in fsify.

tree -J > tree.json
const fs = require('fs')
const fsify = require('fsify')()

const structure = fs.readFileSync('tree.json', 'utf8')

fsify(structure)
	.then((structure) => console.log(structure))
	.catch((err) => console.error(err))

API

Usage

const fsify = require('fsify')()
const fsify = require('fsify')({
	cwd: process.cwd(),
	persistent: true,
	force: false
})

Parameters

  • opts {?Object} Options.
    • cwd {?String} - Custom relative or absolute path. Defaults to process.cwd().
    • persistent {?Boolean} - Keep directories and files even when the process exists. Defaults to true.
    • force {?Boolean} - Allow deleting the current working directory and outside. Defaults to false.

Returns

Instance API

Usage

const structure = [
	{
		type: fsify.FILE,
		name: 'filename'
	}
]

fsify(structure)
	.then((structure) => console.log(structure))
	.catch((err) => console.error(err))

Parameters

  • structure {?Array} Array of objects containing information about a directory or file.

Returns

  • {Promise<Array>} A promise that resolves a structure. Equal to the input structure, but parsed and with a absolute path as the name.

Structure

A structure is an array of objects that represents a directory structure. Each object must contain information about a directory or file.

The structure …

.
├── dirname
│   └── filename
└── filename

… is equal to …

[
	{
		type: fsify.DIRECTORY,
		name: 'dirname',
		contents: [
			{
				type: fsify.FILE,
				name: 'filename',
				contents: 'data'
			}
		]
	},
	{
		type: fsify.FILE,
		name: 'filename',
		contents: 'data'
	}
]

Directory

A directory must have the type of a directory and a name. It can also contain another nested structure in its contents and a mode.

{
	type: fsify.DIRECTORY,
	name: 'dirname',
	mode: 0o777,
	contents: []
}

File

A file must have the type of a file and a name. It can also contain contents (data of the file). encoding, mode and flag will be passed directly to fs.writeFile.

{
	type: fsify.FILE,
	name: 'filename',
	contents: 'data',
	encoding: 'utf8',
	mode: 0o666,
	flag: 'w'
}

Current Tags

  • 4.0.1                                ...           latest (2 months ago)

10 Versions

  • 4.0.1                                ...           2 months ago
  • 4.0.0                                ...           3 months ago
  • 3.0.0                                ...           2 years ago
  • 2.0.4                                ...           3 years ago
  • 2.0.3                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 3
Dependencies (4)
Dev Dependencies (6)

Copyright 2014 - 2017 © taobao.org |