Code Monkey home page Code Monkey logo

vfile's Introduction

vfile

Build Status Coverage Status

VFile is a virtual file format used by retext (natural language) and mdast (markdown). Two processors which parse, transform, and compile text. Both need a virtual representation of files and a place to store metadata and messages. And, they work in the browser. VFile provides these requirements.

Also, VFile exposes a warning mechanism compatible with ESLints formatters, making it easy to expose stylish warnings, or export tap compliant messages.

VFile is different from (the excellent πŸ‘) vinyl in that it does not include file-system or node-only functionality. No buffers, streams, or stats. In addition, the focus on metadata and messages are useful when processing a file through a middleware pipeline.

Installation

npm:

npm install vfile

VFile is also available for bower, component, and duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed.

Table of Contents

Usage

var VFile = require('vfile');

var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt',
  'contents': 'Foo *bar* baz'
});

file.toString(); // 'Foo *bar* baz'
file.filePath(); // '~/example.txt'

file.move({'extension': 'md'});
file.filePath(); // '~/example.md'

file.warn('Something went wrong', {'line': 1, 'column': 3});
// { [~/example.md:1:3: Something went wrong]
//   name: '~/example.md:1:3',
//   file: '~/example.md',
//   reason: 'Something went wrong',
//   line: 1,
//   column: 3,
//   fatal: false }

VFiles are used by both retext and mdast.

In addition, here’s a list of useful tools:

API

VFile()

VFile objects make it easy to move files, to trigger warnings and errors, and to store supplementary metadata relating to files, all without accessing the file-system.

Example

var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt',
  'contents': 'Foo *bar* baz'
});

file === VFile(file); // true
file === new VFile(file); // true

VFile('foo') instanceof VFile; // true

Signatures

  • file = VFile(contents|options|vFile?).

Parameters

  • contents (string) β€” Contents of the file;

  • vFile (VFile) β€” Existing representation, returned without modification;

  • options (Object):

    • directory (string?, default: '') β€” Parent directory;

    • filename (string?, default: '') β€” Name, without extension;

    • extension (string?, default: '') β€” Extension(s), without initial dot;

    • contents (string?, default: '') β€” Raw value.

Returns

vFile β€” Instance.

Notes

VFile exposes an interface compatible with ESLint’s formatters. For example, to expose warnings using ESLint’s compact formatter, execute the following:

var compact = require('eslint/lib/formatters/compact');
var VFile = require('vfile');

var vFile = new VFile({
    'directory': '~',
    'filename': 'hello',
    'extension': 'txt'
});

vFile.warn('Whoops, something happened!');

console.log(compact([vFile]));

Which would yield the following:

~/hello.txt: line 0, col 0, Warning - Whoops, something happened!

1 problem

VFile#contents

string β€” Content of file.

VFile#directory

string β€” Path to parent directory.

VFile#filename

string β€” Filename. A file-path can still be generated when no filename exists.

VFile#extension

string β€” Extension. A file-path can still be generated when no extension exists.

VFile#quiet

boolean? β€” Whether an error created by VFile#fail() is returned (when truthy) or thrown (when falsey).

Ensure all messages associated with a file are handled properly when setting this to true.

VFile#messages

Array.<VFileMessage> β€” List of associated messages.

Notes

VFile#message(), and in turn VFile#warn() and VFile#fail(), return Error objects that adhere to the VFileMessage schema. Its results can populate messages.

VFile#history

Array.<String> β€” List of file-paths the file moved between.

VFile#toString()

Get the value of the file.

Example

var vFile = new VFile('Foo');
String(vFile); // 'Foo'

Signatures

  • string = vFile.toString().

Returns

string β€” Contents.

VFile#filePath()

Get the filename, with extension and directory, if applicable.

Example

var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt'
});

String(file.filePath); // ~/example.txt
file.filePath() // ~/example.txt

Signatures

  • string = vFile.filePath().

Returns

string β€” If the vFile has a filename, it will be prefixed with the directory (slashed), if applicable, and suffixed with the (dotted) extension (if applicable). Otherwise, an empty string is returned.

VFile#move(options)

Move a file by passing a new directory, filename, and extension. When these are not given, the default values are kept.

Example

var file = new VFile({
  'directory': '~',
  'filename': 'example',
  'extension': 'txt',
  'contents': 'Foo *bar* baz'
});

file.move({'directory': '/var/www'});
file.filePath(); // '/var/www/example.txt'

file.move({'extension': 'md'});
file.filePath(); // '/var/www/example.md'

Signatures

  • vFile = vFile.move(options?).

Parameters

  • options (Object):

    • directory (string, default: '') β€” Parent directory;

    • filename (string?, default: '') β€” Name, without extension;

    • extension (string, default: '') β€” Extension(s), without initial dot.

Returns

vFile β€” Context object (chainable).

VFile#namespace(key)

Access metadata.

Example

var file = new VFile('Foo');

file.namespace('foo').bar = 'baz';

console.log(file.namespace('foo').bar) // 'baz';

Parameters

  • key (string) β€” Namespace key.

Returns

Object β€” Private namespace for metadata.

VFile#message(reason, position?)

Create a message with reason at position. When an error is passed in as reason, copies the stack. This does not add a message to messages.

Example

var file = new VFile();

file.message('Something went wrong');
// { [1:1: Something went wrong]
//   name: '1:1',
//   file: '',
//   reason: 'Something went wrong',
//   line: null,
//   column: null }

Signatures

  • VFileMessage = vFile.message(err|reason, node|location|position?).

Parameters

  • err (Error) β€” Original error, whose stack and message are used;

  • reason (string) β€” Reason for message;

  • node (Node) β€” Syntax tree object;

  • location (Object) β€” Syntax tree location (found at node.position);

  • position (Object) β€” Syntax tree position (found at node.position.start or node.position.end).

Returns

VFileMessage β€” File-related message with location information.

VFile#warn(reason, position?)

Warn. Creates a non-fatal message (see VFile#message()), and adds it to the file's messages list.

Example

var file = new VFile();

file.warn('Something went wrong');
// { [1:1: Something went wrong]
//   name: '1:1',
//   file: '',
//   reason: 'Something went wrong',
//   line: null,
//   column: null,
//   fatal: false }

See

VFile#fail(reason, position?)

Fail. Creates a fatal message (see VFile#message()), sets fatal: true, adds it to the file's messages list.

If quiet is not true, throws the error.

Example

var file = new VFile();

file.fail('Something went wrong');
// 1:1: Something went wrong
//     at VFile.exception (vfile/index.js:296:11)
//     at VFile.fail (vfile/index.js:360:20)
//     at repl:1:6

file.quiet = true;
file.fail('Something went wrong');
// { [1:1: Something went wrong]
//   name: '1:1',
//   file: '',
//   reason: 'Something went wrong',
//   line: null,
//   column: null,
//   fatal: true }

See

VFile#hasFailed()

Check if a fatal message occurred making the file no longer processable.

Example

var file = new VFile();
file.quiet = true;

file.hasFailed(); // false

file.fail('Something went wrong');
file.hasFailed(); // true

Signatures

  • boolean = vFile.hasFailed().

Returns

boolean β€” true if at least one of file’s messages has a fatal property set to true.

VFileMessage

Error β€” File-related message with location information.

Properties

  • name (string) β€” (Starting) location of the message, preceded by its file-path when available, and joined by ':'. Used by the native Error#toString();

  • file (string) β€” File-path;

  • reason (string) β€” Reason for message;

  • line (number?) β€” Line of error, when available;

  • column (number?) β€” Column of error, when available;

  • stack (string?) β€” Stack of message, when available;

  • fatal (boolean?) β€” Whether the associated file is still processable.

  • location (object) β€” Full range information, when available. Has start and end properties, both set to an object with line and column, set to number?.

License

MIT Β© Titus Wormer

vfile's People

Contributors

wooorm avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.