Skip to content

Commit

Permalink
better comments and support for better automatic js variant Flyfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Bucaran committed Jul 4, 2015
1 parent 4aa2b81 commit e31e0b7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/cli/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fmt from "../fmt"
import { log } from "../util"

/**
@desc List all tasks available in the flyfile
List all tasks available in the flyfile
@param {Object} flyfile
@param {Object} opts.simple Simple task listing.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/cli/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { plugins } from "../util"
import path from "path"

/**
@desc Resolve flyfile using flypath and create a new Fly instance.
Resolve flyfile using flypath and create a new Fly instance.
@param {String} flypath Path to a flyfile
*/
export default function* (flypath) {
Expand Down
9 changes: 4 additions & 5 deletions src/fly.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ export default class Fly extends Emitter {
*/
watch (globs, tasks) {
this.notify("fly_watch").start(tasks)
_.watch(globs, { ignoreInitial: true })
.on("all", () => this.start(tasks))
_.watch(globs, { ignoreInitial: true }).on("all", () => this.start(tasks))
return this
}
/**
Expand Down Expand Up @@ -121,7 +120,7 @@ export default class Fly extends Emitter {
this._filters = []
this._writers = []

globs.forEach((pattern) => {
_.flatten(globs).forEach((pattern) => {
const base = ((base) => {
return base.length > 1 ? base.shift() : ""
}(pattern.split("*")))
Expand All @@ -134,7 +133,7 @@ export default class Fly extends Emitter {
? reduce(filters[0].filter
.apply(this, [data, filters[0].options]), filters.slice(1))
: { file, data, base }
}.call(this, `${data}`, this._filters))).catch(_=>console.log(_, "||||"))
}.call(this, `${data}`, this._filters)))
})
}))
})
Expand All @@ -155,7 +154,7 @@ export default class Fly extends Emitter {
@param {...String} destination paths
*/
target (...dest) {
return Promise.all(dest.map((dest) => {
return Promise.all(_.flatten(dest).map((dest) => {
return this.unwrap(this._source).then((files) => {
return files.map(({ file, data, base }) => {
return ((file) => {
Expand Down
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Parsec from "parsec"
import { notifyUpdates, resolve } from "./util"
import { notifyUpdates, find } from "./util"
import reporter from "./reporter"
import cli from "./cli/"
import pkg from "../package"
Expand All @@ -16,12 +16,10 @@ export default function* () {

if (help) {
cli.help()

} else if (version) {
cli.version(pkg)

} else {
const path = yield resolve({ file })
const path = yield find({ file })
if (list) {
cli.list(path, { simple: list === "simple" })
} else {
Expand Down
96 changes: 83 additions & 13 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from "mz/fs"
import fmt from "./fmt"
import glob from "glob"
import pretty from "prettyjson"
import chokidar from "chokidar"
import { join } from "path"
import { jsVariants } from "interpret"
Expand All @@ -15,6 +17,17 @@ export function error (...args) {
console.error.apply(console, args)
}

/**
*/
export function trace (e) {
error(pretty.render(e)
.replace(/(\sFunction|\sObject)\./g, `${fmt.blue("$1")}.`)
.replace(/\((~?\/.*)\)/g, `(${fmt.gray("$1")})`)
.replace(/:([0-9]*):([0-9]*)/g, ` ${fmt.yellow("$1")}:${fmt.yellow("$2")}`)
.replace(new RegExp(process.env.HOME, "g"), "~")
)
}

/**
Promisify an async function.
@param {Function} async function to promisify
Expand All @@ -27,18 +40,67 @@ export function defer (asyncFunc) {
}

/**
Resolve the Flyfile path. Check the file extension for JavaScript variants.
Resolve the Flyfile path. Check the file extension and attempt to load
every possible JavaScript variant if `file` is a directory.
@param {String} file or path to the Flyfile
@param [{String}] Flyfile variant name
@return {String} path to the Flyfile
*/
export function *resolve ({ file, name = "Flyfile" }) {
export function* find ({ file, names = ["Flyfile", "Flypath"] }) {
const root = join(process.cwd(), file)
const path = (yield fs.stat(file)).isDirectory() ? join(root, name) : root
const mod = jsVariants[`.${path.split(".").slice(1).join(".") || "js"}`]
if (Array.isArray(mod)) require(mod[0])
else if (mod) require(mod.module)
return path
return hook(require, (yield fs.stat(file)).isDirectory()
? yield resolve(match(
names.concat(names.map((name) => name.toLowerCase()))
.map((name) => join(root, name)),
Object.keys(jsVariants)
))
: root)

/**
Add require hook so that subsequent calls to require transform the
JavaScript source variant (ES7, CoffeeScript, etc.) in the fly.
@param {Function} require function to load selected module
@param {String} path to Flyfile
@return {String} path to Flyfile
@private
*/
function hook (require, path) {
const js = jsVariants[`.${path.split(".").slice(1).join(".") || "js"}`]
if (Array.isArray(js)) {
(function reduce (modules) {
if (modules.length === 0) return
try { require(modules[0].module) }
catch (_) { reduce(modules.slice(1)) }
}(js))
} else if (js) require(js.module)
return path
}

/**
Resolve to the first existing file in paths.
@param {Array:String} list of paths to search
@return {String} path of an existing file
@private
*/
function* resolve (paths) {
if (paths.length === 0) throw { code: "ENOENT" }
try {
if (yield fs.stat(paths[0])) return paths[0]
} catch (e) { return yield resolve(paths.slice(1)) }
}

/**
Match files and extensions.
@param {Array:String} List of files to match
@param {Array:String} List of extensions to match
@return {Array} Product of matched files * extensions
@private
*/
function match (files, exts) {
return files.length === 1
? exts.map((ext) => `${files[0]}${ext}`)
: match([files[0]], exts).concat(match(files.slice(1), exts))
}
}

/**
Expand Down Expand Up @@ -76,21 +138,29 @@ export function expand (pattern, handler) {
})
}

/**
Flattens a nested array recursively.
@return [[a],[b],[c]] -> [a,b,c]
*/
export function flatten (array) {
return array.reduce((flat, next) =>
flat.concat(Array.isArray(next) ? flatten(next) : next), [])
}

/**
Wrapper for chokidar.watch. Array of globs are flattened.
@param {Array:String} globs
@param {...String} tasks Tasks to run
@return {chokidar.FSWatcher}
*/
export function watch (globs, opts) {
return chokidar.watch(
(function flatten (array) {
return array.reduce((flat, next) =>
flat.concat(Array.isArray(next) ? flatten(next) : next), [])
}([globs])), opts)
return chokidar.watch(flatten([globs]), opts)
}

/** Wrapper for update-notifier */
/**
Wrapper for update-notifier.
@param {Array} options
*/
export function notifyUpdates (options) {
updateNotifier(options).notify()
}

0 comments on commit e31e0b7

Please sign in to comment.