-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathpfe-assets.cjs
More file actions
109 lines (95 loc) · 3.82 KB
/
Copy pathpfe-assets.cjs
File metadata and controls
109 lines (95 loc) · 3.82 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
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
const fs = require('fs');
const path = require('path');
/**
* Generate a map of files per package which should be copied to the site dir
* @param {object} [options]
* @param {string} [options.prefix='pf-v5'] element prefix e.g. 'pf-v5' for 'pf-v6-button'
*/
function getFilesToCopy(options) {
const cwd = process.cwd();
const prefix = `${(options?.prefix ?? 'pf').replace(/-$/, '')}-`;
const hasCore = fs.existsSync(path.join(cwd, 'core'));
if (!hasCore) {
return null;
}
const files = {};
const corePkgs = fs.readdirSync(path.join(cwd, 'core'));
Object.assign(files, Object.fromEntries(corePkgs.map(dir => [
`core/${dir}`,
`core/${dir.replace(prefix, '')}`,
])));
return files;
}
const DEMO_PATHS_RE =
// eslint-disable-next-line @stylistic/max-len
/(?<attr>href|src)="\/(?<workspace>elements|core)\/pf-v5-(?<unprefixed>.*)\/(?<filename>.*)\.(?<extension>[.\w]+)"/g;
/**
* Replace paths in demo files from the dev SPA's format to 11ty's format
* @param {string} content demo file
*/
function demoPaths(content) {
if (this.outputPath.match(/(components|core|tools)\/.*\/demo\/index\.html$/)) {
return content.replace(DEMO_PATHS_RE, (...args) => {
const [{ attr, workspace, unprefixed, filename, extension }] = args.reverse();
return `${attr}="/${workspace === 'elements' ? 'components' : workspace}/${unprefixed}/${filename}.${extension}"`;
});
} else {
return content;
}
}
module.exports = {
configFunction(eleventyConfig, options) {
eleventyConfig.addPassthroughCopy('docs/images/favicon.ico');
eleventyConfig.addPassthroughCopy('docs/bundle.{js,map,ts}');
eleventyConfig.addPassthroughCopy('docs/demo.{js,map,ts}');
eleventyConfig.addPassthroughCopy('docs/main.mjs');
eleventyConfig.addPassthroughCopy({
'node_modules/@rhds/elements': '/assets/@rhds',
});
eleventyConfig.addPassthroughCopy({
'node_modules/@patternfly/icons/': '/assets/@patternfly/icons/',
});
eleventyConfig.addPassthroughCopy({
'elements': '/assets/@patternfly/elements',
});
eleventyConfig.addPassthroughCopy({
'./core/pfe-core': '/assets/@patternfly/pfe-core',
});
eleventyConfig.addPassthroughCopy({
'tools/pfe-tools': '/assets/@patternfly/pfe-tools',
});
eleventyConfig.addPassthroughCopy('brand/**/*');
// Copy static assets (screenshots, demo images/css/js) from element folders
// to the site with the tag prefix stripped from the directory name.
// Markdown and HTML are handled by 11ty templates; this covers everything else.
const prefix = `${(options?.prefix ?? 'pf').replace(/-$/, '')}-`;
for (const dir of fs.readdirSync(path.join(process.cwd(), 'elements'))) {
const slug = dir.replace(prefix, '');
const screenshot = path.join('elements', dir, 'docs', 'screenshot.png');
if (fs.existsSync(screenshot)) {
eleventyConfig.addPassthroughCopy({
[screenshot]: `/components/${slug}/docs/screenshot.png`,
});
}
const demoDir = path.join(process.cwd(), 'elements', dir, 'demo');
if (fs.existsSync(demoDir)) {
const assets =
fs.readdirSync(demoDir, { recursive: true })
.filter(f => !String(f).endsWith('.html'));
for (const asset of assets) {
const src = path.join('elements', dir, 'demo', String(asset));
eleventyConfig.addPassthroughCopy({
[src]: `/components/${slug}/demo/${asset}`,
});
}
}
}
const filesToCopy = getFilesToCopy(options);
if (filesToCopy) {
eleventyConfig.addPassthroughCopy(filesToCopy);
}
// The demo files are written primarily for the dev SPA (`npm start`),
// so here we transform the paths found in those files to match the docs site's file structure
eleventyConfig.addTransform('demo-paths', demoPaths);
},
};