shoelace/scripts/make-icons.cjs

69 wiersze
2.2 KiB
JavaScript
Czysty Zwykły widok Historia

2021-02-26 14:09:13 +00:00
//
// This script downloads and generates icons and icon metadata.
//
2020-07-15 21:30:37 +00:00
const Promise = require('bluebird');
const promisify = require('util').promisify;
const chalk = require('chalk');
const copy = require('recursive-copy');
const del = require('del');
const download = require('download');
2021-02-26 14:09:13 +00:00
const mkdirp = require('mkdirp');
2020-07-15 21:30:37 +00:00
const fm = require('front-matter');
const fs = require('fs').promises;
const glob = promisify(require('glob'));
const path = require('path');
2021-02-26 14:09:13 +00:00
const baseDir = path.dirname(__dirname);
const iconDir = './dist/assets/icons';
2020-07-15 21:30:37 +00:00
let numIcons = 0;
(async () => {
try {
2021-02-26 14:09:13 +00:00
const version = require('bootstrap-icons/package').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 {
await fs.stat(`${srcPath}/LICENSE.md`);
2021-02-26 14:09:13 +00:00
console.log(chalk.cyan('Generating icons from cache'));
} catch {
// Download the source from GitHub (since not everything is published to NPM)
2020-07-18 11:02:28 +00:00
console.log(chalk.cyan(`Downloading and extracting Bootstrap Icons ${version} 📦`));
await download(url, './.cache/icons', { extract: true });
}
2020-07-15 21:30:37 +00:00
// Copy icons
2021-02-26 14:09:13 +00:00
console.log(chalk.cyan(`Copying icons and license`));
await del([iconDir]);
await mkdirp(iconDir);
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
2021-02-26 14:09:13 +00:00
console.log(chalk.cyan(`Generating icon metadata`));
2020-07-15 21:30:37 +00:00
const files = await glob(`${srcPath}/docs/content/icons/**/*.md`);
const metadata = await Promise.map(files, async file => {
const name = path.basename(file, path.extname(file));
const data = fm(await fs.readFile(file, 'utf8')).attributes;
numIcons++;
return {
name,
title: data.title,
categories: data.categories,
tags: data.tags
};
});
2021-02-26 14:09:13 +00:00
await fs.writeFile(path.join(iconDir, 'icons.json'), JSON.stringify(metadata, null, 2), 'utf8');
2020-07-15 21:30:37 +00:00
2021-02-26 14:09:13 +00:00
console.log(chalk.green(`Successfully processed ${numIcons} icons ✨\n`));
2020-07-15 21:30:37 +00:00
} catch (err) {
console.error(err);
}
})();