src/run.js   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 1.13

Size

Lines of Code 70
Function Count 15

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
c 2
b 0
f 0
nc 1
dl 0
loc 70
rs 10
wmc 17
mnd 1
bc 4
fnc 15
bpm 0.2666
cpm 1.1333
noi 2

3 Functions

Rating   Name   Duplication   Size   Complexity  
A run.js ➔ compileSources 0 4 1
A run.js ➔ ??? 0 1 1
A run.js ➔ tableView 0 12 1
1
import fs from 'fs';
2
import { all, promisify } from 'bluebird';
3
import {
4
    curry,
5
    mapObjIndexed,
6
    mergeAll,
7
    prop,
8
    values
9
} from 'ramda';
10
import Table from 'cli-table';
11
import { runAndGetAlerts, getProperties } from './vm';
12
import { ask } from './input';
13
import { compileES6 } from './compiler';
14
import { read } from './db';
15
import { getLocale, getLocaleStrings } from './i18n';
16
import { compileModulesFromSource } from './module';
17
import live from './live';
18
19
const percentOf = curry((value, percent) => value / 100 * percent);
20
21
export const readFile = promisify(fs.readFile);
22
23
function tableView(alerts) {
24
    const valuesFrom = mapObjIndexed(({ title, content = '', comment = '' }, key) =>
25
        [key, title, content, comment]) & values;
26
27
    const table = new Table({
28
        head: ['Key', 'Title', 'Content', 'Comment'],
29
        colWidths: [10, 20, 35, 26].map(percentOf(process.stdout.columns || 100) & Math.round)
30
    });
31
32
    table.push(...valuesFrom(alerts));
33
    return table.toString();
34
}
35
36
export function compileSources() {
37
    return readFile('index.js', 'utf-8')
38
        .then(index => all([compileES6(index), compileModulesFromSource(index)]));
39
}
40
41
/**
42
 * Executes a function with the provided parameters
43
 *
44
 * @param {Object} params
45
 */
46
export const executeWithParams = params => readFile('package.json', 'utf-8')
47
    .then(JSON.parse)
48
    .then(({ name }) => all([name, read(name), getLocaleStrings(), getLocale()]))
49
    .spread((name, db, strings, locale) => compileSources()
50
        .spread((source, modules) => runAndGetAlerts(
51
            { name, source }, { params, db, locale }, strings, modules)))
52
    .get('alerts');
53
54
export default args => readFile('package.json', 'utf-8')
55
    .then(JSON.parse)
56
    .then(({ name }) => all([name, read(name), getLocaleStrings(), getLocale()]))
57
    .spread((name, db, strings, locale) => compileSources()
58
        .spread((source, modules) => getProperties({ name, source }, strings, modules)
59
            .then(prop('params') & ask)
60
            .then(mergeAll)
61
            .then(params => all([
62
                runAndGetAlerts({ name, source }, { params, db, locale }, strings, modules),
63
                params
64
            ]))))
65
    .spread(({ alerts }, params) => {
66
        if (args.live) {
67
            return live(alerts, params);
68
        }
69
        console.log(args.raw ? alerts : tableView(alerts));
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...
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...
70
    });
71