-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblock-report.js
163 lines (141 loc) · 5.9 KB
/
block-report.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import xlsx from 'xlsx';
import { mkdir } from 'fs/promises';
import process from 'process';
import { getMdast, getTableMap, getNodesByType } from './utils/mdast-utils.js';
import { loadMarkdowns, readIndex } from './utils/fetch-utils.js';
const MD_DIR = 'md';
const REPORT_DIR = 'reports';
export async function runReport(project, site, index, cached = true) {
const report = {};
const totals = { blocks: {}, variants: {} };
const entries = await readIndex(`./${index}`);
const mdFolder = `./${MD_DIR}/${project}`;
await loadMarkdowns(site, mdFolder, entries, cached, async (markdown, entry, i) => {
if (markdown === null) {
console.warn(`Skipping ${entry} as markdown could not be fetched.`);
return;
}
const entryReport = {
blocks: [],
links: [],
};
const mdast = await getMdast(markdown);
const tableMap = getTableMap(mdast);
for (let j = 0; j < tableMap.length; j++) {
const table = tableMap[j];
const { blockName, options } = table;
const blockReport = {
name: blockName,
index: j,
links: [],
};
if (options) {
const variant = `${blockName} (${options.join(', ')})`;
blockReport.variant = variant;
totals.variants[variant] = (totals.variants[variant] || 0) + 1;
}
totals.blocks[blockName] = (totals.blocks[blockName] || 0) + 1;
const links = getNodesByType(table.table, 'link');
links.forEach(({ url }) => {
blockReport.links.push(url);
});
entryReport.blocks.push(blockReport);
}
const links = getNodesByType(mdast, 'link');
links?.forEach((link) => {
const { url } = link;
entryReport.links.push(url);
});
report[entry] = entryReport;
console.log(`${i}/${entries.length}`, entry, entryReport.blocks.map((block) => block.name));
});
return { report, totals };
}
export async function createReports(project, site, { report, totals }) {
const allBlocks = Object.keys(totals.blocks).sort();
const ws_data = [['Path', 'URL', ...allBlocks]];
// Block Entries
console.log('creating block entries sheet');
for (const entry in report) {
const blocks = allBlocks.map((key) => report[entry].blocks.reduce((acc, block) => acc + (block.name === key ? 1 : 0), 0));
const row = [entry, `${site}${entry}`, ...blocks];
ws_data.push(row);
}
const wb = xlsx.utils.book_new();
const ws_entries = xlsx.utils.aoa_to_sheet(ws_data);
xlsx.utils.book_append_sheet(wb, ws_entries, 'Entries');
// Variants
console.log('creating variants sheet');
const allVariants = Object.keys(totals.variants).sort();
const variantsData = [['Path', 'URL', ...allVariants]];
for (const entry in report) {
const variants = allVariants.map((key) => report[entry].blocks.reduce((acc, block) => acc + (block.variant === key ? 1 : 0), 0));
if (variants.reduce((acc, val) => acc + val, 0) > 0) {
const row = [entry, `${site}${entry}`, ...variants];
variantsData.push(row);
}
}
const ws_variants = xlsx.utils.aoa_to_sheet(variantsData);
xlsx.utils.book_append_sheet(wb, ws_variants, 'Variants');
// Links
console.log('creating links sheet');
const linksData = [['Path', 'URL', 'Links']];
for (const entry in report) {
const row = [entry, `${site}${entry}`, ...report[entry].links];
linksData.push(row);
}
const ws_links = xlsx.utils.aoa_to_sheet(linksData);
xlsx.utils.book_append_sheet(wb, ws_links, 'All Links');
// Block Links
console.log('creating block links sheet');
const blockLinksData = [['Path', 'URL', 'Block', 'Index', 'Links']];
for (const entry in report) {
report[entry].blocks.map((block) => {
if (block.links.length === 0) return;
const row = [entry, `${site}${entry}`, block.name, block.index, ...block.links];
blockLinksData.push(row);
});
}
const ws_blocklinks = xlsx.utils.aoa_to_sheet(blockLinksData);
xlsx.utils.book_append_sheet(wb, ws_blocklinks, 'Block Links');
// Totals
console.log('creating totals sheet');
const ws_totals = xlsx.utils.aoa_to_sheet([['Block', 'Total'], ...Object.entries(totals.blocks)]);
xlsx.utils.book_append_sheet(wb, ws_totals, 'Totals');
const dateStr = new Date().toLocaleString('en-US', {
hour12: false, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit',
}).replace(/\/|,|:| /g, '-').replace('--', '_');
const reportDir = `./${REPORT_DIR}/${project}`;
const reportFile = `${reportDir}/Report ${dateStr}.xlsx`;
await mkdir(reportDir, { recursive: true });
await xlsx.writeFile(wb, reportFile);
console.log(`Report written to ${reportFile}`);
}
async function main(project, site, index, cached) {
await mkdir(`./${project}`, { recursive: true });
const report = await runReport(project, site, index, cached);
console.log('totals', report.totals);
await createReports(project, site, report);
}
// node block-report.js <project> <site> <index> <cached>
if (import.meta.url === `file://${process.argv[1]}`) {
const PROJECT = 'bacom-blog';
const SITE = 'https://main--business-website--adobe.hlx.page';
const INDEX = 'bacom-blog/bacom-blog-all.json';
const USE_CACHE = true;
const args = process.argv.slice(2);
const [project = PROJECT, site = SITE, index = INDEX, cached = USE_CACHE] = args;
await main(project, site, index, cached);
process.exit(0);
}