diff --git a/bin/build.js b/bin/build.js index 25e24f1..025e269 100644 --- a/bin/build.js +++ b/bin/build.js @@ -18,9 +18,12 @@ async function build() { console.log('>> Generating from ulka files'.green) await generateFromUlka() } catch (e) { - console.log(`>> ${e.message}\n`.red, e) + console.log(`>> ${e.toString()}\n`.red) + + if (e.name !== 'ReferenceError') console.log(e) + console.log('>> Build Failed'.red) - console.log(`>> Removing ${globalInfo.configs.buildPath}`) + console.log(`>> Removing ${globalInfo.configs.buildPath} folder`.red) await removeDirectories(globalInfo.configs.buildPath) process.exit(0) } diff --git a/bin/index.js b/bin/index.js index a660909..662b3fe 100755 --- a/bin/index.js +++ b/bin/index.js @@ -13,9 +13,16 @@ const { version } = require('../package.json') program.version(version) program.command('build').action(async () => { - console.log('>> Building static sites'.green) + console.log('>> Building static files\n'.green) + + const startBuild = new Date().getTime() await build() - console.log('>> Build finished'.green) + const finishBuild = new Date().getTime() + + console.log( + `\n>> Build finished in`.green, + `${finishBuild - startBuild} ms`.green.bold + ) }) program.command('serve').action(async () => { await build() diff --git a/bin/serve.js b/bin/serve.js index 7d50106..1538092 100644 --- a/bin/serve.js +++ b/bin/serve.js @@ -10,6 +10,9 @@ const portfinder = require('portfinder') const build = require('./build') const configs = require('../src/parse/parseConfig') const mimeType = require('../src/utils/mimeTypes') +const copyAssets = require('../src/fs/copyAssets') +const removeDirectories = require('../src/fs/rmdir') +const globalInfo = require('../src') const createServer = (req, res) => { try { @@ -67,13 +70,12 @@ const createServer = (req, res) => { } const liveServer = async () => { - // Generates available port - const port = await portfinder.getPortPromise({ startPort: 3000 }) + const port = await portfinder.getPortPromise({ port: 3000 }) const server = http.createServer(createServer) const wss = new WebSocket.Server({ server: server.listen(port) }) - console.log(`\n>> Server listening on port ${port}`.green) + console.log(`\n>> Server listening on port ${port}`.yellow) let socket wss.on('connection', ws => { @@ -88,17 +90,32 @@ const liveServer = async () => { stabilityThreshold: 400 } }) - .on('add', chokidarEvent) .on('change', chokidarEvent) - .on('unlink', chokidarEvent) - - async function chokidarEvent(e) { - await build() - console.log('\n>> File change detected'.green) - if (socket) - path.parse(e).ext === '.css' - ? socket.send('refresh-css') - : socket.send('reload-page') + .on('add', chokidarEvent) + .on('unlink', async (p, s) => { + const assetsPath = path.join(globalInfo.configs.buildPath, '__assets__') + await removeDirectories(assetsPath) + await chokidarEvent(p, s) + }) + + async function chokidarEvent(p) { + console.log('\n>> File change detected'.yellow) + + const ext = path.parse(p).ext + + if (ext === '.css') { + console.log('>> Copying assets'.green) + await copyAssets() + if (socket) socket.send('refresh-css') + } else { + await build() + if (socket) socket.send('reload-page') + } + // await build() + // if (socket) + // path.parse(p).ext === '.css' + // ? socket.send('refresh-css') + // : socket.send('reload-page') } } diff --git a/package.json b/package.json index 617c0d4..0b67b97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ulka", - "version": "0.3.6", + "version": "0.3.7", "description": "Ulka - static site generator", "main": "./src/index.js", "bin": { diff --git a/src/generate/generateMd.js b/src/generate/generateMd.js index 52ed1f4..a1d75f9 100644 --- a/src/generate/generateMd.js +++ b/src/generate/generateMd.js @@ -55,11 +55,13 @@ async function generateFromMd() { `${createFilePath}/${parsedPath.name}.html` ) + const mfdData = await mfd.data + const templateData = await parseUlka( templateUlkaData, { - frontMatter: (await mfd.data).frontMatter, - data: (await mfd.data).html, + frontMatter: mfdData.frontMatter, + data: mfdData.html, ...configs }, markdownTemplatePath @@ -68,7 +70,7 @@ async function generateFromMd() { const link = createFilePath.split(configs.buildPath)[1] const html = templateData.html - const frontMatter = (await mfd.data).frontMatter + const frontMatter = mfdData.frontMatter globalInfo.contentFiles.push({ createFilePath, @@ -82,7 +84,7 @@ async function generateFromMd() { }) } catch (e) { console.log(`\n>> Error while generating ${mfd.path}`.red) - process.exit(0) + throw e } } } diff --git a/src/generate/generateUlka.js b/src/generate/generateUlka.js index 6c4df5b..4ed65ec 100644 --- a/src/generate/generateUlka.js +++ b/src/generate/generateUlka.js @@ -58,9 +58,8 @@ async function generateFromUlka() { fs.writeFileSync(absoluteFilePath, html) ) } catch (e) { - console.log(`\n>> ${e.message}`.red) console.log(`>> Error while generating ${ufd.path}`.red) - process.exit(0) + throw e } } } diff --git a/src/parse/parseMd.js b/src/parse/parseMd.js index 4acd180..68391fb 100644 --- a/src/parse/parseMd.js +++ b/src/parse/parseMd.js @@ -18,9 +18,10 @@ const markdownImageRender = markdown => { const parseMd = async (markdown, filePath) => { const data = frontmatter(markdown) const toHtml = parseMarkdown(markdownImageRender(data.body)) + const ulkaPrase = await parseUlka(toHtml.trim(), globalInfo, filePath) return { frontMatter: data.attributes, - html: (await parseUlka(toHtml.trim(), globalInfo, filePath)).html + html: ulkaPrase.html } }