|
| 1 | +const {resolve} = require('path') |
1 | 2 | const grpc = require('@grpc/grpc-js') |
2 | 3 | const maybe = require('call-me-maybe') |
3 | 4 | const collectStream = require('stream-collector') |
| 5 | +const mirrorFolder = require('mirror-folder') |
4 | 6 | const pumpify = require('pumpify') |
5 | 7 | const map = require('through2-map') |
6 | 8 | const codecs = require('codecs') |
@@ -28,6 +30,11 @@ const { |
28 | 30 | fromDriveInfo, |
29 | 31 | toRPCMetadata: toMetadata |
30 | 32 | } = require('../common') |
| 33 | +const DriveWatcher = require('../driveWatcher') |
| 34 | +const { |
| 35 | + importKeyFilePath: IMPORT_KEY_FILE_PATH, |
| 36 | + exportKeyFilePath: EXPORT_KEY_FILE_PATH |
| 37 | +} = require('../constants') |
31 | 38 |
|
32 | 39 | module.exports = class DriveClient { |
33 | 40 | constructor (endpoint, token) { |
@@ -99,6 +106,60 @@ module.exports = class DriveClient { |
99 | 106 | }) |
100 | 107 | })) |
101 | 108 | } |
| 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 | + } |
102 | 163 | } |
103 | 164 |
|
104 | 165 | class RemoteHyperdrive { |
|
0 commit comments