2024-01-07 15:59:33 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2024-04-02 17:48:08 +02:00
|
|
|
import { parseSync } from 'svgson';
|
|
|
|
|
import {
|
|
|
|
|
shuffle,
|
|
|
|
|
readSvgDirectory,
|
|
|
|
|
getCurrentDirPath,
|
|
|
|
|
minifySvg,
|
|
|
|
|
toPascalCase,
|
|
|
|
|
} from './helpers.mjs';
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const currentDir = getCurrentDirPath(import.meta.url);
|
|
|
|
|
const ICONS_DIR = path.resolve(currentDir, '../icons');
|
|
|
|
|
const BASE_URL = 'https://lucide.dev/api/gh-icon';
|
|
|
|
|
|
|
|
|
|
const changedFilesPathString = process.env.CHANGED_FILES;
|
|
|
|
|
|
|
|
|
|
const changedFiles = changedFilesPathString
|
|
|
|
|
.split(' ')
|
|
|
|
|
.map((file) => file.replace('.json', '.svg'))
|
|
|
|
|
.filter((file, idx, arr) => arr.indexOf(file) === idx);
|
|
|
|
|
|
|
|
|
|
const getImageTagsByFiles = (files, getBaseUrl, width) =>
|
2024-03-06 21:00:26 +01:00
|
|
|
files.map((file) => {
|
|
|
|
|
const svgContent = fs.readFileSync(path.join(process.cwd(), file), 'utf-8');
|
|
|
|
|
const strippedAttrsSVG = svgContent.replace(/<svg[^>]*>/, '<svg>');
|
|
|
|
|
const minifiedSvg = minifySvg(strippedAttrsSVG);
|
2024-01-07 15:59:33 +01:00
|
|
|
|
2024-03-06 21:00:26 +01:00
|
|
|
const base64 = Buffer.from(minifiedSvg).toString('base64');
|
|
|
|
|
const url = getBaseUrl(file);
|
|
|
|
|
const widthAttr = width ? `width="${width}"` : '';
|
2024-01-07 15:59:33 +01:00
|
|
|
|
2024-03-06 21:00:26 +01:00
|
|
|
return `<img title="${file}" alt="${file}" ${widthAttr} src="${url}/${base64}.svg"/>`;
|
|
|
|
|
});
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const svgFiles = readSvgDirectory(ICONS_DIR).map((file) => `icons/${file}`);
|
|
|
|
|
|
|
|
|
|
const iconsFilteredByName = (search) => svgFiles.filter((file) => file.includes(search));
|
|
|
|
|
|
|
|
|
|
const cohesionRandomImageTags = getImageTagsByFiles(
|
|
|
|
|
shuffle(svgFiles).slice(0, changedFiles.length),
|
|
|
|
|
() => `${BASE_URL}/stroke-width/2`,
|
2024-03-06 21:00:26 +01:00
|
|
|
).join('');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const cohesionSquaresImageTags = getImageTagsByFiles(
|
|
|
|
|
shuffle(iconsFilteredByName('square')).slice(0, changedFiles.length),
|
|
|
|
|
() => `${BASE_URL}/stroke-width/2`,
|
2024-03-06 21:00:26 +01:00
|
|
|
).join('');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const changeFiles1pxStrokeImageTags = getImageTagsByFiles(
|
|
|
|
|
changedFiles,
|
|
|
|
|
() => `${BASE_URL}/stroke-width/1`,
|
2024-03-06 21:00:26 +01:00
|
|
|
).join('');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const changeFiles2pxStrokeImageTags = getImageTagsByFiles(
|
|
|
|
|
changedFiles,
|
|
|
|
|
() => `${BASE_URL}/stroke-width/2`,
|
2024-03-06 21:00:26 +01:00
|
|
|
).join('');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const changeFiles3pxStrokeImageTags = getImageTagsByFiles(
|
|
|
|
|
changedFiles,
|
|
|
|
|
() => `${BASE_URL}/stroke-width/3`,
|
2024-03-06 21:00:26 +01:00
|
|
|
).join('');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
2024-03-06 21:00:26 +01:00
|
|
|
const changeFilesLowDPIImageTags = getImageTagsByFiles(
|
|
|
|
|
changedFiles,
|
|
|
|
|
() => `${BASE_URL}/dpi/24`,
|
|
|
|
|
).join(' ');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
|
|
|
|
const changeFilesXRayImageTags = getImageTagsByFiles(
|
|
|
|
|
changedFiles,
|
|
|
|
|
(file) => {
|
|
|
|
|
const iconName = path.basename(file, '.svg');
|
|
|
|
|
|
|
|
|
|
return `${BASE_URL}/${iconName}`;
|
|
|
|
|
},
|
|
|
|
|
400,
|
2024-03-06 21:00:26 +01:00
|
|
|
).join(' ');
|
2024-01-07 15:59:33 +01:00
|
|
|
|
2024-04-02 17:48:08 +02:00
|
|
|
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');
|
|
|
|
|
|
2024-01-07 15:59:33 +01:00
|
|
|
const commentMarkup = `\
|
|
|
|
|
### Added or changed icons
|
|
|
|
|
${changeFiles2pxStrokeImageTags}
|
|
|
|
|
<details>
|
|
|
|
|
<summary>Preview cohesion</summary>
|
|
|
|
|
${cohesionSquaresImageTags}<br/>
|
|
|
|
|
${changeFiles2pxStrokeImageTags}<br/>
|
|
|
|
|
${cohesionRandomImageTags}<br/>
|
|
|
|
|
</details>
|
|
|
|
|
<details>
|
|
|
|
|
<summary>Preview stroke widths</summary>
|
|
|
|
|
${changeFiles1pxStrokeImageTags}<br/>
|
|
|
|
|
${changeFiles2pxStrokeImageTags}<br/>
|
|
|
|
|
${changeFiles3pxStrokeImageTags}<br/>
|
|
|
|
|
</details>
|
|
|
|
|
<details>
|
|
|
|
|
<summary>DPI Preview (24px)</summary>
|
|
|
|
|
${changeFilesLowDPIImageTags}<br/>
|
|
|
|
|
</details>
|
|
|
|
|
<details>
|
|
|
|
|
<summary>Icon X-rays</summary>
|
|
|
|
|
${changeFilesXRayImageTags}
|
|
|
|
|
</details>
|
2024-04-02 17:48:08 +02:00
|
|
|
|
|
|
|
|
${
|
|
|
|
|
// collapse code block if it's too long
|
|
|
|
|
readyToUseCode.split('/n').length < 20
|
|
|
|
|
? '### Icons as code'
|
|
|
|
|
: `<details>
|
|
|
|
|
<summary><h3>Icons as code</h3></summary>
|
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
Only working for:
|
2024-04-03 09:54:30 +02:00
|
|
|
\`lucide-react\`, \`lucide-react-native\`, \`lucide-preact\`, \`lucide-vue-next\`
|
2024-04-02 17:48:08 +02:00
|
|
|
\`\`\`ts
|
|
|
|
|
${readyToUseCode}
|
|
|
|
|
\`\`\`${readyToUseCode.split('/n').length < 20 ? '' : '\n\n</details>'}
|
2024-01-07 15:59:33 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
console.log(commentMarkup);
|