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
![]() |
|||
70 | }); |
||
71 |