mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 20:29:01 +00:00
MacOS will sometimes drop a `.DS_Store` file into these folders, and then the build fails with /themes/Legacy/.DS_Store/settings.json is not a valid path.
79 lines
3.1 KiB
JavaScript
79 lines
3.1 KiB
JavaScript
// vite-plugins/generateAssetsPlugin.js
|
|
import fs from 'fs-extra';
|
|
import path from 'path';
|
|
import less from 'less';
|
|
|
|
export function generateAssetsPlugin(isDev = false) {
|
|
return {
|
|
name : 'generate-assets',
|
|
async buildStart() {
|
|
const buildDir = path.resolve(process.cwd(), 'build');
|
|
|
|
// Copy favicon
|
|
await fs.copy('./client/homebrew/favicon.ico', `${buildDir}/assets/favicon.ico`);
|
|
|
|
//Copy this image into multiple paths to avoid server spam where browser requests an image
|
|
//at the wrong path, gets a 406, then in response requests broken-image which also fails. Error handler
|
|
//used the wrong path for this image causing an infinite request loop. Should be able to remove
|
|
//in some time once all users haver refreshed their browser
|
|
await fs.copy('./client/icons/broken-image.jpg', `${buildDir}/edit/client/icons/broken-image.jpg`);
|
|
await fs.copy('./client/icons/broken-image.jpg', `${buildDir}/new/client/icons/broken-image.jpg`);
|
|
await fs.copy('./client/icons/broken-image.jpg', `${buildDir}/client/icons/broken-image.jpg`);
|
|
|
|
// Copy shared styles/fonts
|
|
const assets = fs.readdirSync('./shared/naturalcrit/styles');
|
|
for (const file of assets) {
|
|
await fs.copy(`./shared/naturalcrit/styles/${file}`, `${buildDir}/fonts/${file}`);
|
|
}
|
|
|
|
// Compile Legacy themes
|
|
const themes = { Legacy: {}, V3: {} };
|
|
const legacyDirs = fs.readdirSync('./themes/Legacy', { withFileTypes: true })
|
|
.filter(entry => entry.isDirectory())
|
|
.map(entry => entry.name);
|
|
for (const dir of legacyDirs) {
|
|
const themeData = JSON.parse(fs.readFileSync(`./themes/Legacy/${dir}/settings.json`, 'utf-8'));
|
|
themeData.path = dir;
|
|
themes.Legacy[dir] = themeData;
|
|
|
|
const src = `./themes/Legacy/${dir}/style.less`;
|
|
const outputDir = `${buildDir}/themes/Legacy/${dir}/style.css`;
|
|
const lessOutput = await less.render(fs.readFileSync(src, 'utf-8'), { compress: !isDev });
|
|
await fs.outputFile(outputDir, lessOutput.css);
|
|
}
|
|
|
|
// Compile V3 themes
|
|
const v3Dirs = fs.readdirSync('./themes/V3', { withFileTypes: true })
|
|
.filter(entry => entry.isDirectory())
|
|
.map(entry => entry.name);
|
|
for (const dir of v3Dirs) {
|
|
const themeData = JSON.parse(fs.readFileSync(`./themes/V3/${dir}/settings.json`, 'utf-8'));
|
|
themeData.path = dir;
|
|
themes.V3[dir] = themeData;
|
|
|
|
await fs.copy(
|
|
`./themes/V3/${dir}/dropdownTexture.png`,
|
|
`${buildDir}/themes/V3/${dir}/dropdownTexture.png`,
|
|
);
|
|
await fs.copy(
|
|
`./themes/V3/${dir}/dropdownPreview.png`,
|
|
`${buildDir}/themes/V3/${dir}/dropdownPreview.png`,
|
|
);
|
|
|
|
const src = `./themes/V3/${dir}/style.less`;
|
|
const outputDir = `${buildDir}/themes/V3/${dir}/style.css`;
|
|
const lessOutput = await less.render(fs.readFileSync(src, 'utf-8'), { compress: !isDev });
|
|
await fs.outputFile(outputDir, lessOutput.css);
|
|
}
|
|
|
|
// Write themes.json
|
|
await fs.outputFile('./themes/themes.json', JSON.stringify(themes, null, 2));
|
|
|
|
// Copy fonts/assets/icons
|
|
await fs.copy('./themes/fonts', `${buildDir}/fonts`);
|
|
await fs.copy('./themes/assets', `${buildDir}/assets`);
|
|
await fs.copy('./client/icons', `${buildDir}/icons`);
|
|
},
|
|
};
|
|
}
|