11/* libs */
22import { oraPromise } from "ora" ;
3- import { mkdir , writeFile } from "node:fs/promises" ;
3+ import { mkdir , writeFile , rm } from "node:fs/promises" ;
44import inquirer from "inquirer" ;
55import path from "node:path" ;
66import chalk from "chalk" ;
77import fs from "node:fs" ;
88import figures from "figures" ;
9+ import compressing from "compressing" ;
910
1011/* constants */
1112import {
@@ -14,6 +15,9 @@ import {
1415 PACKAGE_MANAGERS ,
1516 TASKMASTER_INIT_MSG ,
1617 TASKS_FILE_WARN ,
18+ TASKS_BCK_DEST_PATH ,
19+ TASKS_SRC_PATH ,
20+ TASKS_FILES ,
1721} from "@/constants" ;
1822
1923/* extras */
@@ -595,4 +599,110 @@ export class TaskMaster {
595599 ] . filter ( Boolean ) ,
596600 ) ;
597601 }
602+
603+ // ==============================================
604+ // Backup, Restore and Clear Methods
605+ // ==============================================
606+
607+ // TODO: done
608+ /**
609+ * @description Creates a backup of the .taskmaster directory
610+ * @param slot Slot number (1-3) for the backup
611+ */
612+ public async backupAsync ( slot : string ) : Promise < void > {
613+ const backupPath = path . join ( TASKS_BCK_DEST_PATH , `slot_${ slot } .zip` ) ;
614+ const backupDir = path . dirname ( backupPath ) ;
615+
616+ if ( ! fs . existsSync ( backupDir ) ) {
617+ await mkdir ( backupDir , { recursive : true } ) ;
618+ }
619+
620+ // Vérifier si le répertoire source existe
621+ if ( ! fs . existsSync ( TASKS_SRC_PATH ) ) {
622+ console . warn (
623+ chalk . yellow (
624+ `Source directory ${ TASKS_SRC_PATH } does not exist. Skipping backup.` ,
625+ ) ,
626+ ) ;
627+ return ;
628+ }
629+
630+ const oraOptions = {
631+ text : `Creating backup in slot ${ slot } ...` ,
632+ successText : chalk . bgGreen (
633+ `Backup created successfully in slot ${ slot } !` ,
634+ ) ,
635+ failText : chalk . bgRed ( `Failed to create backup in slot ${ slot } ` ) ,
636+ } ;
637+
638+ await oraPromise ( async ( ) => {
639+ await compressing . zip . compressDir ( TASKS_SRC_PATH , backupPath ) ;
640+ } , oraOptions ) ;
641+ }
642+
643+ // TODO: done
644+ /**
645+ * @description Restores a backup from the specified slot
646+ * @param slot Slot number (1-3) to restore from
647+ */
648+ public async restoreAsync ( slot : string ) : Promise < void > {
649+ const backupPath = path . join ( TASKS_BCK_DEST_PATH , `slot_${ slot } .zip` ) ;
650+
651+ if ( ! fs . existsSync ( backupPath ) ) {
652+ console . warn (
653+ chalk . yellow ( `No backup found in slot ${ slot } . Skipping restore.` ) ,
654+ ) ;
655+ return ;
656+ }
657+
658+ const oraOptions = {
659+ text : `Restoring backup from slot ${ slot } ...` ,
660+ successText : chalk . bgGreen (
661+ `Backup restored successfully from slot ${ slot } !` ,
662+ ) ,
663+ failText : chalk . bgRed ( `Failed to restore backup from slot ${ slot } ` ) ,
664+ } ;
665+
666+ await oraPromise ( async ( ) => {
667+ if ( fs . existsSync ( TASKS_SRC_PATH ) ) {
668+ await rm ( TASKS_SRC_PATH , { recursive : true , force : true } ) ;
669+ }
670+
671+ const parentDir = path . dirname ( TASKS_SRC_PATH ) ;
672+ if ( ! fs . existsSync ( parentDir ) ) {
673+ await mkdir ( parentDir , { recursive : true } ) ;
674+ }
675+
676+ await compressing . zip . uncompress ( backupPath , parentDir ) ;
677+ } , oraOptions ) ;
678+ }
679+
680+ // TODO: done
681+ /**
682+ * @description Clears all task-related files and directories
683+ */
684+ public async clearTasksAsync ( ) : Promise < void > {
685+ for ( const filePath of TASKS_FILES ) {
686+ if ( ! fs . existsSync ( filePath ) ) continue ;
687+
688+ const { confirm } = await inquirer . prompt ( {
689+ type : "confirm" ,
690+ name : "confirm" ,
691+ message : chalk . red ( `Delete ${ filePath } ?` ) ,
692+ default : false ,
693+ } ) ;
694+
695+ if ( confirm ) {
696+ const oraOptions = {
697+ text : `Deleting ${ filePath } ...` ,
698+ successText : chalk . bgGreen ( `${ filePath } deleted successfully!` ) ,
699+ failText : chalk . bgRed ( `Failed to delete ${ filePath } ` ) ,
700+ } ;
701+
702+ await oraPromise ( async ( ) => {
703+ await rm ( filePath , { recursive : true , force : true } ) ;
704+ } , oraOptions ) ;
705+ }
706+ }
707+ }
598708}
0 commit comments