brasil-ceps-offline 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const https_1 = __importDefault(require("https"));
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const zlib_1 = __importDefault(require("zlib"));
|
|
10
|
+
// Substitua pelo link direto que você copiou do seu GitHub Releases
|
|
11
|
+
const URL_DO_BANCO = 'https://github.com/kaique-oliveira/lib-address-br/releases/download/v1.0.0/brasil-ceps.sqlite.gz';
|
|
12
|
+
// Subimos DOIS níveis (de dist/scripts -> dist -> raiz) para criar a pasta no lugar certo
|
|
13
|
+
const DB_DIR = path_1.default.join(__dirname, '..', '..', '.db');
|
|
14
|
+
// Ajustamos o nome para casar exatamente com o que a lib está procurando
|
|
15
|
+
const DB_PATH = path_1.default.join(DB_DIR, 'ceps.sqlite');
|
|
16
|
+
function downloadDatabase() {
|
|
17
|
+
if (!fs_1.default.existsSync(DB_DIR)) {
|
|
18
|
+
fs_1.default.mkdirSync(DB_DIR, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
// Se o banco já existe (ex: o dev tá só rodando npm install de novo), não baixa de novo
|
|
21
|
+
if (fs_1.default.existsSync(DB_PATH)) {
|
|
22
|
+
console.log('✅ Banco de CEPs já está instalado e pronto para uso.');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
console.log('📦 Baixando banco de dados de CEPs do Brasil offline (isso acontece só uma vez)...');
|
|
26
|
+
https_1.default
|
|
27
|
+
.get(URL_DO_BANCO, (response) => {
|
|
28
|
+
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
29
|
+
// O GitHub faz redirecionamento, precisamos seguir o link
|
|
30
|
+
https_1.default.get(response.headers.location, processDownload);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
processDownload(response);
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
.on('error', (err) => {
|
|
37
|
+
console.error('❌ Erro ao baixar o banco de CEPs:', err.message);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function processDownload(response) {
|
|
41
|
+
const unzip = zlib_1.default.createGunzip(); // Descompactador nativo do Node
|
|
42
|
+
const file = fs_1.default.createWriteStream(DB_PATH);
|
|
43
|
+
response.pipe(unzip).pipe(file);
|
|
44
|
+
file.on('finish', () => {
|
|
45
|
+
file.close();
|
|
46
|
+
console.log('🚀 Banco de dados baixado e descompactado com sucesso! Zero-latência ativada.');
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
downloadDatabase();
|
package/package.json
CHANGED