-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (164 loc) · 4.72 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node -r esm
// @flow
import fs from 'fs'
import p from 'path'
import {promisify} from 'util'
import promisifyAll from 'util-promisifyall'
import jsonfile from 'jsonfile'
import Ftp from 'jsftp'
import Log from 'log-color-optionaldate'
import Progress from 'progress'
import {maybe} from 'maybes'
import hashOrig from 'hash-files'
import minimist from 'minimist'
import getConfig, {type LocalRoot, type RemoteRoot} from './get-config'
import Store from './json-store'
const args: {debug?: boolean, dry?: boolean} = minimist(process.argv.slice(2), {
boolean: true,
})
const loglevel = args.debug === true ? 'debug' : 'info'
const log = new Log({level: loglevel, color: true, date: false})
type FTP = {
authAsync: (string, string) => Promise<void>,
putAsync: (string, string) => Promise<void>,
rawAsync: (string, ...args: $ReadOnlyArray<mixed>) => Promise<void>,
useList: boolean,
}
const isDirectory = path => fs.statSync(path).isDirectory()
const getProgress = total =>
loglevel !== 'debug'
? new Progress('[:bar] :percent :elapseds elapsed :etas remaining', {
total,
width: 40,
complete: '•',
incomplete: ' ',
})
: {tick: x => null}
const hash = promisify(hashOrig)
const getDirectoryHash = files => hash({files, noGlob: true})
const matches = async (
hashStore: Store,
localRoot: LocalRoot,
path: string,
files: $ReadOnlyArray<string>
) => {
log.debug(`Checking if hash matches for path ${path}`)
const newHash = await getDirectoryHash(
files.map(f => p.join(localRoot, path, f))
)
const oldHash = hashStore.get(path)
const match = newHash === oldHash
log.debug(`Previous hash ${oldHash}`)
log.debug(`New hash ${newHash}`)
log.debug(`Hashes ${match ? 'DO' : 'DON’T'} match`)
if (!match) hashStore.set(path, newHash)
return match
}
const dirParse = (
localRoot: LocalRoot,
startDir: string,
result: Map<string, string[]> = new Map([[p.sep, []]])
): Map<string, string[]> =>
fs.readdirSync(startDir).reduce((res, file, i) => {
if (isDirectory(p.join(startDir, file))) {
const tmpPath = p.relative(localRoot, p.join(startDir, file))
if (!res.has(tmpPath)) res.set(tmpPath, [])
return dirParse(localRoot, p.join(startDir, file), res)
} else {
maybe(res.get(p.relative(localRoot, startDir) || p.sep)).forEach(arr =>
arr.push(file)
)
return res
}
}, result)
const ftpPut = async (
ftp: FTP,
localRoot: LocalRoot,
path: string,
file: string
) => {
try {
await ftp.putAsync(p.normalize(p.join(localRoot, path, file)), file)
log.debug('Uploaded file: ' + file + ' to: ' + path)
} catch (e) {
log.error('Cannot upload file: ' + file + ' --> ' + e)
throw e
}
}
const ftpCwd = async (ftp: FTP, path: string) => {
try {
log.debug(`ftp cwd ${path}`)
await ftp.rawAsync('cwd', path)
} catch (e) {
try {
await ftp.rawAsync('mkd', path)
log.debug('New remote folder created ' + path)
return ftpCwd(ftp, path)
} catch (e) {
log.error('Error creating new remote folder', path, '-->', e)
throw e
}
}
}
const ftpProcessLocation = async (
hashStore: Store,
ftp: FTP,
localRoot: LocalRoot,
remoteRoot: RemoteRoot,
path: string,
files: $ReadOnlyArray<string>,
progress
) => {
log.debug('')
log.debug(`Processing location ${path}`)
if (!files) throw new Error(`Data for ${path} not found`)
await ftpCwd(ftp, p.normalize('/' + p.join(remoteRoot, path)))
if (!await matches(hashStore, localRoot, path, files)) {
for (const file of files) {
log.debug(`Uploading ${file}`)
if (args.dry !== true) {
await ftpPut(ftp, localRoot, path, file)
}
progress.tick()
}
} else {
log.debug(`Skipping directory ${path}`)
progress.tick(files.length)
}
}
void (async () => {
try {
const {
src: localRoot,
dest: remoteRoot,
auth: {host, port, authKey},
projectRoot,
} = await getConfig()
const hashStore = new Store(p.join(projectRoot, '.hashes.json'))
const ftp: FTP = promisifyAll(new Ftp({host, port}))
ftp.useList = true
const {username, password} = (await promisify(jsonfile.readFile)(
p.join(projectRoot, '.ftppass')
))[authKey]
await ftp.authAsync(username, password)
const data = dirParse(localRoot, localRoot)
const progress = getProgress([].concat(...data.values()).length)
for (const [path, files] of data) {
await ftpProcessLocation(
hashStore,
ftp,
localRoot,
remoteRoot,
path,
files,
progress
)
}
await hashStore.close()
await ftp.rawAsync('quit')
log.info('FTP upload completed')
} catch (e) {
console.error(e)
process.exit(1)
}
})()