Skip to content

Commit

Permalink
page and template edge-files generate
Browse files Browse the repository at this point in the history
  • Loading branch information
talp-525 committed Feb 21, 2022
1 parent 1648e37 commit 5207b99
Show file tree
Hide file tree
Showing 96 changed files with 1,210 additions and 103 deletions.
2 changes: 1 addition & 1 deletion altrpnjs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ coverage
.DS_STORE
.env
tmp
views/templates
.env-prod

#bundles
Expand All @@ -15,3 +14,4 @@ app/AltrpModels/
app/AltrpControllers/
app/AltrpPlugins/
start/altrp-routes/
/resources/views/altrp
12 changes: 9 additions & 3 deletions altrpnjs/app/Controllers/Http/TemplatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PagesTemplate from "App/Models/PagesTemplate";
import Category from "App/Models/Category";
import CategoryObject from "App/Models/CategoryObject";
import filtration from "../../../helpers/filtration";
import TemplateGenerator from "App/Generators/TemplateGenerator";

export default class TemplatesController {
public async index({ request }) {
Expand All @@ -30,6 +31,7 @@ export default class TemplatesController {
.preload("user")
.preload("currentArea")
.whereNotNull('guid')
.whereNull('deleted_at')
.where("type", "template")
.preload("categories")
.whereHas("currentArea", (query) => {
Expand Down Expand Up @@ -141,8 +143,8 @@ export default class TemplatesController {
}
}
}

// await TemplateFactory.createMany(100)
let templateGenerator = new TemplateGenerator()
await templateGenerator.run(template)
return {
message: "Success",
redirect: true,
Expand Down Expand Up @@ -170,7 +172,9 @@ export default class TemplatesController {
public async delete({ params }) {
const template = await Template.query().where("id", parseInt(params.id)).firstOrFail();

template.delete()
let templateGenerator = new TemplateGenerator()
await templateGenerator.deleteFile(template)
await template.delete()
return {
success: true
}
Expand Down Expand Up @@ -207,6 +211,8 @@ export default class TemplatesController {

await template.save()

let templateGenerator = new TemplateGenerator()
await templateGenerator.run(template)
return {
currentTemplate: template,
prevVersions: prevVersions,
Expand Down
21 changes: 15 additions & 6 deletions altrpnjs/app/Controllers/Http/admin/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import Application from '@ioc:Adonis/Core/Application'
import sharp from "sharp";
import FAVICONS_SIZES from "../../../../helpers/const/FAVICONS_SIZES";
import Drive from '@ioc:Adonis/Core/Drive'
import Template from "App/Models/Template";
import TemplateGenerator from "App/Generators/TemplateGenerator";
import PageGenerator from "App/Generators/PageGenerator";
import Page from "App/Models/Page";

export default class AdminController {

Expand All @@ -18,6 +22,8 @@ export default class AdminController {
// const step = 10
const modelGenerator = new ModelGenerator()
const controllerGenerator = new ControllerGenerator()
const templateGenerator = new TemplateGenerator()
const pageGenerator = new PageGenerator()

for (let model of models){
if(model.name.toLowerCase() === 'user' || model.name.toLowerCase() === 'media'){
Expand All @@ -35,12 +41,15 @@ export default class AdminController {
}
await controllerGenerator.run(controller)
}
await Promise.all(models.map(async model=>{
if(! model.altrp_controller){
console.log(model.name);
}
}))
return response.json({success: true,})
const templates = await Template.query().where('type', 'template').whereNull('deleted_at').select('*')
for (let template of templates) {
await templateGenerator.run(template)
}
const pages = await Page.query().whereNull('deleted_at').select('*')
for (let page of pages) {
await pageGenerator.run(page)
}
return response.json({success: true,})
}


Expand Down
7 changes: 3 additions & 4 deletions altrpnjs/app/Controllers/Http/admin/CategoriesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ export default class CategoriesController {
}
let categories = await query
.orderBy('title', )
return response.json({
categories,
pageCount: 0,
})
return response.json(
categories
)
}

public async create({request, response}) {
Expand Down
8 changes: 5 additions & 3 deletions altrpnjs/app/Controllers/Http/admin/PagesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Template from "App/Models/Template";
import PagesTemplate from "App/Models/PagesTemplate";
import Category from "App/Models/Category";
import CategoryObject from "App/Models/CategoryObject";
import PageGenerator from "App/Generators/PageGenerator";

export default class PagesController {
public async create({auth, request, response}) {
Expand Down Expand Up @@ -81,7 +82,8 @@ export default class PagesController {
res.success = true
res.page = page
page.parseRoles(request.input('roles'));

const pageGenerator = new PageGenerator()
await pageGenerator.run(page)
await page.save()

return res
Expand Down Expand Up @@ -202,9 +204,9 @@ export default class PagesController {
page[input] = body[input]
}
})

await page.save()

const pageGenerator = new PageGenerator()
await pageGenerator.run(page)
if(request.input("categories")) {
await page.related("categories").detach()

Expand Down
20 changes: 17 additions & 3 deletions altrpnjs/app/Generators/BaseGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as mustache from 'mustache'
import fs from 'fs'
import Application from "@ioc:Adonis/Core/Application";
import path from "path";

export abstract class BaseGenerator{
private fileName: string;
private directory: string;
protected directory: string;
private stubFilePath: string;

protected addFile(fileName: string):this {
Expand All @@ -30,13 +32,25 @@ export abstract class BaseGenerator{

content = mustache.render(content, vars)
if(! fs.existsSync(this.directory)){
fs.mkdirSync(this.directory)
fs.mkdirSync(this.directory, {recursive:true})
}
fs.writeFileSync(this.getFullFileName(), content)
return
}

private getFullFileName():string{
return `${this.directory}/${this.fileName}`
return path.join(this.directory,this.fileName)
}

static async generateCssFile(fileName:string, content:string):Promise<void >{
if(fileName.indexOf('.css') !== fileName.length - 4){
fileName += '.css'
}
fileName = Application.publicPath(`altrp/css/${fileName}`)
if(! fs.existsSync(Application.publicPath(`altrp/css/`))){
fs.mkdirSync(Application.publicPath(`altrp/css/`), {recursive:true})
}
fs.writeFileSync(fileName, content)
return
}
}
4 changes: 2 additions & 2 deletions altrpnjs/app/Generators/ModelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export default class ModelGenerator extends BaseGenerator {
public async deleteFiles(model: Model): Promise<void> {
let fileName = this.getFilename(model)
if (fs.existsSync(ModelGenerator.directory + fileName)) {
fs.unlinkSync(ModelGenerator.directory + fileName);
fs.rmSync(ModelGenerator.directory + fileName);
}
fileName =`${model.name}Controller${ModelGenerator.ext}`
if (fs.existsSync(ControllerGenerator.directory + fileName)) {
fs.unlinkSync(ControllerGenerator.directory + fileName);
fs.rmSync(ControllerGenerator.directory + fileName);
}
return
}
Expand Down
46 changes: 46 additions & 0 deletions altrpnjs/app/Generators/PageGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from 'fs'
import {BaseGenerator} from "./BaseGenerator";
import Application from "@ioc:Adonis/Core/Application";
import app_path from "../../helpers/app_path";
import Page from "App/Models/Page";

export default class PageGenerator extends BaseGenerator {

public static directory = Application.resourcesPath('/views/altrp/pages')
public static template = app_path(`altrp-templates/views/Page.stub`)


async deleteFile(page: Page): Promise<void> {
let fileName = this.getFilename(page)
if (fs.existsSync(PageGenerator.directory + fileName)) {
fs.rmSync(PageGenerator.directory + fileName);
}
}

getFilename(page):string{
return page.guid + '.edge'
}

async run(page: Page) {
if(! page){
return
}


let fileName = this.getFilename(page)
if (!page.guid ) {
console.error(`Page ${page.id} render error. Need more data`);
return
}
let children_content = await page.getChildrenContent()
let all_styles = await page.getAllStyles()
return await this.addFile(fileName)
.destinationDir(PageGenerator.directory)
.stub(PageGenerator.template)
.apply({
children_content,
all_styles,
})

}
}
68 changes: 68 additions & 0 deletions altrpnjs/app/Generators/TemplateGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { parse } from 'node-html-parser'
import fs from 'fs'
import {BaseGenerator} from './BaseGenerator';
import Application from '@ioc:Adonis/Core/Application';
import app_path from '../../helpers/app_path';
import Template from 'App/Models/Template';
import path from 'path';
import * as _ from 'lodash'
import Logger from '@ioc:Adonis/Core/Logger'

export default class TemplateGenerator extends BaseGenerator {

public static directory = '/views/altrp/templates'
public static template = app_path(`altrp-templates/views/Template.stub`)


async deleteFile(template: Template): Promise<void> {
if (fs.existsSync(path.join(this.getDirectory(template),this.getFilename(template)))) {
fs.rmSync(path.join(this.getDirectory(template),this.getFilename(template)));
}
}

getDirectory(template:Template){
return Application.resourcesPath(`${TemplateGenerator.directory}/${template.currentArea?.name}`)
}

getFilename(template:Template):string{
return template.guid + '.edge'
}

async run(template: Template) {
if(! template){
return
}
await template.load('currentArea')

const styles = JSON.parse(template.styles)

let all_styles = _.get(styles, 'all_styles', [])
//
all_styles = all_styles.join('')
all_styles = parse(all_styles)
if(template.guid){
await BaseGenerator.generateCssFile(template.guid, all_styles.textContent)
}
let fileName = this.getFilename(template)
if (! template.currentArea?.name) {
console.error(`Template ${template.id} render error. Need Area name`);
Logger.warn(`Try Render Template without area (id: ${template.id})`)
return
}
if (!template.guid ) {
console.error(`Template ${template.id} render error. Need guid`);
Logger.warn(`Try Render Template without guid (id: ${template.id})`)
return
}
let children_content = await template.getChildrenContent()
return await this.addFile(fileName)
.destinationDir(this.getDirectory(template))
.stub(TemplateGenerator.template)
.apply({
children_content,
all_styles:'',
area_name: template.currentArea?.name,
})

}
}
Loading

0 comments on commit 5207b99

Please sign in to comment.