-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·105 lines (85 loc) · 3.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env node
const yargs = require('yargs');
const path = require('path')
const chalk = require('chalk');
const Stepper = require('./lib/read/stepper');
const Reporter = require('./lib/read/reporter');
const transformers = require('./lib/transformers');
(async () => {
yargs.command('report <doc> [html]', 'Test markdown/steps file and report feedback into rendered output',
(yargs) => {
yargs.positional('html', {
describe: 'html file to execute'
})
},
async (argv) => {
await docable(argv, true);
})
.option({
output: {
alias: 'o',
describe: 'output report path',
type: 'string'
}
});
yargs.command('run <doc> [html]', 'Execute markdown/steps file',
(yargs) => {
yargs.positional('html', {
describe: 'html file to execute'
})
},
async (argv) => {
await docable(argv, false);
})
.option({
output: {
alias: 'o',
describe: 'output report path',
type: 'string'
}
});
// Turn on help and access argv
yargs.help().argv;
})();
async function fromHtml($, setup, cwd, docDir, onProgress, vars, stdioStreams) {
let cells = [];
$('[data-docable="true"]').each(function (index, elem) {
cells.push({
index: index,
content: $(elem).text().trim(),
...$(elem).data(),
elem,
id: $(elem).attr('id')
});
});
let stepper = new Stepper();
if( onProgress ) {
stepper.progress.on('data', onProgress );
}
let connectors = await stepper.buildConnector(setup, cwd, docDir);
/* let { results, _, status } */
return stepper.runSteps(cells, $, connectors, cwd, undefined, vars, stdioStreams);
}
async function docable(options, report, verbose = true) {
let stepper = new Stepper(path.resolve(options.doc), options.html ? path.resolve(options.html) : undefined);
await stepper.setup(options.setupObj);
const { $, results, status } = await stepper.run(options.stepIndex);
// print execution results in console
if (verbose) {
const passingCount = results.filter(r => r.result.status).length;
const failingCount = results.filter(r => !r.result.status).length;
const summaryColor = failingCount > 0 ? 'red' : 'green';
// print summary of tasks
console.log(chalk`{${summaryColor} \nSummary: ${Number((100 * passingCount / results.length).toFixed(1))}% of all tasks passed.} ` +
chalk`{${summaryColor} ${passingCount} passed - ${failingCount} failed.}`);
}
if (report) {
const reporter = new Reporter($, results);
await reporter.report(options.output);
}
process.exitCode = status ? 0 : 1;
// TODO:
// await stepper.tearDown();
return results;
}
module.exports = {docable, transformers, Stepper, fromHtml};