fQuery
file selection and processing for Node.js
Documentation is a work in progress... find fQuery at npm and GitHub.
Getting started
First require fQuery:
var fQuery = require('fquery');or if you prefer a shorter notation:
var $ = require('fquery');example usage
concat all JavaScript files under
src/, minify and save$('src/*.js').concat().uglifyjs().write('dist/all.min.js');$('src/*.js') .concat(';') .wrap('(function () {"use strict";', '}())') .uglifyjs() .write('dist/all.js');apply less, minify result and save
$('src/styles.less').less().cssmin().write('dist/styles.css');apply mustache to a HTML file
<!-- index.html --> <h1>Welcome to {{pkg.name}}</h1> <p>This is version {{pkg.version}}</p>var view = { pkg: require('./package.json') }; $('src/index.html').mustache(view).write('dist/index.html');apply mustache to all HTML files under
src/and save them todist/var view = { pkg: require('./package.json') }; $('src/*.html').mustache(view).write(function (blob) { return blob.source.replace('src/', 'dist/'); });
already included plugins
- clean-css · a well tested node.js CSS minifier
- css-condense · a CSS compressor that shows no mercy
- cssmin · the Yahoo! CSS Compressor (YUI Compressor)
- Handlebars.js · minimal templating on steroids
- includify · include file content into JavaScript files
- Jade · template engine
- JSHint · a JavaScript code quality tool
- LESS · the dynamic stylesheet language
- {{ mustache }} · logic-less templates
- UglifyJS · JavaScript minification
fQuery
A fQuery object behaves like an array of Blobs. It has a property length and
the associated indeces are referencing the Blobs. You can create fQuery objects only through the main factory
function fQuery().
A Blob stores information about a file, that might be real or virtual, with readable or binary content.
Core
fQuery()
// select files and read their content
fQuery(selector: String): fQuery
fQuery(blob: Blob): fQuery
fQuery(selector1: String, selector2: String, blob1: Blob, ...): fQuery
fQuery( [ selector1: String, selector2: String, blob1: Blob, ... ] ): fQuery
// select a specific file
fQuery('a.txt')
fQuery('folder/b.js')
fQuery('/home/user/README.txt')
// select with globstar notation
fQuery('src/css/*.less')
fQuery('src/js/**/*.js')
// multiple selections are separated by ','
fQuery('folder/*.txt, assets/**/*')
// a group of multiple selections may share a common prefix separated by a ':'
fQuery('folder/src: *.js, *.html')
// multiple groups are separated by ';'
fQuery('folder/src: *.js, *.html; other/project: *.html, *.js, **/*.txt')fQuery.uid()
// returns a unique ID fQuery.uid(): int
fQuery.plugin()
// register a plugin fQuery.plugin(module: String) fQuery.plugin(obj: Object) fQuery.plugin(fn(fQuery): Function)
fQuery.selectBlob()
fQuery.virtualBlob()
.pushStack()
// push blops onto the stack fquery.pushStack([blob1: Blob, blob2: Blob, ...]): fQuery fquery.pushStack(blob: Blob): fQuery
.end()
// pop the stack fquery.end(): fQuery
.get()
// get the nth blob, might be negative to count backwards fquery.get(idx: int): Blob fquery.get(): Array[Blob]
.each()
// iterate over all blobs fquery.each( callback(blob, idx, fquery) ): fQuery
.edit()
// iterate over clones of all blobs and push them on the stack afterwards fquery.each( callback(blob, idx, fquery) ): fQuery
.toString()
// returns a string representing this fQuery instance // optionally set the max count of displayed lines per blob and the max line length fquery.toString(): String fquery.toString(lines: int, len: int): String
.log()
// writes a string representation of this fQuery instance to stdout // optionally set the max count of displayed lines per blob and the max line length fquery.log(): fQuery fquery.log(lines: int, len: int): fQuery
Content
.content()
// returns the concatenated content of all blobs fquery.content(): String fquery.content(sep: String): String
.concat()
// concats the content of all blobs and puts it as a new blob on top of the stack fquery.concat(): fQuery fquery.concat(sep: String): fQuery
.wrap()
// wraps the content of each blob, puts the new blobs onto the stack fquery.wrap(prepend: String, append: String): fQuery
.replace()
// replaces matched parts of the content of each blob, puts the new blobs onto the stack fquery.replace([ [selection: String/RegExp, replacement: String], [.. , ..], ... ]): fQuery
Filter
.eq()
// selects the nth blob and puts it on top of the stack, idx might be negative to count backwards fquery.eq(idx: int): fQuery
.first()
// selects the first blob and puts it on top of the stack fquery.first(): fQuery
.last()
// selects the last blob and puts it on top of the stack fquery.last(): fQuery
.not()
.add()
.oldest()
.newest()
File System
!! Besides read you should be really careful with these methods since they write to your file system !!
.read()
// rereads the blob's content from the source fquery.read(): fQuery
.remove()
// deletes the source file! fquery.remove(): fQuery
.write()
// writes the blob's content to a file! fquery.write( callback(blob: Blob, idx: int, fquery: fQuery): String ): fQuery // if only one blob is selected fquery.write(target: String): fQuery
.move()
// moves the blob's source file! fquery.move( callback(blob: Blob, idx: int, fquery: fQuery): String ): fQuery // if only one blob is selected fquery.move(target: String): fQuery
.copy()
// copies the blob's source file! fquery.copy( callback(blob: Blob, idx: int, fquery: fQuery): String ): fQuery // if only one blob is selected fquery.copy(target: String): fQuery
fQuery.OVERWRITE
// to make it a bit more safe you have pass in this object // as first argument to overwrite existing files, e.g. fquery.write(fQuery.OVERWRITE, target: String): fQuery fquery.move(fQuery.OVERWRITE, target: String): fQuery fquery.copy(fQuery.OVERWRITE, target: String): fQuery
.modified()
Plugins
.less()
.cssmin()
.includify()
.uglifyjs()
.jshint()
.mustache()
Blob
Blobs are the entities fQuery is all about. If you want to manipulate the blobs itself this section will be useful.
Properties
.source
.content
.buffer
.timestamp
.stats
Methods
.isFile()
.isDirectory()
.isVirtual()
.read()
.remove()
.write()
.move()
.copy()
.toString()
.log()
License
fQuery is provided under the terms of the MIT License.