-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparser.js
More file actions
92 lines (76 loc) · 2.48 KB
/
parser.js
File metadata and controls
92 lines (76 loc) · 2.48 KB
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
const fs = require('fs-extra')
const path = require('path')
const Papa = require('papaparse')
const inputDirectory = './items'
const outputFile = './radar.csv'
const previousOutputFile = './items/radar.csv'
const folderOrder = ['Adopt', 'Trial', 'Assess', 'Hold'] // Add folder names in the desired order
async function parseDirectory(dir, previousData, depth = 0, quadrant = null, ring = null) {
const parsedData = []
const files = await fs.readdir(dir)
// Sort folders based on folderOrder
const sortedFiles = files.sort((a, b) => {
const aIndex = folderOrder.indexOf(a)
const bIndex = folderOrder.indexOf(b)
if (aIndex === -1 && bIndex === -1) {
return a.localeCompare(b)
}
if (aIndex === -1) {
return 1
}
if (bIndex === -1) {
return -1
}
return aIndex - bIndex
})
for (const file of sortedFiles) {
const filePath = path.join(dir, file)
const stat = await fs.stat(filePath)
if (stat.isDirectory()) {
const currentDepth = depth + 1
let currentRing, currentQuadrant
if (currentDepth === 1) {
currentRing = path.basename(filePath)
currentQuadrant = null
} else if (currentDepth === 2) {
currentRing = ring
currentQuadrant = path.basename(filePath)
}
const nestedData = await parseDirectory(filePath, previousData, currentDepth, currentQuadrant, currentRing)
parsedData.push(...nestedData)
} else if (path.extname(file) === '.txt') {
const description = (await fs.readFile(filePath, 'utf8')).replace(/\r?\n/g, '<br>')
const name = path.basename(file, '.txt')
const isNew = !previousData.some(
(item) =>
item.name === name && item.description === description && item.quadrant === quadrant && item.ring == ring,
)
parsedData.push({
quadrant,
ring: ring || 'default',
name,
description,
isNew,
})
}
}
return parsedData
}
async function main() {
try {
const previousData = await fs
.readFile(previousOutputFile, 'utf8')
.then((csv) => Papa.parse(csv, { header: true, dynamicTyping: true }).data)
.catch(() => [])
const parsedData = await parseDirectory(inputDirectory, previousData)
const output = Papa.unparse(parsedData, {
header: true,
newline: '\r\n',
})
await fs.writeFile(outputFile, output)
console.log(`CSV output written to ${outputFile}`)
} catch (error) {
console.error('Error:', error)
}
}
main()