Skip to content

Commit

Permalink
Merge pull request #63 from glaucia86/GL/7-final-solution-create-file…
Browse files Browse the repository at this point in the history
…s-directories

chore: final solution exercise 07 from Module: 'Work with files and directories in a Node.js app'
  • Loading branch information
glaucia86 authored Feb 6, 2024
2 parents 041b09a + 6651b46 commit 698bf52
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 49 deletions.
49 changes: 0 additions & 49 deletions nodejs-files/7-exercise-create-files-directories/index-unit-7.js

This file was deleted.

54 changes: 54 additions & 0 deletions nodejs-files/7-exercise-create-files-directories/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require('fs').promises;
const path = require('path');

async function findSalesFiles(folderName) {
// (1) Add an array at the top, to hold the paths to all the sales files that the program finds.
let results = [];

try {
// (2) Read the currentFolder with the `readdir` method.
const items = await fs.readdir(folderName, { withFileTypes: true });

// (3) Add a block to loop over each item returned from the `readdir` function using the asynchronous `for...of` loop.
for (const item of items) {
// (4) Add an `if` statement to determine if the item is a file or a directory.
if (item.isDirectory()) {
// (5) If the item is a directory, _resursively call the function `findSalesFiles` again, passing in the path to the item.
const resultsReturned = await findSalesFiles(
path.join(folderName, item.name),
);
results = results.concat(resultsReturned);
} else {
// (6) If it's not a directory, add a check to make sure the item name matches *sales.json*.
if (path.extname(item.name) === '.json') {
results.push(`${folderName}/${item.name}`);
}
}
return results;
}
} catch (error) {
console.error('Error reading folder:', error.message);
throw error;
}
}

async function main() {
const salesDir = path.join(__dirname, '..', 'stores');
const salesTotalsDir = path.join(__dirname, '..', 'salesTotals');

// create the salesTotal directory if it doesn't exist
try {
await fs.mkdir(salesTotalsDir);
} catch {
console.log(`${salesTotalsDir} already exists.`);
}

// find paths to all the sales files
const salesFiles = await findSalesFiles(salesDir);

// write the total to the "totals.txt" file
await fs.writeFile(path.join(salesTotalsDir, 'totals.txt'), String());
console.log(`Wrote sales totals to ${salesTotalsDir}`);
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs').promises;
const path = require('path');

async function findSalesFiles(folderName) {
let results = [];

try {
const items = await fs.readdir(folderName, { withFileTypes: true });
for (const item of items) {
if (item.isDirectory()) {
const resultsReturned = await findSalesFiles(
path.join(folderName, item.name),
);
results = results.concat(resultsReturned);
} else {
if (path.extname(item.name) === '.json') {
results.push(`${folderName}/${item.name}`);
}
}

return results;
}
} catch (error) {
console.error('Error reading folder:', error.message);
throw error;
}
}

async function main() {
const salesDir = path.join(__dirname, '..', 'stores');

const salesTotalsDir = path.join(__dirname, '..', 'salesTotals');

try {
await fs.mkdir(salesTotalsDir);
} catch (error) {
console.log(`${salesTotalsDir} already exists.`);
}

const salesFiles = await findSalesFiles(salesDir);

await fs.writeFile(path.join(salesTotalsDir, 'totals.txt'), String());
console.log(`Wrote sales totals to ${salesTotalsDir}`);
}

main();

0 comments on commit 698bf52

Please sign in to comment.