-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmanifest.js
130 lines (119 loc) · 3.54 KB
/
manifest.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
/**
* External dependencies
*/
const { pascalCase } = require( 'change-case' );
const fs = require( 'fs' );
const glob = require( 'glob' ).sync;
const { join } = require( 'path' );
const baseRepoUrl = '..';
const componentPaths = glob( 'packages/components/src/*/**/README.md', {
// Don't expose documentation for mobile only and G2 components just yet.
ignore: [
'**/src/mobile/**/README.md',
'**/src/ui/**/README.md',
'packages/components/src/theme/README.md',
'packages/components/src/view/README.md',
'packages/components/src/dropdown-menu-v2/README.md',
],
} );
const packagePaths = glob( 'packages/*/package.json' )
.filter(
// Ignore private packages.
( fileName ) =>
! require( join( __dirname, '..', '..', fileName ) ).private
)
.map( ( fileName ) => fileName.split( '/' )[ 1 ] );
/**
* Generates the package manifest.
*
* @param {Array} packageFolderNames Package folder names.
*
* @return {Array} Manifest
*/
function getPackageManifest( packageFolderNames ) {
return packageFolderNames.reduce( ( manifest, folderName ) => {
const path = `${ baseRepoUrl }/packages/${ folderName }/README.md`;
const tocPath = `${ baseRepoUrl }/packages/${ folderName }/docs/toc.json`;
// First add any README files to the TOC
manifest.push( {
title: `@wordpress/${ folderName }`,
slug: `packages-${ folderName }`,
markdown_source: path,
parent: 'packages',
} );
// Next add any items in the docs/toc.json if found.
if ( fs.existsSync( join( __dirname, '..', tocPath ) ) ) {
const toc = require( join( __dirname, '..', tocPath ) ).values();
manifest.push( ...toc );
}
return manifest;
}, [] );
}
/**
* Generates the components manifest.
*
* @param {Array} paths Paths for all components
*
* @return {Array} Manifest
*/
function getComponentManifest( paths ) {
return paths.map( ( filePath ) => {
const pathFragments = filePath.split( '/' );
const slug = pathFragments[ pathFragments.length - 2 ];
return {
title: pascalCase( slug ),
slug,
markdown_source: `${ baseRepoUrl }/${ filePath }`,
parent: 'components',
};
} );
}
function getRootManifest( tocFileName ) {
return generateRootManifestFromTOCItems( require( tocFileName ) );
}
function generateRootManifestFromTOCItems( items, parent = null ) {
let pageItems = [];
items.forEach( ( obj ) => {
const fileName = Object.keys( obj )[ 0 ];
const children = obj[ fileName ];
const fileNameFragments = fileName.split( '/' );
let slug = fileNameFragments[ fileNameFragments.length - 1 ].replace(
'.md',
''
);
if ( 'readme' === slug.toLowerCase() ) {
slug = fileNameFragments[ fileNameFragments.length - 2 ];
// Special case - the root 'docs' readme needs the 'handbook' slug.
if ( parent === null && 'docs' === slug ) {
slug = 'handbook';
}
}
let title = pascalCase( slug );
const markdownSource = fs.readFileSync( fileName, 'utf8' );
const titleMarkdown = markdownSource.match( /^#\s(.+)$/m );
if ( titleMarkdown ) {
title = titleMarkdown[ 1 ];
}
pageItems.push( {
title,
slug,
markdown_source: `${ baseRepoUrl }\/${ fileName }`,
parent,
} );
if ( Array.isArray( children ) && children.length ) {
pageItems = pageItems.concat(
generateRootManifestFromTOCItems( children, slug )
);
} else if ( children === '{{components}}' ) {
pageItems = pageItems.concat(
getComponentManifest( componentPaths )
);
} else if ( children === '{{packages}}' ) {
pageItems = pageItems.concat( getPackageManifest( packagePaths ) );
}
} );
return pageItems;
}
module.exports = {
getRootManifest,
};