ModuleBuilder/src/preprocessors/bdTheme.js

71 lines
1.4 KiB
JavaScript

import { readFileSync, writeFileSync, mkdirSync, rmSync } from 'fs';
export default (themePath, repo) => {
const content = readFileSync(themePath);
const metaReg = /@([^ ]*) (.*)/g;
let manifest = {
main: 'index.js',
tags: ['theme', 'port']
};
let match;
while ((match = metaReg.exec(content)) !== null) {
const [_, key, value] = match;
if (key === 'import') break;
// console.log(key, value);
switch (key) {
case 'name':
manifest.name = value;
break;
case 'description':
manifest.description = value;
break;
case 'version':
manifest.version = value;
break;
case 'author':
manifest.authors = [ value ];
break;
case 'authorId':
manifest.authors = [ value ];
break;
}
}
manifest = Object.assign(manifest, repo[4]);
rmSync(themePath);
mkdirSync(themePath);
const jsCode = `// Generated by MS2Builder - bdTheme preprocessor / porter
let style;
export default {
goosemodHandlers: {
onImport: async () => {
style = document.createElement("style");
document.head.appendChild(style);
style.appendChild(
document.createTextNode(
\`${content}\`
)
);
},
onRemove: async () => {
style.remove();
},
}
};`;
writeFileSync(`${themePath}/goosemodModule.json`, JSON.stringify(manifest));
writeFileSync(`${themePath}/index.js`, jsCode);
};