Skip to content

Commit ead86ae

Browse files
Elliott Marquezcopybara-github
authored andcommitted
build(catalog): node scripts to copy readmes and stories
PiperOrigin-RevId: 534960601
1 parent a7a061f commit ead86ae

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

catalog/scripts/copy-readmes.mjs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import {cp, readFile, writeFile} from 'fs/promises';
8+
import {join, relative} from 'path';
9+
import tinyGlob from 'tiny-glob';
10+
11+
/**
12+
* Recursively copies the images from
13+
*
14+
* /docs/components/images
15+
* to
16+
* /catalog/site/components/images
17+
*/
18+
async function copyImages() {
19+
await cp(
20+
join('..', 'docs', 'components', 'images'),
21+
join('site', 'components', 'images'), {recursive: true}, (err) => {
22+
if (err) throw err;
23+
});
24+
}
25+
26+
/**
27+
* Finds and returns all the filepaths of markdown files in
28+
* /docs/components/
29+
*
30+
* @return A promise of all the markdwon filepaths in /docs/components/
31+
*/
32+
async function getReadmeFiles() {
33+
const readmeFilesGlob = ['../docs/components/**/*.md'];
34+
const readmeFiles = readmeFilesGlob.map(async (entry) => tinyGlob(entry));
35+
return (await Promise.all(readmeFiles)).flat();
36+
}
37+
38+
/**
39+
* Transforms to apply to the markdown files
40+
*/
41+
const transforms = [
42+
// catalog-only code comments are removed
43+
{
44+
before: /<!-- catalog-only-start -->(\n)*?<!--\s*/gm,
45+
after: '',
46+
},
47+
{
48+
before: /\s*-->(\n)*?<!-- catalog-only-end -->/gm,
49+
after: '',
50+
},
51+
// removes everything in between github-only-start and github-only-end
52+
// comments
53+
{
54+
before:
55+
/\s*<!-- github-only-start -->(.|\n)*?<!-- github-only-end -->\s*/gm,
56+
after: '\n\n',
57+
},
58+
// eleventy pages end with `/` so `components/checkbox.md` will turn into the
59+
// url `/components/checkbox`. Thus we need to transform the relative
60+
// `./images` links to `../images`.
61+
{
62+
before: /images\//gm,
63+
after: '../images/',
64+
},
65+
];
66+
67+
/**
68+
* Applies the transforms to readme files at the given filepaths and outputs the
69+
* result to /catalog/site/components/<component-name>.md
70+
*
71+
* @param {Array<string>} filepaths The readme file paths to transform.
72+
*/
73+
async function transformReadmes(filepaths) {
74+
const readmePromises = filepaths.map(async (entry) => {
75+
let readme = await readFile(entry, 'utf8');
76+
console.log(`Transforming ${entry}`);
77+
78+
transforms.forEach((transform) => {
79+
readme = readme.replaceAll(transform.before, transform.after);
80+
});
81+
82+
// The `components/<component-name>.md` path.
83+
const localPath = relative(join('..', 'docs'), entry);
84+
// The output path at
85+
// /catalog/site/components/<?local path>/<component name>.md
86+
const outputPath = join('site', localPath);
87+
console.log(`Writing trasnformed file to: ${outputPath}`);
88+
return writeFile(outputPath, readme);
89+
});
90+
91+
await Promise.all(readmePromises);
92+
}
93+
94+
const readmeFiles = await getReadmeFiles();
95+
96+
console.log('Copying images...');
97+
await copyImages();
98+
console.log('Images copied!');
99+
console.log('Transforming readmes...');
100+
await transformReadmes(readmeFiles);
101+
console.log('Transformations complete!');

catalog/scripts/copy-stories.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import {cp} from 'fs/promises';
8+
import {join, parse} from 'path';
9+
import tinyGlob from 'tiny-glob';
10+
11+
// Glob(s) from which to copy story files
12+
const storyDirectories = ['../*/demo'];
13+
const dirPromises = storyDirectories.map(async (entry) => tinyGlob(entry));
14+
const directories = (await Promise.all(dirPromises)).flat();
15+
16+
const parsedDirectories = directories.map((entry) => {
17+
// get the component name from the directory name
18+
const componentName = parse(parse(entry).dir).base;
19+
// set the destination to /catalog/stories/<component name>
20+
const destination = join('.', 'stories', componentName);
21+
22+
console.log(`Copying ${entry} to ${destination}`);
23+
24+
// recursively copy the files
25+
return cp(entry, destination, {recursive: true}, (err) => {
26+
if (err) throw err;
27+
});
28+
});
29+
30+
await Promise.all(parsedDirectories);

0 commit comments

Comments
 (0)