Adicionado função mapearPasta
This commit is contained in:
parent
aa36f4dfc8
commit
2dd74d88e0
10 changed files with 272 additions and 198 deletions
68
dist/mapear_pasta.js
vendored
Normal file
68
dist/mapear_pasta.js
vendored
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.mapearPasta = void 0;
|
||||
const node_fs_1 = __importDefault(require("node:fs"));
|
||||
const node_path_1 = __importDefault(require("node:path"));
|
||||
const glob_1 = require("glob");
|
||||
const mapearPasta = ({ prefixos, pasta, arquivoDestino, variavel, }) => {
|
||||
const pasta_estaticos = pasta.endsWith("/") ? pasta : `${pasta}/`;
|
||||
if (!node_fs_1.default.existsSync(pasta_estaticos))
|
||||
throw new Error(`Pasta ${pasta_estaticos} não existe`);
|
||||
const gerar = (async () => {
|
||||
// listar arquivos da pasta estáticos
|
||||
const files = (0, glob_1.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] || Prefixos.relativo;
|
||||
|
||||
return ${(() => {
|
||||
const arquivos = {};
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
return JSON.stringify(arquivos, null, 2)
|
||||
.replace(/"~~~/g, "`")
|
||||
.replace(/~~~"/g, "`");
|
||||
})()};
|
||||
};
|
||||
`;
|
||||
// criar pasta de arquivo destino
|
||||
const pastaDestino = node_path_1.default.dirname(arquivoDestino);
|
||||
if (!node_fs_1.default.existsSync(pastaDestino)) {
|
||||
node_fs_1.default.mkdirSync(pastaDestino, { recursive: true });
|
||||
}
|
||||
// escrever arquivo
|
||||
node_fs_1.default.writeFileSync(arquivoDestino, arquivo_ts);
|
||||
})();
|
||||
};
|
||||
exports.mapearPasta = mapearPasta;
|
||||
//# sourceMappingURL=mapear_pasta.js.map
|
||||
Loading…
Add table
Add a link
Reference in a new issue