Passed
Push — 6.5.0.0 ( 85bd4e...fe6596 )
by Christian
24:46 queued 09:10
created

app.js ➔ showLog   F

Complexity

Conditions 16

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 2
dl 0
loc 3
rs 2.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like app.js ➔ showLog often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
const decoder = new TextDecoder();
0 ignored issues
show
Bug introduced by
The variable TextDecoder seems to be never declared. If this is a global, consider adding a /** global: TextDecoder */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
3
async function tailLog(response, element) {
4
    const reader = response.body.getReader();
5
6
    while (true) {
7
        const {value, done} = await reader.read();
8
        if (done) break;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
9
10
        const text = decoder.decode(value);
11
12
        let result = null
13
14
        try {
15
            let strings = text.split("\n");
16
            result = JSON.parse(strings.pop());
17
18
            element.innerHTML += strings.join("\n");
19
            element.scrollTop = element.scrollHeight;
20
        } catch (e) {
21
            element.innerHTML += text;
22
            element.scrollTop = element.scrollHeight;
23
        }
24
25
        if (result) {
26
            if (!result.success) {
27
                throw new Error('update failed');
28
            }
29
30
            return result
31
        }
32
    }
33
34
    throw new Error('Unexpected end of stream');
35
}
36
37
const installButton = document.getElementById('install-start');
38
const updateButton = document.getElementById('update-start');
39
const logCard = document.getElementById('log-card');
40
const logOutput = document.getElementById('log-output');
41
const logError = document.getElementById('log-error');
42
43
if (installButton) {
44
    installButton.onclick = async function () {
45
        logCard.style.removeProperty('display');
46
47
        installButton.disabled = true;
48
49
        const folder = document.getElementById('folder');
50
51
        const installResponse = await fetch(`${baseUrl}/install/_run?folder=` + folder.value, {method: 'POST'});
0 ignored issues
show
Bug introduced by
The variable baseUrl seems to be never declared. If this is a global, consider adding a /** global: baseUrl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
53
        try {
54
            const result = await tailLog(installResponse, logOutput);
55
            if (result.newLocation) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if result.newLocation is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
56
                window.location = result.newLocation;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
57
            }
58
        } catch (e) {
59
            console.log(e);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
60
            return showLog();
61
        }
62
    }
63
}
64
65
if (updateButton) {
66
    updateButton.onclick = async function () {
67
        updateButton.disabled = true;
68
        logCard.style.removeProperty('display');
69
70
        const prepareUpdate = await fetch(`${baseUrl}/update/_prepare`, {method: 'POST'})
0 ignored issues
show
Bug introduced by
The variable baseUrl seems to be never declared. If this is a global, consider adding a /** global: baseUrl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
71
        if (prepareUpdate.status !== 200) {
72
            logOutput.innerHTML += 'Failed to prepare update' + "\n"
73
            return showLog();
74
        } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
75
            try {
76
                await tailLog(prepareUpdate, logOutput);
77
            } catch (e) {
78
                return showLog();
79
            }
80
        }
81
82
        if (!isFlexProject) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable isFlexProject is declared in the current environment, consider using typeof isFlexProject === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
83
            logOutput.innerHTML += 'Updating to Flex Project' + "\n"
84
85
            const migrate = await fetch(`${baseUrl}/update/_migrate-template`, {method: 'POST'})
86
87
            if (migrate.status !== 204 && migrate.status !== 200) {
88
                logOutput.innerHTML += 'Failed to update to Flex Project' + "\n"
89
                logOutput.innerHTML += await migrate.text();
90
                return showLog();
91
            } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
92
                logOutput.innerHTML += 'Updated to Flex Project' + "\n"
93
            }
94
        }
95
96
        const updateRun = await fetch(`${baseUrl}/update/_run`, {method: 'POST'});
97
98
        try {
99
            await tailLog(updateRun, logOutput);
100
        } catch (e) {
101
            return showLog();
102
        }
103
104
        const resetConfig = await fetch(`${baseUrl}/update/_reset_config`, {method: 'POST'});
105
106
        if (resetConfig.status !== 200) {
107
            logOutput.innerHTML += 'Failed to update config files' + "\n"
108
            logOutput.innerHTML += await resetConfig.text();
109
        } else {
110
            try {
111
                await tailLog(resetConfig, logOutput);
112
            } catch (e) {
113
                return showLog();
114
            }
115
        }
116
117
        const finishUpdate = await fetch(`${baseUrl}/update/_finish`, {method: 'POST'})
118
        if (finishUpdate.status !== 200) {
119
            logOutput.innerHTML += 'Failed to prepare update' + "\n"
120
            logOutput.innerHTML += await finishUpdate.text();
121
        } else {
122
            try {
123
                await tailLog(finishUpdate, logOutput);
124
            } catch (e) {
125
                return showLog();
126
            }
127
        }
128
129
        window.location.href = `${baseUrl}/finish`;
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
130
    }
131
}
132
133
function showLog() {
134
    logError.style.removeProperty('display');
135
}
136
137
const downloadLogButton = document.getElementById('download-log');
138
139
if (downloadLogButton) {
140
    downloadLogButton.onclick = function () {
141
        const text = new Blob([logOutput.innerText], {type: 'text/plain'});
0 ignored issues
show
Bug introduced by
The variable Blob seems to be never declared. If this is a global, consider adding a /** global: Blob */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
142
        const element = document.createElement('a');
143
        element.href = URL.createObjectURL(text);
0 ignored issues
show
Bug introduced by
The variable URL seems to be never declared. If this is a global, consider adding a /** global: URL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
144
        element.setAttribute('download', 'log.txt');
145
146
        element.style.display = 'none';
147
        document.body.appendChild(element);
148
149
        element.click();
150
151
        document.body.removeChild(element);
152
    }
153
}
154