11import { join } from "https://deno.land/std@0.213.0/path/mod.ts" ;
22import { copy } from "https://deno.land/std@0.213.0/fs/copy.ts" ;
33
4- import { bin } from "../wwwzip/ui.ts" ;
4+ import { bin , version } from "../wwwzip/ui.ts" ;
55import { decompress } from "./zip.ts" ;
66
77export async function initServer ( wwwRoot : string ) : Promise < boolean > {
88 try {
9- const zipFile = await tsToZip ( wwwRoot ) ;
109 const indexFile = join ( wwwRoot , "index.html" ) ;
1110 const isExist = await exists ( indexFile ) ;
1211 if ( isExist ) {
13- return true ;
12+ //Check for update
13+ const installedVersion = await getUiVersion ( indexFile )
14+
15+ if ( installedVersion === version ) {
16+ return true ;
17+ }
18+
19+ console . log ( `Updating UI from V${ installedVersion } to V${ version } ` )
20+ //We need to remove old version
21+ await Deno . remove ( indexFile ) ;
22+ await Deno . remove ( join ( wwwRoot , "favicon.ico" ) ) ;
23+ await Deno . remove ( join ( wwwRoot , "assets" ) , { recursive : true } ) ;
24+ await Deno . remove ( join ( wwwRoot , "icons" ) , { recursive : true } ) ;
1425 }
1526
27+ const zipFile = await tsToZip ( wwwRoot ) ;
28+
29+ //Decompress zip file
1630 await decompress ( zipFile , wwwRoot ) ;
1731 await Deno . remove ( zipFile ) ;
32+
1833 const spa = join ( wwwRoot , "spa" ) ;
1934 const spa_assets = join ( spa , "assets" ) ;
2035 const spa_icons = join ( spa , "icons" ) ;
@@ -28,18 +43,30 @@ export async function initServer(wwwRoot: string): Promise<boolean> {
2843 await copy ( join ( spa , "index.html" ) , join ( wwwRoot , "index.html" ) ) ;
2944 await copy ( join ( spa , "favicon.ico" ) , join ( wwwRoot , "favicon.ico" ) ) ;
3045
31- Deno . remove ( spa , { recursive : true } ) ;
46+ await Deno . remove ( spa , { recursive : true } ) ;
3247 return true ;
3348 } catch ( err ) {
3449 console . log ( err ) ;
3550 return false ;
3651 }
3752}
3853
54+ /**
55+ * Convert zip file to base64 ts
56+ * @date 2/3/2024 - 6:23:34 PM
57+ *
58+ * @export
59+ * @async
60+ * @param {string } path
61+ * @param {string } fileName
62+ * @returns {* }
63+ */
3964export async function zipToTs ( path : string , fileName : string ) {
65+ const version = await getUiVersion ( join ( path , `spa\\index.html` ) ) ;
66+
4067 const binPath = join ( path , `${ fileName } .zip` ) ;
4168 const uint = await Deno . readFile ( binPath ) ;
42-
69+
4370 let binary = "" ;
4471 const len = uint . length ;
4572
@@ -50,12 +77,25 @@ export async function zipToTs(path: string, fileName: string) {
5077 const binBase64 = btoa ( binary ) ;
5178 const base64 = trunString ( binBase64 , 100 ) ;
5279
53- const tsFileContent = `export const bin=\`${ base64 } \`` ;
80+ const tsFileContent = `export const version='${ version } ';
81+
82+ export const bin=\`${ base64 } \`` ;
5483 const tsFilePath = join ( "./wwwzip" , `${ fileName } .ts` ) ;
5584 await Deno . writeTextFile ( tsFilePath , tsFileContent ) ;
56- console . log ( `TS File saved to: ${ tsFilePath } ` ) ;
85+ await Deno . remove ( binPath ) ;
5786}
5887
88+ /**
89+ * This function converts the ts base64 file to .zip file
90+ * The zip file will be wwwroot
91+ * example: C:\Users\USERNAME\AppData\Local\deno\denoman\wwwroot
92+ * @date 2/3/2024 - 6:09:39 PM
93+ *
94+ * @export
95+ * @async
96+ * @param {string } wwwRoot
97+ * @returns {Promise<string> }
98+ */
5999export async function tsToZip ( wwwRoot : string ) : Promise < string > {
60100 const binContent = atob ( bin ) ;
61101 const binArray = new Uint8Array ( binContent . length ) ;
@@ -70,6 +110,17 @@ export async function tsToZip(wwwRoot: string): Promise<string> {
70110 return zipFile ;
71111}
72112
113+ async function getUiVersion ( indexFile : string ) : Promise < string > {
114+ const content = await Deno . readTextFile ( indexFile ) ;
115+ const regEx = / c o n t e n t \= \" D e n o M a n \s ( .* ?) \" \> / gm;
116+ const matches = content . matchAll ( regEx ) ;
117+ let version = "0" ;
118+ for ( const m of matches ) {
119+ version = m [ 1 ] ;
120+ }
121+ return version ;
122+ }
123+
73124function trunString ( input : string , width : number ) : string {
74125 const it = Math . ceil ( input . length / width ) ;
75126 let rtnVal = "" ;
0 commit comments