Skip to content

Commit e31e0b7

Browse files
author
Jorge Bucaran
committed
better comments and support for better automatic js variant Flyfiles
1 parent 4aa2b81 commit e31e0b7

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

src/cli/list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fmt from "../fmt"
22
import { log } from "../util"
33

44
/**
5-
@desc List all tasks available in the flyfile
5+
List all tasks available in the flyfile
66
@param {Object} flyfile
77
@param {Object} opts.simple Simple task listing.
88
*/

src/cli/spawn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { plugins } from "../util"
33
import path from "path"
44

55
/**
6-
@desc Resolve flyfile using flypath and create a new Fly instance.
6+
Resolve flyfile using flypath and create a new Fly instance.
77
@param {String} flypath Path to a flyfile
88
*/
99
export default function* (flypath) {

src/fly.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ export default class Fly extends Emitter {
7979
*/
8080
watch (globs, tasks) {
8181
this.notify("fly_watch").start(tasks)
82-
_.watch(globs, { ignoreInitial: true })
83-
.on("all", () => this.start(tasks))
82+
_.watch(globs, { ignoreInitial: true }).on("all", () => this.start(tasks))
8483
return this
8584
}
8685
/**
@@ -121,7 +120,7 @@ export default class Fly extends Emitter {
121120
this._filters = []
122121
this._writers = []
123122

124-
globs.forEach((pattern) => {
123+
_.flatten(globs).forEach((pattern) => {
125124
const base = ((base) => {
126125
return base.length > 1 ? base.shift() : ""
127126
}(pattern.split("*")))
@@ -134,7 +133,7 @@ export default class Fly extends Emitter {
134133
? reduce(filters[0].filter
135134
.apply(this, [data, filters[0].options]), filters.slice(1))
136135
: { file, data, base }
137-
}.call(this, `${data}`, this._filters))).catch(_=>console.log(_, "||||"))
136+
}.call(this, `${data}`, this._filters)))
138137
})
139138
}))
140139
})
@@ -155,7 +154,7 @@ export default class Fly extends Emitter {
155154
@param {...String} destination paths
156155
*/
157156
target (...dest) {
158-
return Promise.all(dest.map((dest) => {
157+
return Promise.all(_.flatten(dest).map((dest) => {
159158
return this.unwrap(this._source).then((files) => {
160159
return files.map(({ file, data, base }) => {
161160
return ((file) => {

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Parsec from "parsec"
2-
import { notifyUpdates, resolve } from "./util"
2+
import { notifyUpdates, find } from "./util"
33
import reporter from "./reporter"
44
import cli from "./cli/"
55
import pkg from "../package"
@@ -16,12 +16,10 @@ export default function* () {
1616

1717
if (help) {
1818
cli.help()
19-
2019
} else if (version) {
2120
cli.version(pkg)
22-
2321
} else {
24-
const path = yield resolve({ file })
22+
const path = yield find({ file })
2523
if (list) {
2624
cli.list(path, { simple: list === "simple" })
2725
} else {

src/util.js

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import fs from "mz/fs"
2+
import fmt from "./fmt"
23
import glob from "glob"
4+
import pretty from "prettyjson"
35
import chokidar from "chokidar"
46
import { join } from "path"
57
import { jsVariants } from "interpret"
@@ -15,6 +17,17 @@ export function error (...args) {
1517
console.error.apply(console, args)
1618
}
1719

20+
/**
21+
*/
22+
export function trace (e) {
23+
error(pretty.render(e)
24+
.replace(/(\sFunction|\sObject)\./g, `${fmt.blue("$1")}.`)
25+
.replace(/\((~?\/.*)\)/g, `(${fmt.gray("$1")})`)
26+
.replace(/:([0-9]*):([0-9]*)/g, ` ${fmt.yellow("$1")}:${fmt.yellow("$2")}`)
27+
.replace(new RegExp(process.env.HOME, "g"), "~")
28+
)
29+
}
30+
1831
/**
1932
Promisify an async function.
2033
@param {Function} async function to promisify
@@ -27,18 +40,67 @@ export function defer (asyncFunc) {
2740
}
2841

2942
/**
30-
Resolve the Flyfile path. Check the file extension for JavaScript variants.
43+
Resolve the Flyfile path. Check the file extension and attempt to load
44+
every possible JavaScript variant if `file` is a directory.
3145
@param {String} file or path to the Flyfile
3246
@param [{String}] Flyfile variant name
3347
@return {String} path to the Flyfile
3448
*/
35-
export function *resolve ({ file, name = "Flyfile" }) {
49+
export function* find ({ file, names = ["Flyfile", "Flypath"] }) {
3650
const root = join(process.cwd(), file)
37-
const path = (yield fs.stat(file)).isDirectory() ? join(root, name) : root
38-
const mod = jsVariants[`.${path.split(".").slice(1).join(".") || "js"}`]
39-
if (Array.isArray(mod)) require(mod[0])
40-
else if (mod) require(mod.module)
41-
return path
51+
return hook(require, (yield fs.stat(file)).isDirectory()
52+
? yield resolve(match(
53+
names.concat(names.map((name) => name.toLowerCase()))
54+
.map((name) => join(root, name)),
55+
Object.keys(jsVariants)
56+
))
57+
: root)
58+
59+
/**
60+
Add require hook so that subsequent calls to require transform the
61+
JavaScript source variant (ES7, CoffeeScript, etc.) in the fly.
62+
@param {Function} require function to load selected module
63+
@param {String} path to Flyfile
64+
@return {String} path to Flyfile
65+
@private
66+
*/
67+
function hook (require, path) {
68+
const js = jsVariants[`.${path.split(".").slice(1).join(".") || "js"}`]
69+
if (Array.isArray(js)) {
70+
(function reduce (modules) {
71+
if (modules.length === 0) return
72+
try { require(modules[0].module) }
73+
catch (_) { reduce(modules.slice(1)) }
74+
}(js))
75+
} else if (js) require(js.module)
76+
return path
77+
}
78+
79+
/**
80+
Resolve to the first existing file in paths.
81+
@param {Array:String} list of paths to search
82+
@return {String} path of an existing file
83+
@private
84+
*/
85+
function* resolve (paths) {
86+
if (paths.length === 0) throw { code: "ENOENT" }
87+
try {
88+
if (yield fs.stat(paths[0])) return paths[0]
89+
} catch (e) { return yield resolve(paths.slice(1)) }
90+
}
91+
92+
/**
93+
Match files and extensions.
94+
@param {Array:String} List of files to match
95+
@param {Array:String} List of extensions to match
96+
@return {Array} Product of matched files * extensions
97+
@private
98+
*/
99+
function match (files, exts) {
100+
return files.length === 1
101+
? exts.map((ext) => `${files[0]}${ext}`)
102+
: match([files[0]], exts).concat(match(files.slice(1), exts))
103+
}
42104
}
43105

44106
/**
@@ -76,21 +138,29 @@ export function expand (pattern, handler) {
76138
})
77139
}
78140

141+
/**
142+
Flattens a nested array recursively.
143+
@return [[a],[b],[c]] -> [a,b,c]
144+
*/
145+
export function flatten (array) {
146+
return array.reduce((flat, next) =>
147+
flat.concat(Array.isArray(next) ? flatten(next) : next), [])
148+
}
149+
79150
/**
80151
Wrapper for chokidar.watch. Array of globs are flattened.
81152
@param {Array:String} globs
82153
@param {...String} tasks Tasks to run
83154
@return {chokidar.FSWatcher}
84155
*/
85156
export function watch (globs, opts) {
86-
return chokidar.watch(
87-
(function flatten (array) {
88-
return array.reduce((flat, next) =>
89-
flat.concat(Array.isArray(next) ? flatten(next) : next), [])
90-
}([globs])), opts)
157+
return chokidar.watch(flatten([globs]), opts)
91158
}
92159

93-
/** Wrapper for update-notifier */
160+
/**
161+
Wrapper for update-notifier.
162+
@param {Array} options
163+
*/
94164
export function notifyUpdates (options) {
95165
updateNotifier(options).notify()
96166
}

0 commit comments

Comments
 (0)