1- const pack = require ( './package.json' ) ;
2- let ver ;
3- const envConfig = require ( "./environmentPath.js" ) ;
4- envConfig . setConfig ( ) ;
5- let mainpack = require ( targetPath + "/package.json" ) ;
6- const db = require ( "./functions/db.js" ) ;
1+ const pack = require ( './package.json' ) ;
2+ const Path = require ( "path" ) ;
3+ const db = require ( "./functions/db.js" ) ;
4+ const Enquirer = require ( "enquirer" ) ;
5+ const VersionList = require ( "./VersionList" ) . versions ;
6+ const EnvPath = require ( "./EnvironmentPath.js" ) ;
7+ const { setEnvPath} = require ( "./EnvironmentPath" ) ;
78
8- async function main ( ) {
9- switch ( process . argv [ 2 ] ) {
9+
10+ ( async ( ) => {
11+ await modeBranch ( process . argv [ 2 ] ) ;
12+ await db . close ( ) ;
13+ } ) ( ) ;
14+
15+ /***
16+ * Branch processing by mode
17+ * @param {string } mode Running type
18+ */
19+ async function modeBranch ( mode ) {
20+ switch ( mode ) {
1021 case "test" :
11- ver = "0.0.0"
22+ global . mode = mode ;
23+ await testMord ( ) ;
1224 break ;
1325 case "dev" :
14- ver = pack . version ;
15- break ;
1626 case "run" :
17- ver = pack . version ;
27+ global . mode = mode ;
28+ await productMode ( ) ;
29+ break ;
30+ case undefined :
31+ await modeChoice ( ) ;
1832 break ;
1933 default :
20- console . error ( "\u001b[33mPlease set command line argument to test, dev or run\u001b[0m" ) ;
21- console . error ( "Running type set to test" ) ;
22- ver = "0.0.0" ;
34+ console . log ( "Cannot find the mode " + mode ) ;
35+ await modeChoice ( ) ;
2336 break ;
2437 }
25- const mainver = mainpack . version ;
38+ }
39+
40+ /***
41+ * Running process of test mode
42+ */
43+ async function testMord ( ) {
44+ let runType ;
45+ await ( async ( ) => {
46+ const testRunSet = {
47+ type : "select" ,
48+ name : "setRunningType" ,
49+ message : "Please select running type" ,
50+ choices : [ "Update" , "Outdate" , "Both" ]
51+ } ;
52+ runType = ( await Enquirer . prompt ( testRunSet ) ) . setRunningType ;
53+ } ) ( ) ;
54+ global . targetPath = Path . resolve ( __dirname , "../update-test" ) ;
55+ switch ( runType ) {
56+ case "Update" :
57+ await RunUpdate ( ) ;
58+ break ;
59+ case "Outdate" :
60+ await RunOutdate ( ) ;
61+ break ;
62+ case "Both" :
63+ await RunUpdate ( ) ;
64+ await RunOutdate ( ) ;
65+ }
66+
67+ }
68+
69+ /***
70+ * Running Update.js in ./test/
71+ */
72+ async function RunUpdate ( ) {
73+ const Update = require ( "./test/Update.js" ) ;
74+ await Update . update ( ) ;
75+ }
76+
77+ /***
78+ * Running Outdate.js in ./test/
79+ */
80+ async function RunOutdate ( ) {
81+ const Outdate = require ( "./test/Outdate.js" ) ;
82+ await Outdate . outdate ( ) ;
83+ }
84+
85+ /***
86+ * Running process
87+ */
88+ async function productMode ( ) {
89+ let targetV ;
90+ await ( async ( ) => {
91+ const targetSelect = {
92+ type : "select" ,
93+ name : "setTargetVer" ,
94+ message : "Please select the update (outdate) destination" ,
95+ choices : VersionList . map ( elem => elem . version )
96+ } ;
97+ targetV = ( await Enquirer . prompt ( targetSelect ) ) . setTargetVer ;
98+ } ) ( ) ;
99+ await setEnvPath ( ) ;
100+ global . targetPath = Path . resolve ( __dirname , "../bot-main" ) ;
26101
27- console . log ( `${ mainver } ---> ${ ver } ` ) ;
102+ let mainPack = require ( Path . resolve ( targetPath , "/package.json" ) ) ;
103+ const mainV = mainPack . version ;
104+ console . log ( `${ mainV } ---> ${ targetV } ` ) ;
28105
29- const versions = require ( './VersionList.json' ) . versions ;
30- const ind = versions . findIndex ( versionData => versionData . version === ver ) ;
31- const mainInd = versions . findIndex ( versionData => versionData . version === mainver ) ;
106+ const versions = require ( './VersionList.json' ) . versions ;
107+ const targetInd = versions . findIndex ( versionData => versionData . version === targetV ) ;
108+ const mainInd = versions . findIndex ( versionData => versionData . version === mainV ) ;
32109
33- if ( ind > mainInd ) {
34- for ( let i = mainInd + 1 ; i <= ind ; i ++ ) {
35- if ( versions [ i ] . update === true ) {
36- console . log ( `Update v${ versions [ i - 1 ] . version } to v${ versions [ i ] . version } ` ) ;
37- const Update = require ( `./${ versions [ i ] . version } /Update.js` ) ;
38- await Update . update ( ) ;
110+ if ( targetInd > mainInd ) {
111+ for ( let i = mainInd + 1 ; i <= targetInd ; i ++ ) {
112+ if ( versions [ i ] . update === true ) {
113+ console . log ( `Update v${ versions [ i - 1 ] . version } to v${ versions [ i ] . version } ` ) ;
114+ const Update = require ( `./${ versions [ i ] . version } /Update.js` ) ;
115+ await Update . update ( ) ;
39116 }
40117 else {
41- console . log ( `No updates between v${ versions [ i - 1 ] . version } and v${ versions [ i ] . version } ` ) ;
118+ console . log ( `No updates between v${ versions [ i - 1 ] . version } and v${ versions [ i ] . version } ` ) ;
42119 }
43120 }
44121 }
45- else if ( ind < mainInd ) {
46- for ( let i = mainInd ; i < ind + 1 ; i -- ) {
47- if ( versions [ i ] . outdate === true ) {
48- console . log ( `Outdate v${ versions [ i ] . version } to v${ versions [ i - 1 ] . version } ` ) ;
49- const Outdate = require ( `./${ versions [ i ] . version } /Outdate.js` ) ;
50- await Outdate . outdate ( ) ;
122+ else if ( targetInd < mainInd ) {
123+ for ( let i = mainInd ; i < targetInd + 1 ; i -- ) {
124+ if ( versions [ i ] . outdate === true ) {
125+ console . log ( `Outdate v${ versions [ i ] . version } to v${ versions [ i - 1 ] . version } ` ) ;
126+ const Outdate = require ( `./${ versions [ i ] . version } /Outdate.js` ) ;
127+ await Outdate . outdate ( ) ;
51128 }
52129 else {
53- console . log ( `No outdates between v${ versions [ i ] . version } and v${ versions [ i - 1 ] . version } ` ) ;
130+ console . log ( `No outdates between v${ versions [ i ] . version } and v${ versions [ i - 1 ] . version } ` ) ;
54131 }
55132 }
56133 }
57- else if ( ind === mainInd ) {
58- console . log ( "No updates needed." )
134+ else if ( targetInd === mainInd ) {
135+ console . log ( "No updates needed." )
59136 }
60-
61- if ( process . argv [ 2 ] === "test" && versions [ versions . length - 1 ] . outdate === true ) {
62- console . log ( `Outdate test` ) ;
63- const Outdate = require ( `./0.0.0/Outdate.js` ) ;
64- await Outdate . outdate ( ) ;
65- }
66- await db . close ( ) ;
67137}
68- main ( ) ;
138+
139+ /***
140+ *Select running mode
141+ */
142+ async function modeChoice ( ) {
143+ await ( async ( ) => {
144+ const modeSelect = {
145+ type :"select" ,
146+ name :"setMode" ,
147+ message :"Please select running mode" ,
148+ choices : [ "test" , "dev" , "run" ]
149+ }
150+ await modeBranch ( ( await Enquirer . prompt ( modeSelect ) ) . setMode ) ;
151+ } ) ( ) ;
152+ }
0 commit comments