Skip to content

Instantly share code, notes, and snippets.

@dmitryshelomanov
Created January 16, 2018 14:35
Show Gist options
  • Save dmitryshelomanov/068322c9e7934d891ed4329e5470b2ae to your computer and use it in GitHub Desktop.
Save dmitryshelomanov/068322c9e7934d891ed4329e5470b2ae to your computer and use it in GitHub Desktop.
const cheerio = require('cheerio')
const fs = require('fs-extra')
const { process } = require('../temp-path')()
const { Area, Rules } = require('../../models')
const types = require('./types')
function addElement({
type, bind_element: element, position, content,
}, folderName, fileName) {
return new Promise(async (res, rej) => {
const $ = cheerio.load(await fs.readFile(process(`${folderName}/${fileName}`)))
if ($(element).length === 0) {
return res({
error: true,
message: `element ${element} not found`,
status: 404,
})
}
res(true)
})
}
function computed(rule, folderName, fileName) {
switch (rule.type) {
case types.ADD: return addElement(rule, folderName, fileName)
default: return null
}
}
module.exports = async ({ nameFolder, area: areaName, fileName }) => {
const data = await Area.findOne({
include: [{
model: Rules,
attributes: {
exclude: ['id'],
},
}],
where: { name: areaName },
})
if (data.rules.length > 0) {
for (let i = 0; i < data.rules.length; i++) {
const rule = data.rules[i]
try {
const rs = await computed(rule, nameFolder, fileName)
}
catch (error) {
throw error
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment