Skip to content

Commit

Permalink
imporoved error and dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
coderosh committed Aug 9, 2020
1 parent 6641162 commit 587a5b8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 25 deletions.
7 changes: 5 additions & 2 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 9 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 30 additions & 13 deletions bin/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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')
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ulka",
"version": "0.3.6",
"version": "0.3.7",
"description": "Ulka - static site generator",
"main": "./src/index.js",
"bin": {
Expand Down
10 changes: 6 additions & 4 deletions src/generate/generateMd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -82,7 +84,7 @@ async function generateFromMd() {
})
} catch (e) {
console.log(`\n>> Error while generating ${mfd.path}`.red)
process.exit(0)
throw e
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/generate/generateUlka.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/parse/parseMd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 587a5b8

Please sign in to comment.