-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (78 loc) · 2.6 KB
/
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
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
const puppeteer = require( 'puppeteer' );
const fs = require( 'fs' );
const util = require('util');
const readFile = util.promisify(fs.readFile);
class Analyzer {
async setup() {
this.browser = await puppeteer.launch();
const page = await this.browser.newPage();
await page.goto('https://testgutenberg.com');
this.page = page
}
async teardown() {
this.browser.close();
}
async analyzeFile( path ) {
const json = await readFile( path );
if ( json == "" ) {
return;
}
const post = await JSON.parse( json );
const blocks = await this.analyze( post.content );
return {
blog: post.site_ID,
post: post.ID,
blocks: blocks,
};
}
async analyze( content ) {
if ( this.page === undefined ) {
throw new Error( 'You need to call setup before analyze' );
}
const blockList = await this.page.evaluate( content => {
const nameMap = (block) => block.name;
var blockList = wp.blocks.parse(content).map(nameMap);
if ( blockList.length == 1 && blockList[0] == 'core/freeform' ) {
blockList = wp.blocks.rawHandler( {
HTML: content,
mode: 'BLOCKS'
}).map( block => block.name )
}
return blockList;
}, content);
return blockList.reduce((list, block) => {
list[block] = (list[block] || 0) + 1;
return list
},{});
}
}
const files = process.argv.slice(2);
(async () => {
const analyzer = new Analyzer();
await analyzer.setup();
const results = [];
for ( const file of files ) {
const result = await analyzer.analyzeFile(file);
if ( result !== undefined ) {
results.push( result );
}
}
const totalCount = results.length;
console.log(`Sample size: ${totalCount}`);
const blockCounts = results
.reduce( (accum, post) => {
Object.keys(post.blocks).forEach(key => {
accum[key] = ( accum[key] || 0 ) + 1;
});
return accum;
}, {});
const blockRanking = Object.keys(blockCounts)
.sort( (a,b) => blockCounts[b] - blockCounts[a])
.map( key => {
return {name: key, count: blockCounts[key], percentage: Math.round( 100 * blockCounts[key] / totalCount )};
})
blockRanking.forEach( ({name, count, percentage}) => {
console.log(`${name} ${percentage}% (${count})`);
});
await analyzer.teardown();
})();