Files
mol-robot/plugins/cook/db/index.js
mol b1709958e4
All checks were successful
continuous-integration/drone/push Build is passing
test
2023-12-14 13:39:51 +08:00

47 lines
993 B
JavaScript

import pkg from 'node-csv';
import logger from '#root/utils/logger.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()
recipeData = data.map(d => {
const res = {}
keys.forEach((key, index) => {
if (key) {
res[key] = d[index]
}
})
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)
// })