chore: import json data and display

This commit is contained in:
YunYouJun
2022-04-13 23:53:07 +08:00
parent 86d0fe7bfe
commit 2120e79726
12 changed files with 61 additions and 11 deletions

View File

@@ -1,17 +1,20 @@
// convert csv to json
import fs from 'fs'
import path from 'path'
import consola from 'consola'
import type { Recipe } from '~/types'
const recipeCsvFile = path.resolve(__dirname, '../public/data/recipe.csv')
const recipeJsonFile = path.resolve(__dirname, '../public/data/recipe.json')
const recipeCsvFile = path.resolve(__dirname, '../src/data/recipe.csv')
const recipeJsonFile = path.resolve(__dirname, '../src/data/recipe.json')
function run() {
const csvData = fs.readFileSync(recipeCsvFile, 'utf-8')
const lines = csvData.split(/\r?\n/)
if (lines[0] !== '名称,食材,链接,标签/描述,方法,工具')
console.log('Headers Changed!')
if (lines[0].trim() !== '名称,食材,链接,标签/描述,方法,工具') {
consola.warn(`Headers Changed: ${lines[0]}`)
return
}
const recipeJson: Recipe = []
const sep = '、'
@@ -20,12 +23,12 @@ function run() {
if (line) {
const attrs = line.split(',')
recipeJson.push({
name: attrs[0],
stuff: attrs[1].split(sep),
link: attrs[2],
tags: attrs[3].split(sep),
methods: attrs[4].split(sep),
tools: attrs[5].split(sep),
name: attrs[0].trim(),
stuff: attrs[1].trim().split(sep),
link: attrs[2].trim(),
tags: attrs[3].trim().split(sep),
methods: attrs[4].trim().split(sep),
tools: attrs[5].trim().split(sep),
})
}
})