-
Notifications
You must be signed in to change notification settings - Fork 34
/
case.ts
211 lines (176 loc) · 6.92 KB
/
case.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {findLast, reject, isNil} from 'ramda';
import parse from 'remark-parse';
import gfm from 'remark-gfm';
import stringify from 'remark-stringify';
import {unified} from 'unified';
import {Content, Root, Text, Code, List} from 'mdast';
import {currentUserName, pMap} from '@reskript/core';
import {PlayCase, PlayCaseMeta} from '../interface.js';
import {formatTime} from './time.js';
const parser = unified().use(parse).use(gfm);
const serializer = unified().use(stringify);
const stringifyNodesToMarkdown = (nodes: Content[]): string => {
const root: Root = {
type: 'root',
children: nodes,
};
return serializer.stringify(root);
};
const findReplCodeBlock = (nodes: Content[]) => findLast(v => v.type === 'code' && v.lang === 'jsx', nodes);
const isListItem = (node: Content) => node.type === 'listItem';
const parseMetaText = (text: string): [string, string, string] | null => {
const matches = /^(\w+): (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \(([^)]+)\)$/.exec(text);
return matches ? [matches[1], matches[2], matches[3]] : null;
};
const parseMeta = (metaNode: List): PlayCaseMeta => {
const items = metaNode.children.filter(isListItem);
const meta = items.reduce<PlayCaseMeta>(
(meta, item) => {
const childNode = item.children[0];
const textNode = childNode.type === 'paragraph' ? childNode.children[0] : null;
if (textNode?.type !== 'text') {
return meta;
}
const parsed = parseMetaText(textNode.value);
if (parsed) {
const [name, date, user] = parsed;
switch (name) {
case 'Create':
meta.createAt = date;
meta.createBy = user;
break;
case 'Run':
meta.lastRunAt = date;
meta.lastRunBy = user;
break;
}
}
return meta;
},
{createAt: '', createBy: '', lastRunAt: '', lastRunBy: ''}
);
return meta;
};
const extractMeta = async (metaNode: Content): Promise<PlayCaseMeta> => {
const parsedMeta: PlayCaseMeta = metaNode.type === 'list'
? parseMeta(metaNode)
: {createAt: '', createBy: '', lastRunAt: '', lastRunBy: ''};
const now = new Date();
const user = await currentUserName();
if (!parsedMeta.createAt) {
parsedMeta.createAt = formatTime(now);
parsedMeta.createBy = user;
}
if (!parsedMeta.lastRunAt) {
parsedMeta.lastRunAt = formatTime(now);
parsedMeta.lastRunBy = user;
}
return parsedMeta;
};
const parseToCase = async ([heading, meta, ...nodes]: Content[]): Promise<PlayCase | null> => {
if (heading.type !== 'heading' || heading.depth !== 2) {
return null;
}
const replCodeBlock = findReplCodeBlock(nodes);
if (!replCodeBlock) {
return null;
}
const description = stringifyNodesToMarkdown(nodes.filter(v => v !== replCodeBlock));
const metaInfo = await extractMeta(meta);
return {
name: (heading.children[0] as Text).value,
description: description.trim(),
code: (replCodeBlock as Code).value,
...metaInfo,
};
};
interface ParseContext {
saved: Content[][];
workingInProgress: Content[];
}
export const splitToCaseNodes = (markdown: string): Content[][] => {
const root = parser.parse(markdown);
const {saved, workingInProgress} = root.children.reduce(
(context, node) => {
// 每个二级标题是一个用例
if (node.type === 'heading' && node.depth === 2) {
context.saved.push(context.workingInProgress);
context.workingInProgress = [node];
}
else {
context.workingInProgress.push(node);
}
return context;
},
{saved: [], workingInProgress: []} as ParseContext
);
return [...saved, workingInProgress];
};
export const parseMarkdownToCases = async (markdown: string): Promise<PlayCase[]> => {
if (!markdown) {
return [];
}
const nodes = splitToCaseNodes(markdown);
const cases = await pMap(nodes, parseToCase);
return reject(isNil, cases);
};
export const serializeCaseToMarkdown = (caseToSerialize: PlayCase): string => {
const description = caseToSerialize.description.trim();
const lines = [
`## ${caseToSerialize.name.trim()}`,
'',
`- Create: ${caseToSerialize.createAt} (${caseToSerialize.createBy})`,
`- Run: ${caseToSerialize.lastRunAt} (${caseToSerialize.lastRunBy})`,
];
if (description) {
lines.push('', description);
}
lines.push('', '```jsx', caseToSerialize.code.trim(), '```');
return lines.join('\n');
};
const isMatchCaseName = (nodes: Content[], name: string): boolean => {
const [heading] = nodes;
return heading.type === 'heading' && heading.depth === 2 && (heading.children[0] as Text).value === name;
};
export const replaceLastRun = async (markdown: string, caseName: string, time: string, user: string) => {
const nodes = splitToCaseNodes(markdown);
const nodesToUpdate = nodes.find(v => isMatchCaseName(v, caseName));
const caseToUpdate = nodesToUpdate ? await parseToCase(nodesToUpdate) : null;
if (!caseToUpdate || !nodesToUpdate) {
throw new Error(`Cannot find a case named ${caseName}`);
}
const mayBeMetaNode = nodesToUpdate[1];
const newMetaContent = [
`- Create: ${caseToUpdate.createAt} (${caseToUpdate.createBy})`,
`- Run: ${time} (${user})`,
];
if (mayBeMetaNode.type === 'list') {
const start = mayBeMetaNode?.position?.start.offset;
const end = mayBeMetaNode?.position?.end.offset;
return markdown.slice(0, start) + newMetaContent.join('\n') + markdown.slice(end);
}
const headingEnd = nodesToUpdate[0].position?.end.offset;
if (!headingEnd) {
throw new Error(`Cannot find a place to insert meta to case ${caseName}`);
}
return markdown.slice(0, headingEnd) + '\n\n' + newMetaContent.join('\n') + '\n' + markdown.slice(headingEnd + 1);
};
export const replaceCodeBlockForCase = (markdown: string, caseName: string, newCode: string) => {
const nodes = splitToCaseNodes(markdown);
const nodesToUpdate = nodes.find(v => isMatchCaseName(v, caseName));
if (!nodesToUpdate) {
throw new Error(`Cannot find a case named ${caseName}`);
}
const replCodeBlock = findReplCodeBlock(nodesToUpdate);
const start = replCodeBlock?.position?.start.offset;
const end = replCodeBlock?.position?.end.offset;
if (!start || !end) {
throw new Error(`Cannot find code block in case named ${caseName}`);
}
const newCodeBlockContent = [
'```jsx',
newCode.trim(),
'```',
];
return markdown.slice(0, start) + newCodeBlockContent.join('\n') + markdown.slice(end);
};