Skip to content

Commit a73729f

Browse files
committed
Update: add import/export methods
1 parent 76cd014 commit a73729f

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

lib/clients/drive.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const {resolve} = require('path')
12
const grpc = require('@grpc/grpc-js')
23
const maybe = require('call-me-maybe')
34
const collectStream = require('stream-collector')
5+
const mirrorFolder = require('mirror-folder')
46
const pumpify = require('pumpify')
57
const map = require('through2-map')
68
const codecs = require('codecs')
@@ -28,6 +30,11 @@ const {
2830
fromDriveInfo,
2931
toRPCMetadata: toMetadata
3032
} = require('../common')
33+
const DriveWatcher = require('../driveWatcher')
34+
const {
35+
importKeyFilePath: IMPORT_KEY_FILE_PATH,
36+
exportKeyFilePath: EXPORT_KEY_FILE_PATH
37+
} = require('../constants')
3138

3239
module.exports = class DriveClient {
3340
constructor (endpoint, token) {
@@ -99,6 +106,60 @@ module.exports = class DriveClient {
99106
})
100107
}))
101108
}
109+
110+
import (dir, drive) {
111+
if (!dir) {
112+
dir = process.cwd()
113+
} else {
114+
dir = resolve(dir)
115+
}
116+
117+
if (!drive.writable) {
118+
console.error('The target drive is not writable!')
119+
return process.exit(1)
120+
}
121+
122+
function shouldIgnore (name) {
123+
if (!name) return true
124+
if (name.indexOf(EXPORT_KEY_FILE_PATH) !== -1) return true
125+
else if (name.indexOf(IMPORT_KEY_FILE_PATH) !== -1) return true
126+
return false
127+
}
128+
129+
return mirrorFolder(dir, { fs: drive, name: '/' }, {
130+
watch: true,
131+
dereference: true,
132+
keepExisting: true,
133+
ignore: (file, stat, cb) => {
134+
if (shouldIgnore(file)) return process.nextTick(cb, null, true)
135+
return process.nextTick(cb, null, false)
136+
}
137+
})
138+
}
139+
140+
export (drive, dir, opts = { recursive: false }) {
141+
if (!drive) {
142+
throw new Error('missing argument: drive')
143+
}
144+
145+
if (!dir) {
146+
throw new Error('missing argument: dir')
147+
}
148+
149+
dir = resolve(dir)
150+
151+
const driveWatcher = new DriveWatcher(this._client, drive, {
152+
recursive: opts.recursive
153+
})
154+
155+
mirrorFolder({ fs: drive, name: '/' }, dir, {
156+
watch: driveWatcher.watch.bind(driveWatcher),
157+
keepExisting: true,
158+
ensureParents: true
159+
})
160+
161+
return driveWatcher
162+
}
102163
}
103164

104165
class RemoteHyperdrive {

0 commit comments

Comments
 (0)