melhorias em estáticos

This commit is contained in:
Luiz Silva 2024-05-07 08:10:35 -03:00
parent 942c1e7dfc
commit 5c7a3ee19a
7 changed files with 173 additions and 160 deletions

2
dist/index.js vendored
View file

@ -4,7 +4,7 @@ exports.estaticos = exports.Prefixos = void 0;
exports.Prefixos = { exports.Prefixos = {
relativo: "/estaticos", relativo: "/estaticos",
"link-local": "http://localhost:5020/estaticos", "link-local": "http://localhost:5020/estaticos",
"link-servidor": "https://carro-de-boi.idz.one/estaticos", "link-servidor": "https://carro-de-boi-idz-one.b-cdn.net/estaticos",
"relativo-disco": "~estaticos/estaticos", "relativo-disco": "~estaticos/estaticos",
}; };
/** /**

2
dist/index.js.map vendored
View file

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,QAAQ,GAAG;IACvB,QAAQ,EAAE,YAAY;IACtB,YAAY,EAAE,iCAAiC;IAC/C,eAAe,EAAE,wCAAwC;IACzD,gBAAgB,EAAE,sBAAsB;CACxC,CAAC;AAEF;;;;;;;;GAQG;AACI,MAAM,SAAS,GAAG,CAAC,OAA8B,EAAE,EAAE;IAC3D,MAAM,OAAO,GAAG,gBAAQ,CAAC,OAAO,CAAC,IAAI,gBAAQ,CAAC,QAAQ,CAAC;IAEvD,OAAO;QACN,KAAK,EAAE;YACN,gBAAgB,EAAE;gBACjB,kCAAkC,EAAE,GAAG,OAAO,wDAAwD;gBACtG,+BAA+B,EAAE,GAAG,OAAO,qDAAqD;gBAChG,oCAAoC,EAAE,GAAG,OAAO,0DAA0D;gBAC1G,oCAAoC,EAAE,GAAG,OAAO,0DAA0D;gBAC1G,2CAA2C,EAAE,GAAG,OAAO,iEAAiE;gBACxH,2CAA2C,EAAE,GAAG,OAAO,iEAAiE;aACxH;YACD,qBAAqB,EAAE;gBACtB,uCAAuC,EAAE,GAAG,OAAO,kEAAkE;gBACrH,uCAAuC,EAAE,GAAG,OAAO,kEAAkE;gBACrH,yCAAyC,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E;aACxH;YACD,qBAAqB,EAAE;gBACtB,uCAAuC,EAAE,GAAG,OAAO,kEAAkE;gBACrH,uCAAuC,EAAE,GAAG,OAAO,kEAAkE;gBACrH,yCAAyC,EAAE,GAAG,OAAO,oEAAoE;gBACzH,yCAAyC,EAAE,GAAG,OAAO,oEAAoE;gBACzH,gDAAgD,EAAE,GAAG,OAAO,2EAA2E;gBACvI,gDAAgD,EAAE,GAAG,OAAO,2EAA2E;gBACvI,qCAAqC,EAAE,GAAG,OAAO,gEAAgE;gBACjH,qCAAqC,EAAE,GAAG,OAAO,gEAAgE;aACjH;SACD;QACD,OAAO,EAAE;YACR,iBAAiB,EAAE,GAAG,OAAO,0BAA0B;YACvD,qBAAqB,EAAE,GAAG,OAAO,8BAA8B;SAC/D;QACD,IAAI,EAAE;YACL,UAAU,EAAE,GAAG,OAAO,gBAAgB;SACtC;QACD,KAAK,EAAE;YACN,mBAAmB,EAAE,GAAG,OAAO,0BAA0B;YACzD,iBAAiB,EAAE,GAAG,OAAO,wBAAwB;SACrD;KACD,CAAC;AACH,CAAC,CAAC;AApCW,QAAA,SAAS,aAoCpB"}

View file

@ -1,25 +1,23 @@
import { globSync } from "glob"; import { globSync } from "glob";
import path from "node:path"; import path from "node:path";
import fs from "node:fs"; import fs from "node:fs";
import { cdn_carro_de_boi } from "~comuns";
const pasta_estaticos = path.resolve(process.cwd(), "estaticos"); const pasta_estaticos = path.resolve(process.cwd(), "estaticos");
const pasta_src = path.resolve(process.cwd(), "src"); const pasta_src = path.resolve(process.cwd(), "src");
import { ambiente } from "./ambiente";
const { PREFIXO, PORTA } = ambiente;
const enderecoLocal = `http://127.0.0.1:${PORTA}${PREFIXO}`;
export const gerar = (async () => { export const gerar = (async () => {
// listar arquivos da pasta estáticos // listar arquivos da pasta estáticos
const files = globSync(`${pasta_estaticos}/**/*`, { nodir: true }); const files = globSync(`${pasta_estaticos}/**/*`, { nodir: true });
const arquivo_ts = ` const arquivo_ts = `
export const Prefixos = { export const Prefixos = {
relativo: "/estaticos", relativo: "/estaticos",
"link-local": "http://localhost:5020/estaticos", "link-local": "http://localhost:5020/estaticos",
"link-servidor": "https://carro-de-boi.idz.one/estaticos", "link-servidor": "${cdn_carro_de_boi}/estaticos",
"relativo-disco": "~estaticos/estaticos", "relativo-disco": "~estaticos/estaticos",
}; };
@ -36,7 +34,7 @@ export const gerar = (async () => {
const prefixo = Prefixos[caminho] || Prefixos.relativo; const prefixo = Prefixos[caminho] || Prefixos.relativo;
return ${(() => { return ${(() => {
/* /*
${files ${files
.map((f) => f.slice(pasta_estaticos.length)) .map((f) => f.slice(pasta_estaticos.length))
@ -45,43 +43,43 @@ export const gerar = (async () => {
*/ */
type tp = { type tp = {
[key: string]: string | tp; [key: string]: string | tp;
}; };
const arquivos = {} as tp; const arquivos = {} as tp;
for (const arquivo of files) { for (const arquivo of files) {
const partes = arquivo const partes = arquivo
.slice(pasta_estaticos.length) .slice(pasta_estaticos.length)
.split("/") .split("/")
.slice(1); .slice(1);
let pasta = arquivos; let pasta = arquivos;
for (const [i, parte] of partes.entries()) { for (const [i, parte] of partes.entries()) {
if (i === partes.length - 1) { if (i === partes.length - 1) {
pasta[parte] = `~~~\${prefixo}${encodeURI( pasta[parte] = `~~~\${prefixo}${encodeURI(
arquivo.slice(pasta_estaticos.length), arquivo.slice(pasta_estaticos.length),
)}~~~`; )}~~~`;
} else { } else {
pasta[parte] = pasta[parte] || {}; pasta[parte] = pasta[parte] || {};
pasta = pasta[parte] as tp; pasta = pasta[parte] as tp;
} }
} }
} }
return JSON.stringify(arquivos, null, 2) return JSON.stringify(arquivos, null, 2)
.replace(/"~~~/g, "`") .replace(/"~~~/g, "`")
.replace(/~~~"/g, "`"); .replace(/~~~"/g, "`");
})()}; })()};
}; };
`; `;
// escrever arquivo // escrever arquivo
fs.writeFileSync(path.resolve(pasta_src, "index.ts"), arquivo_ts); fs.writeFileSync(path.resolve(pasta_src, "index.ts"), arquivo_ts);
})(); })();
if (process.argv.includes("gerar")) { if (process.argv.includes("gerar")) {
gerar.then(() => { gerar.then(() => {
console.log("Arquivo gerado com sucesso"); console.log("Arquivo gerado com sucesso");
}); });
} }

View file

@ -13,95 +13,95 @@ const { PORTA, PREFIXO } = ambiente;
const _iframe = String(Math.random()); const _iframe = String(Math.random());
const criarHtml = (entrada: { const criarHtml = (entrada: {
[key: string]: string | {}; // biome-ignore lint/complexity/noBannedTypes: <explanation>
[key: string]: string | {};
}): React.JSX.Element[] => { }): React.JSX.Element[] => {
const retorno = [] as React.JSX.Element[]; const retorno = [] as React.JSX.Element[];
for (const [k, v] of Object.entries(entrada)) { for (const [k, v] of Object.entries(entrada)) {
if (typeof v === "string") { if (typeof v === "string") {
retorno.push( retorno.push(
<p key={Math.random()}> <p key={Math.random()}>
<a target={_iframe} href={`${v}?aleatório=${Math.random()}`}> <a target={_iframe} href={`${v}?aleatório=${Math.random()}`}>
{v} {v}
</a> </a>
</p>, </p>,
); );
} else { } else {
retorno.push( retorno.push(
<div <div
key={Math.random()} key={Math.random()}
style={{ style={{
margin: 10, margin: 10,
padding: 5, padding: 5,
borderColor: "black", borderColor: "black",
borderWidth: "1px", borderWidth: "1px",
borderStyle: "solid", borderStyle: "solid",
}} }}
> >
<details open> <details open>
<summary>{k}</summary> <summary>{k}</summary>
{criarHtml(v)} {criarHtml(v)}
</details> </details>
</div>, </div>,
); );
} }
} }
return retorno; return retorno;
}; };
gerar.then(() => { gerar.then(() => {
const app = express(); const app = express();
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded()); app.use(express.urlencoded());
app.get([PREFIXO, `${PREFIXO}/`], (req, res) => { app.get([PREFIXO, `${PREFIXO}/`], (req, res) => {
const html = ( const html = (
<html lang="pt-BR"> <html lang="pt-BR">
<head> <head>
<title>Arquivos Estáticos</title> <title>Arquivos Estáticos</title>
</head>{" "} </head>{" "}
<body> <body>
<h4>Arquivos Estáticos</h4> <h4>Arquivos Estáticos</h4>
<table width={"100%"} > <table width={"100%"}>
<tr> <tr>
<td width={"50%"}> {criarHtml(estaticos("relativo"))} </td> <td width={"50%"}> {criarHtml(estaticos("relativo"))} </td>
<td width={"50%"}> <td width={"50%"}>
<iframe <iframe
title="Iframe" title="Iframe"
name={_iframe} name={_iframe}
height={600} height={600}
width={"100%"} width={"100%"}
/> />
</td> </td>
</tr> </tr>
</table> </table>
</body> </body>
</html> </html>
); );
const renderHtml = renderToStaticMarkup(html, {}); const renderHtml = renderToStaticMarkup(html, {});
res.setHeader("content-type", "text/html"); res.setHeader("content-type", "text/html");
res.send(renderHtml); res.send(renderHtml);
}); });
app.get(`${PREFIXO}/*`, (req, res) => { app.get(`${PREFIXO}/*`, (req, res) => {
const caminho_arquivo = path.join(process.cwd(), decodeURI(req.path)); const caminho_arquivo = path.join(process.cwd(), decodeURI(req.path));
if (fs.existsSync(caminho_arquivo)) { if (fs.existsSync(caminho_arquivo)) {
console.log(`200: ${req.url}`); console.log(`200: ${req.url}`);
return res.sendFile(caminho_arquivo); return res.sendFile(caminho_arquivo);
} else { }
console.log(`404: ${req.url}`); console.log(`404: ${req.url}`);
return res.status(404).send(`404: ${req.url}`); return res.status(404).send(`404: ${req.url}`);
} });
});
app.all("*", (req, res) => { app.all("*", (req, res) => {
res.redirect(PREFIXO); res.redirect(PREFIXO);
}); });
app.listen(PORTA, () => { app.listen(PORTA, () => {
console.log(`Servidor ${PREFIXO} Rodando em http://localhost:${PORTA}`); console.log(`Servidor ${PREFIXO} Rodando em http://localhost:${PORTA}`);
}); });
}); });

View file

@ -1,42 +1,43 @@
{ {
"name": "~estaticos", "name": "~estaticos",
"version": "0.4.0", "version": "0.5.0",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
"#1": "Executa o servidor de desenvolvimento e sem produção", "#1": "Executa o servidor de desenvolvimento e sem produção",
"dev": "tsx ferramentas/rodar_servidor.tsx", "dev": "tsx ferramentas/rodar_servidor.tsx",
"#2": "Gerar o pacote para ser imortado em outros projetos", "#2": "Gerar o pacote para ser imortado em outros projetos",
"build": "pnpm run gerar && npx @biomejs/biome check --apply ./src && tsc && npm --no-git-tag-version version minor", "build": "pnpm run gerar && npx @biomejs/biome check --apply ./src && tsc && npm --no-git-tag-version version minor",
"#3": "Faz uma checagem de versão do node", "#3": "Faz uma checagem de versão do node",
"nodev": "check-node-version --node '>= 20'", "nodev": "check-node-version --node '>= 20'",
"#4": "Mata o processo que está rodando na porta 5020", "#4": "Mata o processo que está rodando na porta 5020",
"kill": "npx kill-port -p 5020", "kill": "npx kill-port -p 5020",
"#5": "Gerar a listagem de arquivos estaticos", "#5": "Gerar a listagem de arquivos estaticos",
"gerar": "dotenv -e .env tsx ferramentas/listar_arquivos.ts gerar", "gerar": "dotenv -e .env tsx ferramentas/listar_arquivos.ts gerar",
"#6": "Faz a formatação do código", "#6": "Faz a formatação do código",
"biome": "npx @biomejs/biome check --apply ./src && npx @biomejs/biome format --write ./src" "biome": "npx @biomejs/biome check --apply ./src && npx @biomejs/biome format --write ./src"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
"zod": "^3.22.4" "zod": "^3.22.4"
}, },
"devDependencies": { "devDependencies": {
"@biomejs/biome": "^1.6.1", "@biomejs/biome": "^1.6.1",
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/node": "^20.11.30", "@types/node": "^20.11.30",
"@types/react": "^18.3.1", "@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0", "@types/react-dom": "^18.3.0",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"dotenv-cli": "^7.4.1", "dotenv-cli": "^7.4.1",
"express": "^4.19.1", "express": "^4.19.1",
"glob": "^10.3.10", "glob": "^10.3.10",
"kill-port-process": "^3.2.1" "~comuns": "git+http://leitura:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTQ3NTA1NzYsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjgsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MzJ9fQ.OYdExOVQm5UI3wfeTaWjmD0o65Y1hrjFz5EvMB1a__U@git.idz.one:3000/git/multi-modulos-ambientais/_comuns.git",
}, "kill-port-process": "^3.2.1"
"peerDependencies": { },
"typescript": "^5.0.0" "peerDependencies": {
} "typescript": "^5.0.0"
}
} }

14
pnpm-lock.yaml generated
View file

@ -51,6 +51,9 @@ importers:
kill-port-process: kill-port-process:
specifier: ^3.2.1 specifier: ^3.2.1
version: 3.2.1 version: 3.2.1
~comuns:
specifier: git+http://leitura:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTQ3NTA1NzYsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjgsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MzJ9fQ.OYdExOVQm5UI3wfeTaWjmD0o65Y1hrjFz5EvMB1a__U@git.idz.one:3000/git/multi-modulos-ambientais/_comuns.git
version: git+http://leitura:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTQ3NTA1NzYsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjgsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MzJ9fQ.OYdExOVQm5UI3wfeTaWjmD0o65Y1hrjFz5EvMB1a__U@git.idz.one:3000/git/multi-modulos-ambientais/_comuns.git#cd6d9c35a22f0ef41196b0c0701acb085cf9d618(typescript@5.4.3)
packages: packages:
@ -643,6 +646,12 @@ packages:
zod@3.22.4: zod@3.22.4:
resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==} resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==}
~comuns@git+http://leitura:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTQ3NTA1NzYsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjgsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MzJ9fQ.OYdExOVQm5UI3wfeTaWjmD0o65Y1hrjFz5EvMB1a__U@git.idz.one:3000/git/multi-modulos-ambientais/_comuns.git#cd6d9c35a22f0ef41196b0c0701acb085cf9d618:
resolution: {commit: cd6d9c35a22f0ef41196b0c0701acb085cf9d618, repo: http://leitura:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTQ3NTA1NzYsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjgsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MzJ9fQ.OYdExOVQm5UI3wfeTaWjmD0o65Y1hrjFz5EvMB1a__U@git.idz.one:3000/git/multi-modulos-ambientais/_comuns.git, type: git}
version: 0.3.0
peerDependencies:
typescript: ^5.0.0
snapshots: snapshots:
'@biomejs/biome@1.6.3': '@biomejs/biome@1.6.3':
@ -1247,3 +1256,8 @@ snapshots:
yallist@2.1.2: {} yallist@2.1.2: {}
zod@3.22.4: {} zod@3.22.4: {}
~comuns@git+http://leitura:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MTQ3NTA1NzYsImlzcyI6IkdpdG5lc3MiLCJwaWQiOjgsInRrbiI6eyJ0eXAiOiJwYXQiLCJpZCI6MzJ9fQ.OYdExOVQm5UI3wfeTaWjmD0o65Y1hrjFz5EvMB1a__U@git.idz.one:3000/git/multi-modulos-ambientais/_comuns.git#cd6d9c35a22f0ef41196b0c0701acb085cf9d618(typescript@5.4.3):
dependencies:
typescript: 5.4.3
zod: 3.22.4

View file

@ -1,7 +1,7 @@
export const Prefixos = { export const Prefixos = {
relativo: "/estaticos", relativo: "/estaticos",
"link-local": "http://localhost:5020/estaticos", "link-local": "http://localhost:5020/estaticos",
"link-servidor": "https://carro-de-boi.idz.one/estaticos", "link-servidor": "https://carro-de-boi-idz-one.b-cdn.net/estaticos",
"relativo-disco": "~estaticos/estaticos", "relativo-disco": "~estaticos/estaticos",
}; };