-
Notifications
You must be signed in to change notification settings - Fork 0
/
processInstructions.js
106 lines (99 loc) · 2.96 KB
/
processInstructions.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
106
import { lexer } from 'marked'
export function processInstructions(fileContents) {
const tokens = lexer(fileContents)
const actions = []
debugger
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i]
if (token.type === 'heading' && token.depth === 2) {
actions.push({
type: 'instruction',
instruction: token.text,
})
} else if (token.type === 'paragraph') {
actions.push({
type: 'information',
information: token.text,
})
} else if (token.type === 'list') {
for (let index = 0; index < token.items.length; index++) {
const item = token.items[index]
if (
index === token.items.length - 1 &&
tokens[i + 1]?.type === 'space' &&
tokens[i + 2]?.type === 'code'
) {
const codeToken = tokens[i + 2]
const commands = codeToken.text.split('\n')
const instruction = item.raw
if (commands.length === 1) {
actions.push({
type: 'runCommand',
instruction,
command: commands[0],
})
} else {
actions.push({
type: 'runCommands',
instruction,
commands,
})
}
i += 2
} else {
const text = item.raw
const textToken = item.tokens[0]
let hadAnyCommands = false
for (let index = 0; index < textToken.tokens.length; index++) {
const subToken = textToken.tokens[index]
if (subToken.type === 'codespan') {
if (index >= 1) {
const previousToken = textToken.tokens[index - 1]
if (
previousToken.type === 'text' &&
previousToken.text.endsWith('in ')
) {
actions.push({
type: 'instructionToWriteCodeInAFile',
filePath: subToken.text,
instruction: text.trim(),
})
hadAnyCommands = true
continue
}
}
actions.push({
type: 'runCommand',
instruction: text.trim(),
command: subToken.text,
})
hadAnyCommands = true
}
}
if (!hadAnyCommands) {
actions.push({
type: 'instruction',
instruction: text.trim(),
})
}
}
}
} else if (token.type === 'code') {
const regExp =
/\/\/ (.+?)(?:\n|\r|\r\n)(.+?(?:\n|\r|\r\n))(?:\n|\r|\r\n)\/\/ /gs
let match
const text = token.text + '\n\n// '
while ((match = regExp.exec(text))) {
const filePath = match[1]
const code = match[2]
actions.push({
type: 'writeCode',
filePath,
code,
})
regExp.lastIndex -= 3
}
}
}
return actions
}