Tentei criar uma função que extrai os links de textos de diferentes arquivos, como ensinado na seção "Faça como eu fiz". Segue meu código:
// index.js
export async function getFilesLinks(dirPath){
const absolutePath = path.join(__dirname, '..',dirPath)
try{
const files = await fs.promises.readdir(absolutePath, { encoding });
const data = await Promise.all(
files.map( async (filename) => {
const fileDir = `${absolutePath}/${filename}`
const text = await fs.promises.readFile(fileDir, encoding);
return getLinks(text);
})
);
return data;
}
catch(err){
treatError(err, dirPath)
}
}
//cli.js
import chalk from 'chalk';
import { getFilesLinks } from './index.js';
const path = process.argv
async function displayLinks(path){
const text = await getFilesLinks(path[2]);
console.log(chalk.bgWhiteBright(chalk.red('links list:')), text)
}
displayLinks(path)
E estou recebendo o seguinte erro no terminal:
➜ lib-markdown node cli.js ./files
const absolutePath = path.join(__dirname, '..',dirPath)
^
ReferenceError: __dirname is not defined
at getFilesLinks (file:///Users/igortb/Documents/code/practice/js/lib-markdown/index.js:40:36)
at displayLinks (file:///Users/igortb/Documents/code/practice/js/lib-markdown/cli.js:7:24)
at file:///Users/igortb/Documents/code/practice/js/lib-markdown/cli.js:11:1
at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)