-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExportImportModule.js
More file actions
86 lines (73 loc) · 2.4 KB
/
Copy pathExportImportModule.js
File metadata and controls
86 lines (73 loc) · 2.4 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
const fs = require('fs').promises;
const fss = require('fs');
const path = require('path');
const CruParser = require('./CruParser');
async function exportData(fileName, folderName) {
const folderPath = path.join(__dirname, folderName);
const exportFilePath = path.join(folderPath, fileName.slice(12));
try {
await fs.access(folderPath);
console.log('Directory already exists!');
} catch (err) {
try {
await fs.mkdir(folderPath, { recursive: true });
console.log('Directory created successfully!');
} catch (err) {
console.log(`Error creating directory: ${err.message}`);
return;
}
}
try {
const data = await fs.readFile(fileName,'utf-8');
const analyzer = new CruParser();
analyzer.parse(data);
await fs.writeFile(exportFilePath, data);
console.log('Data written successfully!');
} catch (err) {
console.log(`Error processing file: ${err.message}`);
}
}
async function importData(filename) {
const filePath = "SujetA_data\\"+filename
try {
const data = await fs.readFile(filename, 'utf-8');
const analyzer = new CruParser();
analyzer.parse(data);
await fs.writeFile(filePath, data);
console.log('Data imported successfully!');
} catch (err) {
console.log(`Error reading file: ${err.message}`);
}
}
function CRUification(data) {
let result = '';
for (const course of data) {
result += `+${course.code}\n`;
if (Array.isArray(course.creneaux) && course.creneaux.length > 0) {
for (const creneau of course.creneaux) {
const slot = creneau;
result += `1,${slot.type || ''},P=${slot.nbplaces || ''},H=${slot.jour || ''} ${slot.horaire || ''},F1,S=${slot.salle || ''}//\n`;
}
}
}
const filePath = './FichierExporte.cru';
fss.writeFile(filePath, '', (err) => {
if (err) {
console.error('Erreur lors de l\'effacement du fichier :', err);
return;
}
});
fss.writeFile(filePath, result, (err) => {
if (err) {
console.error('Erreur lors de l\'effacement du fichier :', err);
return;
}
console.log('Fichier généré avec succès');
});
return result;
}
module.exports = {
exportData,
importData,
CRUification
};