-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
31 lines (27 loc) · 935 Bytes
/
file.js
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
const { writeFile, mkdir } = require('fs');
const { tmpdir } = require('os');
const { join } = require('path');
const destDir = join(tmpdir(), 'monDossier');
let counter = 0;
const messages = [];
const pushFile = (filename, numFiles) => {
return new Promise( (resolve, reject) => {
mkdir(destDir, () => {
const interval = setInterval(() => {
counter++;
const filenameToCreate = join(destDir, `${filename}_${counter}.txt`);
const content = `Contenu du fichier ${filename} ${counter}`;
const message = `Chemin du fichier qui vient d'être créer : ${filenameToCreate}`;
writeFile(filenameToCreate, content, (err) => {
if(err) reject(err);
messages.push(message);
if (numFiles === counter) {
resolve(messages);
clearInterval(interval);
}
});
}, 1000);
});
});
}
module.exports = { pushFile };