shoelace/scripts/make-icons.js

56 wiersze
1.7 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 20:14:25 +00:00
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 20:14:25 +00:00
try {
await fs.stat(`${srcPath}/LICENSE.md`);
} catch {
2023-06-12 20:22:20 +00:00
// Download the source from GitHub (since not everything is published to npm)
2023-06-07 20:14:25 +00:00
await download(url, './.cache/icons', { extract: true });
2023-06-07 17:28:22 +00:00
}
2023-06-07 20:14:25 +00:00
// Copy icons
await deleteAsync([iconDir]);
await fs.mkdir(iconDir, { recursive: true });
await Promise.all([
copy(`${srcPath}/icons`, iconDir),
2023-08-10 16:59:44 +00:00
copy(`${srcPath}/LICENSE`, path.join(iconDir, 'LICENSE')),
2023-06-07 20:14:25 +00:00
copy(`${srcPath}/bootstrap-icons.svg`, './docs/assets/images/sprite.svg', { overwrite: true })
]);
// Generate metadata
const files = await globby(`${srcPath}/docs/content/icons/**/*.md`);
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;
return {
name,
title: data.title,
categories: data.categories,
tags: data.tags
};
})
);
await fs.writeFile(path.join(iconDir, 'icons.json'), JSON.stringify(metadata, null, 2), 'utf8');