-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (66 loc) · 2.49 KB
/
index.js
File metadata and controls
79 lines (66 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs').promises;
const path = require('path');
async function calculateSalesTotal(salesFiles) {
// Final sales total
let salesTotal = 0;
// (1) Tterates over the `salesFiles` array.
for (file of salesFiles) {
// (2) Reads the file.
const fileContents = await fs.readFile(file);
// (3) Parses the content as JSON.
const data = JSON.parse(fileContents);
// (4) Increments the `salesTotal` variable with the `total` value from the file.
salesTotal += data.total;
}
return salesTotal;
}
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, recursively 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}`);
}
}
}
} catch (error) {
console.error('Error reading folder:', error.message);
throw error;
}
return results;
}
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);
// read through each sales file to calculate the sales total
const salesTotal = await calculateSalesTotal(salesFiles);
// write the total to the "totals.json" file
await fs.writeFile(
path.join(salesTotalsDir, 'totals.txt'),
`${salesTotal}\r\n`,
{ flag: 'a' },
);
console.log(`Wrote sales totals to ${salesTotalsDir}`);
}
main();