91 lines
2.2 KiB
TypeScript
91 lines
2.2 KiB
TypeScript
// esse arquivo não pode ser importado em index.ts para não gerar conflito
|
|
|
|
import fs from "node:fs"
|
|
import path from "node:path"
|
|
import { globSync } from "glob"
|
|
|
|
export const mapearPasta = ({
|
|
prefixos,
|
|
pasta,
|
|
arquivoDestino,
|
|
variavel,
|
|
}: {
|
|
prefixos: { [k: string]: string }
|
|
pasta: string
|
|
arquivoDestino: string
|
|
variavel?: string
|
|
}) => {
|
|
const pasta_estaticos = pasta.endsWith("/") ? pasta : `${pasta}/`
|
|
if (!fs.existsSync(pasta_estaticos))
|
|
throw new Error(`Pasta ${pasta_estaticos} não existe`)
|
|
|
|
const _gerar = (async () => {
|
|
// listar arquivos da pasta estáticos
|
|
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 ${(() => {
|
|
/*
|
|
|
|
${files
|
|
.map((f) => f.slice(pasta_estaticos.length))
|
|
.map((f) => `"${f}": \`\${prefixo}${encodeURI(f)}\``)
|
|
.join(",\n")}
|
|
|
|
|
|
*/
|
|
type tp = {
|
|
[key: string]: string | tp
|
|
}
|
|
|
|
const arquivos = {} as tp
|
|
|
|
for (const arquivo of files) {
|
|
const partes = arquivo.slice(pasta_estaticos.length).split("/")
|
|
let pasta = arquivos
|
|
for (const [i, parte] of partes.entries()) {
|
|
if (i === partes.length - 1) {
|
|
pasta[parte] = `~~~\${prefixo}${encodeURI(
|
|
arquivo.slice(pasta_estaticos.length - 1),
|
|
)}~~~`
|
|
} else {
|
|
pasta[parte] = pasta[parte] || {}
|
|
pasta = pasta[parte] as tp
|
|
}
|
|
}
|
|
}
|
|
|
|
return JSON.stringify(arquivos, null, 2)
|
|
.replace(/"~~~/g, "`")
|
|
.replace(/~~~"/g, "`")
|
|
})()};
|
|
};
|
|
`
|
|
|
|
// criar pasta de arquivo destino
|
|
const pastaDestino = path.dirname(arquivoDestino)
|
|
if (!fs.existsSync(pastaDestino)) {
|
|
fs.mkdirSync(pastaDestino, { recursive: true })
|
|
}
|
|
|
|
// escrever arquivo
|
|
|
|
fs.writeFileSync(arquivoDestino, arquivo_ts)
|
|
})()
|
|
}
|