const {compiler, resolveName, ensureDirectoryExistence, writeFile} = require("./utils"); const path = require("path"); const fs = require("fs"); const moment = require('moment'); const { t } = require("./tables") async function createTable(name, fields, options) { const fileRes = await compiler("tableTemplate.ejs", { name: resolveName(name), originalName: name, fields, options }) ensureDirectoryExistence(path.resolve(__dirname, `../src/views/${resolveName(name)}`)) const filePath = path.resolve(__dirname, `../src/views/${resolveName(name)}`, `${resolveName(name)}.vue`) await writeFile(filePath, fileRes) } async function createDrawer(name, fields, options) { const fileRes = await compiler("drawerTemplate.ejs", { name: resolveName(name), originalName: name, fields, options }) ensureDirectoryExistence(path.resolve(__dirname, `../src/views/${resolveName(name)}`)) ensureDirectoryExistence(path.resolve(__dirname, `../src/views/${resolveName(name)}/components`)) const filePath = path.resolve(__dirname, `../src/views/${resolveName(name)}/components`, `Add${resolveName(name)}.vue`) await writeFile(filePath, fileRes) } async function createDetail(name, fields, options) { const fileRes = await compiler("showTemplate.ejs", { name: resolveName(name), originalName: name, fields, options }) ensureDirectoryExistence(path.resolve(__dirname, `../src/views/${resolveName(name)}`)) ensureDirectoryExistence(path.resolve(__dirname, `../src/views/${resolveName(name)}/components`)) const filePath = path.resolve(__dirname, `../src/views/${resolveName(name)}/components`, `Show${resolveName(name)}.vue`) await writeFile(filePath, fileRes) } async function createApi(name) { const fileRes = await compiler("apiTemplate.ejs", { name }) ensureDirectoryExistence(path.resolve(__dirname, `../src/api/${name}`)) const filePath = path.resolve(__dirname, `../src/api/${name}`, `${name}.js`) await writeFile(filePath, fileRes) } function create(name, fields, options) { try { createApi(name) createTable(name, fields, options) createDrawer(name, fields, options) createDetail(name, fields, options) t[name] = { fields, options } const textPath = path.resolve(__dirname, './log.txt') if (!fs.existsSync(textPath)) { fs.writeFileSync(textPath, '') } fs.appendFileSync(textPath, name + '\n', 'utf8') fs.appendFileSync(textPath, JSON.stringify(fields) + '\n', 'utf8') fs.appendFileSync(textPath, JSON.stringify(options) + '\n', 'utf8') fs.appendFileSync(textPath, moment().format('YYYY-MM-DD HH:mm:ss') + '\n', 'utf8') } catch (err) { console.error(err) } } module.exports = { create }