mecano
Version:
Common functions for system deployment.
60 lines (55 loc) • 1.72 kB
text/coffeescript
path = require 'path'
glob = require 'glob'
{Minimatch} = require 'minimatch'
exec = require 'ssh2-exec'
string = require './string'
getprefix = (pattern) ->
prefix = null
n = 0
while typeof pattern[n] is "string" then n++
switch n
when pattern.length
prefix = pattern.join '/'
return prefix
when 0
return null
else
prefix = pattern.slice 0, n
prefix = prefix.join '/'
return prefix
Important: for now, only the "dot" options has been tested.
module.exports = (ssh, pattern, options, callback) ->
if arguments.length is 3
callback = options
options = {}
if ssh
pattern = path.normalize pattern
minimatch = new Minimatch pattern, options
cmd = "find"
for s in minimatch.set
prefix = getprefix s
cmd += " #{prefix}"
exec ssh, cmd, (err, stdout) ->
return callback null, [] if err
files = string.lines stdout.trim()
files = files.filter (file) -> minimatch.match file
for s in minimatch.set
n = 0
while typeof s[n] is "string" then n++
if s[n] is Minimatch.GLOBSTAR
prefix = getprefix s
files.unshift prefix if prefix
callback err, files
else
glob "#{pattern}", options, (err, files) ->
callback err, files