Skip to content

Commit bed5100

Browse files
committed
Update: add import/export methods
1 parent 431c335 commit bed5100

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

lib/clients/drive.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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')
45
const pump = require('pump')
6+
const mirrorFolder = require('mirror-folder')
57
const codecs = require('codecs')
68
const { Writable, Transform } = require('streamx')
79

@@ -27,6 +29,11 @@ const {
2729
fromDriveInfo,
2830
toRPCMetadata: toMetadata
2931
} = require('../common')
32+
const DriveWatcher = require('../driveWatcher')
33+
const {
34+
importKeyFilePath: IMPORT_KEY_FILE_PATH,
35+
exportKeyFilePath: EXPORT_KEY_FILE_PATH
36+
} = require('../constants')
3037

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

103164
class RemoteHyperdrive {

0 commit comments

Comments
 (0)