Completed
Push — master ( c513eb...ef5348 )
by Dimas
08:42
created

libs/bin/syncjs/src/classes/Config.ts   A

Complexity

Total Complexity 7
Complexity/F 2.33

Size

Lines of Code 82
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 7
mnd 4
bc 4
fnc 3
bpm 1.3333
cpm 2.3333
noi 0
1
import { parse } from "jsonplus";
2
import { readFileSync, existsSync } from "fs";
3
import { join as pathJoin } from "path";
4
import CLI, { EXIT_CODE } from "./CLI";
5
6
export interface SyncConfig {
7
    "username"?: string;
8
    "password"?: string;
9
    "port"?: number;
10
    "host": string;
11
    "localPath": string;
12
    "remotePath": string;
13
    "privateKey"?: string;
14
    "ignores"?: Array<string | RegExp>;
15
    "pathMode"?: string;
16
}
17
18
export const CONFIG_FILE_NAME = "sync-config.json";
19
20
export default class Config implements SyncConfig {
21
    private _filename: string;
22
    private _config: SyncConfig;
23
24
    // properties
25
    host: string;
26
    username: string;
27
    password: string;
28
    port: number;
29
    localPath: string;
30
    remotePath: string;
31
    privateKey: string;
32
    ignores: Array<string | RegExp>;
33
    pathMode: string = "0755";
34
35
    constructor(private cli: CLI) {
36
        this._filename = pathJoin(process.cwd(), this.cli.getArgument("config", CONFIG_FILE_NAME));
37
    }
38
39
    ready(): Promise<void> {
40
        return new Promise<void>((resolve) => {
41
            this._fetch();
42
            this._expand();
43
44
            // Temporary
45
            if (!this.password && !this.privateKey) {
46
                this.cli.read("Enter password to connect:", true).then(answer => {
47
                    this.password = this._config.password = answer;
48
                    resolve();
49
                });
50
            } else {
51
                resolve();
52
            }
53
        });
54
    }
55
56
    private _fetch() {
57
        if (existsSync(this._filename)) {
58
            let configraw;
59
            if (configraw = readFileSync(this._filename)) {
60
                try {
61
                    this._config = parse(configraw.toString());
62
                } catch (e) {
63
                    this.cli.usage("Could not parse DB file. Make sure JSON is correct", EXIT_CODE.RUNTIME_FAILURE);
64
                }
65
            } else {
66
                this.cli.usage("Cannot read config file. Make sure you have permissions", EXIT_CODE.INVALID_ARGUMENT);
67
            }
68
        } else {
69
            this.cli.usage("Config file not found", EXIT_CODE.INVALID_ARGUMENT);
70
        }
71
    }
72
73
    /**
74
     * @TODO add checks on required values
75
     */
76
    private _expand() {
77
        ["host", "port", "username", "password", "pathMode",
78
            "localPath", "remotePath", "ignores", "privateKey"].forEach(prop => {
79
                this[prop] = this._config[prop] || this[prop];
80
            });
81
    }
82
}