-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
87 lines (82 loc) · 2.23 KB
/
parse.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
import assert from 'assert';
const delimiter = '*'.repeat(22);
export default async function parse(/** @type {string} */ transcript) {
const lines = transcript.split(/\r\n/g);
let state = '';
let startTime;
let endTime;
const meta = {};
const data = [];
for (const line of lines) {
switch (state) {
case '': {
assert.strictEqual(line, '\ufeff' /* Unicode byte order mark */ + delimiter);
state = 'start-banner';
break;
}
case 'start-banner': {
assert.strictEqual(line, 'Windows PowerShell transcript start');
state = 'start-time';
break;
}
case 'start-time': {
const regex = /^Start time: (?<value>\d+)$/;
assert.match(line, regex);
const match = regex.exec(line);
startTime = (match.groups.value);
state = 'meta';
break;
}
case 'meta': {
if (line === delimiter) {
state = 'transcript';
break;
}
const regex = /^(?<key>.*?): (?<value>.*?)$/;
assert.match(line, regex);
const match = regex.exec(line);
meta[match.groups.key] = match.groups.value;
break;
}
case 'transcript': {
assert.match(line, /^Transcript started, output file is .*?$/);
state = 'data';
break;
}
case 'data': {
if (line === delimiter) {
state = 'end-banner';
break;
}
data.push(line);
break;
}
case 'end-banner': {
assert.strictEqual(line, 'Windows PowerShell transcript end');
state = 'end-time';
break;
}
case 'end-time': {
const regex = /^End time: (?<value>\d+)$/;
assert.match(line, regex);
const match = regex.exec(line);
endTime = (match.groups.value);
state = 'fin';
break;
}
case 'fin': {
assert.strictEqual(line, delimiter);
state = 'eof';
break;
}
case 'eof': {
assert.strictEqual(line, '');
break;
}
default: {
throw new Error('TODO');
}
}
}
return { startTime, endTime, meta, data };
}