79 lines
1.8 KiB
JavaScript
79 lines
1.8 KiB
JavaScript
var express = require('express');
|
|
var fs = require('fs');
|
|
|
|
var parseString = require("xml2js").parseString;
|
|
var router = express.Router();
|
|
var fileToOpen = 'gtg_tasks'
|
|
|
|
// const convertFiles = true;
|
|
const convertFiles = false;
|
|
|
|
const json = {
|
|
tasks : {},
|
|
projects : {},
|
|
tags : {},
|
|
}
|
|
/* GET home page. */
|
|
/**
|
|
* obtenir la liste de tags que l'on a entré
|
|
*/
|
|
router.get('/', async function (req, res, next) {
|
|
|
|
if(convertFiles){
|
|
|
|
convertOneXmlToJson('gtg_tasks',res)
|
|
convertOneXmlToJson('tags',res)
|
|
convertOneXmlToJson('projects',res)
|
|
}else{
|
|
console.log('================== conversion de fichiers désactivée dans index.js ================== ')
|
|
}
|
|
|
|
// récupérer le contenu des json
|
|
fs.readFile(`sources/tags_gtg.json`, "utf-8", function (err, data) {
|
|
if (err) console.log(err);
|
|
json.tags = JSON.parse(data)
|
|
console.log('json', json)
|
|
res.render('index', {title: "Conversion de GTG tâches", json: json})
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
function convertOneXmlToJson(fileToOpen,res){
|
|
fs.readFile(`sources/${fileToOpen}.xml`, "utf-8", function (err, data) {
|
|
if (err) console.log(err);
|
|
const results = transformToJson(data, fileToOpen, res)
|
|
console.log('fichier converti en json', fileToOpen)
|
|
|
|
});
|
|
}
|
|
|
|
function transformToJson(xml,fileToOpen, res) {
|
|
return parseString(xml, function (err, result) {
|
|
if (err) console.log(err);
|
|
|
|
// console.dir(JSON.stringify(result));
|
|
console.log('Done');
|
|
|
|
// write file to disk
|
|
fs.writeFile(`sources/${fileToOpen}_gtg.json`, JSON.stringify(result, null, 4), 'utf8', (err) => {
|
|
|
|
if (err) {
|
|
console.log(`Error writing file: ${err}`);
|
|
// return JSON.stringify(result)
|
|
|
|
res.send(err)
|
|
} else {
|
|
console.log(`File \`sources/${fileToOpen}_gtg.json\` is written successfully!`);
|
|
|
|
}
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
module.exports = router;
|