@plone/registry
Version:
Add-on and configuration registry for Plone and for JavaScript and TypeScript-based apps.
43 lines (41 loc) • 1.41 kB
JavaScript
// src/addon-registry/create-theme-loader.ts
import fs from "fs";
import tmp from "tmp";
import cryptoRandomString from "crypto-random-string";
var titleCase = (w) => w.slice(0, 1).toUpperCase() + w.slice(1, w.length);
function nameFromPackage(name) {
name = name.replace(/[@~./\\:\s]/gi, "") || cryptoRandomString({ length: 10, characters: "abcdefghijk" });
return name.split("-").map((w, i) => i > 0 ? titleCase(w) : w).join("");
}
function getThemeLoaderCode(name, customThemeAddons = []) {
let buf = `/*
This file is autogenerated. Don't change it directly.
Add a ./theme/_${name}.scss in your add-on to load your theme customizations in the current theme.
*/
`;
customThemeAddons.forEach((addon) => {
const customization = `${addon}/theme/${name}`;
const line = `@import '${customization}';
`;
buf += line;
});
return buf;
}
function createThemeAddonsLoader({
main,
variables
}) {
const addonsThemeLoaderVariablesPath = tmp.tmpNameSync({ postfix: ".scss" });
const addonsThemeLoaderMainPath = tmp.tmpNameSync({ postfix: ".scss" });
fs.writeFileSync(
addonsThemeLoaderVariablesPath,
getThemeLoaderCode("variables", variables)
);
fs.writeFileSync(addonsThemeLoaderMainPath, getThemeLoaderCode("main", main));
return [addonsThemeLoaderVariablesPath, addonsThemeLoaderMainPath];
}
export {
createThemeAddonsLoader,
getThemeLoaderCode,
nameFromPackage
};