-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-parser.ts
29 lines (27 loc) · 986 Bytes
/
file-parser.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
import * as path from "https://deno.land/[email protected]/path/mod.ts"
import { Todo } from "./todo.ts"
import { parse } from "./xotodo-parser/pkg/xotodo_parser.js"
export function parseFile(text: string, filePath: string): Todo[] {
const project = parseProject(filePath)
const todos = parse(text, filePath) as Todo[]
return todos.map(t => {
t.project = project
return t
})
}
// default project name is either the name of the git directory or the parent folder name
function parseProject(filePath: string): string {
const parts = filePath.split('/')
for (let i = 0; i < parts.length; i++) {
try {
const dir = path.join(...parts.slice(0, parts.length - i))
const gitDir = path.join(dir, '.git')
Deno.readDirSync("/" + gitDir)
return parts[parts.length - i - 1] // .git exists
} catch (_) {
// not a (git) directory, keep going
}
}
// did not find a git repo, default to the directory name
return parts[parts.length - 2]
}