Append file's content to the new file is not working

76 views Asked by At

I have a folder with 4 files with CSS styles. Using Node.js modules tried to create a new file 'bundle.css' and append content of these 4 files with style (css). But the code instead of styles, return:

[object Promise] [object Promise] [object Promise] [object Promise]

Directory:

distr
  |--project
  |     |-bundle.css
  |--styles
        |-style1.css
        |-style2.css
        |-style3.css
        |-style4.css

const fs = require('node:fs/promises');
const path = require('node:path');

const dir = path.join(__dirname, 'styles');
const dir2 = path.join(__dirname, 'project-dist');

function append(text) {
  fs.appendFile(`${dir2}/bundle.css`, text, 'utf-8', (err) => {
    if (err) {
      throw err;
    }
    console.log('data appended');
  });
}

async function getStyles() {
  try {
    const files = await fs.readdir(dir);
    for (const file of files) {
      const buffer = fs.readFile(`${dir}/${file}`);
      const content = buffer.toString();
      append(content);
    }
  } catch {
    console.log('Error');
  }
}
getStyles();
0

There are 0 answers