-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathairtar.js
executable file
·148 lines (127 loc) · 3.4 KB
/
airtar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
var minimist = require('minimist')
var log = require('single-line-log').stdout
var prettysize = require('prettysize')
var isAbsolute = require('absolute-path')
var progress = require('progress-stream')
var through = require('through2')
var circulate = require('array-loop')
var airpaste = require('airpaste')
var tarfs = require('tar-fs')
var fs = require('fs')
var glob = require('glob')
var chalk = require('chalk')
var cwd = process.cwd()
var dots = circulate(['.', '..', '...', '...'])
var opts = minimist(process.argv.slice(2), {
boolean: ['r', 'o', 'help', 'h', '?']
})
if (opts.help || opts.h || opts['?']) {
console.log([
'',
'Usage',
' Send : airtar [-n <name>] <source> [<source>, ...]',
' Receive : airtar [-r] [-n <name>] [-o] [<target>]',
'',
'Option Meaning',
'-r receive a file (defaults true if no args are specified)',
'-n <name> define a namespace',
'-o overwrite existing files',
'-h view usage',
''
].join('\n'))
process.exit()
}
var measure = function (fileStream, header) {
var prog = progress({
length: header.size,
time: 200
})
prog.on('progress', function (info) {
var str = ' ' + header.name + ' ' + chalk.gray('(' + prettysize(header.size) + ')')
if (info.percentage === 100) {
log('')
console.log(chalk.green('[ DONE ]') + str)
} else {
log(chalk.gray('[ ' + info.percentage.toFixed(0) + ' % ] ') + str + '\n')
}
})
return fileStream.pipe(prog)
}
var counter = function () {
var total = 0
return {
through: through(function (chunk, enc, next) {
total += chunk.length
this.push(chunk)
next()
}),
total: function () {
return total
}
}
}
var wait = function (msg) {
var inter = setInterval(function () {
log(msg + dots())
}, 300)
return function () {
log('')
clearInterval(inter)
}
}
var send = function () {
var source = opts._.reduce(function (memo, entry) {
return memo.concat(glob.sync(entry))
}, [])
if (source.filter(isAbsolute).length) {
console.log(chalk.red('Absolute paths are not allowed.'))
process.exit()
}
var stream = airpaste(opts.namespace)
var found = wait('waiting for receiver')
var count = counter()
stream.once('uncork', function () {
found()
tarfs
.pack(cwd, { entries: source, mapStream: measure })
.pipe(count.through)
.pipe(stream)
})
stream.once('finish', function () {
console.log(chalk.gray(prettysize(count.total()) + ' sent'))
})
}
var receive = function () {
var stream = airpaste(opts.namespace)
var ignore = function (name) {
if (!opts.o && fs.existsSync(name) && fs.statSync(name).isFile()) {
console.log(chalk.red('[ EXISTS ]') + ' ' + name)
return true
}
return false
}
var dir = opts._[0] || process.cwd()
var found = wait('waiting for sender', dir)
var count = counter()
var target = tarfs.extract(dir, { ignore: ignore, mapStream: measure })
stream.once('uncork', function () {
found()
stream
.pipe(count.through)
.pipe(target)
})
target.once('finish', function () {
console.log(chalk.gray(prettysize(count.total()) + ' received'))
})
}
process.on('uncaughtException', function () {
log('')
console.log(chalk.red('oooops, something went wrong'))
})
if (opts._.length === 0) opts.r = true
if (!opts.r) {
send()
} else {
receive()
}