Skip to content

Commit

Permalink
refactor: Avoid reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Dec 20, 2017
1 parent d7bcee4 commit bce6603
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 31 deletions.
51 changes: 20 additions & 31 deletions src/resolve-hookfiles.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,42 +8,31 @@ basename = if process.platform is 'win32' then path.win32.basename else path.bas


# Expand hookfiles - sort files alphabetically and resolve their paths # Expand hookfiles - sort files alphabetically and resolve their paths
resolveHookfiles = (hookfiles, cwd = null) -> resolveHookfiles = (hookfiles, cwd = null) ->
return [] if not hookfiles or not hookfiles.length
cwd ?= process.cwd() cwd ?= process.cwd()


return hookfiles.reduce((result, unresolvedPath) -> resolvedPathsArrays = hookfiles.map((hookfile) ->
# glob.sync does not resolve paths, only glob patterns # glob.sync does not resolve paths, only glob patterns

if glob.hasMagic(hookfile)
if glob.hasMagic(unresolvedPath) resolvedPaths = glob.sync(hookfile, {cwd}).map((p) -> path.resolve(cwd, p))
unresolvedPaths = glob.sync(unresolvedPath, {cwd})
else else
p = path.resolve(cwd, unresolvedPath) resolvedPath = path.resolve(cwd, hookfile)
if fs.existsSync(p) resolvedPaths = if fs.existsSync(resolvedPath) then [resolvedPath] else []
unresolvedPaths = [p]
else unless resolvedPaths.length
unresolvedPaths = [] throw new Error("Could not find any hook file(s) on path: '#{hookfile}'")


if unresolvedPaths.length == 0 return resolvedPaths
throw new Error("Hook file(s) not found on path: #{unresolvedPath}") )

resolvedPaths = Array.concat.apply([], resolvedPathsArrays)
# Gradually append sorted and resolved paths resolvedPaths = resolvedPaths.sort((p1, p2) ->
result.concat unresolvedPaths [p1, p2] = [basename(p1), basename(p2)]
# Create a filename / filepath map for easier sorting switch
# Example: when p1 < p2 then -1
# [ when p1 > p2 then 1
# { basename: 'filename1.coffee', path: './path/to/filename1.coffee' } else 0
# { basename: 'filename2.coffee', path: './path/to/filename2.coffee' }
# ]
.map((filepath) -> basename: basename(filepath), path: filepath)
# Sort 'em up
.sort((a, b) -> switch
when a.basename < b.basename then -1
when a.basename > b.basename then 1
else 0
)
# Resolve paths to absolute form. Take into account current working dir
.map((item) -> path.resolve(cwd, item.path))
, [] # Start with empty result
) )
return resolvedPaths




module.exports = resolveHookfiles module.exports = resolveHookfiles
111 changes: 111 additions & 0 deletions test/unit/resolve-hookfiles-test.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,111 @@
path = require('path')
{assert} = require('chai')

resolveHookfiles = require('../../src/resolve-hookfiles')


describe('resolveHookfiles()', ->
cwd = path.join(__filename, '..', '..', 'fixtures')

describe('when given no paths', ->
it('produces no results', ->
paths = resolveHookfiles([], cwd = cwd)
assert.deepEqual(paths, [])
)
)

describe('when given existing absolute filenames', ->
it('resolves them into absolute paths', ->
hookfiles = [
path.join(cwd, 'hooks.js'),
path.join(cwd, 'non-js-hooks.rb'),
]
paths = resolveHookfiles(hookfiles, cwd = cwd)
assert.deepEqual(paths, hookfiles)
)
)

describe('when given existing relative filenames', ->
it('resolves them into absolute paths', ->
paths = resolveHookfiles(['./hooks.js', './non-js-hooks.rb'], cwd = cwd)
assert.deepEqual(paths, [
path.join(cwd, 'hooks.js'),
path.join(cwd, 'non-js-hooks.rb'),
])
)
)

describe('when given non-existing filenames', ->
it('throws an error', ->
assert.throws( ->
resolveHookfiles(['./hooks.js', './foo/bar/42'], cwd = cwd)
, './foo/bar/42')
)
)

describe('when given glob pattern resolving to existing files', ->
it('resolves them into absolute paths', ->
paths = resolveHookfiles(['./**/hooks.js'], cwd = cwd)
assert.deepEqual(paths, [
path.join(cwd, 'hooks.js'),
])
)
)

describe('when given glob pattern resolving to no files', ->
it('throws an error', ->
assert.throws( ->
resolveHookfiles(['./**/hooks.js', './**/foo/bar/foobar.js'], cwd = cwd)
, './**/foo/bar/foobar.js')
)
)

describe('when given both globs and filenames', ->
it('resolves them into absolute paths', ->
paths = resolveHookfiles(['./non-js-hooks.rb', './**/hooks.js'], cwd = cwd)
assert.deepEqual(paths, [
path.join(cwd, 'hooks.js'),
path.join(cwd, 'non-js-hooks.rb'),
])
)

it('throws an error on non-existing filenams', ->
assert.throws( ->
resolveHookfiles(['./**/hooks.js', './foo/bar/42'], cwd = cwd)
, './foo/bar/42')
)

it('throws an error on globs resolving to no files', ->
assert.throws( ->
resolveHookfiles(['./hooks.js', './**/foo/bar/foobar.js'], cwd = cwd)
, './**/foo/bar/foobar.js')
)

it('returns the absolute paths alphabetically sorted', ->
paths = resolveHookfiles([
'./**/*_hooks.*',
'./hooks-glob/baz/x.js',
'./hooks-glob/foo/y.js',
'./hooks-glob/bar/z.js',
'./hooks-glob/foo/a.js',
'./hooks-glob/bar/b.js',
'./hooks-glob/baz/c.js',
'./hooks-glob/foo/o.js',
'./hooks-glob/bar/p.js',
], cwd = cwd)
assert.deepEqual(paths, [
path.join(cwd, 'hooks-glob/foo/a.js'),
path.join(cwd, 'hooks-glob/bar/b.js'),
path.join(cwd, 'hooks-glob/baz/c.js'),
path.join(cwd, 'multifile/multifile_hooks.coffee'),
path.join(cwd, 'hooks-glob/foo/o.js'),
path.join(cwd, 'hooks-glob/bar/p.js'),
path.join(cwd, 'test2_hooks.js'),
path.join(cwd, 'test_hooks.coffee'),
path.join(cwd, 'hooks-glob/baz/x.js'),
path.join(cwd, 'hooks-glob/foo/y.js'),
path.join(cwd, 'hooks-glob/bar/z.js'),
])
)
)
)

0 comments on commit bce6603

Please sign in to comment.