@@ -2,15 +2,18 @@ const prompt = require("prompt-sync")();
22const inquirer = require ( "inquirer" ) ;
33const fs = require ( "fs" ) ;
44const { type } = require ( "os" ) ;
5+ const { measureMemory } = require ( "vm" ) ;
56
67const file_path = "data-karyawan.txt" ;
8+ const backup_path = "data-karyawan-backup.txt" ;
9+ const log_path = "logs/data-terhapus.txt" ;
710
811if ( ! fs . existsSync ( file_path ) ) {
912 console . warn ( `File "${ file_path } " tidak ditemukan. Membuat file baru...` ) ;
1013 fs . writeFileSync ( file_path , "" ) ;
1114}
1215
13- let = isi_file = "" ;
16+ let isi_file = "" ;
1417
1518try {
1619 isi_file = fs . readFileSync ( file_path , "utf-8" ) . trim ( ) ;
@@ -35,6 +38,16 @@ for (let i = 0; i < baris.length; i++) {
3538 }
3639}
3740
41+ // BACKUP DATA SEBELUMNYAA ========================================================================
42+ function backup_data ( ) {
43+ try {
44+ fs . copyFileSync ( file_path , backup_path ) ;
45+ } catch ( err ) {
46+ console . error ( "Gagal melakukan backup : " , err . message ) ;
47+ }
48+ }
49+ // ================================================================================================
50+
3851// TAMPILKAN DATA =================================================================================
3952function tampilkan_data ( ) {
4053 console . log ( "========== DATA KARYAWAN ==========" ) ;
@@ -138,6 +151,7 @@ async function tambah_data() {
138151
139152 try {
140153 fs . writeFileSync ( file_path , write_data ) ;
154+ backup_data ( ) ;
141155 console . log (
142156 "========== DATA BERHASIL DITAMBAHKAN DAN DISIMPAN =========="
143157 ) ;
@@ -231,17 +245,17 @@ async function cari_data() {
231245
232246// SORTING DATA BERDASARKAN ID KARYAWAN ===========================================================
233247async function sort_by_id ( ) {
234- const data_sort = [ ...data ] ;
248+ // const data_sort = [...data];
235249
236- async function sort_by_id_ascending ( ) {
237- data_sort . sort ( ( a , b ) => a . ID . localeCompare ( b . ID ) ) ;
238- }
250+ // async function sort_by_id_ascending() {
251+ // data_sort.sort((a, b) => a.ID.localeCompare(b.ID));
252+ // }
239253
240- async function sort_by_id_descending ( ) {
241- data_sort . sort ( ( a , b ) => b . ID . localeCompare ( a . ID ) ) ;
242- }
254+ // async function sort_by_id_descending() {
255+ // data_sort.sort((a, b) => b.ID.localeCompare(a.ID));
256+ // }
243257
244- async function hasil_sorting ( ) {
258+ async function hasil_sorting ( data_sort ) {
245259 console . log ( "\n========= HASIL SORTING ==========" ) ;
246260 console . table ( data_sort ) ;
247261
@@ -277,6 +291,7 @@ async function sort_by_id() {
277291 try {
278292 fs . writeFileSync ( file_path , new_data ) ;
279293 data = data_sort ;
294+ backup_data ( ) ;
280295 console . log ( "Data telah disimpan ke file." ) ;
281296 } catch ( err ) {
282297 console . error ( "Gagal menyimpan file." , err . message ) ;
@@ -302,16 +317,16 @@ async function sort_by_id() {
302317 switch ( menu ) {
303318 case "Ascending (A-Z)" : {
304319 console . log ( "\n" ) ;
305- await sort_by_id_ascending ( ) ;
306- await hasil_sorting ( ) ;
320+ const data_sort = [ ... data ] . sort ( ( a , b ) => a . ID . localeCompare ( b . ID ) ) ;
321+ await hasil_sorting ( data_sort ) ;
307322 console . log ( "\n" ) ;
308323 break ;
309324 }
310325
311326 case "Descending (Z-A)" : {
312327 console . log ( "\n" ) ;
313- await sort_by_id_descending ( ) ;
314- await hasil_sorting ( ) ;
328+ const data_sort = [ ... data ] . sort ( ( a , b ) => b . ID . localeCompare ( a . ID ) ) ;
329+ await hasil_sorting ( data_sort ) ;
315330 console . log ( "\n" ) ;
316331 break ;
317332 }
@@ -463,6 +478,7 @@ async function edit_data() {
463478
464479 try {
465480 fs . writeFileSync ( file_path , new_file_data ) ;
481+ backup_data ( ) ;
466482 console . log ( "Data berhasil diperbarui dan disimpan ke file." ) ;
467483 } catch ( err ) {
468484 console . error ( "Gagal menyimpan file." , err . message ) ;
@@ -574,8 +590,24 @@ async function delete_data() {
574590 . map ( ( item ) => `${ item . ID } |${ item . NAMA } |${ item . JABATAN } |${ item . TELP } ` )
575591 . join ( "\n" ) + "\n" ;
576592
593+ backup_data ( ) ;
594+
595+ if ( ! fs . existsSync ( "logs" ) ) {
596+ fs . mkdirSync ( "logs" ) ;
597+ }
598+
577599 try {
578- fs . writeFileSync ( "data-karyawan.txt" , new_file_data ) ;
600+ fs . appendFileSync (
601+ log_path ,
602+ `${ target . ID } |${ target . NAMA } |${ target . JABATAN } |${ target . TELP } \n`
603+ ) ;
604+ console . log ( "Data yang terhapus telah dicatat di `data-terhapus.txt`" ) ;
605+ } catch ( err ) {
606+ console . error ( "Gagal mencatat log penghapusan" , err . message ) ;
607+ }
608+
609+ try {
610+ fs . writeFileSync ( file_path , new_file_data ) ;
579611 console . log ( "File berhasil diperbarui setelah penghapusan." ) ;
580612 } catch ( err ) {
581613 console . error ( "Gagal menyimpan file." , err . message ) ;
@@ -587,6 +619,7 @@ async function delete_data() {
587619}
588620// ================================================================================================
589621
622+ // MENAMPILKAN STATISTIK KARYAWAN =================================================================
590623function show_statistic ( ) {
591624 console . log ( "========== STATISTIK DATA KARYAWAN ==========" ) ;
592625
@@ -616,6 +649,82 @@ function show_statistic() {
616649 console . log ( "\nJumlah berdasarkan awalan ID : " ) ;
617650 console . table ( per_awalan_id ) ;
618651}
652+ // ================================================================================================
653+
654+ // RESTORE DATA DARI BACKUP =======================================================================
655+ async function restore_data ( ) {
656+ console . log ( "========== RESTORE DATA DARI BACKUP ==========" ) ;
657+
658+ // CEK APAKAH BACKUP ADA ------------------------
659+ if ( ! fs . existsSync ( backup_path ) ) {
660+ console . warn ( "File backup tidak ditemukan" ) ;
661+ return ;
662+ }
663+ // ----------------------------------------------
664+
665+ // KONFIRMASI PERTAMA -----------------------------------------------------------------------------
666+ const { confirm_action } = await inquirer . prompt ( [
667+ {
668+ type : "confirm" ,
669+ name : "confirm_action" ,
670+ message :
671+ "[PERINGATAN] File backup akan menimpa file utama. Apakah Anda yakin ingin melanjutkan?" ,
672+ } ,
673+ ] ) ;
674+ // ------------------------------------------------------------------------------------------------
675+
676+ if ( ! confirm_action ) {
677+ console . log ( "Restore Data dibatalkan." ) ;
678+ return ;
679+ }
680+
681+ // KONFIRMASI KEDUA ---------------------------------------------------
682+ const { double_confirm_action } = await inquirer . prompt ( [
683+ {
684+ type : "confirm" ,
685+ name : "double_confirm_action" ,
686+ message : "Apakah kamu yakin ingin mengembalikan data dari backup?" ,
687+ } ,
688+ ] ) ;
689+ // --------------------------------------------------------------------
690+
691+ if ( ! double_confirm_action ) {
692+ console . log ( "Restore Data dibatalkan." ) ;
693+ return ;
694+ }
695+
696+ // SETELAH KONFIRMASI -------------------------------------------------
697+ try {
698+ fs . copyFileSync ( backup_path , file_path ) ;
699+ console . log (
700+ "Restore data berhasil.File Utama telah ditimpa dengan File Backup"
701+ ) ;
702+ console . log ( `${ data . length } data berhasil dimuat dari backup.` ) ;
703+
704+ // LOAD ULANG DATA KE MEMORY -------------------------------
705+ const isi_file_restore = fs . readFileSync ( file_path , "utf-8" ) . trim ( ) ;
706+ const baris_restore = isi_file_restore ? isi_file_restore . split ( "\n" ) : [ ] ;
707+
708+ data = [ ] ;
709+
710+ for ( let i = 0 ; i < baris_restore . length ; i ++ ) {
711+ const kolom = baris_restore [ i ] . split ( "|" ) ;
712+ if ( kolom . length >= 4 ) {
713+ data . push ( {
714+ ID : kolom [ 0 ] ,
715+ NAMA : kolom [ 1 ] ,
716+ JABATAN : kolom [ 2 ] ,
717+ TELP : kolom [ 3 ] ,
718+ } ) ;
719+ }
720+ }
721+ // ---------------------------------------------------------
722+ } catch ( err ) {
723+ console . error ( "Gagal melakukan restore" , err . message ) ;
724+ }
725+ // --------------------------------------------------------------------
726+ }
727+ // ================================================================================================
619728
620729// MENU PILIHAN ===================================================================================
621730async function main_menu ( ) {
@@ -632,7 +741,8 @@ async function main_menu() {
632741 "5. Cari Karyawan" ,
633742 "6. Edit Data" ,
634743 "7. Hapus Data" ,
635- "8. Keluar" ,
744+ "8. Restore Data dari Backup" ,
745+ "9. Keluar" ,
636746 ] ,
637747 } ,
638748 ] ) ;
@@ -647,7 +757,7 @@ async function main_menu() {
647757
648758 case "2. Tampilkan Statistik Data Karyawan" : {
649759 console . log ( "\n" ) ;
650- await show_statistic ( ) ;
760+ show_statistic ( ) ;
651761 console . log ( "\n" ) ;
652762 break ;
653763 }
@@ -687,7 +797,14 @@ async function main_menu() {
687797 break ;
688798 }
689799
690- case "8. Keluar" : {
800+ case "8. Restore Data dari Backup" : {
801+ console . log ( "\n" ) ;
802+ await restore_data ( ) ;
803+ console . log ( "\n" ) ;
804+ break ;
805+ }
806+
807+ case "9. Keluar" : {
691808 console . log ( "\n" ) ;
692809 console . log ( "Keluar dari program." ) ;
693810 process . exit ( ) ;
0 commit comments