Use chokidar to add files
This commit is contained in:
parent
7692e15802
commit
1c4f75963e
|
@ -1,4 +1,4 @@
|
||||||
import { NotFoundWrapper } from '../containers'
|
import { NotFoundContainer } from '../containers'
|
||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import routes from '../utils/routes'
|
import routes from '../utils/routes'
|
||||||
import { Route, Switch } from 'react-router-dom'
|
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>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,7 +12,7 @@ const port = process.env.PORT || 3000
|
||||||
const app = express()
|
const app = express()
|
||||||
app.set('trust proxy', true)
|
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) {
|
if (config == null) {
|
||||||
throw new Error('Config file not found!')
|
throw new Error('Config file not found!')
|
||||||
}
|
}
|
||||||
|
@ -20,26 +20,22 @@ if (config == null) {
|
||||||
const scanner = new Scanner(config)
|
const scanner = new Scanner(config)
|
||||||
|
|
||||||
const watcher = chokidar.watch(path.join(process.cwd(), 'content'), {
|
const watcher = chokidar.watch(path.join(process.cwd(), 'content'), {
|
||||||
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
ignored: /(^|[/\\])\../, // ignore dotfiles
|
||||||
persistent: true,
|
persistent: true
|
||||||
ignoreInitial: true
|
})
|
||||||
});
|
|
||||||
|
|
||||||
watcher
|
watcher
|
||||||
.on('ready', () => {
|
.on('add', filepath => {
|
||||||
scanner.scan()
|
console.log(`[Watcher] File ${filepath} has been added`)
|
||||||
|
scanner.addFile(filepath)
|
||||||
})
|
})
|
||||||
.on('add', path => {
|
.on('change', filepath => {
|
||||||
console.log(`[Watcher] File ${path} has been added`)
|
console.log(`[Watcher] File ${filepath} has been changed`)
|
||||||
scanner.scan()
|
scanner.updateFile(filepath)
|
||||||
})
|
})
|
||||||
.on('change', path => {
|
.on('unlink', filepath => {
|
||||||
console.log(`[Watcher] File ${path} has been changed`)
|
console.log(`[Watcher] File ${filepath} has been removed`)
|
||||||
scanner.scan()
|
scanner.deleteFile(filepath)
|
||||||
})
|
|
||||||
.on('unlink', path => {
|
|
||||||
console.log(`[Watcher] File ${path} has been removed`)
|
|
||||||
scanner.scan()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use(morgan('common'))
|
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'))
|
let head = jsonfile.readFileSync(path.join(process.cwd(), 'config/head.json'))
|
||||||
if (head == null) {
|
if (head == null) {
|
||||||
head = {
|
head = {
|
||||||
"scripts": []
|
scripts: []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ function readFile (fileName, type, options) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
fs.readFile(fileName, options, (err, data) => {
|
fs.readFile(fileName, options, (err, data) => {
|
||||||
err ? reject(err) : resolve({
|
err ? reject(err) : resolve({
|
||||||
'type': type,
|
type: type,
|
||||||
'data': data
|
data: data
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -9,50 +9,59 @@ export class Scanner {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
this.config = config
|
this.config = config
|
||||||
this.initData()
|
this.initData()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
initData () {
|
initData () {
|
||||||
this.data = {
|
this.data = {
|
||||||
'posts': [],
|
posts: [],
|
||||||
'other': {}
|
other: {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readdir (dirname) {
|
addFile (filepath) {
|
||||||
return new Promise((resolve, reject) => {
|
if (path.extname(filepath) === '.jpg' || path.extname(filepath) === '.png' || path.extname(filepath) === '.gif') {
|
||||||
fs.readdir(dirname, function (err, filenames) {
|
this.copyImage(filepath)
|
||||||
if (err) {
|
.then((file) => this.gzipImage(file))
|
||||||
reject(err)
|
} else {
|
||||||
} else {
|
this.readfile(filepath)
|
||||||
resolve(filenames)
|
.then((data) => this.processFile(data[0], data[1]))
|
||||||
}
|
.then(() => this.writeData())
|
||||||
})
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readfile (filename) {
|
updateFile (filepath) {
|
||||||
const filePath = path.join(process.cwd(), 'content', filename)
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.readFile(filePath, 'utf8', (err, data) => {
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
} else {
|
} else {
|
||||||
resolve([filename, data])
|
resolve([relPath, data])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
copyImage (filename) {
|
copyImage (filePath) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const inputPath = path.join(process.cwd(), 'content', filename)
|
const relPath = path.relative(path.join(process.cwd(), 'content'), filePath)
|
||||||
const outputPath = path.join(process.cwd(), 'public/static', filename)
|
const outputPath = path.join(process.cwd(), 'public/static', relPath)
|
||||||
fs.copyFile(inputPath, outputPath, (err) => {
|
fs.copyFile(filePath, outputPath, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
} else {
|
} else {
|
||||||
resolve(filename)
|
resolve(relPath)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -63,14 +72,13 @@ export class Scanner {
|
||||||
const inputPath = path.join(process.cwd(), 'public/static', filename)
|
const inputPath = path.join(process.cwd(), 'public/static', filename)
|
||||||
const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`)
|
const outputPath = path.join(process.cwd(), 'public/static', `${filename}.gz`)
|
||||||
|
|
||||||
const fileContents = fs.createReadStream(inputPath);
|
const fileContents = fs.createReadStream(inputPath)
|
||||||
const writeStream = fs.createWriteStream(outputPath);
|
const writeStream = fs.createWriteStream(outputPath)
|
||||||
const zip = zlib.createGzip();
|
const zip = zlib.createGzip()
|
||||||
fileContents.pipe(zip).pipe(writeStream).on('finish', (err) => {
|
fileContents.pipe(zip).pipe(writeStream).on('finish', (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err)
|
reject(err)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -85,7 +93,7 @@ export class Scanner {
|
||||||
const frontMatter = fm(data)
|
const frontMatter = fm(data)
|
||||||
|
|
||||||
if (frontMatter.attributes.draft) {
|
if (frontMatter.attributes.draft) {
|
||||||
return
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
let published
|
let published
|
||||||
|
@ -109,6 +117,8 @@ export class Scanner {
|
||||||
} else {
|
} else {
|
||||||
this.data.other[metadata.filename] = data
|
this.data.other[metadata.filename] = data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
writeData () {
|
writeData () {
|
||||||
|
@ -138,43 +148,43 @@ export class Scanner {
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
scan () {
|
// scan () {
|
||||||
this.readdir(path.join(process.cwd(), 'content'))
|
// this.readdir(path.join(process.cwd(), 'content'))
|
||||||
.then(
|
// .then(
|
||||||
(files) => {
|
// (files) => {
|
||||||
const filtered = files.filter(
|
// const filtered = files.filter(
|
||||||
(file) => (
|
// (file) => (
|
||||||
fs.statSync(path.join(process.cwd(), 'content', file)).isFile()
|
// fs.statSync(path.join(process.cwd(), 'content', file)).isFile()
|
||||||
)
|
// )
|
||||||
)
|
// )
|
||||||
|
|
||||||
const images = filtered.filter(
|
// const images = filtered.filter(
|
||||||
(file) => (
|
// (file) => (
|
||||||
path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif'
|
// path.extname(file) == '.jpg' || path.extname(file) == '.png' || path.extname(file) == '.gif'
|
||||||
)
|
// )
|
||||||
)
|
// )
|
||||||
Promise.all(images.map(this.copyImage))
|
// Promise.all(images.map(this.copyImage))
|
||||||
.then((files) => files.map(this.gzipImage))
|
// .then((files) => files.map(this.gzipImage))
|
||||||
|
|
||||||
const posts = filtered.filter(
|
// const posts = filtered.filter(
|
||||||
(file) => (
|
// (file) => (
|
||||||
path.extname(file) == '.md'
|
// path.extname(file) == '.md'
|
||||||
)
|
// )
|
||||||
)
|
// )
|
||||||
|
|
||||||
return Promise.all(posts.map(this.readfile))
|
// return Promise.all(posts.map(this.readfile))
|
||||||
}
|
// }
|
||||||
).then(
|
// ).then(
|
||||||
(files) => {
|
// (files) => {
|
||||||
files.forEach(
|
// files.forEach(
|
||||||
(item) => { this.processFile(item[0], item[1]) }
|
// (item) => { this.processFile(item[0], item[1]) }
|
||||||
)
|
// )
|
||||||
return this.writeData()
|
// return this.writeData()
|
||||||
}
|
// }
|
||||||
).then(
|
// ).then(
|
||||||
console.log('[Scanner] Scan complete')
|
// console.log('[Scanner] Scan complete')
|
||||||
).catch(
|
// ).catch(
|
||||||
(err) => console.log(err)
|
// (err) => console.log(err)
|
||||||
)
|
// )
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,12 @@ import serialize from 'serialize-javascript'
|
||||||
import manifest from '../../public/static/manifest.json'
|
import manifest from '../../public/static/manifest.json'
|
||||||
|
|
||||||
export class ServerRenderer {
|
export class ServerRenderer {
|
||||||
|
|
||||||
constructor (head, config) {
|
constructor (head, config) {
|
||||||
this.head = head
|
this.head = head
|
||||||
this.config = config
|
this.config = config
|
||||||
}
|
}
|
||||||
|
|
||||||
render (req, res, next) {
|
render (req, res, next) {
|
||||||
|
|
||||||
const activeRoute = routes.find((route) => matchPath(req.url, route)) || false
|
const activeRoute = routes.find((route) => matchPath(req.url, route)) || false
|
||||||
const head = this.head
|
const head = this.head
|
||||||
const config = this.config
|
const config = this.config
|
||||||
|
|
Loading…
Reference in New Issue