Files
mol-robot/plugins/cook/db/index.js
mol 16196c46ac
All checks were successful
continuous-integration/drone/push Build is passing
test
2023-12-14 14:35:47 +08:00

56 lines
1.3 KiB
JavaScript

import pkg from 'node-csv';
import logger from '#root/utils/logger.js';
import { emojiMap } from './food.js'
import path from 'path';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
const { createParser } = pkg
let recipeData;
let loaded = false;
function parser() {
const csv = createParser();
return new Promise((r, j) => {
csv.parseFile(path.join(__dirname, './recipe.csv'), function (err, data) {
if (err) {
logger.err(err);
return j();
}
logger('食谱加载成功');
const keys = data.shift()
keys[0] = keys[0].slice(1)
recipeData = data.map(d => {
const res = {}
keys.forEach((key, index) => {
if (key) {
let text = d[index]
if (key === 'stuff') {
text = text.split('、').map(t => {
return `${emojiMap.has(t) ? emojiMap.get(t) + ' ' : ''}${t}`
}).join('、')
}
res[key] = text
}
})
return res
});
loaded = true;
r(recipeData);
});
});
}
export default async function getDb() {
if (loaded) {
return recipeData;
} else {
await parser()
return recipeData;
}
}
// getDb().then((res) => {
// // console.log(recipeData)
// })