Skip to content

Commit

Permalink
chore: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wanoo21 committed Jan 15, 2025
1 parent b58ec44 commit f5153e8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4,315 deletions.
4 changes: 2 additions & 2 deletions generate-template-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from 'fs'
import { getDirectoriesNames } from './utils';
import {writeFileSync} from 'node:fs'
import {getDirectoriesNames} from './utils';

const templates = getDirectoriesNames('./templates').map(category => {
return {
Expand Down
23 changes: 13 additions & 10 deletions generate-thumbnails.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { promisify } from 'util'
import {promisify} from 'node:util'
import webshot from 'webshot'
import path from 'path'
import fs from 'fs'
import path, {dirname} from 'node:path'
import fs from 'node:fs'
import mjml2html from 'mjml'
import {fileURLToPath} from "node:url";

import categories from './templates/templates.json'
import { getFilePathByType } from './utils'
import {getFilePathByType} from './utils'

const access = promisify(fs.access)
const mkdir = promisify(fs.mkdir)

const TEMPLATES_FOLDER = path.join(__dirname, 'templates')
const THUMB_FOLDER = path.join(__dirname, 'thumbnails')
const TEMPLATES_FOLDER = path.join(dirname(fileURLToPath(import.meta.url)), 'templates')
const THUMB_FOLDER = path.join(dirname(fileURLToPath(import.meta.url)), 'thumbnails')

const WEBSHOT_OPTIONS = {
siteType: 'html',
Expand Down Expand Up @@ -42,7 +43,7 @@ async function generateThumbnail(category: string, template: string) {
console.log(` > Creating ${category}/${template}`)
const mjml = fs.readFileSync(
getFilePathByType(path.join(TEMPLATES_FOLDER, category, template), '.mjml'),
{ encoding: 'utf-8' }
{encoding: 'utf-8'}
)
const html = await getHTML(mjml);
await Promise.all([shot(category, template, html), /** shot(category, template, html, true) */])
Expand All @@ -68,9 +69,11 @@ function shot(category: string, template: string, html: string, small = false) {
}
return new Promise<void>((resolve, reject) => {
webshot(html, thumbName, {
...WEBSHOT_OPTIONS, ...(small && { screenSize: { width: 300 }, shotSize: { width: 300 } })
...WEBSHOT_OPTIONS, ...(small && {screenSize: {width: 300}, shotSize: {width: 300}})
}, (err: Error) => {
if (err) { return reject(err) }
if (err) {
return reject(err)
}
resolve()
})
})
Expand All @@ -82,7 +85,7 @@ function shot(category: string, template: string, html: string, small = false) {
await isWritableOrCreate(THUMB_FOLDER)

console.log('>> Generating thumbnails')
await categories.reduce(async (promise, { templates, category }) => {
await categories.reduce(async (promise, {templates, category}) => {
await promise
return await templates.reduce(async (promise2, template) => {
await promise2
Expand Down
8 changes: 4 additions & 4 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {createReadStream, readFileSync} from 'fs';
import {createReadStream, readFileSync} from 'node:fs';
import express, {Request, Response} from 'express';
import cors from 'cors';
import {json, urlencoded} from 'body-parser';
import bodyParser from 'body-parser';

import {convertIPEmail, getFilePathByType, onlyMJML} from './index';
import {convertMjmlToIpEmail} from './mjml-to-email';
Expand All @@ -12,8 +12,8 @@ const app = express();

app.disable('etag').disable('x-powered-by');

app.use(json({limit: '1mb'}));
app.use(urlencoded({limit: '1mb', extended: true}));
app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({limit: '1mb', extended: true}));
app.use(cors());

app.set('port', PORT || 3000);
Expand Down
Loading

0 comments on commit f5153e8

Please sign in to comment.