-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-point-type.js
More file actions
114 lines (95 loc) · 3.47 KB
/
add-point-type.js
File metadata and controls
114 lines (95 loc) · 3.47 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fs = require('fs');
const path = require('path');
// Function to recursively find all JSON files in a directory
function findJsonFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findJsonFiles(filePath, fileList);
} else if (file.endsWith('.json')) {
fileList.push(filePath);
}
});
return fileList;
}
// Function to process a JSON file and add pointType if needed
function processJsonFile(filePath) {
try {
// Read the file
const data = fs.readFileSync(filePath, 'utf8');
let jsonData;
try {
jsonData = JSON.parse(data);
} catch (parseError) {
console.error(`Error parsing JSON in file ${filePath}:`, parseError.message);
return false;
}
// Check if it's a building file (has a name property)
if (jsonData && typeof jsonData === 'object' && jsonData.name) {
// Check if pointType is already defined
if (jsonData.pointType === undefined) {
// Find the position of the name property to insert after it
const keys = Object.keys(jsonData);
const nameIndex = keys.indexOf('name');
if (nameIndex !== -1) {
// Create a new object with properties in the desired order
const newJsonData = {};
keys.forEach((key, index) => {
newJsonData[key] = jsonData[key];
// Insert pointType after name
if (index === nameIndex) {
newJsonData.pointType = "building";
}
});
// Write the updated JSON back to the file
fs.writeFileSync(filePath, JSON.stringify(newJsonData, null, 2));
console.log(`Added pointType to ${filePath}`);
return true;
} else {
// If name property not found, just add pointType at the beginning
jsonData.pointType = "building";
fs.writeFileSync(filePath, JSON.stringify(jsonData, null, 2));
console.log(`Added pointType to ${filePath} (no name property found)`);
return true;
}
} else {
console.log(`File ${filePath} already has pointType defined: ${jsonData.pointType}`);
return false;
}
} else {
console.log(`File ${filePath} doesn't appear to be a building definition (no name property)`);
return false;
}
} catch (error) {
console.error(`Error processing file ${filePath}:`, error.message);
return false;
}
}
// Main function
function main() {
const dataDir = path.join(process.cwd(), 'data', 'buildings');
// Check if the directory exists
if (!fs.existsSync(dataDir)) {
console.error(`Directory ${dataDir} does not exist`);
return;
}
console.log(`Searching for building JSON files in ${dataDir}...`);
// Find all JSON files
const jsonFiles = findJsonFiles(dataDir);
console.log(`Found ${jsonFiles.length} JSON files`);
// Process each file
let modifiedCount = 0;
jsonFiles.forEach(filePath => {
const modified = processJsonFile(filePath);
if (modified) {
modifiedCount++;
}
});
console.log(`\nSummary:`);
console.log(`Total files processed: ${jsonFiles.length}`);
console.log(`Files modified: ${modifiedCount}`);
}
// Run the script
main();