This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/packages/monolith/scripts/build-storybook-import-maps.ts
Ken Sternberg 12559f518a Merge branch 'main' into web/lage
* main: (21 commits)
  web: bump API Client version (#7964)
  release: 2023.10.5
  web: bump API Client version (#7962)
  providers/scim: use lock for sync (#7948)
  stages/email: prevent authentik emails from being marked as spam (also add text template support) (#7949)
  website/docs: prepare 2023.10.5 (#7947)
  web: bump the sentry group in /web with 2 updates (#7955)
  web: bump the wdio group in /tests/wdio with 4 updates (#7957)
  core: bump goauthentik.io/api/v3 from 3.2023104.4 to 3.2023104.5 (#7958)
  web: bump API Client version (#7953)
  events: add ASN Database reader (#7793)
  translate: Updates for file web/xliff/en.xlf in fr (#7951)
  website/docs: add expression example for geoip (#7739)
  website: fix hosted API browser (#7946)
  core: bump github.com/redis/go-redis/v9 from 9.3.0 to 9.3.1 (#7942)
  web: bump the sentry group in /web with 2 updates (#7944)
  web: bump the storybook group in /web with 7 updates (#7945)
  core: bump goauthentik.io/api/v3 from 3.2023104.3 to 3.2023104.4 (#7943)
  providers/scim: set timeout based on page and page count (#7941)
  translate: Updates for file web/xliff/en.xlf in zh_CN (#7928)
  ...
2023-12-21 12:11:25 -08:00

85 lines
2.6 KiB
TypeScript

import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function* walkFilesystem(dir: string): Generator<string, undefined, any> {
const openeddir = fs.opendirSync(dir);
if (!openeddir) {
return;
}
let d: fs.Dirent | null;
while ((d = openeddir?.readSync())) {
if (!d) {
break;
}
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walkFilesystem(entry);
else if (d.isFile()) yield entry;
}
openeddir.close();
}
const import_re = /^(import \w+ from .*\.css)";/;
function extractImportLinesFromFile(path: string) {
const source = fs.readFileSync(path, { encoding: "utf8", flag: "r" });
const lines = source?.split("\n") ?? [];
return lines.filter((l) => import_re.test(l));
}
function createOneImportLine(line: string) {
const importMatch = import_re.exec(line);
if (!importMatch) {
throw new Error("How did an unmatchable line get here?");
}
const importContent = importMatch[1];
if (!importContent) {
throw new Error("How did an unmatchable line get here!?");
}
return `'${importContent}";': '${importContent}?inline";',`;
}
const isSourceFile = /\.ts$/;
function getTheSourceFiles() {
return Array.from(walkFilesystem(path.join(__dirname, "..", "src"))).filter((path) =>
isSourceFile.test(path),
);
}
function getTheImportLines(importPaths: string[]) {
const importLines: string[] = importPaths.reduce(
(acc: string[], path) => [...acc, extractImportLinesFromFile(path)].flat(),
[],
);
const uniqueImportLines = new Set(importLines);
const sortedImportLines = Array.from(uniqueImportLines.keys());
sortedImportLines.sort();
return sortedImportLines;
}
const importPaths = getTheSourceFiles();
const importLines = getTheImportLines(importPaths);
const outputFile = `
// THIS IS A GENERATED FILE. DO NOT EDIT BY HAND.
//
// This file is generated by the build-storybook-import-maps script in the UI's base directory.
// This is a *hack* to work around an inconsistency in the way rollup, vite, and storybook
// import CSS modules.
//
// Sometime around 2030 or so, the Javascript community may finally get its collective act together
// and we'll have one unified way of doing this. I can only hope.
export const cssImportMaps = {
${importLines.map(createOneImportLine).join("\n")}
};
`;
fs.writeFileSync(path.join(__dirname, "..", ".storybook", "css-import-maps.ts"), outputFile, {
encoding: "utf8",
flag: "w",
});