|
| 1 | +import * as ftp from 'basic-ftp'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +require('dotenv').config(); |
| 5 | + |
| 6 | +console.log(process.env.ftphost); |
| 7 | + |
| 8 | +const remoteRootFolder = '/pdfviewer.net/relaunch'; |
| 9 | +// const remoteRootFolder = '/angularfaces'; |
| 10 | + |
| 11 | +synchronizeDistFolderWithFtpFolder(); |
| 12 | + |
| 13 | +function collectFiles(dir: string, allFiles: string[] = [], allFolders: string[] = []) { |
| 14 | + const files = fs.readdirSync(dir); |
| 15 | + |
| 16 | + files.forEach((file) => { |
| 17 | + const filePath = path.join(dir, file); |
| 18 | + if (fs.statSync(filePath).isDirectory()) { |
| 19 | + allFolders.push(filePath); |
| 20 | + collectFiles(filePath, allFiles, allFolders); |
| 21 | + } else { |
| 22 | + allFiles.push(filePath); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + return allFiles; |
| 27 | +} |
| 28 | + |
| 29 | +async function collectRemoteFiles(client: ftp.Client, dir: string, allFiles: any, allFolders: any) { |
| 30 | + console.log('Reading ' + dir); |
| 31 | + await client.ensureDir(dir); |
| 32 | + let files; |
| 33 | + try { |
| 34 | + files = await client.list(dir); |
| 35 | + } catch { |
| 36 | + console.log('Retry...'); |
| 37 | + files = await client.list(dir); |
| 38 | + } |
| 39 | + for (const file of files) { |
| 40 | + if (file !== 'relaunch') { |
| 41 | + const filePath = dir + '/' + file.name; |
| 42 | + if (file.isDirectory) { |
| 43 | + allFolders[filePath] = file; |
| 44 | + await collectRemoteFiles(client, filePath, allFiles, allFolders); |
| 45 | + } else { |
| 46 | + allFiles[filePath] = file; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return allFiles; |
| 52 | +} |
| 53 | + |
| 54 | +async function synchronizeDistFolderWithFtpFolder() { |
| 55 | + const allFiles: string[] = []; |
| 56 | + const allFolders: string[] = []; |
| 57 | + collectFiles('../dist/showcase/browser', allFiles, allFolders); |
| 58 | + console.log(allFolders.length + ' folders'); |
| 59 | + console.log(allFiles.length + ' files'); |
| 60 | + const client = new ftp.Client(); |
| 61 | + |
| 62 | + try { |
| 63 | + await client.access({ |
| 64 | + host: process.env.ftphost, |
| 65 | + user: process.env.ftpuser, |
| 66 | + password: process.env.ftppassword, |
| 67 | + secure: true, |
| 68 | + }); |
| 69 | + |
| 70 | + const allRemoteFiles = {}; |
| 71 | + const allRemoteFolders = {}; |
| 72 | + console.log('Collecting the remote files (this takes 1-2 minutes)'); |
| 73 | + await collectRemoteFiles(client, remoteRootFolder, allRemoteFiles, allRemoteFolders); |
| 74 | + console.log('--------'); |
| 75 | + console.log(Object.keys(allRemoteFolders).length + ' remote folders'); |
| 76 | + console.log(allFolders.length + ' local folders'); |
| 77 | + console.log(Object.keys(allRemoteFiles).length + ' remote files'); |
| 78 | + console.log(allFiles.length + ' local files'); |
| 79 | + |
| 80 | + console.log('--------'); |
| 81 | + |
| 82 | + await client.cd(remoteRootFolder); |
| 83 | + let i = 0; |
| 84 | + |
| 85 | + for (const folder of allFolders) { |
| 86 | + const remoteFolder = folder.replace('../dist/showcase/browser', remoteRootFolder); |
| 87 | + if (!allRemoteFolders[remoteFolder]) { |
| 88 | + console.log(i++ + ' Creating folder ' + remoteFolder); |
| 89 | + |
| 90 | + try { |
| 91 | + await client.ensureDir(remoteFolder); |
| 92 | + } catch (exception) { |
| 93 | + console.log('Retry...'); |
| 94 | + await client.ensureDir(remoteFolder); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + await client.cd(remoteRootFolder); |
| 100 | + i = 0; |
| 101 | + for (const file of allFiles) { |
| 102 | + const remoteFile = file.replace('../dist/showcase/browser', remoteRootFolder); |
| 103 | + try { |
| 104 | + if (allRemoteFiles[remoteFile]) { |
| 105 | + const info = fs.statSync(file); |
| 106 | + const remoteInfo = allRemoteFiles[remoteFile] as ftp.FileInfo; |
| 107 | + if (info.size === remoteInfo.size && !file.includes('index.html') && !file.includes('.mjs')) { |
| 108 | + // console.log("Skipping " + remoteFile); |
| 109 | + } else { |
| 110 | + // console.log("Different size"); |
| 111 | + console.log(i++ + ' of ' + allFiles.length + ' Uploading (updated)' + remoteFile); |
| 112 | + try { |
| 113 | + await client.uploadFrom(file, remoteFile); |
| 114 | + } catch (exception) { |
| 115 | + console.log('Retry...'); |
| 116 | + await client.uploadFrom(file, remoteFile); |
| 117 | + } |
| 118 | + } |
| 119 | + } else { |
| 120 | + console.log(i++ + ' of ' + allFiles.length + ' Uploading (new) ' + remoteFile); |
| 121 | + try { |
| 122 | + await client.uploadFrom(file, remoteFile); |
| 123 | + } catch (exception) { |
| 124 | + console.log('Retry...'); |
| 125 | + await client.uploadFrom(file, remoteFile); |
| 126 | + } |
| 127 | + } |
| 128 | + } catch (exception) { |
| 129 | + console.log("Couldn't upload " + file); |
| 130 | + console.log(exception); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + for (const remoteFile of Object.keys(allRemoteFiles)) { |
| 135 | + if (remoteFile) { |
| 136 | + if (!remoteFile.includes('.htaccess')) { |
| 137 | + const localFile = remoteFile.replace(remoteRootFolder, '../dist/showcase/browser'); |
| 138 | + if (!allFiles.includes(localFile)) { |
| 139 | + console.log('delete ' + remoteFile); |
| 140 | + try { |
| 141 | + await client.remove(remoteFile); |
| 142 | + } catch (exception) { |
| 143 | + console.log('Retry...'); |
| 144 | + await client.remove(remoteFile); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + for (const remoteFolder of Object.keys(allRemoteFolders)) { |
| 151 | + if (remoteFolder) { |
| 152 | + const localFolder = remoteFolder.replace(remoteRootFolder, '../dist/showcase/browser'); |
| 153 | + if (!allFolders.includes(localFolder)) { |
| 154 | + console.log('delete ' + remoteFolder); |
| 155 | + try { |
| 156 | + await client.removeDir(remoteFolder); |
| 157 | + } catch (exception) { |
| 158 | + console.log('Retry...'); |
| 159 | + await client.removeDir(remoteFolder); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } catch (err) { |
| 165 | + console.log(err); |
| 166 | + } |
| 167 | + client.close(); |
| 168 | +} |
0 commit comments