const yaml = require('js-yaml');
const fs   = require('fs');
const path = "./content/news/"
var articles = []
fs.readdirSync(path).forEach(files=>{ 
    // console.log(files); 
    try {
        const doc = yaml.loadAll(fs.readFileSync(path + files, 'utf8'));
        articles.push(doc[0])
      } catch (e) {
        console.log(e);
      }
})

articles.sort((a, b) => {
    ta = new Date(a.createDate)
    tb = new Date(b.createDate)
    return tb.getTime() - ta.getTime()
})
try {
    let str = yaml.dump(articles, "./content/newsList.md")
    str = `---
news:
${str}
---`
    fs.writeFile('./content/newsList.md', str, function (error) {
        if (error) {
          console.log('写入失败')
        } else {
          console.log('写入成功了')
        }
      })
} catch(e) {
    console.log(e)
}