Skip to content

Commit

Permalink
feat: include code snippet in preview comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jguddas committed Mar 10, 2024
1 parent 16ee591 commit 8b5f444
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ jobs:
comment-author: 'github-actions[bot]'
body-includes: Added or changed icons

- uses: actions/setup-node@v4
- name: Install svgo for code preview (safer and faster than installing all deps)
run: npm install svgo

- name: Generate comment markup
run: node ./scripts/generateChangedIconsCommentMarkup.mjs >> comment-markup.md
id: comment-markup
Expand Down
26 changes: 25 additions & 1 deletion scripts/generateChangedIconsCommentMarkup.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import fs from 'fs';
import path from 'path';
import { shuffle, readSvgDirectory, getCurrentDirPath, minifySvg } from './helpers.mjs';
import { parseSync } from 'svgson';
import {
shuffle,
readSvgDirectory,
getCurrentDirPath,
minifySvg,
toPascalCase,
} from './helpers.mjs';

const currentDir = getCurrentDirPath(import.meta.url);
const ICONS_DIR = path.resolve(currentDir, '../icons');
Expand Down Expand Up @@ -70,6 +77,18 @@ const changeFilesXRayImageTags = getImageTagsByFiles(
400,
).join(' ');

const readyToUseCode = changedFiles
.map((changedFile) => {
const svgContent = fs.readFileSync(path.join(process.cwd(), changedFile), 'utf-8');
const name = path.basename(changedFile, '.svg');
return `const ${toPascalCase(name)}Icon = createLucideIcon('${toPascalCase(name)}', [
${parseSync(svgContent)
.children.map(({ name, attributes }) => JSON.stringify([name, attributes]))
.join(',\n ')}
])`;
})
.join('\n\n');

const commentMarkup = `\
### Added or changed icons
${changeFiles2pxStrokeImageTags}
Expand All @@ -93,6 +112,11 @@ ${changeFilesLowDPIImageTags}<br/>
<summary>Icon X-rays</summary>
${changeFilesXRayImageTags}
</details>
### Icons as code
\`\`\`ts
${readyToUseCode}
\`\`\`
`;

console.log(commentMarkup);

0 comments on commit 8b5f444

Please sign in to comment.