Skip to content

Commit

Permalink
chore: build script persists optimized SVGs
Browse files Browse the repository at this point in the history
  • Loading branch information
liamdebeasi committed Mar 22, 2024
1 parent b63d12b commit 10ac9f2
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ async function build(rootDir: string) {
await createCheatsheet(version, rootDir, distDir, svgSymbolsContent, srcSvgData);

await createWebTypes(version, rootDir, distDir, srcSvgData);

await copyToTesting(rootDir, distDir, srcSvgData);

await Promise.all([
await copyToTesting(rootDir, distDir, srcSvgData),
/**
* Writes the optimized SVGs back to its source directory. This directory is where
* SVGs are pulled from when previewing Ionicons in the Stencil dev server.
* As a result, we want to ensure that what we are testing is that same as
* what users will see in production.
*/
await writeSVGData(srcSvgDir, srcSvgData)
]);
} catch (e) {
console.error(e);
process.exit(1);
Expand Down Expand Up @@ -192,18 +201,27 @@ async function optimizeSvg(
await fs.writeFile(svgData.distSvgFilePath, sourceSvg.data);
}

/**
* Writes SVG data to a directory.
* @param distDir - The directory to write the SVG files
* @param svgData - An array of SVG data to write
*/
async function writeSVGData(distDir: string, svgData: SvgData[]) {
await Promise.all(
svgData.map(async (svgData) => {
const outPath = join(distDir, svgData.fileName);
await fs.writeFile(outPath, svgData.optimizedSvgContent);
}),
);
}

async function copyToTesting(rootDir: string, distDir: string, srcSvgData: SvgData[]) {
const testDir = join(rootDir, 'www');
const testBuildDir = join(testDir, 'build');
const testSvgDir = join(testBuildDir, 'svg');
await fs.ensureDir(testSvgDir);

await Promise.all(
srcSvgData.map(async (svgData) => {
const testSvgFilePath = join(testSvgDir, svgData.fileName);
await fs.writeFile(testSvgFilePath, svgData.optimizedSvgContent);
}),
);

await writeSVGData(testSvgDir, srcSvgData);

const distCheatsheetFilePath = join(distDir, 'cheatsheet.html');
const testCheatsheetFilePath = join(testDir, 'cheatsheet.html');
Expand Down

0 comments on commit 10ac9f2

Please sign in to comment.