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,30 +10,39 @@ 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) {
reject(err)
} else {
resolve(filenames)
} }
})
processAll (files, callback) {
console.log('[Scanner] Discovered files: ' + files)
async.each(files, this.processFile, (err) => {
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 (err) throw err
if (config.files.indexOf(file) === -1) { if (config.files.indexOf(file) === -1) {
const frontMatter = fm(data) const frontMatter = fm(data)
if (frontMatter.attributes.draft) { if (frontMatter.attributes.draft) {
callback(null, null)
return return
} }
@ -58,21 +66,31 @@ export class Scanner {
[metadata.filename]: data [metadata.filename]: data
}) })
} }
})
} }
init (callback) { init () {
return new Promise((resolve, reject) => {
jsonfile.readFile(config.dataPath, (err, data) => { jsonfile.readFile(config.dataPath, (err, data) => {
if (err) throw err if (err) {
reject(err)
} else {
this.data = data this.data = data
callback() 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)
)
} }
} }