Use chokidar to add files

This commit is contained in:
LordMathis 2019-11-24 00:42:45 +01:00
parent 7692e15802
commit 1c4f75963e
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
5 changed files with 97 additions and 93 deletions

View File

@ -1,4 +1,4 @@
import { NotFoundWrapper } from '../containers'
import { NotFoundContainer } from '../containers'
import React, { Component } from 'react'
import routes from '../utils/routes'
import { Route, Switch } from 'react-router-dom'
@ -18,7 +18,7 @@ export default class App extends Component {
)}
/>
))}
<Route render={(props) => <NotFoundWrapper {...props} />} />
<Route render={(props) => <NotFoundContainer {...props} />} />
</Switch>
</div>
)

View File

@ -12,7 +12,7 @@ const port = process.env.PORT || 3000
const app = express()
app.set('trust proxy', true)
let config = jsonfile.readFileSync(path.join(process.cwd(), 'config/config.json'))
const config = jsonfile.readFileSync(path.join(process.cwd(), 'config/config.json'))
if (config == null) {
throw new Error('Config file not found!')
}
@ -20,26 +20,22 @@ if (config == null) {
const scanner = new Scanner(config)
const watcher = chokidar.watch(path.join(process.cwd(), 'content'), {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
ignoreInitial: true
});
ignored: /(^|[/\\])\../, // ignore dotfiles
persistent: true
})
watcher
.on('ready', () => {
scanner.scan()
.on('add', filepath => {
console.log(`[Watcher] File ${filepath} has been added`)
scanner.addFile(filepath)
})
.on('add', path => {
console.log(`[Watcher] File ${path} has been added`)
scanner.scan()
.on('change', filepath => {
console.log(`[Watcher] File ${filepath} has been changed`)
scanner.updateFile(filepath)
})
.on('change', path => {
console.log(`[Watcher] File ${path} has been changed`)
scanner.scan()
})
.on('unlink', path => {
console.log(`[Watcher] File ${path} has been removed`)
scanner.scan()
.on('unlink', filepath => {
console.log(`[Watcher] File ${filepath} has been removed`)
scanner.deleteFile(filepath)
})
app.use(morgan('common'))
@ -65,7 +61,7 @@ app.get('/favicon.ico', (req, res) => {
let head = jsonfile.readFileSync(path.join(process.cwd(), 'config/head.json'))
if (head == null) {
head = {
"scripts": []
scripts: []
}
}

View File

@ -18,8 +18,8 @@ function readFile (fileName, type, options) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, options, (err, data) => {
err ? reject(err) : resolve({
'type': type,
'data': data
type: type,
data: data
})
})
})

View File

@ -9,50 +9,59 @@ export class Scanner {
constructor (config) {
this.config = config
this.initData()
}
initData () {
this.data = {
'posts': [],
'other': {}
posts: [],
other: {}
}
}
readdir (dirname) {
return new Promise((resolve, reject) => {
fs.readdir(dirname, function (err, filenames) {
if (err) {
reject(err)
addFile (filepath) {
if (path.extname(filepath) === '.jpg' || path.extname(filepath) === '.png' || path.extname(filepath) === '.gif') {
this.copyImage(filepath)
.then((file) => this.gzipImage(file))
} else {
resolve(filenames)
this.readfile(filepath)
.then((data) => this.processFile(data[0], data[1]))
.then(() => this.writeData())
}
})
})
}
readfile (filename) {
const filePath = path.join(process.cwd(), 'content', filename)
updateFile (filepath) {
this.deleteFile(filepath)
this.addFile(filepath)
}
deleteFile (filepath) {
this.data.posts = this.data.posts.filter((post) =>
post.filename !== filepath
)
}
readfile (filePath) {
const relPath = path.relative(path.join(process.cwd(), 'content'), filePath)
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
reject(err)
} else {
resolve([filename, data])
resolve([relPath, data])
}
})
})
}
copyImage (filename) {
copyImage (filePath) {
return new Promise((resolve, reject) => {
const inputPath = path.join(process.cwd(), 'content', filename)
const outputPath = path.join(process.cwd(), 'public/static', filename)
fs.copyFile(inputPath, outputPath, (err) => {
const relPath = path.relative(path.join(process.cwd(), 'content'), filePath)
const outputPath = path.join(process.cwd(), 'public/static', relPath)
fs.copyFile(filePath, outputPath, (err) => {
if (err) {
reject(err)
} else {
resolve(filename)
resolve(relPath)
}
})
})
@ -63,14 +72,13 @@ export class Scanner {
const inputPath = path.join(process.cwd(), 'public/static', filename)
const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`)
const fileContents = fs.createReadStream(inputPath);
const writeStream = fs.createWriteStream(outputPath);
const zip = zlib.createGzip();
const fileContents = fs.createReadStream(inputPath)
const writeStream = fs.createWriteStream(outputPath)
const zip = zlib.createGzip()
fileContents.pipe(zip).pipe(writeStream).on('finish', (err) => {
if (err) {
reject(err)
}
else {
} else {
resolve()
}
})
@ -85,7 +93,7 @@ export class Scanner {
const frontMatter = fm(data)
if (frontMatter.attributes.draft) {
return
return Promise.resolve()
}
let published
@ -109,6 +117,8 @@ export class Scanner {
} else {
this.data.other[metadata.filename] = data
}
return Promise.resolve()
}
writeData () {
@ -138,43 +148,43 @@ export class Scanner {
return metadata
}
scan () {
this.readdir(path.join(process.cwd(), 'content'))
.then(
(files) => {
const filtered = files.filter(
(file) => (
fs.statSync(path.join(process.cwd(), 'content', file)).isFile()
)
)
// scan () {
// this.readdir(path.join(process.cwd(), 'content'))
// .then(
// (files) => {
// const filtered = files.filter(
// (file) => (
// fs.statSync(path.join(process.cwd(), 'content', file)).isFile()
// )
// )
const images = filtered.filter(
(file) => (
path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif'
)
)
Promise.all(images.map(this.copyImage))
.then((files) => files.map(this.gzipImage))
// const images = filtered.filter(
// (file) => (
// path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif'
// )
// )
// Promise.all(images.map(this.copyImage))
// .then((files) => files.map(this.gzipImage))
const posts = filtered.filter(
(file) => (
path.extname(file) == '.md'
)
)
// const posts = filtered.filter(
// (file) => (
// path.extname(file) == '.md'
// )
// )
return Promise.all(posts.map(this.readfile))
}
).then(
(files) => {
files.forEach(
(item) => { this.processFile(item[0], item[1]) }
)
return this.writeData()
}
).then(
console.log('[Scanner] Scan complete')
).catch(
(err) => console.log(err)
)
}
// return Promise.all(posts.map(this.readfile))
// }
// ).then(
// (files) => {
// files.forEach(
// (item) => { this.processFile(item[0], item[1]) }
// )
// return this.writeData()
// }
// ).then(
// console.log('[Scanner] Scan complete')
// ).catch(
// (err) => console.log(err)
// )
// }
}

View File

@ -7,14 +7,12 @@ import serialize from 'serialize-javascript'
import manifest from '../../public/static/manifest.json'
export class ServerRenderer {
constructor (head, config) {
this.head = head
this.config = config
}
render (req, res, next) {
const activeRoute = routes.find((route) => matchPath(req.url, route)) || false
const head = this.head
const config = this.config