src/commands/utils.ts   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 63
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 50
mnd 4
bc 4
fnc 1
dl 0
loc 63
rs 10
bpm 4
cpm 5
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B utils.ts ➔ CommandDecorator 0 51 5
1
import * as vscode from 'vscode';
2
import { isString } from 'myrmidon';
3
import logger from '../logger';
4
5
const MESSAGES = {
6
    ERROR_MESSAGE    : 'Error occured',
7
    EDITOR_NOT_FOUND : 'No editor found'
8
};
9
10
export function CommandDecorator(
11
    // eslint-disable-next-line no-undef
12
    fatumRunner: FatumInsertionHandler,
13
    opts: { prompt?:any} = {}
14
) {
15
    return async function () {
16
        try {
17
            const { prompt } = opts;
18
19
            const editor = vscode.window.activeTextEditor;
20
21
            let promptedValue: any;
22
23
            if (prompt) {
24
                const value = await vscode.window.showInputBox({
25
                    placeHolder : prompt.placeHolder,
26
                    prompt      : prompt.helperText,
27
                    value       : prompt.defaultValue
28
                });
29
30
                logger.log('debug', 'received prompt: %s', value);
31
                promptedValue = prompt.validate(value);
32
                logger.log('info', 'prompt value %s', promptedValue);
33
            }
34
35
            if (!editor) {
36
                vscode.window.showErrorMessage(MESSAGES.EDITOR_NOT_FOUND);
37
                logger.log('error', MESSAGES.EDITOR_NOT_FOUND);
38
39
                return;
40
            }
41
42
            const fatumValue = fatumRunner(promptedValue);
43
            const asString = isString(fatumValue)
44
                ? fatumValue
45
                : fatumValue.toString();
46
47
            logger.log('info', 'generated %s', fatumValue);
48
49
            editor.edit(edit => {
50
                editor.selections.forEach(selection => {
51
                    edit.insert(
52
                        selection.active,
53
                        asString
54
                    );
55
                });
56
            });
57
        } catch (error) {
58
            vscode.window.showErrorMessage(MESSAGES.ERROR_MESSAGE);
59
            logger.log('error', error);
60
        }
61
    };
62
}
63