Fly is a highly performant task automatation tool, much like Gulp or Grunt, but written with concurrency in mind. With Fly, everything is a coroutine, which allows for cascading and composable tasks; but unlike Gulp, it's not limited to the stream metaphor.
Fly is extremely extensible, so anything can be a task. Our core system will accept whatever you throw at it, resulting in a modular system of reusable plugins and tasks, connected by a declarative flyfile.js
that's easy to read.
Table of Contents
- [Features](#features) - [Example](#example) - [Concepts](#concepts) * [Core](#core) * [Plugins](#plugins) * [Tasks](#tasks) * [Flyfiles](#flyfiles) - [CLI](#cli) - [API](#api) * [Fly](#fly-1) * [Plugin](#plugin) * [Task](#task-1) * [Utilities](#utilities) - [Installation](#installation) - [Usage](#usage) * [Getting Started](#getting-started) * [Programmatic](#programmatic) - [Ecosystem](#ecosystem)- lightweight: with
5
dependencies, installation takes seconds - minimal api: Fly only exposes a couple methods, but they're everything you'll ever need
- performant: because of Bluebird, creating and running Tasks are quick and inexpensive
- cascading: sequential Task chains can cascade their return values, becoming the next Task's argument
- asynchronous: concurrent Task chains run without side effects & can be
yield
ed consistently - composable: chain APIs and Tasks directly; say goodbye to
pipe()
x 100! - modular: easily share or export individual Tasks or Plugins for later use
- stable: requires Node
6.x
to run (LTS is6.9
)
Here's a simple flyfile
(with shorthand generator methods) depicting a parallel chain.
const sass = "src/{admin,client}/*.sass"
const js = "src/{admin,client}/*.js"
const dist = "build"
module.exports = {
*lint(fly) {
yield fly.source(js).xo({ esnext: true })
},
*scripts(fly) {
yield fly.source(js).babel({ presets: ["es2015"] }).target(`${dist}/js`)
},
*styles(fly) {
yield fly.source(sass).sass({ outputStyle: "compressed" }).autoprefixer().target(`${dist}/css`)
},
*build(fly) {
yield fly.parallel(["lint", "scripts", "styles"])
}
}
Fly is a task runner. It's designed to get you from A
to B
-- that's it.
If it helps, imagine you're dining in a restaraunt and Fly is the food runner. Fly's role is solely to collect meals from the kitchen (fly.source
) and deliver them to the correct table (fly.target
). As a food runner, Fly may do this one plate at a time (fly.serial
) or deliver multiple plates at once (fly.parallel
). Either way, Fly only cares about going from A
to B
. It may not be the most glamarous job, but as far as you (the patron) are concerned, it's incredibly important because it brings you food.
Because Fly is single-minded and cares only about executing tasks, everything else is a plugin. This keeps development with Fly easy, approachable, and lightweight.
You see, installing Fly gives access to a reliable task runner. You decide what it can do, provide it functionality, and dictate when to do it. You're in full control.
Through plugins, you are able to capture useful behavior and share them across tasks or projects for repeated use. Plugins come in three flavors:
- external - installed via NPM; called "external" because they live outside your codebase
- inline - generally simple, one-time functions; not sensible for reuse since declared within a task (hence "inline")
- local - private, reusable plugins; appear exactly like external plugins but not public on NPM.
Tasks are used to tell Fly what to do. They are written as generator functions & converted to coroutines internally. They're also fully self-contained and, like plugins, can be shared across projects if desired.
Upon runtime, tasks are cheap to create, so are also destroyed once completed. This also helps Fly remain efficient; history won't weigh it down.
Lastly, tasks have the power to start other Tasks, including serial and parallel chains!
Much like Gulp, Fly uses a flyfile.js
(case sensitive) to read and run your Tasks. However, because it's a regular JavaScript file, you may also require()
additional modules and incorporate them directly into your Tasks, without the need for a custom Plugin!
const browserSync = require("browser-sync")
exports.serve = function * (fly) {
browserSync({
port: 3000,
server: "dist",
middleware: [
require("connect-history-api-fallback")()
]
})
yield fly.$.log("> Listening on localhost:3000")
}
Flyfiles should generally be placed in the root of your project, alongside your package.json
. Although this is not required, Fly (strongly) prefers this location.
Note: You may set an alternate directory path through the CLI's
cwd
option.
Through Node, Fly only supports ES5 syntax; however, if you prefer ES6 or ES7, just install fly-esnext
!
Fly's CLI tool is very simple and straightforward.
fly [options] <tasks>
fly --mode=parallel task1 task2 ...
Please run
fly --help
orfly -h
for usage information.
Most commonly, the CLI is used for NPM script definitions.
// package.json
{
"scripts": {
"build": "fly task1 task2"
}
}
Fly itself acts as a "parent" class to its Task
children. Because of this, Fly's methods are purely executive; aka, they manage Tasks and tell them how & when to run.
Yield: Any
Start a Task by its name; may also pass initial values. Can return anything the Task is designed to.
Type: String
Default: 'default'
The Task's name to run. Task must exist/be defined or an Error is thrown.
Important! Fly expects a
default
task if no task is specified. This also applies to CLI usage.
Type: Object
Default: {src: null, val: null}
Initial/Custom values to start with. You may customize the object shape, but only val
will be cascaded from Task to Task.
Yield: Any
Run a group of tasks simultaneously. Cascading is disabled.
Type: Array
The names of Tasks to run. Task names must be string
s and must be defined.
Type: Object
Initial values to start with; passed to each task in the group. Does not cascade.
Yield: Any
Run a group of tasks sequentially. Cascading is enabled.
Type: Array
The names of Tasks to run. Task names must be string
s and must be defined.
Type: Object
Initial values to start with; passed to each task in the group. Does cascade.
module.exports = {
*default(fly) {
yield fly.serial(["first", "second"], {val: 10})
},
*first(fly, opts) {
yield fly.$.log(`first: ${opts.val}`)
return opts.val * 4
},
*second(fly, opts) {
yield fly.$.log(`second: ${opts.val}`)
return opts.val + 2
}
}
const output = yield fly.start()
//=> first: 10
//=> second: 40
console.log(output)
//=> 42
Plugins can be external, internal, or local. However, all plugins share the same options:
Type: Boolean
Default: true
If the plugin function should iterate through every file|glob
.
Type: Boolean
Default: true
If the plugin should receive the Task's glob
patterns or its expanded file
objects. Uses globs
if false
.
Every plugin must also pass a generator function, which will be wrapped into a coroutine. This function's arguments will be the file|glob
(s), depending on the options.every
and options.files
combination. The function's second argument is the user-provided config object.
The plugin's generator function is always bound to the current Task
, which means this
refers to the Task instance.
Internal plugins are for single-use only. If you're defining the same behavior repeatedly, it should be extracted to a local or external plugin instead.
Note: Inline plugins have no need for a second argument in their generator function; you are the "user" here.
See task.run
for a simple example. The same inline example may be writen purely as an object:
exports.foo = function * (fly) {
yield fly.source("src/*.js").run({
every: false,
*func(files) {
Array.isArray(files) //=> true
yield Promise.resolve("this will run once.")
}
}).target("dist")
}
Unlike "inline" plugins, external and local plugins are defined before a Task is performed. Because of this, they must define a name
for their method to use within a Task.
Similar to inline plugins, there are two ways of defining an exported module -- via functional or object definitions.
When using a functional definition, the definition receives the Fly instance and the utilities object.
module.exports = function (fly, utils) {
// promisify before running else repeats per execution
const render = utils.promisify(function () {})
// verbose API
fly.plugin("myName", {every: false}, function * (files, opts) {
console.log("all my files: ", files) //=> Array
console.log(this._.files === files) //=> true
console.log(this instanceof Task) //=> true
console.log("user options: ", opts)
yield render(opts)
})
// or w/condensed API
fly.plugin({
name: "myName",
every: false,
*func(files, opts) {
// ...same
}
})
}
When using an object definition, you are not provided the fly
or utils
objects. This assumes that you do not need any prep work for your plugin!
module.exports = {
name: "myName",
every: false,
*func(files, opts) {
// do stuff
}
}
Then, within your Task, you may use it like so:
exports.default = function * (fly) {
yield fly.source("src/*.js").myName({ foo: "bar" }).target("dist")
}
Local plugins are defined exactly like external plugins. The only difference is that they're not installable via NPM.
In order to use a local plugin, add a fly
key to your package.json
file. Then define a requires
array with paths to your plugins.
{
"fly": {
"requires": [
"./build/custom-plugin-one.js",
"./build/custom-plugin-two.js"
]
}
}
For programmatic usage, simply pass an array of definitions to the plugins
key:
const Fly = require('fly')
const fly = new Fly({
plugins: [
require("./build/custom-plugin-one.js"),
require("./build/custom-plugin-two.js"),
{
name: "plugThree",
every: false,
files: false,
*func(globs, opts) {
// nifty, eh?
}
}
]
})
A Task receives itself as its first argument. We choose to name the parameter fly
simply because of convention; of course, you may call it whatever you'd like.
Tasks are exported from a flyfile.js
, which means you can use either syntax:
exports.foo = function * (fly) {
yield fly.source("src/*.js").target("dist/js")
}
exports.bar = function * (fly) {
yield fly.source("src/*.css").target("dist/css")
}
// or
module.exports = {
*foo(fly) {
yield fly.source("src/*.js").target("dist/js")
},
*bar(fly) {
yield fly.source("src/*.css").target("dist/css")
}
}
Each Task also receives an opts
object, consisting of src
and val
keys. Although src
is primarily used for fly-watch
, the val
key can be used or set at any time see fly.serial
.
All methods and values below are exposed within a Task's function.
Type: String
The directory wherein flyfile.js
resides, now considered the root. Also accessible within plugins.
Type: Object
The Task's utility helpers. Also accessible within plugins. See Utilities.
Type: Object
The Task's internal state, populated by task.source()
. Also accessible within plugins.
Type: Array
The Task's active files. Each object contains a dir
and base
key from its pathObject
and maintains the file's Buffer contents as a data
key.
Type: Array
The Task's glob patterns, from task.source()
. Used to populate task._.files
.
Type: Array
The Task's last-known (aka, outdated) set of glob patterns. USed only for fly-watch
.
Type: Array|String
Any valid glob pattern or array of patterns.
Type: Object
Default: {}
Additional options, passed directly to node-glob
.
Type: Array|String
The destination folder(s).
Please note that task.source()
glob ambiguity affects the destination structure.
yield fly.source("src/*.js").target("dist")
//=> dist/foo.js, dist/bar.js
yield fly.source("src/**/*.js").target("dist")
//=> dist/foo.js, dist/bar.js, dist/sub/baz.js, dist/sub/deep/bat.js
Perform an inline plugin.
Type: Object
The See plugin options.
Type: Function
The action to perform; must be a Generator
function.
exports.foo = function * (fly) {
yield fly.source("src/*.js").run({every: false}, function * (files) {
Array.isArray(files) //=> true
yield Promise.resolve("this will run once.")
}).target("dist")
}
See fly.start
.
See fly.parallel
.
See fly.serial
.
A collection of utility helpers to make life easy.
Print to console with timestamp and alert coloring.
See Bluebird.coroutine.
Print to console with timestamp and error coloring.
Yield: Array
Get all filepaths that match the glob pattern constraints.
Type: Array|String
Type: Object
Default: {}
Additional options, passed directly to node-glob
.
Yield: String|null
Find a complete filepath from a given path, or optional directory.
Type: String
The file to file; may also be a complete filepath.
Type: String
Default: '.'
The directory to look within. Will be prepended to the filename
value.
Print to console with timestamp and normal coloring.
See Bluebird.promisify.
Yield: Buffer|String|null
Get a file's contents. Ignores directory paths.
Type: String
The full filepath to read.
Type: Object
Additional options, passed to fs.readFile
.
Parse and prettify an Error's stack.
Yield: null
Write given data to a filepath. Will create directories as needed.
Type: String
The full filepath to write into.
Type: String|Buffer
The data to be written; see fs.writeFile
.
$ npm install --save-dev fly
- Install Fly & any desired plugins. (see installation and ecosystem)
- Create a
flyfile.js
next to yourpackage.json
. - Define
default
and additional tasks within yourflyfile.js
.
export.default = function * (fly) {
yield fly.parallel(["styles", "scripts"])
}
export.styles = function * (fly) {
yield fly.source("src/**/*.css").autoprefixer().target("dist/css")
}
export.scripts = function * (fly) {
yield fly.source("src/**/*.js").babel({
presets: [
["es2015", {loose: true, modules: false}]
]
})
}
- Add a
"scripts"
key to yourpackage.json
:
{
"name": "my-project",
"scripts": {
"build": "fly"
}
}
Note: The
default
task is run if no other tasks are specified.
- Run your
build
command:
$ npm run build
You may be interested in checking out a Web Starter Kit for a head start.
Fly is extremely flexible should you choose to use Fly outside of it's standard configuration.
The quickest path to a valid Fly
instance is to send a tasks
object:
const Fly = require("Fly")
const fly = new Fly({
tasks: {
*foo(f) {},
*bar(f) {}
}
})
fly.start("foo")
Passing plugins
is also achievable; applicable for external and local plugins.
const fly = new Fly({
plugins: [
require("fly-clear"),
require("fly-concat"),
require("./my-plugin")
]
})
Important: If you don't provide a
file
or atasks
object, yourplugins
will not be mounted to the instance.
You may also supply a flyfile.js
path to file
and a directory path to cwd
.
Below is a list of official plugins. You may also browse all fly-related plugins on NPM.
If you'd like to create and share a plugin for Fly, we have a Yeoman generator to speed up the process.
- fly-clear - Remove one or multiple directories
- fly-watch - Watch files & Execute specified tasks on change
- fly-babel - Babel plugin for Fly
- fly-uglify - UglifyJS2 plugin for Fly
- fly-eslint - ESLint plugin for Fly
- fly-coffee - Compile CoffeeScript with Fly
- fly-concat - Concatenate files with optional source maps.
- fly-esnext - Allow for ES6 and ES7 support throughout a Fly environment.
- fly-shell - Execute shell commands with Fly
- fly-rev - Append a unique hash to filenames
- fly-zip - ZIP compress files with Fly
- fly-gzip - Gzip plugin for Fly
- fly-precache - Generate a service worker to cache resources and make them available offline.
- fly-htmlmin - Minify HTML with Fly
- fly-autoprefixer -
- fly-mocha - Fly plugin for Mocha
- fly-xo - XO plugin for Fly
- fly-ava - Fly plugin for AVA