8 Commits

4 changed files with 49 additions and 35 deletions

1
.gitignore vendored
View File

@@ -103,4 +103,5 @@ distribution/*
mirrorlist
mirrors*.json
mirrors.md
head.md
.env

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "artix-mlg",
"version": "0.2.4",
"version": "0.2.7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "artix-mlg",
"version": "0.2.4",
"version": "0.2.7",
"license": "MIT",
"dependencies": {
"country-code-lookup": "0.1.3",

View File

@@ -1,6 +1,6 @@
{
"name": "artix-mlg",
"version": "0.2.5",
"version": "0.2.7",
"description": "mirrorlist generator for Artix Linux",
"keywords": [
"artix",

View File

@@ -10,6 +10,7 @@ const inputFile = process.env['INPUT'] || path.join(process.cwd(), 'mirrors.json
const fixtureFile = process.env['FIXTURE'] || path.join(process.cwd(), 'mirrors.fixture.json');
const mirrorList = process.env['MIRRORLIST'] || path.join(process.cwd(), 'mirrorlist');
const mirrorMd = process.env['MIRRORMD'] || path.join(process.cwd(), 'mirrors.md');
const mdHeadFile = process.env['MDHEADER'] || path.join(process.cwd(), 'head.md');
const verbose = !!process.env['VERBOSE'];
const protocolId: Record<Protocol, number> = {
@@ -69,7 +70,6 @@ interface UrlComponents {
type MirrorMap = { [name: string]: Mirror };
let mirrorCounter = 0;
let mirrorUrlCounter = 0;
function setMirrors(fixture: FixtureObject[]) {
const mirrors: MirrorMap = {};
@@ -118,9 +118,9 @@ function getMirror(name: string): Mirror {
function updateMirror(m: Mirror, profile: MirrorProfile, url: UrlComponents) {
m.fields.tier = Math.max(m.fields.tier, profile.tier);
m.fields.admin_email ||= profile.admin_email || '';
m.fields.alternate_email ||= profile.alternate_email || '';
m.fields.notes ||= profile.notes || '';
m.fields.admin_email = profile.admin_email || m.fields.admin_email;
m.fields.alternate_email = profile.alternate_email || m.fields.alternate_email;
m.fields.notes = profile.notes || m.fields.notes;
m.fields.active ||= profile.active;
m.fields.public &&= profile.public;
m.fields.isos ||= !!profile.stable_isos || !!profile.weekly_isos;
@@ -130,25 +130,25 @@ function updateMirror(m: Mirror, profile: MirrorProfile, url: UrlComponents) {
}
}
function processMirrorProfile(m: MirrorProfile) {
function processMirrorProfile(m: MirrorProfile, index: number) {
const url: UrlComponents = processUrl(m.url);
const mirror: Mirror = getMirror(m.force_mirror_name || url.name);
updateMirror(mirror, m, url);
const mirrorUrl: MirrorUrl = {
pk: mirrorUrlCounter++,
model: 'mirrors.MirrorUrl',
fields: {
url: url.partial,
protocol: protocolId[url.protocol],
mirror: mirror.pk,
country: resolveCountry(m.country)?.iso2 || null,
// populate ip fields with `mirrorresolv`
has_ipv4: false,
has_ipv6: false,
active: m.active
}
};
mirrorUrls.push(mirrorUrl);
updateMirror(mirror, m, url);
const mirrorUrl: MirrorUrl = {
pk: index + 1,
model: 'mirrors.MirrorUrl',
fields: {
url: url.partial,
protocol: protocolId[url.protocol],
mirror: mirror.pk,
country: resolveCountry(m.country)?.iso2 || undefined,
// populate ip fields with `mirrorresolv`
has_ipv4: false,
has_ipv6: false,
active: m.active
}
};
mirrorUrls.push(mirrorUrl);
pushMirrorProfile(m.force_mirror_name || url.name, m);
}
@@ -160,7 +160,7 @@ function pushMirrorProfile(name: string, m: MirrorProfile) {
function updateUpstream(m: MirrorProfile) {
const url: UrlComponents = processUrl(m.url);
const mirror: Mirror = mirrors[m.force_mirror_name || url.name];
mirror.fields.upstream ||= m.upstream && mirrors[m.upstream]?.pk;
mirror.fields.upstream = (m.upstream && mirrors[m.upstream]?.pk) || mirror.fields.upstream;
}
function composeMirrorFixture(): FixtureObject[] {
@@ -183,7 +183,7 @@ function generateMirrorlist(mirrors: MirrorProfile[] = []): string {
const lines: string[] = [
'##',
'## Artix Linux repository mirrorlist',
`## Generated on ${getDateTime()}`,
`## Generated on ${getDateTime()} by artix-mlg`,
'##',
'',
'# Artix mirrors',
@@ -199,7 +199,18 @@ function generateMirrorlist(mirrors: MirrorProfile[] = []): string {
return lines.join('\n');
}
function generateMirrorMd(): string {
async function generateMirrorMd(): Promise<string> {
async function tryReadHeader(): Promise<string[]> {
try {
return [await fsp.readFile(mdHeadFile, 'utf-8')];
}
catch (err) {
if (verbose) {
console.error(err);
}
return [];
}
}
function pushTableRowIfTruthy(lines: string[], label: string, value: string | undefined | null | false) {
if (value) {
lines.push(`| ${label} | ${value} |`);
@@ -208,13 +219,15 @@ function generateMirrorMd(): string {
function findFirstWithChild<T, K extends keyof T>(profiles: T[], key: K): T[K] | undefined {
return profiles.find(p => !!p[key])?.[key];
}
const lines: string[] = [
'# Mirrors\n\nContact or other information for the mirrors of our repositories and ISOs.\n'
]
const lines: string[] = await tryReadHeader();
lines.push('# Mirrors\n\nContact or other information for the mirrors of our repositories and ISOs.\n');
for (let mirrorName in mirrorProfilesByMirrorName) {
const profiles: MirrorProfile[] = mirrorProfilesByMirrorName[mirrorName];
const activeProfiles: MirrorProfile[] = profiles.filter(p => p.active);
const urls: string[] = activeProfiles.map(p => p.url).map(u => u.split('$repo')[0]);
const urls: string[] = profiles.map(p => {
const url = p.url.split('$repo')[0];
return p.active ? url : `${url} (inactive)`;
});
const upstream = findFirstWithChild(activeProfiles, 'upstream');
lines.push(`### ${mirrorName}`);
lines.push(`| Mirror | ${mirrorName} |`);
@@ -228,9 +241,9 @@ function generateMirrorMd(): string {
// pushTableRowIfTruthy(lines, 'Bandwidth', findFirstWithChild(activeProfiles, 'bandwidth'));
// pushTableRowIfTruthy(lines, 'Frequency', findFirstWithChild(activeProfiles, 'frequency'));
// pushTableRowIfTruthy(lines, 'Hosted by', findFirstWithChild(activeProfiles, 'org'));
pushTableRowIfTruthy(lines, 'Location', findFirstWithChild(activeProfiles, 'country'));
pushTableRowIfTruthy(lines, 'Contact Details', findFirstWithChild(activeProfiles, 'admin_email'));
pushTableRowIfTruthy(lines, 'Altenate Contact Details', findFirstWithChild(activeProfiles, 'alternate_email'));
pushTableRowIfTruthy(lines, 'Location', findFirstWithChild(activeProfiles, 'country') || findFirstWithChild(profiles, 'country'));
pushTableRowIfTruthy(lines, 'Contact Details', findFirstWithChild(activeProfiles, 'admin_email') || findFirstWithChild(profiles, 'admin_email'));
pushTableRowIfTruthy(lines, 'Altenate Contact Details', findFirstWithChild(activeProfiles, 'alternate_email') || findFirstWithChild(profiles, 'alternate_email'));
lines.push('');
}
return lines.join('\n');
@@ -262,7 +275,7 @@ async function main() {
await fsp.writeFile(fixtureFile, JSON.stringify(composeMirrorFixture(), null, 4));
await fsp.writeFile(mirrorList, generateMirrorlist(input.mirrors?.filter(m => m.public && m.active && !m.suppress) || []));
await fsp.writeFile(mirrorMd, generateMirrorMd());
await fsp.writeFile(mirrorMd, await generateMirrorMd());
}
export default main;