Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
firestoreから取得下データをjsonファイルにしてgenerate
- Loading branch information
Showing
8 changed files
with
170 additions
and
702 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,3 +82,7 @@ dist | |
|
||
# Service worker | ||
sw.* | ||
|
||
# generate file | ||
/data/* | ||
!/data/.gitkeep |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.