mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 09:42:43 +00:00
renaming "get" functions
rename `getStaticTheme` to `getStaticThemeCSS` rename `getBrewThemeWithCSS` to `getBrewThemeCSS` rename `getBrewThemeParent` to `getBrewThemeParentCSS` to avoid confusion with other "get" endpoints like `getBrew`, and unify naming for endpoint functions that return CSS. Simplify `isStaticTheme` function (getting the parent theme is handled elsewhere)
This commit is contained in:
@@ -9,7 +9,7 @@ const yaml = require('js-yaml');
|
|||||||
const app = express();
|
const app = express();
|
||||||
const config = require('./config.js');
|
const config = require('./config.js');
|
||||||
|
|
||||||
const { homebrewApi, getBrew, getBrewThemeWithCSS, getStaticTheme, getBrewThemeParent } = require('./homebrew.api.js');
|
const { homebrewApi, getBrew, getBrewThemeCSS, getStaticThemeCSS, getBrewThemeParentCSS } = require('./homebrew.api.js');
|
||||||
const GoogleActions = require('./googleActions.js');
|
const GoogleActions = require('./googleActions.js');
|
||||||
const serveCompressedStaticAssets = require('./static-assets.mv.js');
|
const serveCompressedStaticAssets = require('./static-assets.mv.js');
|
||||||
const sanitizeFilename = require('sanitize-filename');
|
const sanitizeFilename = require('sanitize-filename');
|
||||||
@@ -79,9 +79,9 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
|
|
||||||
// Theme
|
// Theme
|
||||||
|
|
||||||
app.get('/css/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeWithCSS));
|
app.get('/css/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeCSS));
|
||||||
app.get('/css/:engine/:id/', asyncHandler(getStaticTheme));
|
app.get('/css/:engine/:id/', asyncHandler(getStaticThemeCSS));
|
||||||
app.get('/cssParent/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeParent));
|
app.get('/cssParent/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getBrewThemeParentCSS));
|
||||||
|
|
||||||
|
|
||||||
//Home page
|
//Home page
|
||||||
|
|||||||
@@ -13,17 +13,10 @@ var url = require('url');
|
|||||||
|
|
||||||
const { DEFAULT_BREW, DEFAULT_BREW_LOAD } = require('./brewDefaults.js');
|
const { DEFAULT_BREW, DEFAULT_BREW_LOAD } = require('./brewDefaults.js');
|
||||||
|
|
||||||
const themes = require('../themes/themes.json');
|
const Themes = require('../themes/themes.json');
|
||||||
|
|
||||||
const isStaticTheme = (engine, themeName)=>{
|
const isStaticTheme = (renderer, themeName)=>{
|
||||||
if(!themes.hasOwnProperty(engine)) {
|
return Themes[renderer]?.[themeName] !== undefined;
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if(themes[engine].hasOwnProperty(themeName)) {
|
|
||||||
return themes[engine][themeName].baseTheme;
|
|
||||||
} else {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// const getTopBrews = (cb) => {
|
// const getTopBrews = (cb) => {
|
||||||
@@ -281,35 +274,40 @@ const api = {
|
|||||||
|
|
||||||
res.status(200).send(saved);
|
res.status(200).send(saved);
|
||||||
},
|
},
|
||||||
getBrewThemeWithCSS : async (req, res)=>{
|
getBrewThemeCSS : async (req, res)=>{
|
||||||
const brew = req.brew;
|
const brew = req.brew;
|
||||||
|
console.log(`getBrewThemeCSS for ${brew.shareId}`)
|
||||||
splitTextStyleAndMetadata(brew);
|
splitTextStyleAndMetadata(brew);
|
||||||
res.setHeader('Content-Type', 'text/css');
|
res.setHeader('Content-Type', 'text/css');
|
||||||
const themePath = themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
|
const themePath = Themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
|
||||||
// Drop Parent theme if it has already been loaded.
|
// Drop Parent theme if it has already been loaded.
|
||||||
// This assumes the continued use of the V3/5ePHB and V3/Blank themes for the app.
|
// This assumes the continued use of the V3/5ePHB and V3/Blank themes for the app.
|
||||||
|
console.log(`and parentThemeImport for ${brew.theme}`)
|
||||||
const parentThemeImport = ((req.brew.theme != '5ePHB') && (req.brew.theme != 'Blank')) ? `@import url(\"${themePath}\");\n\n`:'';
|
const parentThemeImport = ((req.brew.theme != '5ePHB') && (req.brew.theme != 'Blank')) ? `@import url(\"${themePath}\");\n\n`:'';
|
||||||
const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`;
|
const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`;
|
||||||
return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}${req.brew.style}`);
|
return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}${req.brew.style}`);
|
||||||
},
|
},
|
||||||
getBrewThemeParent : async (req, res)=>{
|
getBrewThemeParentCSS : async (req, res)=>{
|
||||||
const brew = req.brew;
|
const brew = req.brew;
|
||||||
|
console.log(`getBrewThemeParentCSS for ${brew.shareId}`)
|
||||||
splitTextStyleAndMetadata(brew);
|
splitTextStyleAndMetadata(brew);
|
||||||
res.setHeader('Content-Type', 'text/css');
|
res.setHeader('Content-Type', 'text/css');
|
||||||
const themePath = themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
|
console.log(`getBrewThemeParentCSS parent is ${brew.theme}`)
|
||||||
|
const themePath = Themes[_.upperFirst(req.brew.renderer)].hasOwnProperty(req.brew.theme) ? `/css/${req.brew.renderer}/${req.brew.theme}` : `/css/${req.brew.theme}`;
|
||||||
const parentThemeImport = `@import url(\"${themePath}\");\n\n`;
|
const parentThemeImport = `@import url(\"${themePath}\");\n\n`;
|
||||||
const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`;
|
const themeLocationComment = `/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.brew.shareId} */\n\n`;
|
||||||
return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}`);
|
return res.status(200).send(req.brew.renderer == 'legacy' ? '' : `${parentThemeImport}${themeLocationComment}`);
|
||||||
},
|
},
|
||||||
getStaticTheme : async(req, res)=>{
|
getStaticThemeCSS : async(req, res)=>{
|
||||||
const themeParent = isStaticTheme(req.params.engine, req.params.id);
|
if (!isStaticTheme(req.params.engine, req.params.id))
|
||||||
if(themeParent === undefined){
|
res.status(404).send(`Invalid Theme - Renderer: ${req.params.engine}, Name: ${req.params.id}`);
|
||||||
res.status(404).send(`Invalid Theme - Engine: ${req.params.engine}, Name: ${req.params.id}`);
|
else {
|
||||||
} else {
|
const themeParent = Themes[req.params.engine][req.params.id].baseTheme;
|
||||||
|
console.log(`getStaticThemeCSS for ${themeParent}`)
|
||||||
res.setHeader('Content-Type', 'text/css');
|
res.setHeader('Content-Type', 'text/css');
|
||||||
res.setHeader('Cache-Control', 'public, max-age: 43200, must-revalidate');
|
res.setHeader('Cache-Control', 'public, max-age: 43200, must-revalidate');
|
||||||
const parentTheme = themeParent ? `@import url(\"/css/${req.params.engine}/${themeParent}\");\n/* Static Theme ${themes[req.params.engine][themeParent].name} */\n` : '';
|
const parentTheme = themeParent ? `@import url(\"/css/${req.params.engine}/${themeParent}\");\n/* Static Theme ${Themes[req.params.engine][themeParent].name} */\n` : '';
|
||||||
return res.status(200).send(`${parentTheme}@import url(\"/themes/${req.params.engine}/${req.params.id}/style.css\");\n/* Static Theme ${themes[req.params.engine][req.params.id].name} */\n`);
|
return res.status(200).send(`${parentTheme}@import url(\"/themes/${req.params.engine}/${req.params.id}/style.css\");\n/* Static Theme ${Themes[req.params.engine][req.params.id].name} */\n`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
updateBrew : async (req, res)=>{
|
updateBrew : async (req, res)=>{
|
||||||
|
|||||||
@@ -594,7 +594,7 @@ brew`);
|
|||||||
const next = jest.fn();
|
const next = jest.fn();
|
||||||
await fn(req, null, next);
|
await fn(req, null, next);
|
||||||
|
|
||||||
api.getBrewThemeWithCSS(req, res);
|
api.getBrewThemeCSS(req, res);
|
||||||
const sent = res.send.mock.calls[0][0];
|
const sent = res.send.mock.calls[0][0];
|
||||||
expect(sent).toBe(`@import url("/css/V3/5eDMG");\n\n/* From Brew: https://localhost/share/iAmAUserTheme */\n\nI Have a style!`);
|
expect(sent).toBe(`@import url("/css/V3/5eDMG");\n\n/* From Brew: https://localhost/share/iAmAUserTheme */\n\nI Have a style!`);
|
||||||
expect(res.status).toHaveBeenCalledWith(200);
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
@@ -610,14 +610,14 @@ brew`);
|
|||||||
const next = jest.fn();
|
const next = jest.fn();
|
||||||
await fn(req, null, next);
|
await fn(req, null, next);
|
||||||
|
|
||||||
api.getBrewThemeWithCSS(req, res);
|
api.getBrewThemeCSS(req, res);
|
||||||
const sent = res.send.mock.calls[0][0];
|
const sent = res.send.mock.calls[0][0];
|
||||||
expect(sent).toBe(`@import url("/css/IamATheme");\n\n/* From Brew: https://localhost/share/iAmAUserTheme */\n\nI Have a style!`);
|
expect(sent).toBe(`@import url("/css/IamATheme");\n\n/* From Brew: https://localhost/share/iAmAUserTheme */\n\nI Have a style!`);
|
||||||
expect(res.status).toHaveBeenCalledWith(200);
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getStaticTheme', ()=>{
|
describe('getStaticThemeCSS', ()=>{
|
||||||
it('should return an import of the theme without including a parent.', async ()=>{
|
it('should return an import of the theme without including a parent.', async ()=>{
|
||||||
const req = {
|
const req = {
|
||||||
params : {
|
params : {
|
||||||
@@ -625,7 +625,7 @@ brew`);
|
|||||||
id : '5ePHB'
|
id : '5ePHB'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
api.getStaticTheme(req, res);
|
api.getStaticThemeCSS(req, res);
|
||||||
const sent = res.send.mock.calls[0][0];
|
const sent = res.send.mock.calls[0][0];
|
||||||
expect(sent).toBe('@import url("/themes/V3/5ePHB/style.css");\n/* Static Theme 5e PHB */\n');
|
expect(sent).toBe('@import url("/themes/V3/5ePHB/style.css");\n/* Static Theme 5e PHB */\n');
|
||||||
expect(res.status).toHaveBeenCalledWith(200);
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
@@ -637,7 +637,7 @@ brew`);
|
|||||||
id : '5eDMG'
|
id : '5eDMG'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
api.getStaticTheme(req, res);
|
api.getStaticThemeCSS(req, res);
|
||||||
const sent = res.send.mock.calls[0][0];
|
const sent = res.send.mock.calls[0][0];
|
||||||
expect(sent).toBe('@import url("/css/V3/5ePHB");\n/* Static Theme 5e PHB */\n@import url("/themes/V3/5eDMG/style.css");\n/* Static Theme 5e DMG */\n');
|
expect(sent).toBe('@import url("/css/V3/5ePHB");\n/* Static Theme 5e PHB */\n@import url("/themes/V3/5eDMG/style.css");\n/* Static Theme 5e DMG */\n');
|
||||||
expect(res.status).toHaveBeenCalledWith(200);
|
expect(res.status).toHaveBeenCalledWith(200);
|
||||||
@@ -649,7 +649,7 @@ brew`);
|
|||||||
id : '5eDMGGGG'
|
id : '5eDMGGGG'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
api.getStaticTheme(req, res);
|
api.getStaticThemeCSS(req, res);
|
||||||
const sent = res.send.mock.calls[0][0];
|
const sent = res.send.mock.calls[0][0];
|
||||||
expect(sent).toBe('Invalid Theme - Engine: V3, Name: 5eDMGGGG');
|
expect(sent).toBe('Invalid Theme - Engine: V3, Name: 5eDMGGGG');
|
||||||
expect(res.status).toHaveBeenCalledWith(404);
|
expect(res.status).toHaveBeenCalledWith(404);
|
||||||
|
|||||||
Reference in New Issue
Block a user