Skip to content

Commit

Permalink
firestoreから取得下データをjsonファイルにしてgenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
hikarut committed Jul 25, 2019
1 parent 4b93d0f commit 1136268
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 702 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -82,3 +82,7 @@ dist

# Service worker
sw.*

# generate file
/data/*
!/data/.gitkeep
Empty file added data/.gitkeep
Empty file.
6 changes: 0 additions & 6 deletions lib/date.js
Expand Up @@ -70,9 +70,6 @@ export function getAllTerm() {
const nowDay = toDoubleDigits(now.getDate())
const nowKey = `${nowYear}${nowMonth}`
const nowValue = `${nowYear}${nowMonth}${nowDay}`
// let nowData = {}
// nowData[nowKey] = nowValue
// dateArray.push(nowData)
dateArray.push({ key: nowKey, value: nowValue })

while (startDay !== endDay) {
Expand All @@ -83,9 +80,6 @@ export function getAllTerm() {
const tmpDay = toDoubleDigits(now.getDate())
const tmpKey = `${tmpYear}${tmpMonth}`
const tmpValue = `${tmpYear}${tmpMonth}${tmpDay}`
// let tmpData = {}
// tmpData[tmpKey] = tmpValue
// dateArray.push(tmpData)
dateArray.push({ key: tmpKey, value: tmpValue })
endDay = `${tmpYear}${tmpMonth}${tmpDay}`
}
Expand Down
158 changes: 90 additions & 68 deletions modules/generator.js
@@ -1,71 +1,93 @@
// import firebase from 'firebase/app'
// import 'firebase/firestore'
// import { getTopTerm } from '../lib/date'
//
// module.exports = function generateModule(moduleOptions) {
// console.log('modules generator')
//
// // nuxtのビルド前
// this.nuxt.hook('build:before', async ({ app }) => {
// if (!firebase.apps.length) {
// firebase.initializeApp({
// apiKey: process.env.API_KEY,
// authDomain: process.env.AUTH_DOMAIN,
// databaseURL: process.env.DATABASE_URL,
// projectId: process.env.PROJECT_ID,
// storageBucket: process.env.STORAGE_BUCKET,
// messagingSenderId: process.env.MESSAGING_SENDER_ID,
// appId: process.env.APP_ID
// })
// }
//
// console.log('build:before')
// const { start, end } = getTopTerm()
// const topSnapshot = await firebase
// .firestore()
// .collection('news')
// .where('date', '>=', start)
// .where('date', '<=', end)
// .orderBy('date', 'desc')
// .get()
//
// const topNews = topSnapshot.docs.map(doc => {
// return doc.data()
// })
//
// // JSONを生成
// this.options.build.plugins.push({
// apply(compiler) {
// compiler.plugin('emit', (compilation, cb) => {
// compilation.assets['top.json'] = {
// source: () => JSON.stringify(topNews),
// size: () => {}
// }
// cb()
// console.log('topNews')
// console.log(topNews.length)
// console.log(JSON.stringify(topNews))
// })
// }
// })
// })
// }
import firebase from 'firebase/app'
import 'firebase/firestore'
import fs from 'fs'
import { getTopTerm, getAllTerm } from '../lib/date'

export default function(moduleOptions) {
const info = 'Built by awesome module - 1.3 alpha on ' + Date.now()
module.exports = function generateModule(moduleOptions) {
console.log('modules generator')

// this.options.build.plugins.push({
// apply(compiler) {
// compiler.plugin('emit', (compilation, cb) => {
// // info 変数の内容を用いて `.nuxt/dist/info.txt' を生成する
// // source はバッファとなる
// compilation.assets['info.txt'] = {
// source: () => info,
// size: () => info.length
// }
// // cb()
// console.log(info)
// })
// }
// })
// nuxtのビルド前
this.nuxt.hook('build:before', async ({ app }) => {
if (!firebase.apps.length) {
firebase.initializeApp({
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID,
appId: process.env.APP_ID
})
}

// トップページ用のデータを取得
const { start, end } = getTopTerm()
const topSnapshot = await firebase
.firestore()
.collection('news')
.where('date', '>=', start)
.where('date', '<=', end)
.orderBy('date', 'desc')
.get()

const topNews = topSnapshot.docs.map(doc => {
return doc.data()
})
// JSONを生成
const distJosonFile = './data/top.json'
fs.writeFile(distJosonFile, JSON.stringify(topNews), (err, result) => {
if (err) console.log('error', err)
})

// 日次のデータを取得
const allTerm = getAllTerm()
allTerm.forEach(async day => {
const dailySnapshot = await firebase
.firestore()
.collection('news')
.where('date', '==', day.value)
.get()

let dailyData = []
if (dailySnapshot.docs.length > 0) {
dailyData = dailySnapshot.docs.map(doc => {
return doc.data()
})
}
// JSONを生成
const distJosonFile = `./data/${day.value}.json`
fs.writeFile(distJosonFile, JSON.stringify(dailyData), err => {
if (err) console.log('error', err)
})
})

// 月次のデータを取得
const uniqMonth = allTerm
.map((val, key, self) => {
return val.key
})
.filter(function(x, i, self) {
return self.indexOf(x) === i
})

uniqMonth.forEach(async month => {
const monthlySnapshot = await firebase
.firestore()
.collection('news')
.where('month', '==', month)
.get()

let monthlyData = []
if (monthlySnapshot.docs.length > 0) {
monthlyData = monthlySnapshot.docs.map(doc => {
return doc.data()
})
}
// JSONを生成
const distJosonFile = `./data/${month}.json`
fs.writeFile(distJosonFile, JSON.stringify(monthlyData), err => {
if (err) console.log('error', err)
})
})
})
}
3 changes: 3 additions & 0 deletions nuxt.config.js
Expand Up @@ -150,6 +150,9 @@ export default {
loader: 'json5-loader',
exclude: /(node_modules)/
})
config.node = {
fs: 'empty'
}
}
},

Expand Down

0 comments on commit 1136268

Please sign in to comment.