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 = {
relativo: "/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",
};
/**

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,EAAE,GAAG,OAAO,oEAAoE;gBACzH,yCAAyC,EAAE,GAAG,OAAO,oEAAoE;gBACzH,g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,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 path from "node:path";
import fs from "node:fs";
import { cdn_carro_de_boi } from "~comuns";
const pasta_estaticos = path.resolve(process.cwd(), "estaticos");
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 () => {
// listar arquivos da pasta estáticos
const files = globSync(`${pasta_estaticos}/**/*`, { nodir: true });
// listar arquivos da pasta estáticos
const files = globSync(`${pasta_estaticos}/**/*`, { nodir: true });
const arquivo_ts = `
const arquivo_ts = `
export const Prefixos = {
relativo: "/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",
};
@ -36,7 +34,7 @@ export const gerar = (async () => {
const prefixo = Prefixos[caminho] || Prefixos.relativo;
return ${(() => {
/*
/*
${files
.map((f) => f.slice(pasta_estaticos.length))
@ -45,43 +43,43 @@ export const gerar = (async () => {
*/
type tp = {
[key: string]: string | tp;
};
type tp = {
[key: string]: string | tp;
};
const arquivos = {} as tp;
const arquivos = {} as tp;
for (const arquivo of files) {
const partes = arquivo
.slice(pasta_estaticos.length)
.split("/")
.slice(1);
let pasta = arquivos;
for (const [i, parte] of partes.entries()) {
if (i === partes.length - 1) {
pasta[parte] = `~~~\${prefixo}${encodeURI(
arquivo.slice(pasta_estaticos.length),
)}~~~`;
} else {
pasta[parte] = pasta[parte] || {};
pasta = pasta[parte] as tp;
}
}
}
for (const arquivo of files) {
const partes = arquivo
.slice(pasta_estaticos.length)
.split("/")
.slice(1);
let pasta = arquivos;
for (const [i, parte] of partes.entries()) {
if (i === partes.length - 1) {
pasta[parte] = `~~~\${prefixo}${encodeURI(
arquivo.slice(pasta_estaticos.length),
)}~~~`;
} else {
pasta[parte] = pasta[parte] || {};
pasta = pasta[parte] as tp;
}
}
}
return JSON.stringify(arquivos, null, 2)
.replace(/"~~~/g, "`")
.replace(/~~~"/g, "`");
})()};
return JSON.stringify(arquivos, null, 2)
.replace(/"~~~/g, "`")
.replace(/~~~"/g, "`");
})()};
};
`;
// escrever arquivo
fs.writeFileSync(path.resolve(pasta_src, "index.ts"), arquivo_ts);
// escrever arquivo
fs.writeFileSync(path.resolve(pasta_src, "index.ts"), arquivo_ts);
})();
if (process.argv.includes("gerar")) {
gerar.then(() => {
console.log("Arquivo gerado com sucesso");
});
gerar.then(() => {
console.log("Arquivo gerado com sucesso");
});
}

View file

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

View file

@ -1,42 +1,43 @@
{
"name": "~estaticos",
"version": "0.4.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"#1": "Executa o servidor de desenvolvimento e sem produção",
"dev": "tsx ferramentas/rodar_servidor.tsx",
"#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",
"#3": "Faz uma checagem de versão do node",
"nodev": "check-node-version --node '>= 20'",
"#4": "Mata o processo que está rodando na porta 5020",
"kill": "npx kill-port -p 5020",
"#5": "Gerar a listagem de arquivos estaticos",
"gerar": "dotenv -e .env tsx ferramentas/listar_arquivos.ts gerar",
"#6": "Faz a formatação do código",
"biome": "npx @biomejs/biome check --apply ./src && npx @biomejs/biome format --write ./src"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@biomejs/biome": "^1.6.1",
"@types/express": "^4.17.21",
"@types/node": "^20.11.30",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"dotenv": "^16.4.5",
"dotenv-cli": "^7.4.1",
"express": "^4.19.1",
"glob": "^10.3.10",
"kill-port-process": "^3.2.1"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
"name": "~estaticos",
"version": "0.5.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"#1": "Executa o servidor de desenvolvimento e sem produção",
"dev": "tsx ferramentas/rodar_servidor.tsx",
"#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",
"#3": "Faz uma checagem de versão do node",
"nodev": "check-node-version --node '>= 20'",
"#4": "Mata o processo que está rodando na porta 5020",
"kill": "npx kill-port -p 5020",
"#5": "Gerar a listagem de arquivos estaticos",
"gerar": "dotenv -e .env tsx ferramentas/listar_arquivos.ts gerar",
"#6": "Faz a formatação do código",
"biome": "npx @biomejs/biome check --apply ./src && npx @biomejs/biome format --write ./src"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@biomejs/biome": "^1.6.1",
"@types/express": "^4.17.21",
"@types/node": "^20.11.30",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"dotenv": "^16.4.5",
"dotenv-cli": "^7.4.1",
"express": "^4.19.1",
"glob": "^10.3.10",
"~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"
}
}

14
pnpm-lock.yaml generated
View file

@ -51,6 +51,9 @@ importers:
kill-port-process:
specifier: ^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:
@ -643,6 +646,12 @@ packages:
zod@3.22.4:
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:
'@biomejs/biome@1.6.3':
@ -1247,3 +1256,8 @@ snapshots:
yallist@2.1.2: {}
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 = {
relativo: "/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",
};