• Stars
    star
    415
  • Rank 104,301 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

File edition helpers working on top of mem-fs (https://github.com/SBoudrias/mem-fs)

mem-fs-editor

Node.js CI NPM version Coverage Status

File edition helpers working on top of mem-fs

Usage

import { create as createMemFs } from 'mem-fs';
import editor from 'mem-fs-editor';

const store = createMemFs();
const fs = editor.create(store);

fs.write('somefile.js', 'var a = 1;');

#read(filepath, [options])

Read a file and return its contents as a string.

You can alternatively get the raw contents buffer if you pass options.raw = true.

By default, calling read() on a file path that does not exist throws error. You can, however, pass options.defaults = 'your default content' to get a default content you pass in, if you prefer to not deal with try/catch.

#readJSON(filepath, [defaults])

Read a file and parse its contents as JSON.

readJSON() internally calls read() but will not throw an error if the file path you pass in does not exist. If you pass in an optional defaults, the defaults content will be returned in case of the target file is missing, instead of undefined. (Error would still be thrown if JSON.parse failed to parse your target file.)

#write(filepath, contents)

Replace the content of a file (existing or new) with a string or a buffer.

#writeJSON(filepath, contents[, replacer [, space]])

Replace the content of a file (existing or new) with an object that is to be converted by calling JSON.stringify().

contents should usually be a JSON object, but it can technically be anything that is acceptable by JSON.stringify.

Optionally pass replacer and space as the last two arguments, as defined by JSON.stringify. spacer is used to format the output string (prettify).

Default value for space is 2, when not specified.

#append(filepath, contents, [options])

Append the new contents to the current file contents.

  • options.trimEnd (default true). Trim trailing whitespace of the current file contents.
  • options.separator (default os.EOL). Separator to insert between current and new contents.
  • options.create (default false). Create the file if doesn't exists.

#appendTpl(filepath, contents, context[, templateOptions[, [options]])

Append the new contents to the exsting filepath content and parse the new contents as an ejs template where context is the template context (the variable names available inside the template).

  • options.trimEnd (default true). Trim trailing whitespace of the current file contents.
  • options.separator (default os.EOL). Separator to insert between current and new contents.

#extendJSON(filepath, contents[, replacer [, space]])

Extend the content of an existing JSON file with the partial objects provided as argument.

Optionally take the same JSON formatting arguments than #writeJSON().

#delete(filepath, [options])

Delete a file or a directory.

filePath can also be a glob. If filePath is glob, you can optionally pass in an options.globOptions object to change its pattern matching behavior. The full list of options are being described here. The sync flag is forced to be true in globOptions.

#copy(from, to, [options], context[, templateOptions ])

Copy file(s) from the from path to the to path. When passing array, you should pass options.fromBasePath to be used to calculate the to relative path. The common directory will be detected and used as fromBasePath otherwise.

Optionally, pass an options.process function (process(contents)) returning a string or a buffer who'll become the new file content. The process function will take a single contents argument who is the copied file contents as a Buffer.

options.ignoreNoMatch can be used to silence the error thrown if no files match the from pattern. options.append can be used to append from contents to to instead of copying, when the file is already loaded in mem-fs (safe for regeneration).

from can be a glob pattern that'll be match against the file system. If that's the case, then to must be an output directory. For a globified from, you can optionally pass in an options.globOptions object to change its pattern matching behavior. The full list of options are being described here. The nodir flag is forced to be true in globOptions to ensure a vinyl object representing each matching directory is marked as deleted in the mem-fs store.

Optionally, when from is a glob pattern, pass an options.processDestinationPath function (processDestinationPath(destinationFile)) returning a string who'll become the new file name.

options.noGlob can be used to by bypass glob matching entirely. In that case, from will directly match file paths against the file system.

#copyAsync(from, to, [options], context[, templateOptions ])

Async version of copy.

copy loads from to memory and copy its contents to to. copyAsync copies directly from the disk to to, falling back to copy behavior if the file doesn't exists on disk.

Same parameters of copy (see copy() documentation for more details).

#copyTpl(from, to, context[, templateOptions [, copyOptions]])

Copy the from file and, if it is not a binary file, parse its content as an ejs template where context is the template context (the variable names available inside the template).

You can optionally pass a templateOptions object. mem-fs-editor automatically setup the filename option so you can easily use partials.

You can also optionally pass a copyOptions object (see copy() documentation for more details).

Templates syntax looks like this:

<%= value %>
<%- include('partial.ejs', { name: 'Simon' }) %>

Dir syntax looks like this:

/some/path/dir<%= value %>/...

Refer to the ejs documentation for more details.

#copyTplAsync(from, to, [options], context[, templateOptions ])

Async version of copyTpl that uses copyAsync instead of copy.

Can be used for best performance. Reduces overheads.

Same parameters of copyTpl (see copyTpl() documentation for more details).

#move(from, to, [options])

Move/rename a file from the from path to the to path.

#move internally uses #copy and #delete, so from can be a glob pattern, and you can provide options.globOptions with it.

#exists(filepath)

Returns true if a file exists. Returns false if the file is not found or deleted.

#commit([filters,] [stream,])

Persist every changes made to files in the mem-fs store to disk.

If provided, filters is an array of TransformStream to be applied on a stream of vinyl files (like gulp plugins). If provided, stream is a stream of vinyl files.

returns promise that is resolved once the files are updated on disk.

#dump([cwd,] [filter])

Dump files to compare expected result. Provide a cwd for relative path. Allows to omit temporary path. Provide a filter function or a pattern to focus on specific files. dump returns only modified (committed or not) files when no filter or a pattern is provided.

More Repositories

1

Inquirer.js

A collection of common interactive command line user interfaces.
TypeScript
19,956
star
2

AST-query

Tentative to a simple JavaScript AST modification library
JavaScript
242
star
3

gulp-istanbul

Istanbul unit test coverage plugin for gulp.
JavaScript
185
star
4

mem-fs

Simple in-memory vinyl file store.
TypeScript
123
star
5

class-extend

Backbone like `Class.extend` for Node.js
JavaScript
88
star
6

grouped-queue

In memory queue system prioritizing tasks
JavaScript
86
star
7

gruntfile-editor

An API to modify a Gruntfile.js content
JavaScript
40
star
8

readline2

Node.js Readline faรงade fixing bugs and issues in v0.8 and v0.10
JavaScript
27
star
9

gulp-exclude-gitignore

Gulp plugin to exclude file contained in .gitignore from the stream
JavaScript
27
star
10

run-async

Utility method to run function either synchronously or asynchronously using the common `this.async()` style.
JavaScript
23
star
11

require.replace

Require.js plugin to load conditionnal modules (i18n and such)
JavaScript
21
star
12

generator-jest

Add jest support to any projects
JavaScript
19
star
13

file-utils

Grunt.file utility fork to add some goodness
JavaScript
16
star
14

inquirer-npm-name

Helper function using inquirer to validate a value provided in a prompt does not exist as an npm package.
JavaScript
13
star
15

detect-conflict

Small utility library that check if a new file content can be merged safely in the on-disk existing file.
JavaScript
11
star
16

gulp-validate-jsdoc

Ensure your JsDoc covers every functions parameters.
JavaScript
7
star
17

dotfiles

My OS X dotfiles (based on necolas/dotfiles)
Shell
7
star
18

instagram-feed-slideshow

Fetch an instagram hash tag feed and displays it in a slideshow
JavaScript
4
star
19

frontend-windows-installer

Installer for most common Frontend/JavaScript work/build tools
3
star
20

Detect-Mobile-Device

PHP based mobile redirection code
2
star
21

flight-filedrop

File dropzone component (FileReader API)
JavaScript
2
star
22

bug-next-head

Demo project of a next.js bug with the Head component
JavaScript
1
star
23

generator-py-microlib

Generate python micro library boilerplate
JavaScript
1
star
24

jquery.3dish-slider

3D "ish" slider plugin for jQuery - Work in progress
JavaScript
1
star