Skip to content

Commit

Permalink
ci: Improve build speeds (#2778)
Browse files Browse the repository at this point in the history
* Revert sync to async functions

* Replace more sync fs functions

* Format files

* Fix build svelte package
  • Loading branch information
ericfennis authored Feb 10, 2025
1 parent e28426a commit 50630b3
Show file tree
Hide file tree
Showing 46 changed files with 236 additions and 162 deletions.
18 changes: 10 additions & 8 deletions docs/scripts/writeCategoriesMetadata.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import fs from 'fs';
import fs from 'fs/promises';
import path from 'path';

const currentDir = process.cwd();
const dataDirectory = path.resolve(currentDir, '.vitepress/data');
const directory = path.join(process.cwd(), '../categories');

function getAllCategoryFiles() {
const fileNames = fs.readdirSync(directory).filter((file) => path.extname(file) === '.json');
async function getAllCategoryFiles() {
const categoryDirectoryContents = await fs.readdir(directory);
const fileNames = categoryDirectoryContents.filter((file) => path.extname(file) === '.json');

return fileNames.map((fileName) => {
const categoryJSONReadPromises = fileNames.map(async (fileName) => {
const name = path.basename(fileName, '.json');
const fileContent = fs.readFileSync(path.join(directory, fileName), 'utf8');
const fileContent = await fs.readFile(path.join(directory, fileName), 'utf8');

const parsedFileContent = JSON.parse(fileContent);

Expand All @@ -19,14 +20,15 @@ function getAllCategoryFiles() {
title: parsedFileContent.title,
};
});

return Promise.all(categoryJSONReadPromises);
}

const categoriesFile = path.resolve(dataDirectory, `categoriesData.json`);

const categoriesData = getAllCategoryFiles();
const categoriesData = await getAllCategoryFiles();

fs.promises
.writeFile(categoriesFile, JSON.stringify(categoriesData, null, 2), 'utf-8')
fs.writeFile(categoriesFile, JSON.stringify(categoriesData, null, 2), 'utf-8')
.then(() => {
console.log('Successfully written categoriesData.json file');
})
Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/writeIconDetails.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readSvgDirectory, toCamelCase } from '@lucide/helpers';

const currentDir = process.cwd();
const ICONS_DIR = path.resolve(currentDir, '../icons');
const icons = readSvgDirectory(ICONS_DIR, '.json');
const icons = await readSvgDirectory(ICONS_DIR, '.json');

const iconDetailsDirectory = path.resolve(currentDir, '.vitepress/data', 'iconDetails');

Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/writeIconMetaIndex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readSvgDirectory, toCamelCase } from '@lucide/helpers';

const currentDir = process.cwd();
const ICONS_DIR = path.resolve(currentDir, '../icons');
const iconJsonFiles = readSvgDirectory(ICONS_DIR, '.json');
const iconJsonFiles = await readSvgDirectory(ICONS_DIR, '.json');

const location = path.resolve(currentDir, '.vitepress/data', 'iconMetaData.ts');

Expand Down
6 changes: 3 additions & 3 deletions docs/scripts/writeIconNodes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { readSvgDirectory, toCamelCase } from '@lucide/helpers';

const currentDir = process.cwd();
const ICONS_DIR = path.resolve(currentDir, '../icons');
const svgFiles = readSvgDirectory(ICONS_DIR);
const icons = renderIconsObject(svgFiles, ICONS_DIR, true);
const svgFiles = await readSvgDirectory(ICONS_DIR);
const icons = await renderIconsObject(svgFiles, ICONS_DIR, true);

const iconNodesDirectory = path.resolve(currentDir, '.vitepress/data', 'iconNodes');

Expand All @@ -32,7 +32,7 @@ const writeIconFiles = Object.entries(icons).map(async ([iconName, { children }]
await fs.promises.writeFile(location, output, 'utf-8');

iconIndexFileImports.push(
`import ${toCamelCase(iconName)}Node from './${iconName}.node.json' assert { type: "json" };`,
`import ${toCamelCase(iconName)}Node from './${iconName}.node.json' with { type: "json" };`,
);
iconIndexFileExports.push(` ${toCamelCase(iconName)}Node as ${toCamelCase(iconName)},`);
iconIndexFileDefaultExports.push(` '${iconName}': ${toCamelCase(iconName)}Node,`);
Expand Down
10 changes: 5 additions & 5 deletions docs/scripts/writeIconRelatedIcons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readSvgDirectory } from '@lucide/helpers';

const currentDir = process.cwd();
const ICONS_DIR = path.resolve(currentDir, '../icons');
const svgFiles = readSvgDirectory(ICONS_DIR, '.json');
const svgFiles = await readSvgDirectory(ICONS_DIR, '.json');

const location = path.resolve(currentDir, '.vitepress/data', 'relatedIcons.json');

Expand All @@ -18,9 +18,7 @@ const categoryWeight = 3;

const MAX_RELATED_ICONS = 4 * 17; // grid of 4x17 icons, = 68 icons

const arrayMatches = (a, b) => {
return a.filter((item) => b.includes(item)).length;
};
const arrayMatches = (a, b) => a.filter((item) => b.includes(item)).length;

const nameParts = (icon) =>
[
Expand All @@ -36,6 +34,7 @@ const getRelatedIcons = (currentIcon, icons) => {
nameWeight * arrayMatches(nameParts(item), nameParts(currentIcon)) +
categoryWeight * arrayMatches(item.categories ?? [], currentIcon.categories ?? []) +
tagWeight * arrayMatches(item.tags ?? [], currentIcon.tags ?? []);

return icons
.filter((i) => i.name !== currentIcon.name)
.map((icon) => ({ icon, similarity: iconSimilarity(icon) }))
Expand All @@ -46,7 +45,8 @@ const getRelatedIcons = (currentIcon, icons) => {
};

const iconsMetaDataPromises = svgFiles.map(async (iconName) => {
const metaData = JSON.parse(fs.readFileSync(`../icons/${iconName}`));
const metaDataFileContent = await fs.promises.readFile(`../icons/${iconName}`);
const metaData = JSON.parse(metaDataFileContent);

const name = iconName.replace('.json', '');

Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/writeReleaseMetadata.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const git = simpleGit();

const currentDir = process.cwd();
const ICONS_DIR = path.resolve(currentDir, '../icons');
const iconJsonFiles = readSvgDirectory(ICONS_DIR, '.json');
const iconJsonFiles = await readSvgDirectory(ICONS_DIR, '.json');
const location = path.resolve(currentDir, '.vitepress/data', 'releaseMetaData.json');
const releaseMetaDataDirectory = path.resolve(currentDir, '.vitepress/data', 'releaseMetadata');

Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/writeVercelOutput.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ const output = JSON.stringify(vercelRouteConfig, null, 2);

const vercelOutputJSON = path.resolve(currentDir, '.vercel/output/config.json');

fs.writeFileSync(vercelOutputJSON, output, 'utf-8');
await fs.promises.writeFile(vercelOutputJSON, output, 'utf-8');
11 changes: 9 additions & 2 deletions packages/lucide-angular/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `\
Expand Down
2 changes: 1 addition & 1 deletion packages/lucide-preact/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import plugins from '@lucide/rollup-plugins';
import dts from 'rollup-plugin-dts';
import pkg from './package.json' assert { type: 'json' };
import pkg from './package.json' with { type: 'json' };

const packageName = 'LucidePreact';
const outputFileName = 'lucide-preact';
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-preact/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-react-native/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-react/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-solid/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `
Expand Down
12 changes: 7 additions & 5 deletions packages/lucide-static/scripts/buildLib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ const license = `@license ${pkg.name} v${pkg.version} - ${pkg.license}`;
createDirectory(LIB_DIR);
createDirectory(ICON_MODULE_DIR);

const svgFiles = readSvgDirectory(ICONS_DIR);
const svgs = readSvgs(svgFiles, ICONS_DIR);
const svgFiles = await readSvgDirectory(ICONS_DIR);
const svgs = await readSvgs(svgFiles, ICONS_DIR);

const parsedSvgs = svgs.map(({ name, contents }) => ({
name,
contents,
parsedSvg: parseSync(contents),
}));

generateSprite(parsedSvgs, PACKAGE_DIR, license);
generateIconNodes(parsedSvgs, PACKAGE_DIR);
copyIcons(parsedSvgs, PACKAGE_DIR, license);
await Promise.all([
generateSprite(parsedSvgs, PACKAGE_DIR, license),
generateIconNodes(parsedSvgs, PACKAGE_DIR),
copyIcons(parsedSvgs, PACKAGE_DIR, license),
]);
4 changes: 2 additions & 2 deletions packages/lucide-static/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
let svgContents = getSvg();
export default async ({ componentName, iconName, getSvg, deprecated, deprecationReason }) => {
let svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

svgContents = svgContents.replace(
Expand Down
4 changes: 2 additions & 2 deletions packages/lucide-static/scripts/generateIconNodes.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeFile } from '@lucide/helpers';

export default function generateIconNodes(parsedSvgs, packageDir) {
export default async function generateIconNodes(parsedSvgs, packageDir) {
const iconNodes = parsedSvgs.reduce((acc, { name, parsedSvg }) => {
acc[name] = parsedSvg.children.map(({ name, attributes }) => [name, attributes]);

Expand All @@ -9,5 +9,5 @@ export default function generateIconNodes(parsedSvgs, packageDir) {

const iconNodesStringified = JSON.stringify(iconNodes, null, 2);

writeFile(iconNodesStringified, 'icon-nodes.json', packageDir);
await writeFile(iconNodesStringified, 'icon-nodes.json', packageDir);
}
6 changes: 3 additions & 3 deletions packages/lucide-static/scripts/generateSprite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { stringify } from 'svgson';
import { format } from 'prettier';
import { appendFile } from '@lucide/helpers';

export default function generateSprite(svgs, packageDir, license) {
export default async function generateSprite(svgs, packageDir, license) {
const symbols = svgs.map(({ name, parsedSvg }) => ({
name: 'symbol',
type: 'element',
Expand Down Expand Up @@ -34,6 +34,6 @@ export default function generateSprite(svgs, packageDir, license) {

const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>\n<!-- ${license} -->\n`;

appendFile(xmlMeta, `sprite.svg`, packageDir);
appendFile(prettifiedSprite, `sprite.svg`, packageDir);
await appendFile(xmlMeta, `sprite.svg`, packageDir);
await appendFile(prettifiedSprite, `sprite.svg`, packageDir);
}
6 changes: 4 additions & 2 deletions packages/lucide-static/scripts/readSvgs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { readSvg } from '@lucide/helpers';
* @returns {Object}
*/
export default function readSVGs(svgFiles, iconsDirectory) {
return svgFiles.map((svgFile) => {
const SVGReadPromises = svgFiles.map(async (svgFile) => {
const name = basename(svgFile, '.svg');
const contents = readSvg(svgFile, iconsDirectory);
const contents = await readSvg(svgFile, iconsDirectory);

return { name, contents };
});

return Promise.all(SVGReadPromises);
}
2 changes: 1 addition & 1 deletion packages/lucide-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"build:icons": "build-icons --output=./src --templateSrc=./scripts/exportTemplate.mjs --exportFileName=index.ts --iconFileExtension=.svelte --importImportFileExtension=.svelte --separateIconFileExport --separateIconFileExportExtension=.ts --withAliases --aliasesFileExtension=.ts --separateAliasesFile --separateAliasesFileExtension=.ts --aliasImportFileExtension=.js --pretty=false",
"build:package": "svelte-package --input ./src",
"build:license": "node ./scripts/appendBlockComments.mjs",
"test": "pnpm build:icons && vitest run",
"test": "pnpm copy:license && pnpm build:icons && vitest run",
"test:watch": "vitest watch",
"version": "pnpm version --git-tag-version=false"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/lucide-svelte/scripts/appendBlockComments.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import { getCurrentDirPath } from '@lucide/helpers';
import { getJSBanner } from './license.mjs';

const currentDir = getCurrentDirPath(import.meta.url);
const currentDir = await getCurrentDirPath(import.meta.url);
const targetDirectory = path.join(currentDir, '../dist');

const files = await readdir(targetDirectory, {
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-svelte/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';
import { getJSBanner } from './license.mjs';

export default ({ iconName, children, componentName, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
iconName,
children,
componentName,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `\
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-vue-next/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `
Expand Down
11 changes: 9 additions & 2 deletions packages/lucide-vue/scripts/exportTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import base64SVG from '@lucide/build-icons/utils/base64SVG.mjs';

export default ({ componentName, iconName, children, getSvg, deprecated, deprecationReason }) => {
const svgContents = getSvg();
export default async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);

return `
Expand Down
Loading

0 comments on commit 50630b3

Please sign in to comment.