Advertisement
bai_onzi

file-handler 2

Jul 9th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs from 'fs';
  2. import { dirname, join } from 'path';
  3. import { fileURLToPath } from 'url';
  4.  
  5. const __dirname = dirname(fileURLToPath(import.meta.url));
  6.  
  7. console.log(__dirname);
  8.  
  9. export class FileHandler {
  10.   read(filename = '') {
  11.     filename = join(__dirname, '..', filename);
  12.     return new Promise((resolve, reject) => {
  13.       fs.readFile('data/tasks.all.json', { encoding: 'utf-8' }, (err, data) => {
  14.         if (err) {
  15.           reject(err)
  16.         }else{
  17.           resolve(JSON.parse(data));
  18.         }
  19.       });
  20.     });
  21.   }
  22.   write(filename = '', data = '') {
  23.     filename = join(__dirname, '..', filename);
  24.     return new Promise((resolve, reject) => {
  25.       fs.writeFile('data/tasks.all.json', JSON.stringify(data), { encoding: 'utf-8' }, (err) => {
  26.         if (err) {
  27.           reject(err);
  28.         }
  29.         resolve();
  30.       })
  31.     })
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement