Skip to content

Commit 6f7999f

Browse files
committed
beautified console outputs
1 parent 84afede commit 6f7999f

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

src/obj/ConsoleOutput.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export class ConsoleOutput {
2+
public static log(message: string) {
3+
console.log(`${message}`);
4+
}
5+
6+
public static error(message: string) {
7+
console.log(`\x1b[31m${message}\x1b[0m`);
8+
}
9+
10+
public static info(message: string) {
11+
console.log(`\x1b[34m${message}\x1b[0m`);
12+
}
13+
14+
public static success(message: string) {
15+
console.log(`\x1b[32m${message}\x1b[0m`);
16+
}
17+
18+
public static warning(message: string) {
19+
console.log(`\x1b[33m${message}\x1b[0m`);
20+
}
21+
}

src/obj/backup-manager.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as fs from 'fs';
33
import * as osLocale from 'os-locale';
44
import {FtpManager} from './ftp-manager';
55
import {AppSettings} from '../app-settings';
6+
import {ConsoleOutput} from './ConsoleOutput';
67
import moment = require('moment');
78

89
export class BackupManager {
@@ -11,10 +12,10 @@ export class BackupManager {
1112

1213
constructor() {
1314
osLocale().then((locale) => {
14-
console.log(`locale is ${locale}`);
15+
ConsoleOutput.info(`locale is ${locale}`);
1516
moment.locale(locale);
1617
}).catch((error) => {
17-
console.error(error);
18+
ConsoleOutput.error(error);
1819
});
1920

2021
this.ftpManager = new FtpManager(AppSettings.settings.backup.root, {
@@ -28,7 +29,7 @@ export class BackupManager {
2829
this.ftpManager.afterManagerIsReady().then(() => {
2930
this.doBackup();
3031
}).catch((error) => {
31-
console.log(error);
32+
ConsoleOutput.error(error);
3233
});
3334
}
3435

@@ -41,6 +42,7 @@ export class BackupManager {
4142
fs.unlinkSync(path.join(AppSettings.appPath, 'statistics.txt'));
4243
}
4344
const subscr = this.ftpManager.error.subscribe((message: string) => {
45+
ConsoleOutput.error(`${moment().format('L LTS')}: ${message}`);
4446
const line = `${moment().format('L LTS')}:\t${message}\n`;
4547
errors += line;
4648
fs.appendFile(path.join(AppSettings.appPath, 'errors.log'), line, {
@@ -53,35 +55,34 @@ export class BackupManager {
5355
name = name.substring(name.lastIndexOf('/') + 1);
5456
const downloadPath = (AppSettings.settings.backup.downloadPath === '') ? AppSettings.appPath : AppSettings.settings.backup.downloadPath;
5557

56-
console.log(`Remote path: ${AppSettings.settings.backup.root}\nDownload path: ${downloadPath}\n`);
58+
ConsoleOutput.info(`Remote path: ${AppSettings.settings.backup.root}\nDownload path: ${downloadPath}\n`);
5759

5860
this.ftpManager.statistics.started = Date.now();
5961
this.ftpManager.downloadFolder(AppSettings.settings.backup.root, path.join(downloadPath, name)).then(() => {
6062
this.ftpManager.statistics.ended = Date.now();
6163
this.ftpManager.statistics.duration = (this.ftpManager.statistics.ended - this.ftpManager.statistics.started) / 1000 / 60;
6264

63-
const statistics = `Started: ${moment(this.ftpManager.statistics.started).format('L LTS')}
65+
ConsoleOutput.success('Backup finished!');
66+
const statistics = `\n-- Statistics: --
67+
Started: ${moment(this.ftpManager.statistics.started).format('L LTS')}
6468
Ended: ${moment(this.ftpManager.statistics.ended).format('L LTS')}
6569
Duration: ${this.ftpManager.getTimeString(this.ftpManager.statistics.duration * 60 * 1000)} (H:m:s)
6670
6771
Folders: ${this.ftpManager.statistics.folders}
6872
Files: ${this.ftpManager.statistics.files}
69-
Errors: ${errors.split("\n").length - 1}`;
73+
Errors: ${errors.split('\n').length - 1}`;
7074

71-
console.log('\n' + statistics);
75+
ConsoleOutput.log('\n' + statistics);
7276
fs.writeFileSync(path.join(AppSettings.appPath, 'statistics.txt'), statistics, {
7377
encoding: 'utf-8'
7478
});
75-
76-
console.log(`download ok to ${AppSettings.appPath}`);
7779
if (errors !== '') {
78-
console.log(`There are errors. Please read the errors.log file for further information.`);
80+
ConsoleOutput.error(`There are errors. Please read the errors.log file for further information.`);
7981
}
8082
subscr.unsubscribe();
8183
this.ftpManager.close();
8284
}).catch((error) => {
83-
console.log(error);
84-
console.log(`!END!`);
85+
ConsoleOutput.error(error);
8586
this.ftpManager.close();
8687
});
8788
}

src/obj/ftp-manager.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Path from 'path';
44
import * as fs from 'fs';
55
import {Subject} from 'rxjs';
66
import {FtpEntry, FTPFolder} from './ftp-entry';
7+
import {ConsoleOutput} from './ConsoleOutput';
78
import moment = require('moment');
89

910
export class FtpManager {
@@ -39,7 +40,7 @@ export class FtpManager {
3940
this.gotTo(path).then(() => {
4041
this.onReady();
4142
}).catch((error) => {
42-
console.log('ERROR: ' + error);
43+
ConsoleOutput.error('ERROR: ' + error);
4344
this.onConnectionFailed();
4445
});
4546
});
@@ -77,7 +78,7 @@ export class FtpManager {
7778
public async gotTo(path: string) {
7879
return new Promise<void>((resolve, reject) => {
7980
if (this.isReady) {
80-
console.log(`open ${path}`);
81+
ConsoleOutput.info(`open ${path}`);
8182
this._client.cd(path).then(() => {
8283
this._client.pwd().then((dir) => {
8384
this.currentDirectory = dir;
@@ -148,7 +149,7 @@ export class FtpManager {
148149
newFolder.sortEntries();
149150
result.addEntry(newFolder);
150151
counter++;
151-
console.log(`${folder.path} added, ${counter}/${folders.length}`);
152+
ConsoleOutput.log(`${folder.path} added, ${counter}/${folders.length}`);
152153
}).catch((error) => {
153154
folder.readable = false;
154155
result.addEntry(folder);
@@ -171,7 +172,7 @@ export class FtpManager {
171172
public async downloadFolder(remotePath: string, downloadPath: string) {
172173
this.recursives++;
173174
if ((this.recursives % 10) === 9) {
174-
console.log(`% wait 2 seconds...%`);
175+
ConsoleOutput.info(`wait 2 seconds...`);
175176
await this.wait(2000);
176177
}
177178

@@ -188,7 +189,7 @@ export class FtpManager {
188189
try {
189190
await this.downloadFolder(folderPath, Path.join(downloadPath, fileInfo.name));
190191
this.statistics.folders++;
191-
console.log(`${this.getCurrentTimeString()}===> Directory downloaded: ${remotePath}\n`);
192+
ConsoleOutput.success(`${this.getCurrentTimeString()}===> Directory downloaded: ${remotePath}\n`);
192193
} catch (e) {
193194
this.error.next(e);
194195
}
@@ -201,7 +202,6 @@ export class FtpManager {
201202
}
202203
}
203204
}
204-
console.log(`return!`);
205205
return;
206206
} catch (e) {
207207
this.error.next(e);
@@ -223,7 +223,7 @@ export class FtpManager {
223223
}
224224
procentStr += procent.toFixed(2);
225225

226-
console.log(`${this.getCurrentTimeString()}---> ${info.type} (${procentStr}%): ${info.name}`);
226+
ConsoleOutput.log(`${this.getCurrentTimeString()}---> ${info.type} (${procentStr}%): ${info.name}`);
227227
};
228228

229229
if (this._client.closed) {
@@ -257,7 +257,6 @@ export class FtpManager {
257257
});
258258
}
259259

260-
261260
public getCurrentTimeString(): string {
262261
const duration = Date.now() - this.statistics.started;
263262
return moment().format('L LTS') + ' | Duration: ' + this.getTimeString(duration) + ' ';
@@ -298,7 +297,7 @@ export class FtpManager {
298297
return Math.floor(timespan / 1000 / 60 / 60);
299298
}
300299

301-
public async wait(time: number): Promise<void>{
300+
public async wait(time: number): Promise<void> {
302301
return new Promise<void>((resolve) => {
303302
setTimeout(() => {
304303
resolve();

0 commit comments

Comments
 (0)