shoelace/scripts/make-icons.js

72 wiersze
2.3 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';
import del from 'del';
import download from 'download';
import fm from 'front-matter';
import { readFileSync, mkdirSync } from 'fs';
2021-06-17 21:38:48 +00:00
import { stat, readFile, writeFile } 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');
2021-06-17 21:38:48 +00:00
const iconPackageData = JSON.parse(readFileSync('./node_modules/bootstrap-icons/package.json', 'utf8'));
2020-07-15 21:30:37 +00:00
let numIcons = 0;
(async () => {
try {
2021-06-17 21:38:48 +00:00
const version = iconPackageData.version;
2020-07-18 11:02:28 +00:00
const srcPath = `./.cache/icons/icons-${version}`;
2020-07-15 21:30:37 +00:00
const url = `https://github.com/twbs/icons/archive/v${version}.zip`;
try {
2021-06-17 21:38:48 +00:00
await stat(`${srcPath}/LICENSE.md`);
console.log('Generating icons from cache');
} catch {
// Download the source from GitHub (since not everything is published to NPM)
console.log(`Downloading and extracting Bootstrap Icons ${version} 📦`);
2020-07-18 11:02:28 +00:00
await download(url, './.cache/icons', { extract: true });
}
2020-07-15 21:30:37 +00:00
// Copy icons
console.log(`Copying icons and license`);
2021-02-26 14:09:13 +00:00
await del([iconDir]);
mkdirSync(iconDir, { recursive: true });
2020-07-15 21:30:37 +00:00
await Promise.all([
2021-02-26 14:09:13 +00:00
copy(`${srcPath}/icons`, iconDir),
copy(`${srcPath}/LICENSE.md`, path.join(iconDir, 'LICENSE.md')),
2020-07-15 21:30:37 +00:00
copy(`${srcPath}/bootstrap-icons.svg`, './docs/assets/icons/sprite.svg', { overwrite: true })
]);
// Generate metadata
console.log(`Generating icon metadata`);
const files = await globby(`${srcPath}/docs/content/icons/**/*.md`);
2020-07-15 21:30:37 +00:00
const metadata = await Promise.all(
files.map(async file => {
const name = path.basename(file, path.extname(file));
const data = fm(await readFile(file, 'utf8')).attributes;
numIcons++;
2020-07-15 21:30:37 +00:00
return {
name,
title: data.title,
categories: data.categories,
tags: data.tags
};
})
);
2020-07-15 21:30:37 +00:00
2021-06-17 21:38:48 +00:00
await writeFile(path.join(iconDir, 'icons.json'), JSON.stringify(metadata, null, 2), 'utf8');
2020-07-15 21:30:37 +00:00
console.log(chalk.cyan(`Successfully processed ${numIcons} icons ✨\n`));
2020-07-15 21:30:37 +00:00
} catch (err) {
console.error(err);
}
})();