2023-05-09 23:29:37 +02:00
|
|
|
/* eslint-disable no-restricted-syntax, no-await-in-loop */
|
2023-05-09 20:46:16 +02:00
|
|
|
import { CleanOptions, simpleGit } from 'simple-git';
|
2023-05-08 12:17:34 +02:00
|
|
|
import semver from 'semver';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2023-05-09 20:46:16 +02:00
|
|
|
import { readSvgDirectory } from './helpers.mjs';
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
const currentDir = process.cwd();
|
|
|
|
|
const ICONS_DIR = path.resolve(currentDir, '../icons');
|
|
|
|
|
const iconJsonFiles = readSvgDirectory(ICONS_DIR, '.json');
|
|
|
|
|
|
|
|
|
|
const location = path.resolve(currentDir, '.vitepress/data', 'releaseMetaData.json');
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(location)) {
|
|
|
|
|
fs.unlinkSync(location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const releaseMin = (a, b) => {
|
|
|
|
|
if (!a) return b;
|
|
|
|
|
if (!b) return a;
|
|
|
|
|
return semver.gt(a.version, b.version) ? b : a;
|
2023-05-09 20:46:16 +02:00
|
|
|
};
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
export const releaseMax = (a, b) => {
|
|
|
|
|
if (!a) return b;
|
|
|
|
|
if (!b) return a;
|
|
|
|
|
return semver.gt(a.version, b.version) ? a : b;
|
2023-05-09 20:46:16 +02:00
|
|
|
};
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
export const updateReleaseMetadataWithCommit = (metadata, date, release) => {
|
|
|
|
|
metadata = metadata || {};
|
|
|
|
|
metadata.createdRelease = releaseMin(metadata.createdRelease, release);
|
|
|
|
|
metadata.changedRelease = releaseMax(metadata.changedRelease, release);
|
2023-05-09 20:46:16 +02:00
|
|
|
};
|
2023-05-08 12:17:34 +02:00
|
|
|
|
2023-05-10 14:16:46 +02:00
|
|
|
export const fetchAllReleases = async () => {
|
|
|
|
|
await simpleGit().fetch(['--tags', '--all']);
|
|
|
|
|
const tags = await Promise.all(
|
|
|
|
|
(
|
|
|
|
|
await simpleGit().raw('show-ref', '--tags', '-d')
|
2023-05-09 20:46:16 +02:00
|
|
|
)
|
2023-05-10 14:16:46 +02:00
|
|
|
.trim()
|
|
|
|
|
.split(/\n/)
|
|
|
|
|
.map(async (line) => {
|
|
|
|
|
const [commit, ref] = line.split(/ /);
|
|
|
|
|
if (ref == null || !ref.startsWith('refs/tags/') || commit == null) {
|
|
|
|
|
return { version: null, date: null };
|
|
|
|
|
}
|
|
|
|
|
const { version = null } = semver.coerce(ref.replace('refs/tags/', '')) ?? {};
|
|
|
|
|
const date = (await simpleGit().show(['-s', '--format=%cI', commit])).trim();
|
|
|
|
|
return { version, date };
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return tags.filter(({ version }) => semver.valid(version));
|
|
|
|
|
};
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
simpleGit().clean(CleanOptions.FORCE);
|
|
|
|
|
|
|
|
|
|
const findRelease = (date, releases) => {
|
|
|
|
|
let closestRelease = null;
|
|
|
|
|
for (const release of releases) {
|
|
|
|
|
if (release.date >= date && (closestRelease === null || release.date < closestRelease.date)) {
|
|
|
|
|
closestRelease = release;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return closestRelease;
|
2023-05-09 20:46:16 +02:00
|
|
|
};
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
const fetchCommits = async (name) => {
|
|
|
|
|
const file = `../icons/${name}.svg`;
|
2023-05-09 23:29:37 +02:00
|
|
|
const { all: commits = {} } = await simpleGit().log(['--reverse', '--follow', '--', file]);
|
2023-05-08 12:17:34 +02:00
|
|
|
return commits;
|
2023-05-09 20:46:16 +02:00
|
|
|
};
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
export const getReleaseMetadata = async (name, aliases, releases) => {
|
2023-05-09 23:29:37 +02:00
|
|
|
const metadata = {
|
|
|
|
|
name,
|
|
|
|
|
createdRelease: {
|
|
|
|
|
version: '0.0.0',
|
|
|
|
|
date: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
changedRelease: {
|
|
|
|
|
version: '0.0.0',
|
|
|
|
|
date: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-09 20:46:16 +02:00
|
|
|
for (const alias of [name, ...(aliases ?? [])]) {
|
2023-05-08 12:17:34 +02:00
|
|
|
const commits = await fetchCommits(alias);
|
|
|
|
|
for (const commit of commits) {
|
|
|
|
|
const date = new Date(commit.date).toISOString();
|
2023-05-09 20:46:16 +02:00
|
|
|
const release = findRelease(date, releases);
|
2023-05-08 12:17:34 +02:00
|
|
|
updateReleaseMetadataWithCommit(metadata, date, release);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return metadata;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const releases = await fetchAllReleases();
|
2023-05-09 20:46:16 +02:00
|
|
|
const releaseMetaData = (
|
|
|
|
|
await Promise.all(
|
|
|
|
|
iconJsonFiles.map((iconJsonFile) => {
|
|
|
|
|
const iconName = path.basename(iconJsonFile, '.json');
|
|
|
|
|
const { aliases } = JSON.parse(fs.readFileSync(path.join(ICONS_DIR, iconJsonFile)));
|
|
|
|
|
return getReleaseMetadata(iconName, aliases, releases);
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
).reduce((acc, { name, ...rest }) => {
|
2023-05-08 12:17:34 +02:00
|
|
|
acc[name] = rest;
|
|
|
|
|
return acc;
|
2023-05-09 20:46:16 +02:00
|
|
|
}, {});
|
2023-05-08 12:17:34 +02:00
|
|
|
|
|
|
|
|
fs.promises
|
|
|
|
|
.writeFile(location, JSON.stringify(releaseMetaData, null, 2), 'utf-8')
|
|
|
|
|
.then(() => {
|
|
|
|
|
console.log('Successfully written icon release meta cache file');
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
throw new Error(`Something went wrong generating icon release meta cache file,\n ${error}`);
|
|
|
|
|
});
|