Completed
Push — master ( 1188a3...766e4c )
by Dimas
44:15 queued 35:12
created

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

Complexity

Total Complexity 8
Complexity/F 0

Size

Lines of Code 131
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 8
mnd 8
bc 8
fnc 0
bpm 0
cpm 0
noi 0
1
import * as chalk from "chalk";
2
import * as upath from "upath";
3
import { writeFileSync } from 'fs';
4
import CLI, { EXIT_CODE } from "./CLI";
5
import { SyncConfig, CONFIG_FILE_NAME } from './Config';
6
import inquirer = require("inquirer");
7
8
export default class InitConfig2 {
9
10
    constructor() {
11
12
        let currentConf = <SyncConfig>{};
13
14
        try {
15
            currentConf = require(upath.resolve(process.cwd(), CONFIG_FILE_NAME));
16
            console.log("Existing config found.");
17
        } catch(e) {}
18
19
        let questions: inquirer.Questions = [
20
            {
21
                type: "input",
22
                name: "username",
23
                message: "Username to connect:",
24
                validate: (answer) => {
25
                    if (!answer) {
26
                        return "Username is required";
27
                    }
28
                    return true;
29
                },
30
                default: currentConf.username
31
            },
32
            {
33
                type: "list",
34
                name: "authType",
35
                message: "How do you want to authenticate:",
36
                choices: [
37
                    "Password in config",
38
                    "Ask password during connection",
39
                    "Private key"
40
                ]
41
            },
42
            {
43
                type: "password",
44
                name: "password",
45
                message: "Enter your password:",
46
                when: (answers: any) => answers.authType === "Password in config"
47
            },
48
            {
49
                type: "input",
50
                name: "privateKey",
51
                message: "Path to private key:",
52
                default: currentConf.privateKey,
53
                when: (answers: any) => answers.authType === "Private key",
54
                filter: (answer) => {
55
                    return upath.normalizeSafe(answer);
56
                }
57
            },
58
            {
59
                type: "input",
60
                name: "host",
61
                default: currentConf.host,
62
                message: "Hostname or IP to connect",
63
                validate: (answer) => {
64
                    if (!answer) {
65
                        return "Hostname is required";
66
                    }
67
                    return true;
68
                }
69
            },
70
            {
71
                type: "input",
72
                name: "port",
73
                message: "Port to conenct:",
74
                default: currentConf.port || "use default"
75
            },
76
            {
77
                type: "input",
78
                name: "localPath",
79
                message: "Local Path",
80
                filter: (answer) => {
81
                    return upath.normalizeSafe(answer);
82
                },
83
                default: currentConf.localPath || process.cwd()
84
            },
85
            {
86
                type: "input",
87
                name: "remotePath",
88
                message: "Remote Path",
89
                default: currentConf.remotePath,
90
                validate: (answer) => {
91
                    if (!answer) {
92
                        return "Remote Path is required";
93
                    }
94
                    return true;
95
                }
96
            }
97
        ];
98
99
        inquirer.prompt(questions)['then']((answers) => {
100
            let pass;
101
            // if default, don't put it in config
102
            if (answers.port == "use default") {
103
                delete answers.port;
104
            }
105
106
            // no need this in the config
107
            delete answers.authType;
108
109
            if (answers.password) {
110
                pass = answers.password;
111
                answers.password = "****";
112
            }
113
114
            inquirer.prompt({
115
                type: "confirm",
116
                name: "yes",
117
                message: `${JSON.stringify(answers, null, 4)}\nDoes this look good?`
118
            })['then']((answer) => {
119
                if (answer.yes) {
120
                    if (pass) {
121
                        answers.password = pass;
122
                    }
123
                    writeFileSync(CONFIG_FILE_NAME, JSON.stringify(answers, null, 4), 'utf8');
124
                } else {
125
                    console.log("No config was saved.");
126
                }
127
            })
128
        });
129
    }
130
}
131