Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable linebreak-style */
- /* eslint-disable no-unused-vars */
- /* eslint-disable linebreak-style */
- /* eslint-disable consistent-return */
- /* eslint-disable no-shadow */
- import fs from 'fs';
- import { dirname, join } from 'path';
- import { fileURLToPath } from 'url';
- const __dirname = dirname(fileURLToPath(import.meta.url));
- console.log(__dirname);
- export class FileHandler {
- read(filename = '') {
- filename = join(__dirname, '..', filename);
- fs.readFile('src/data/tasks.all.json', { encoding: 'utf-8' }, (err, data) => {
- if (err) {
- return console.error(err.message);
- }
- const allTasks = JSON.parse(data);
- const idTask = allTasks.tasks.map(task => [task.id, task.name, task.status]);
- for (let i = 0; i < idTask.length; i++) {
- if (idTask[i].includes('done')) {
- fs.writeFile('src/data/tasks.done.json', JSON.stringify(idTask[i]), { encoding: 'utf-8' }, (err) => {
- if (err) {
- return console.error(`Something went wrong... ${err.message}`);
- }
- });
- }else{
- fs.writeFile('src/data/tasks.pending.json', JSON.stringify(idTask[i]), { encoding: 'utf-8' }, (err) => {
- if (err) {
- return console.error(`Something went wrong... ${err.message}`);
- }
- });
- }
- }
- console.log('Tasks were sorted')
- });
- }
- write(filename = '', data = '') {
- filename = join(__dirname, '..', filename);
- fs.writeFile('src/data/tasks.all.json', JSON.stringify(data), { encoding: 'utf-8' }, (err) => {
- if (err) {
- return console.error(`Error with adding data ${err.message}`)
- }
- console.log(`New task was added in src/data/tasks.all.json!`)
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement