1+ const verifyEvolutionInstallation = require ( '../../utils/verifyEvolutionInstallation.js' ) ;
2+ const cliProgress = require ( 'cli-progress' ) ;
3+ const inquirer = require ( 'inquirer' ) ;
4+ const fs = require ( 'fs' ) ;
5+ const path = require ( 'path' ) ;
6+ const colors = require ( 'ansi-colors' ) ;
7+ const mongoose = require ( 'mongoose' ) ;
8+
9+
10+ const migratorsFunctions = {
11+ INSTANCE : require ( './migrators/instance.js' ) ,
12+ CONTACTS : require ( './migrators/contacts.js' ) ,
13+ NEW_MESSAGE : require ( './migrators/newMessage.js' ) ,
14+ MESSAGE_UPDATE : require ( './migrators/messageUpdate.js' ) ,
15+ CHATS : require ( './migrators/chats.js' ) ,
16+ } ;
17+
18+
19+ module . exports = async ( ) => {
20+ const isEvolutionInstalled = verifyEvolutionInstallation ( ) ;
21+ if ( ! isEvolutionInstalled ) return ;
22+
23+ const instancesNames = fs . readdirSync ( path . join ( process . cwd ( ) , 'instances' ) ) . filter ( ( file ) => {
24+ return fs . statSync ( path . join ( process . cwd ( ) , 'instances' , file ) ) . isDirectory ( ) ;
25+ } ) ;
26+
27+ const questions = [
28+ {
29+ type : 'input' ,
30+ name : 'mongodbUrl' ,
31+ message : 'MongoDB url:' ,
32+ default : 'mongodb://admin:password@localhost:27017' ,
33+ } ,
34+ {
35+ type : 'input' ,
36+ name : 'prefix' ,
37+ message : 'MongoDB prefix:' ,
38+ default : 'evolution' ,
39+ } ,
40+ {
41+ type : 'checkbox' ,
42+ name : 'saveData' ,
43+ message : 'What data do you want to migrate?' ,
44+ choices : [
45+ {
46+ name : 'Instance' ,
47+ value : 'INSTANCE' ,
48+ checked : true ,
49+ } ,
50+ {
51+ name : 'New Message' ,
52+ value : 'NEW_MESSAGE' ,
53+ checked : true ,
54+ } ,
55+ {
56+ name : 'Message Update' ,
57+ value : 'MESSAGE_UPDATE' ,
58+ checked : true ,
59+ } ,
60+ {
61+ name : 'Contacts' ,
62+ value : 'CONTACTS' ,
63+ checked : true ,
64+ } ,
65+ {
66+ name : 'Chats' ,
67+ value : 'CHATS' ,
68+ checked : true ,
69+ } ,
70+ ] ,
71+ } ,
72+ {
73+ type : 'checkbox' ,
74+ name : 'selectedInstances' ,
75+ message : 'Select instances to migrate:' ,
76+ choices : instancesNames . map ( ( instanceName ) => {
77+ return {
78+ name : instanceName ,
79+ value : instanceName ,
80+ checked : true ,
81+ }
82+ } ) ,
83+ }
84+ ] ;
85+
86+ // get folders from /instances
87+ const answers = await inquirer . prompt ( questions ) ;
88+
89+
90+ // connect to mongodb
91+ console . log ( '\n\n🌱 Connecting to MongoDB...' ) ;
92+ const conn = await mongoose . createConnection ( answers . mongodbUrl , {
93+ dbName : answers . prefix + '-whatsapp-api' ,
94+ } ) . asPromise ( ) ;
95+ const connInstance = answers . saveData . includes ( 'INSTANCE' ) ? await mongoose . createConnection ( answers . mongodbUrl , {
96+ dbName : answers . prefix + '-instances' ,
97+ } ) . asPromise ( ) : null ;
98+ console . log ( '🌱 Connected to MongoDB!\n\n' ) ;
99+
100+ const instancesBar = new cliProgress . SingleBar ( {
101+ format : 'Instance: ' + colors . blue ( '{instanceName}' ) + ' |' + colors . cyan ( '{bar}' ) + '| {percentage}% || {value}/{total} Instances' ,
102+ } , cliProgress . Presets . shades_classic ) ;
103+
104+ instancesBar . start ( answers . selectedInstances . length , 0 ) ;
105+ for ( const instanceName of answers . selectedInstances ) {
106+ instancesBar . update ( { instanceName } ) ;
107+
108+ const instanceBars = new cliProgress . MultiBar ( {
109+ format : '|' + colors . cyan ( '{bar}' ) + '| ' + colors . blue ( '{process}' ) + ' | {percentage}% || {value}/{total} Files' ,
110+ clearOnComplete : true ,
111+ } , cliProgress . Presets . shades_classic ) ;
112+
113+ for ( const migration of answers . saveData ) {
114+ await migratorsFunctions [ migration ] ( instanceName , answers , instanceBars , conn , connInstance ) ;
115+ }
116+
117+ instanceBars . stop ( ) ;
118+ instancesBar . increment ( ) ;
119+ }
120+ instancesBar . stop ( ) ;
121+
122+
123+ // disconnect from mongodb
124+ console . log ( '\n\n🌱 Disconnecting from MongoDB...' ) ;
125+ await conn . close ( ) ;
126+ console . log ( '🌱 Disconnected from MongoDB!\n\n' ) ;
127+
128+ console . log ( '🌱 Migration completed!\n\n' ) ;
129+ process . exit ( 0 ) ;
130+ } ;
0 commit comments