Last active
December 13, 2023 03:38
-
-
Save brunomarks7/46adff366aba8431fa642dd85ec6a4f2 to your computer and use it in GitHub Desktop.
Newsletter - Unzip, optimize images, replace in files and upload to domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const AdmZip = require('adm-zip'); | |
const readline = require('readline'); | |
const sharp = require('sharp'); | |
const { exec } = require('child_process'); | |
// Cria uma interface readline | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
function optimizeImages(directory, quality) { | |
fs.readdir(directory, (err, files) => { | |
if (err) throw err; | |
files.forEach(file => { | |
const filePath = path.join(directory, file); | |
const ext = path.extname(file); | |
// Verifica se o arquivo é uma imagem | |
if (ext === '.jpg' || ext === '.jpeg' || ext === '.png') { | |
sharp(filePath) | |
.metadata() | |
.then(metadata => { | |
// Se a largura da imagem for maior que 700px, redimensiona para 700px | |
if (metadata.width > 700) { | |
return sharp(filePath) | |
.resize(700) | |
.toFormat(ext === '.png' ? 'png' : 'jpeg', { quality: quality, background: { r: 0, g: 0, b: 0, alpha: 0 } }) | |
.toFile(filePath.replace(ext, '_optimized' + ext)); | |
} else { | |
// Se a largura da imagem for menor ou igual a 700px, apenas otimiza a qualidade | |
return sharp(filePath) | |
.toFormat(ext === '.png' ? 'png' : 'jpeg', { quality: quality, background: { r: 0, g: 0, b: 0, alpha: 0 } }) | |
.toFile(filePath.replace(ext, '_optimized' + ext)); | |
} | |
}) | |
.then(info => { | |
console.log('Image optimized:', info); | |
// Deleta o arquivo original | |
fs.unlink(filePath, (err) => { | |
if (err) throw err; | |
console.log('Original file deleted:', filePath); | |
}); | |
}) | |
.catch(err => { | |
throw err; | |
}); | |
} | |
}); | |
}); | |
} | |
function rsyncToServer(projectName, serverUser, serverHost) { | |
const command = `rsync -avz -e "ssh -i ~/.ssh/id_rsa.pub" ./${projectName}/ ${serverUser}@${serverHost}:/home/public_html/newsletter/${projectName}`; | |
exec(command, (error, stdout, stderr) => { | |
if (error) { | |
console.log(`error: ${error.message}`); | |
return; | |
} | |
if (stderr) { | |
console.log(`stderr: ${stderr}`); | |
return; | |
} | |
console.log(`stdout: ${stdout}`); | |
console.log(`Projeto ${projectName} criado com sucesso!`) | |
console.log(`Acesse: https://domain.com/newsletter/${projectName}/`) | |
}); | |
} | |
function replaceImagePath(projectName) { | |
const filePath = path.join(__dirname, projectName, 'index.html'); | |
fs.readFile(filePath, 'utf8', (err, data) => { | |
if (err) throw err; | |
// Realiza a operação de find/replace para o caminho da imagem | |
let result = data.replace(/src="images\//g, `src="https://domain.com/newsletter/${projectName}/images/`); | |
// Adiciona o sufixo _optimized antes da extensão de todas as imagens | |
result = result.replace(/(src="https:\/\/domain\.com\/newsletter\/[a-zA-Z0-9_-]+\/images\/)([a-zA-Z0-9_-]+)(\.jpg|\.jpeg|\.png)/g, `$1$2_optimized$3`); | |
fs.writeFile(filePath, result, 'utf8', (err) => { | |
if (err) throw err; | |
console.log('The file has been saved!'); | |
}); | |
}); | |
} | |
// Pergunta o nome do projeto ao usuário | |
rl.question('Qual é o nome do projeto? ', (projectName) => { | |
// Substitui espaços por hífens | |
projectName = projectName.replace(/ /g, '-'); | |
// Caminho para o arquivo zip | |
const zipPath = path.join(__dirname, 'news.zip'); | |
// Cria uma instância do AdmZip | |
const zip = new AdmZip(zipPath); | |
// Extrai o conteúdo do arquivo zip para a pasta com o nome do projeto | |
zip.extractAllTo(`./${projectName}`, true); | |
// Lê o diretório do projeto | |
fs.readdir(`./${projectName}`, (err, files) => { | |
if (err) throw err; | |
// Procura por um arquivo .html | |
for (const file of files) { | |
if (path.extname(file) === '.html') { | |
// Renomeia o arquivo .html para index.html | |
fs.rename(`./${projectName}/${file}`, `./${projectName}/index.html`, err => { | |
if (err) throw err; | |
console.log('Rename complete!'); | |
}); | |
break; | |
} | |
} | |
optimizeImages(`./${projectName}/images`, 90); | |
replaceImagePath(projectName); | |
rsyncToServer(projectName, 'user', 'domain'); | |
}); | |
// Fecha a interface readline | |
rl.close(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "projectname", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"adm-zip": "^0.5.10", | |
"sharp": "^0.33.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment