linguist-css/scripts/build.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-04-20 20:19:53 +01:00
import fetch from 'node-fetch';
import { parse, stringify } from 'yaml';
import slugify from 'slugify';
import { paramCase } from 'change-case';
import { writeFile, readFile } from 'node:fs/promises';
import Handlebars from 'handlebars';
import { mkdirp } from 'mkdirp'
2023-04-20 21:08:59 +01:00
import { join } from 'path';
2023-04-20 20:19:53 +01:00
const LINGUIST_URL = "https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml";
2023-04-20 21:08:59 +01:00
const ROOT_DIR = process.cwd();
const DIST_DIR = join(ROOT_DIR, "dist")
2023-04-20 20:19:53 +01:00
function slugifyLanguage(language) {
return paramCase(language.replace("#", " Sharp").replace("++", " Plus Plus"));
}
async function main() {
2023-04-20 21:08:59 +01:00
await mkdirp(DIST_DIR);
2023-04-20 20:19:53 +01:00
const response = await fetch(LINGUIST_URL);
const body = parse(await response.text());
const colours = {};
for (const [key, value] of Object.entries(body)) {
if (!value.color) {
continue;
}
const languageName = slugifyLanguage(key);
if (languageName in colours) {
throw "CONFLICT" + key;
}
colours[key] = {
slug: languageName,
color: value.color
};
}
2023-04-20 21:13:23 +01:00
await writeFile(join(DIST_DIR, "linguist.json"), JSON.stringify(colours));
2023-04-20 20:19:53 +01:00
2023-04-20 21:08:59 +01:00
const cssTemplate = Handlebars.compile((await readFile(join(ROOT_DIR, "templates/template.css"))).toString());
2023-04-20 21:13:23 +01:00
await writeFile(join(DIST_DIR, "linguist.css"), cssTemplate({data: Object.values(colours)}));
2023-04-20 20:19:53 +01:00
2023-04-20 21:08:59 +01:00
const scssTemplate = Handlebars.compile((await readFile(join(ROOT_DIR, "templates/template.scss"))).toString());
2023-04-20 21:13:23 +01:00
await writeFile(join(DIST_DIR, "linguist.scss"), scssTemplate({data: Object.values(colours)}));
2023-04-20 20:19:53 +01:00
}
main();