UNPKG

paranormal

Version:
93 lines (69 loc) 2.78 kB
'use strict'; exports.__esModule = true; var _promise = require('babel-runtime/core-js/promise'); var _promise2 = _interopRequireDefault(_promise); var _stringify = require('babel-runtime/core-js/json/stringify'); var _stringify2 = _interopRequireDefault(_stringify); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _fs = require('./utils/fs'); var fs = _interopRequireWildcard(_fs); var _Example = require('./Example'); var _Example2 = _interopRequireDefault(_Example); var _stripIndent = require('strip-indent'); var _stripIndent2 = _interopRequireDefault(_stripIndent); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class App { constructor(opts) { this.tempDir = opts.tempDir; this.indexPath = _path2.default.join(this.tempDir, 'index.html'); this.entriesPath = _path2.default.join(this.tempDir, 'entries.html'); } async build(examples) { let EXAMPLES_DATA = examples.map(example => { return { title: example.title, href: _path2.default.relative(this.tempDir, example.htmlPath) }; }); let links = []; links.push(_path2.default.relative(this.tempDir, this.indexPath)); examples.forEach(example => { links.push(_path2.default.relative(this.tempDir, example.htmlPath), _path2.default.relative(this.tempDir, example.txtPath), _path2.default.relative(this.tempDir, example.jsPath)); }); let indexContent = (0, _stripIndent2.default)(` <!doctype html> <html> <head> <meta charset="utf-8"> <title>👻 Paranormal</title> </head> <body> <div id="root"></div> <script> window.EXAMPLES_DATA = ${(0, _stringify2.default)(EXAMPLES_DATA)}; </script> <script src="${require.resolve('../app/index.js')}"></script> <a hidden href="./entries.html"></a> </body> </html> `).trim(); let entriesContent = (0, _stripIndent2.default)(` <!doctype html> <html> <head> <meta charset="utf-8"> <title>👻 Paranormal</title> </head> <body> <ul> ${links.map(link => `<li><a href="${link}">${link}</a></li>`).join('')} </ul> </body> </html> `).trim(); await _promise2.default.all([fs.writeFile(this.indexPath, indexContent), fs.writeFile(this.entriesPath, entriesContent)]); } } exports.default = App;