11import { Command , Flags } from '@oclif/core' ;
22import { ConfigService } from '../services/config.service' ;
3- import { DriveFolderService } from '../services/drive/drive-folder.service' ;
43import { CLIUtils } from '../utils/cli.utils' ;
5- import {
6- ItemNotFoundError ,
7- MissingCredentialsError ,
8- NotValidFolderUuidError ,
9- NotValidItemUuidError ,
10- } from '../types/command.types' ;
4+ import { MissingCredentialsError , NotValidFileUuidError , NotValidFolderUuidError } from '../types/command.types' ;
115import { ValidationService } from '../services/validation.service' ;
126import { DriveFileService } from '../services/drive/drive-file.service' ;
13- import { DriveFileItem , DriveFolderItem } from '../types/drive.types' ;
147import { ErrorUtils } from '../utils/errors.utils' ;
158
16- export default class Move extends Command {
9+ export default class MoveFile extends Command {
1710 static readonly args = { } ;
18- static readonly description = 'Move a folder/ file into a destination folder.' ;
19-
11+ static readonly description = 'Move a file into a destination folder.' ;
12+ static readonly aliases = [ 'move:file' ] ;
2013 static readonly examples = [ '<%= config.bin %> <%= command.id %>' ] ;
2114
2215 static readonly flags = {
2316 ...CLIUtils . CommonFlags ,
2417 id : Flags . string ( {
2518 char : 'i' ,
26- description : 'The item id to be moved (it can be a file id or a folder id) .' ,
19+ description : 'The file id to be moved.' ,
2720 required : false ,
2821 } ) ,
2922 destination : Flags . string ( {
3023 char : 'd' ,
31- description : 'The destination folder id where the item is going to be moved.' ,
24+ description : 'The destination folder id where the file is going to be moved.' ,
3225 required : false ,
3326 } ) ,
3427 } ;
3528
3629 public async run ( ) {
37- const { flags } = await this . parse ( Move ) ;
30+ const { flags } = await this . parse ( MoveFile ) ;
3831
3932 const nonInteractive = flags [ 'non-interactive' ] ;
4033
4134 const userCredentials = await ConfigService . instance . readUser ( ) ;
4235 if ( ! userCredentials ) throw new MissingCredentialsError ( ) ;
4336
44- const itemUuid = await this . getItemUuid ( flags [ 'id' ] , nonInteractive ) ;
37+ const fileUuid = await this . getFileUuid ( flags [ 'id' ] , nonInteractive ) ;
4538 const destinationFolderUuid = await this . getDestinationFolderUuid ( flags [ 'destination' ] , nonInteractive ) ;
4639
47- let item : DriveFileItem | DriveFolderItem | undefined ;
48- let isFolder = false ;
49- try {
50- if ( ! item ) {
51- item = await DriveFileService . instance . getFileMetadata ( itemUuid ) ;
52- isFolder = false ;
53- }
54- } catch {
55- /* noop */
56- }
57- try {
58- if ( ! item ) {
59- item = await DriveFolderService . instance . getFolderMetaByUuid ( itemUuid ) ;
60- isFolder = true ;
61- }
62- } catch {
63- /* noop */
64- }
65-
66- if ( ! item ) throw new ItemNotFoundError ( ) ;
67-
68- if ( isFolder ) {
69- await DriveFolderService . instance . moveFolder ( { folderUuid : item . uuid , destinationFolderUuid } ) ;
70- } else {
71- await DriveFileService . instance . moveFile ( { fileUuid : item . uuid , destinationFolderUuid } ) ;
72- }
73- CLIUtils . success ( `${ isFolder ? 'Folder' : 'File' } moved successfully to: ${ destinationFolderUuid } ` ) ;
40+ await DriveFileService . instance . moveFile ( { fileUuid, destinationFolderUuid } ) ;
41+ CLIUtils . success ( `File moved successfully to: ${ destinationFolderUuid } ` ) ;
7442 }
7543
7644 async catch ( error : Error ) {
@@ -79,21 +47,21 @@ export default class Move extends Command {
7947 this . exit ( 1 ) ;
8048 }
8149
82- public getItemUuid = async ( itemUuidFlag : string | undefined , nonInteractive : boolean ) : Promise < string > => {
83- let itemUuid = CLIUtils . getValueFromFlag (
50+ public getFileUuid = async ( fileUuidFlag : string | undefined , nonInteractive : boolean ) : Promise < string > => {
51+ let fileUuid = CLIUtils . getValueFromFlag (
8452 {
85- value : itemUuidFlag ,
86- name : Move . flags [ 'id' ] . name ,
87- error : new NotValidItemUuidError ( ) ,
53+ value : fileUuidFlag ,
54+ name : MoveFile . flags [ 'id' ] . name ,
55+ error : new NotValidFileUuidError ( ) ,
8856 canBeEmpty : true ,
8957 } ,
9058 nonInteractive ,
91- ( itemUuid : string ) => ValidationService . instance . validateUUIDv4 ( itemUuid ) ,
59+ ( fileUuid : string ) => ValidationService . instance . validateUUIDv4 ( fileUuid ) ,
9260 ) ;
93- if ( ! itemUuid ) {
94- itemUuid = ( await this . getItemUuidInteractively ( ) ) . trim ( ) ;
61+ if ( ! fileUuid ) {
62+ fileUuid = ( await this . getFileUuidInteractively ( ) ) . trim ( ) ;
9563 }
96- return itemUuid ;
64+ return fileUuid ;
9765 } ;
9866
9967 public getDestinationFolderUuid = async (
@@ -103,7 +71,7 @@ export default class Move extends Command {
10371 let destinationFolderUuid = CLIUtils . getValueFromFlag (
10472 {
10573 value : destinationFolderUuidFlag ,
106- name : Move . flags [ 'destination' ] . name ,
74+ name : MoveFile . flags [ 'destination' ] . name ,
10775 error : new NotValidFolderUuidError ( ) ,
10876 canBeEmpty : true ,
10977 } ,
@@ -118,14 +86,14 @@ export default class Move extends Command {
11886
11987 private static readonly MAX_ATTEMPTS = 3 ;
12088
121- public getItemUuidInteractively = ( ) : Promise < string > => {
89+ public getFileUuidInteractively = ( ) : Promise < string > => {
12290 return CLIUtils . promptWithAttempts (
12391 {
124- message : 'What is the item id you want to move?' ,
92+ message : 'What is the file id you want to move?' ,
12593 options : { required : true } ,
126- error : new NotValidItemUuidError ( ) ,
94+ error : new NotValidFileUuidError ( ) ,
12795 } ,
128- Move . MAX_ATTEMPTS ,
96+ MoveFile . MAX_ATTEMPTS ,
12997 ValidationService . instance . validateUUIDv4 ,
13098 ) ;
13199 } ;
@@ -137,7 +105,7 @@ export default class Move extends Command {
137105 options : { required : true } ,
138106 error : new NotValidFolderUuidError ( ) ,
139107 } ,
140- Move . MAX_ATTEMPTS ,
108+ MoveFile . MAX_ATTEMPTS ,
141109 ValidationService . instance . validateUUIDv4 ,
142110 ) ;
143111 } ;
0 commit comments