59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { globSync } from "glob";
|
|
const mapearPasta = ({
|
|
prefixos,
|
|
pasta,
|
|
arquivoDestino,
|
|
variavel
|
|
}) => {
|
|
const pasta_estaticos = pasta.endsWith("/") ? pasta : `${pasta}/`;
|
|
if (!fs.existsSync(pasta_estaticos))
|
|
throw new Error(`Pasta ${pasta_estaticos} n\xE3o existe`);
|
|
const _gerar = (async () => {
|
|
const files = globSync(`${pasta_estaticos}**/*`, { nodir: true });
|
|
const arquivo_ts = `
|
|
|
|
|
|
const Prefixos = {
|
|
${Object.entries(prefixos).map(([k, v]) => `"${k}": "${v.replace(/"/g, '\\"')}"`).join(",\n")}
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param caminho
|
|
* @returns String
|
|
*/
|
|
export const ${variavel || "estaticos"} = (caminho: keyof typeof Prefixos) => {
|
|
const prefixo = Prefixos[caminho];
|
|
|
|
return ${(() => {
|
|
const arquivos = {};
|
|
for (const arquivo of files) {
|
|
const partes = arquivo.slice(pasta_estaticos.length).split("/");
|
|
let pasta2 = arquivos;
|
|
for (const [i, parte] of partes.entries()) {
|
|
if (i === partes.length - 1) {
|
|
pasta2[parte] = `~~~\${prefixo}${encodeURI(
|
|
arquivo.slice(pasta_estaticos.length - 1)
|
|
)}~~~`;
|
|
} else {
|
|
pasta2[parte] = pasta2[parte] || {};
|
|
pasta2 = pasta2[parte];
|
|
}
|
|
}
|
|
}
|
|
return JSON.stringify(arquivos, null, 2).replace(/"~~~/g, "`").replace(/~~~"/g, "`");
|
|
})()};
|
|
};
|
|
`;
|
|
const pastaDestino = path.dirname(arquivoDestino);
|
|
if (!fs.existsSync(pastaDestino)) {
|
|
fs.mkdirSync(pastaDestino, { recursive: true });
|
|
}
|
|
fs.writeFileSync(arquivoDestino, arquivo_ts);
|
|
})();
|
|
};
|
|
export {
|
|
mapearPasta
|
|
};
|