Scanner can now promise things

This commit is contained in:
LordMathis 2019-03-24 21:34:25 +01:00
parent a6ed6bd894
commit 7c834b2230
2 changed files with 118 additions and 57 deletions

34
.editorconfig Normal file
View File

@ -0,0 +1,34 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8
# 4 space indentation
[*.py]
indent_style = space
indent_size = 4
# Tab indentation (no size specified)
[Makefile]
indent_style = tab
# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2
# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2

View File

@ -1,5 +1,4 @@
import fs from 'fs' import fs from 'fs'
import async from 'async'
import path from 'path' import path from 'path'
import config from '../../config.json' import config from '../../config.json'
import fm from 'front-matter' import fm from 'front-matter'
@ -11,68 +10,87 @@ export class Scanner {
this.data = {} this.data = {}
} }
readdir (callback) { readdir (dirname) {
fs.readdir(config.contentPath, callback) return new Promise((resolve, reject) => {
} fs.readdir(dirname, function (err, filenames) {
if (err) {
processAll (files, callback) { reject(err)
console.log('[Scanner] Discovered files: ' + files) } else {
async.each(files, this.processFile, (err) => { resolve(filenames)
if (err) throw err }
callback() })
}) })
} }
processFile (file, callback) { readfile (filename) {
const filePath = path.join(process.cwd(), config.contentPath, filename)
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
reject(err)
} else {
resolve([filename, data])
}
})
})
}
processFile (file, data) {
const filePath = path.join(process.cwd(), config.contentPath, file) const filePath = path.join(process.cwd(), config.contentPath, file)
const metadata = this.fileMetadata(filePath) const metadata = this.fileMetadata(filePath)
fs.readFile(filePath, 'utf8', (err, data) => { if (config.files.indexOf(file) === -1) {
if (err) throw err const frontMatter = fm(data)
if (config.files.indexOf(file) === -1) { if (frontMatter.attributes.draft) {
const frontMatter = fm(data) return
if (frontMatter.attributes.draft) {
callback(null, null)
return
}
let published
if (frontMatter.attributes.date) {
published = moment(frontMatter.attributes.date)
} else {
published = moment()
}
const post = {
published: published.format('MMMM DD, YYYY'),
filename: metadata.filename,
title: frontMatter.attributes.title,
link: '/post/' + metadata.filename
}
this.data.posts.push(post)
} else {
this.data.push({
[metadata.filename]: data
})
} }
})
let published
if (frontMatter.attributes.date) {
published = moment(frontMatter.attributes.date)
} else {
published = moment()
}
const post = {
published: published.format('MMMM DD, YYYY'),
filename: metadata.filename,
title: frontMatter.attributes.title,
link: '/post/' + metadata.filename
}
this.data.posts.push(post)
} else {
this.data.push({
[metadata.filename]: data
})
}
} }
init (callback) { init () {
jsonfile.readFile(config.dataPath, (err, data) => { return new Promise((resolve, reject) => {
if (err) throw err jsonfile.readFile(config.dataPath, (err, data) => {
if (err) {
this.data = data reject(err)
callback() } else {
this.data = data
resolve(data)
}
})
}) })
} }
writeData (callback) { writeData (callback) {
console.log(this.data) return new Promise((resolve, reject) => {
jsonfile.writeFile(config.dataPath, this.data, callback) jsonfile.writeFile(config.dataPath, this.data, (err, data) => {
if (err) {
reject(err)
} else {
resolve(this.data)
}
})
})
} }
fileMetadata (filepath) { fileMetadata (filepath) {
@ -90,13 +108,22 @@ export class Scanner {
} }
scan () { scan () {
async.waterfall([ this.init()
this.init.bind(this), .then(
this.readdir.bind(this), () => this.readdir(config.contentPath)
this.processAll.bind(this), ).then(
this.writeData.bind(this) (files) => { return Promise.all(files.map(this.readfile)) }
], (err) => { ).then(
if (err) throw err (files) => {
}) files.forEach(
(item) => { this.processFile(item[0], item[1]) }
)
return this.writeData()
}
).then(
console.log('[Scanner] Scan complete')
).catch(
(err) => console.log(err)
)
} }
} }