-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from glaucia86/GL/7-final-solution-create-file…
…s-directories chore: final solution exercise 07 from Module: 'Work with files and directories in a Node.js app'
- Loading branch information
Showing
3 changed files
with
100 additions
and
49 deletions.
There are no files selected for viewing
49 changes: 0 additions & 49 deletions
49
nodejs-files/7-exercise-create-files-directories/index-unit-7.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
46 changes: 46 additions & 0 deletions
46
nodejs-files/7-final-solution-exercise-create-files-directories/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |