UNPKG

@knapsack/app

Version:

Build Design Systems on top of knapsack, by Basalt

132 lines (110 loc) 3.73 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.setupRoutes = setupRoutes; var _express = _interopRequireDefault(require("express")); var _fsExtra = require("fs-extra"); var _core = require("@knapsack/core"); var _path = require("path"); var _constants = require("../lib/constants"); var _routes = require("../lib/routes"); const router = _express.default.Router(); function setupRoutes({ patterns, knapsackDistDir, distDir, publicDir, cacheDir, plugins }) { router.use('*', async (req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); next(); }); router.use(`${_constants.apiUrlBase}`, (req, res, next) => { if (process.env.NODE_ENV === 'production') { next(); } else { // faking a slight delay in local development to better reflect re-world delays setTimeout(next, 500); } }); router.use(_express.default.static(knapsackDistDir, { maxAge: '1d' })); const designSystemDistDir = (0, _path.dirname)(require.resolve('@knapsack/design-system')); router.use('/ks-design-system', _express.default.static(designSystemDistDir)); if (distDir) { router.use(_express.default.static(distDir, { maxAge: '1d' })); } if (cacheDir) { router.use(_express.default.static(cacheDir, { maxAge: '1d' })); } if (publicDir) { router.use(_express.default.static(publicDir)); } if (plugins) { plugins.filter(p => p.publicDir).forEach(plugin => { router.use(`/plugins/${plugin.id}`, _express.default.static(plugin.publicDir)); }); } function getDemoUrls() { return patterns.getPatterns().map(pattern => { return { id: pattern.id, title: pattern.title, templates: pattern.templates.map(template => { var _template$title; return { id: template.id, title: (_template$title = template.title) !== null && _template$title !== void 0 ? _template$title : template.id, demoUrls: [...template.demos.map(demoId => (0, _routes.createDemoUrl)({ patternId: pattern.id, templateId: template.id, demoId, wrapHtml: true, isInIframe: false }))] }; }) }; }); } router.get('/demo-urls-data', (req, res) => { res.send(getDemoUrls()); }); const demoUrlsHbsPath = (0, _path.join)(__dirname, './templates/demo-urls.html.hbs'); if (!(0, _fsExtra.existsSync)(demoUrlsHbsPath)) { throw new Error(`Demo URLs handlebars template does not exist: ${demoUrlsHbsPath}`); } // This page is mainly so IE can get a list of links to view the individual templates outside of the system router.route('/demo-urls').get(async (req, res) => { const patternDemos = getDemoUrls(); const demoUrlsHbs = await (0, _fsExtra.readFile)(demoUrlsHbsPath, 'utf-8'); const result = (0, _core.hbsRenderString)({ hbsString: demoUrlsHbs, data: { patternDemos } }); res.send(result); }); // Since this is a Single Page App, we will send all html requests to the `index.html` file in the dist router.use('*', (req, res, next) => { const { accept = '' } = req.headers; const accepted = accept.split(','); // this is for serving up a Netlify CMS folder if present if (accepted.includes('text/html')) { res.sendFile((0, _path.join)(knapsackDistDir, 'index.html')); } else { next(); } }); return router; }