-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdump-services.js
executable file
·54 lines (40 loc) · 1.57 KB
/
dump-services.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
const frete = require('../frete');
const path = require('path');
const fs = require('fs');
(async() => {
console.log('Getting services...');
// well, listaServicos returns duplicated services, lets remove them
const services = (await frete().listaServicos()).reduce((acc, curr) => {
if (acc.some((c) => c.codigo === curr.codigo)) { return acc; }
return acc.concat(curr);
}, []);
console.log(`Got a total of: ${services.length}`);
const filepath = path.resolve(
__dirname, '../correios-data', 'listaServicos.json'
);
await fs.promises.writeFile(filepath, JSON.stringify(services));
console.log(`Services saved to: ${filepath}`);
// *super-duper-hacky* {
const freteTypeScriptDefinitionsStr = (await fs.promises.readFile(
path.resolve(__dirname, '../frete.d.ts')
)).toString();
const tsDefinitionParts = freteTypeScriptDefinitionsStr
.split(' CodigosServicoMapName {');
const beforeDefinition = `${tsDefinitionParts[0]} CodigosServicoMapName {`;
const afterDefinition = tsDefinitionParts[1]
.split('}\n')
.slice(1)
.join('}\n');
const servicesContentDef = services
.map((s) => ` '${s.codigo}': '${s.descricao}';`)
.join('\n');
const freteTsDefinition = `${beforeDefinition}\n` +
`${servicesContentDef}\n }\n` +
afterDefinition;
await fs.promises.writeFile(
path.resolve(__dirname, '../frete.d.ts'),
freteTsDefinition);
console.log('Mapped services to ./frete.d.ts.');
// }
})();