I am currently working on a project containing several functions from various files in my project. I exported them with :
module.exports.nomFonction= function{...}
I would like to compile everything in an index.js file that would call all these functions, here is where I am (rudimentary code that I am looking to improve):
var fs = require('fs');
const mt = require("./mainTweet"); // from mainTweet.js
const rp = require("./Reply"); // from Reply.js
const od = require("./getcsv"); // from Reply.js
async function sendTweet(err, data) {
if (err) {
console.log(err)
} else {
await od.onDownload();
await mt.mainTweet();
await rp.Reply();
}
}
sendTweet();
The first function onDownload (code below) allows to download a file each time it is updated. When I launch the sendTweet function, this error appears: Error: ENOENT: no such file or directory, open ‘files/data.csv’. She works when i call it lonely but not in index.js, however i added a waiting time between the execution of the first and second function.getcsv.js (onDownload) :
const fetch = require('isomorphic-fetch');
const converterCsvToJson = require("csvtojson");
const fs = require('fs');
const path = require('path');
// created a folder to store the downloaded files
const PATH_FOLDER_DOWNLOAD = path.join(__dirname, "./files");
if(!fs.existsSync(PATH_FOLDER_DOWNLOAD)) {
// if the download folder does not exist we create it
fs.mkdirSync(PATH_FOLDER_DOWNLOAD);
}
async function onDownload() {
// get the link of the last CSV file
const response = await fetch('https://www.data.gouv.fr/api/2/datasets/6010206e7aa742eb447930f7/resources/?page=1&type=main&page_size=1', {
method: "GET"
});
const data = await response.json();
// console.log(data)
const lastItem = data.data[0];
// check if the file is not already owned, using a hash to uniquely identify the data
const integrity = 'data';
// Using simple `fs.readdirSync` would give you filenames as well as directories.
// This approach creates objects called `Dirent`s where you can check if the the entry is a file or a directory.
// Then you can simply filter out the directories and get the filenames.
const filenames = fs.readdirSync(PATH_FOLDER_DOWNLOAD, {withFileTypes: true, encoding: "utf-8"})
.filter(dirent => dirent.isFile())
.map(dirent => dirent.name);
// Check if some of the filenames contains the integrity hash
const exists = filenames.some(filename => filename.indexOf(integrity) !== -1);
// we already have this file it has not been updated yet we give up the operation
if(exists) {
const {published} = lastItem;
console.log(`operation cancelled, no file has been published since: ${published}`);
} else {
// we don't own this file we download it
const urlCsvFile = lastItem.url;
const response = await fetch(urlCsvFile, {method: "GET"});
const csvContent = await response.text();
// writes the new file to the download folder
// using the hash as the file name, which will allow later to check if you already have it
fs.writeFileSync(
path.join(PATH_FOLDER_DOWNLOAD, (integrity + ".csv")),
csvContent,
{encoding: "utf-8"}
);
// the CSV file has been downloaded and saved in a file at: /download/{hash}.csv
console.log('File downloaded!')
}
}
// onDownload();
So Im looking for a solution to correct this problem, i tried some tricks usings different docs but nothing works Good evening and thank you in advance for your help