1+ const { resolve} = require ( 'path' )
12const grpc = require ( '@grpc/grpc-js' )
23const maybe = require ( 'call-me-maybe' )
34const collectStream = require ( 'stream-collector' )
45const pump = require ( 'pump' )
6+ const mirrorFolder = require ( 'mirror-folder' )
57const codecs = require ( 'codecs' )
68const { 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
3138module . 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
103164class RemoteHyperdrive {
0 commit comments