-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-index.js
52 lines (43 loc) · 1.24 KB
/
build-index.js
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
const process = require('node:process');
const path = require('node:path');
const fs = require('node:fs');
const fsPromises = fs.promises;
const pages = process.argv.slice(2);
Promise.allSettled(
pages.map(async file => {
const packageFile = path.join(__dirname, file, 'package.json');
if ((await fsPromises.access(packageFile, fs.constants.R_OK))) {
throw new Error(`file ${ packageFile } does not exist`);
}
const packageDataRaw = await fsPromises.readFile(packageFile);
const packageData = JSON.parse(packageDataRaw);
return {
name: packageData.name,
description: packageData.description,
url: `${ file }/`,
}
})
).then(values => {
const errors = values.filter(value => value.status === 'rejected').map(value => value.reason);
if (errors.length > 0) {
console.error(errors);
process.exit(1);
return;
}
const pages = values.filter(value => value.status === 'fulfilled').map(value => value.value);
const output = `
<html>
<head>
</head>
<body>
<h1>Available Frontend Mentor pages</h1>
<ul>
${ pages.map(page =>
`<li><a href="${page.url}">${ page.name } - ${ page.description }</a></li>`
).join("\n") }
</ul>
<body>
</html>
`;
console.log(output);
});