shoelace/scripts/make-icons.js

65 wiersze
2.0 KiB
JavaScript
Czysty Zwykły widok Historia

2021-02-26 14:09:13 +00:00
//
// This script downloads and generates icons and icon metadata.
//
2021-06-17 21:38:48 +00:00
import chalk from 'chalk';
import commandLineArgs from 'command-line-args';
2021-06-17 21:38:48 +00:00
import copy from 'recursive-copy';
2022-07-27 20:17:23 +00:00
import { deleteAsync } from 'del';
2021-06-17 21:38:48 +00:00
import download from 'download';
import fm from 'front-matter';
2023-06-07 17:28:22 +00:00
import fs from 'fs/promises';
import { globby } from 'globby';
2021-06-17 21:38:48 +00:00
import path from 'path';
2020-07-15 21:30:37 +00:00
const { outdir } = commandLineArgs({ name: 'outdir', type: String });
const iconDir = path.join(outdir, '/assets/icons');
2023-06-07 17:28:22 +00:00
const iconPackageData = JSON.parse(await fs.readFile('./node_modules/bootstrap-icons/package.json', 'utf8'));
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
try {
const version = iconPackageData.version;
const srcPath = `./.cache/icons/icons-${version}`;
const url = `https://github.com/twbs/icons/archive/v${version}.zip`;
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
try {
await fs.stat(`${srcPath}/LICENSE.md`);
} catch {
// Download the source from GitHub (since not everything is published to NPM)
console.log(`Downloading and extracting Bootstrap Icons ${version}...`);
await download(url, './.cache/icons', { extract: true });
}
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
// Copy icons
console.log(`Copying icons and license...`);
await deleteAsync([iconDir]);
await fs.mkdir(iconDir, { recursive: true });
await Promise.all([
copy(`${srcPath}/icons`, iconDir),
copy(`${srcPath}/LICENSE.md`, path.join(iconDir, 'LICENSE.md')),
copy(`${srcPath}/bootstrap-icons.svg`, './docs/assets/images/sprite.svg', { overwrite: true })
]);
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
// Generate metadata
const files = await globby(`${srcPath}/docs/content/icons/**/*.md`);
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
console.log(`Generating metadata for ${files.length} icons...`);
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
const metadata = await Promise.all(
files.map(async file => {
const name = path.basename(file, path.extname(file));
const data = fm(await fs.readFile(file, 'utf8')).attributes;
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
return {
name,
title: data.title,
categories: data.categories,
tags: data.tags
};
})
);
2020-07-15 21:30:37 +00:00
2023-06-07 17:28:22 +00:00
await fs.writeFile(path.join(iconDir, 'icons.json'), JSON.stringify(metadata, null, 2), 'utf8');
} catch (err) {
console.error(err);
}