text-to-map 1.0.8 → 1.1.0

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.
@@ -7,16 +7,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import fetch from "node-fetch";
11
- import { createWriteStream, createReadStream, rmSync, readdirSync, existsSync, } from "fs";
12
- import { pipeline } from "stream/promises";
13
- import { join } from "path";
14
10
  import AdmZip from "adm-zip";
15
11
  import { parse } from "csv-parse";
12
+ import { createReadStream, createWriteStream, readdirSync, rmSync } from "fs";
16
13
  import iconv from "iconv-lite";
17
- import { prepareOptions, initDb, } from "../utils/helpers";
18
- import { commitAddressPoints, importParsedLine } from "../db/address-points";
14
+ import fetch from "node-fetch";
15
+ import { join } from "path";
16
+ import { pipeline } from "stream/promises";
17
+ import { commitAddressPoints } from "../db/address-points";
18
+ import { disconnectKnex } from "../db/db";
19
19
  import { getLatestUrlFromAtomFeed } from "../utils/atom";
20
+ import { prepareOptions, } from "../utils/helpers";
21
+ const maxBufferSize = 1000;
20
22
  const downloadAndUnzip = (url, options) => __awaiter(void 0, void 0, void 0, function* () {
21
23
  const zipFilePath = join(options.tmpDir, options.addressPointsZipFileName);
22
24
  console.log("Downloading a large ZIP file with RUIAN data (~65 MB)...");
@@ -39,31 +41,39 @@ const importDataToDb = (options) => __awaiter(void 0, void 0, void 0, function*
39
41
  let total = 0;
40
42
  let next = 0;
41
43
  console.log("Initiating import of RUIAN data to search DB (~3 million rows to be imported).");
42
- initDb(options);
44
+ const buffer = [];
43
45
  for (const file of files) {
44
- const parseStream = parse({ delimiter: ";", fromLine: 2 }).on("data", (data) => {
45
- total += importParsedLine(data);
46
- if (total - next >= 100000) {
47
- next += 100000;
48
- console.log(`Total imported rows: ${next}`);
46
+ const parseStream = parse({ delimiter: ";", fromLine: 2 }).on("data", (data) => __awaiter(void 0, void 0, void 0, function* () {
47
+ buffer.push(data);
48
+ if (buffer.length >= maxBufferSize) {
49
+ parseStream.pause();
50
+ total += yield commitAddressPoints(buffer);
51
+ if (total - next >= 100000) {
52
+ next += 100000;
53
+ console.log(`Total imported rows: ${next}`);
54
+ }
55
+ buffer.length = 0;
56
+ parseStream.resume();
49
57
  }
50
- });
58
+ }));
51
59
  yield pipeline(createReadStream(join(extractionFolder, file)), iconv.decodeStream("win1250"), parseStream);
52
- total += commitAddressPoints();
53
60
  }
61
+ total += yield commitAddressPoints(buffer);
54
62
  console.log(`Import completed. Total imported rows: ${total}`);
55
63
  });
56
64
  const getExtractionFolder = (options) => join(options.tmpDir, options.addressPointsCsvFolderName);
57
65
  export const downloadAndImportAddressPoints = (options = {}) => __awaiter(void 0, void 0, void 0, function* () {
58
- const completeOptions = prepareOptions(options);
59
- const datasetFeedLink = yield getLatestUrlFromAtomFeed(completeOptions.addressPointsAtomUrl);
60
- const zipUrl = yield getLatestUrlFromAtomFeed(datasetFeedLink);
61
- yield downloadAndUnzip(zipUrl, completeOptions);
62
- yield importDataToDb(completeOptions);
63
- });
64
- export const deleteDb = (options = {}) => {
65
- const completeOptions = prepareOptions(options);
66
- if (existsSync(completeOptions.dbFilePath)) {
67
- rmSync(completeOptions.dbFilePath);
66
+ try {
67
+ const completeOptions = prepareOptions(options);
68
+ const datasetFeedLink = yield getLatestUrlFromAtomFeed(completeOptions.addressPointsAtomUrl);
69
+ const zipUrl = yield getLatestUrlFromAtomFeed(datasetFeedLink);
70
+ // await downloadAndUnzip(zipUrl, completeOptions);
71
+ yield importDataToDb(completeOptions);
68
72
  }
69
- };
73
+ catch (error) {
74
+ console.error(error);
75
+ }
76
+ finally {
77
+ yield disconnectKnex();
78
+ }
79
+ });
@@ -11,8 +11,9 @@ import { parse } from "csv-parse";
11
11
  import iconv from "iconv-lite";
12
12
  import fetch from "node-fetch";
13
13
  import { pipeline } from "stream/promises";
14
- import { initDb, prepareOptions, } from "../utils/helpers";
15
14
  import { insertRegionsAndOrps } from "../db/regions";
15
+ import { prepareOptions } from "../utils/helpers";
16
+ import { disconnectKnex } from "../db/db";
16
17
  const downloadAndImportDataToDb = (options) => __awaiter(void 0, void 0, void 0, function* () {
17
18
  console.log("Downloading regions and ORP data...");
18
19
  const response = yield fetch(options.regionsCsvUrl);
@@ -32,11 +33,18 @@ const downloadAndImportDataToDb = (options) => __awaiter(void 0, void 0, void 0,
32
33
  // @ts-ignore
33
34
  const schema = yield schemaResponse.json();
34
35
  console.log("Parsing completed. Starting to import data to DB...");
35
- initDb(options);
36
- insertRegionsAndOrps(rows, schema);
36
+ yield insertRegionsAndOrps(rows, schema);
37
37
  console.log(`Import completed. Total imported rows: ${rows.length}`);
38
38
  });
39
39
  export const downloadAndImportRegions = (options = {}) => __awaiter(void 0, void 0, void 0, function* () {
40
- const completeOptions = prepareOptions(options);
41
- yield downloadAndImportDataToDb(completeOptions);
40
+ try {
41
+ const completeOptions = prepareOptions(options);
42
+ yield downloadAndImportDataToDb(completeOptions);
43
+ }
44
+ catch (error) {
45
+ console.log(error);
46
+ }
47
+ finally {
48
+ yield disconnectKnex();
49
+ }
42
50
  });
@@ -8,15 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { createReadStream, createWriteStream, existsSync, rmSync } from "fs";
11
+ import fetch from "node-fetch";
11
12
  import { join } from "path";
12
13
  import sax from "sax";
13
- import fetch from "node-fetch";
14
- import { setDbConfig } from "../db/db";
15
- import { prepareOptions, } from "../utils/helpers";
16
14
  import { pipeline } from "stream/promises";
17
- import { MunicipalityType } from "../db/types";
18
- import { insertSchools } from "../db/schools";
15
+ import { disconnectKnex } from "../db/db";
19
16
  import { insertFounders } from "../db/founders";
17
+ import { insertSchools } from "../db/schools";
18
+ import { MunicipalityType } from "../db/types";
19
+ import { prepareOptions, } from "../utils/helpers";
20
20
  const downloadXml = (options) => __awaiter(void 0, void 0, void 0, function* () {
21
21
  if (existsSync(getXmlFilePath(options))) {
22
22
  return;
@@ -293,21 +293,25 @@ const importDataToDb = (options, saveFoundersToCsv = false, saveSchoolsWithoutRu
293
293
  });
294
294
  csv.end();
295
295
  }
296
- setDbConfig({
297
- filePath: options.dbFilePath,
298
- initFilePath: options.dbInitFilePath,
299
- });
300
- insertSchools(schools);
301
- insertFounders(Array.from(founders.values()));
296
+ yield insertSchools(schools);
297
+ yield insertFounders(Array.from(founders.values()));
302
298
  });
303
299
  const getXmlFilePath = (options) => {
304
300
  return join(options.tmpDir, options.schoolsXmlFileName);
305
301
  };
306
302
  export const downloadAndImportSchools = (options, saveFoundersToCsv = false, saveSchoolsWithoutRuianToCsv = false) => __awaiter(void 0, void 0, void 0, function* () {
307
- const runOptions = prepareOptions(options);
308
- yield downloadXml(runOptions);
309
- yield importDataToDb(runOptions, saveFoundersToCsv, saveSchoolsWithoutRuianToCsv);
310
- deleteSchoolsXmlFile(runOptions);
303
+ try {
304
+ const runOptions = prepareOptions(options);
305
+ yield downloadXml(runOptions);
306
+ yield importDataToDb(runOptions, saveFoundersToCsv, saveSchoolsWithoutRuianToCsv);
307
+ deleteSchoolsXmlFile(runOptions);
308
+ }
309
+ catch (error) {
310
+ console.error(error);
311
+ }
312
+ finally {
313
+ yield disconnectKnex();
314
+ }
311
315
  });
312
316
  export const deleteSchoolsXmlFile = (options = {}) => {
313
317
  const runOptions = prepareOptions(options);
@@ -7,15 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import fetch from "node-fetch";
11
- import { createWriteStream, rmSync, existsSync, mkdirSync } from "fs";
12
- import { pipeline } from "stream/promises";
13
- import { join } from "path";
14
10
  import AdmZip from "adm-zip";
11
+ import { createWriteStream, existsSync, mkdirSync, rmSync } from "fs";
12
+ import fetch from "node-fetch";
15
13
  import parseDBF from "parsedbf";
16
- import { prepareOptions, initDb, } from "../utils/helpers";
14
+ import { join } from "path";
15
+ import { pipeline } from "stream/promises";
17
16
  import { deleteStreets, getAllSyncedStreets, insertStreetsFromDbf, setStreetAsSynced, } from "../db/street-sync";
18
17
  import { getAllUrlsFromAtomFeed, getLatestUrlFromAtomFeed, } from "../utils/atom";
18
+ import { prepareOptions, } from "../utils/helpers";
19
+ import { disconnectKnex } from "../db/db";
20
+ import { areAddressPointsSynced } from "../db/address-points";
19
21
  const prepareFolders = (options) => {
20
22
  const tempFolder = getTempFolder(options);
21
23
  if (!existsSync(tempFolder)) {
@@ -43,36 +45,47 @@ const importDataToDb = (data) => __awaiter(void 0, void 0, void 0, function* ()
43
45
  if (data.length === 0) {
44
46
  return;
45
47
  }
46
- insertStreetsFromDbf(data);
48
+ yield insertStreetsFromDbf(data);
47
49
  });
48
50
  export const downloadAndImportStreets = (options) => __awaiter(void 0, void 0, void 0, function* () {
49
- console.log("Starting to download and import streets. This takes up to 1 hour.");
50
- const completeOptions = prepareOptions(options);
51
- initDb(completeOptions);
52
- prepareFolders(completeOptions);
53
- const allDatasetFeedLinks = yield getAllUrlsFromAtomFeed(completeOptions.streetsAtomUrl);
54
- console.log(`Total of ${allDatasetFeedLinks.length} links to ZIP files.`);
55
- const syncedStreetLinks = getAllSyncedStreets();
56
- const toDelete = [];
57
- // remove all deprecated links (not in the new list)
58
- syncedStreetLinks.forEach((link) => {
59
- if (!allDatasetFeedLinks.includes(link)) {
60
- toDelete.push(link);
61
- delete syncedStreetLinks[link];
51
+ try {
52
+ // first check if address points are already synced
53
+ if (!(yield areAddressPointsSynced())) {
54
+ throw new Error("Address points are not synced yet. First run 'npm run address-points' and then try again.");
62
55
  }
63
- });
64
- deleteStreets(toDelete);
65
- let done = syncedStreetLinks.size;
66
- console.log(`Total of ${done} links to ZIP files already stored.`);
67
- // get all links not yet stored
68
- const newLinks = allDatasetFeedLinks.filter((link) => !syncedStreetLinks.has(link));
69
- console.log(`Loading ${newLinks.length} new links to ZIP files.`);
70
- for (const link of newLinks) {
71
- const zipLink = yield getLatestUrlFromAtomFeed(link);
72
- const dbfObject = yield downloadZipAndParseDbfFile(zipLink, done, completeOptions);
73
- yield importDataToDb(dbfObject);
74
- setStreetAsSynced(link);
75
- done++;
76
- console.log(`Loaded links: ${done}/${allDatasetFeedLinks.length}`);
56
+ console.log("Starting to download and import streets. This takes up to 1 hour.");
57
+ const completeOptions = prepareOptions(options);
58
+ prepareFolders(completeOptions);
59
+ const allDatasetFeedLinks = yield getAllUrlsFromAtomFeed(completeOptions.streetsAtomUrl);
60
+ console.log(`Total of ${allDatasetFeedLinks.length} links to ZIP files.`);
61
+ const syncedStreetLinks = yield getAllSyncedStreets();
62
+ const toDelete = [];
63
+ // remove all deprecated links (not in the new list)
64
+ syncedStreetLinks.forEach((link) => {
65
+ if (!allDatasetFeedLinks.includes(link)) {
66
+ toDelete.push(link);
67
+ delete syncedStreetLinks[link];
68
+ }
69
+ });
70
+ yield deleteStreets(toDelete);
71
+ let done = syncedStreetLinks.size;
72
+ console.log(`Total of ${done} links to ZIP files already stored.`);
73
+ // get all links not yet stored
74
+ const newLinks = allDatasetFeedLinks.filter((link) => !syncedStreetLinks.has(link));
75
+ console.log(`Loading ${newLinks.length} new links to ZIP files.`);
76
+ for (const link of newLinks) {
77
+ const zipLink = yield getLatestUrlFromAtomFeed(link);
78
+ const dbfObject = yield downloadZipAndParseDbfFile(zipLink, done, completeOptions);
79
+ yield importDataToDb(dbfObject);
80
+ yield setStreetAsSynced(link);
81
+ done++;
82
+ console.log(`Loaded links: ${done}/${allDatasetFeedLinks.length}`);
83
+ }
84
+ }
85
+ catch (error) {
86
+ console.log(error);
87
+ }
88
+ finally {
89
+ yield disconnectKnex();
77
90
  }
78
91
  });
@@ -1,5 +1,5 @@
1
1
  import { DbMunicipalityResult } from "./types";
2
2
  export declare const isMunicipalitySwitch: (line: string) => boolean;
3
- export declare const getSwitchMunicipality: (line: string) => DbMunicipalityResult;
3
+ export declare const getSwitchMunicipality: (line: string) => Promise<DbMunicipalityResult>;
4
4
  export declare const isWholeMunicipality: (line: string) => boolean;
5
- export declare const getWholeMunicipality: (line: string) => DbMunicipalityResult;
5
+ export declare const getWholeMunicipality: (line: string) => Promise<DbMunicipalityResult>;
@@ -1,3 +1,12 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { findMunicipalityByNameAndType } from "../db/founders";
2
11
  import { MunicipalityType } from "../db/types";
3
12
  const municipalitySwitchStartPattern = /^navíc ulice /;
@@ -7,7 +16,7 @@ const wholeMunicipalityPattern = /^území (?<type>městské části|městského
7
16
  export const isMunicipalitySwitch = (line) => {
8
17
  return municipalitySwitchStartPattern.test(line);
9
18
  };
10
- export const getSwitchMunicipality = (line) => {
19
+ export const getSwitchMunicipality = (line) => __awaiter(void 0, void 0, void 0, function* () {
11
20
  const match = municipalitySwitchPattern.exec(line);
12
21
  if (match === null) {
13
22
  return {
@@ -22,12 +31,12 @@ export const getSwitchMunicipality = (line) => {
22
31
  };
23
32
  }
24
33
  const { type, name } = match.groups;
25
- return getMunicipalityResult(type, name, line);
26
- };
34
+ return yield getMunicipalityResult(type, name, line);
35
+ });
27
36
  export const isWholeMunicipality = (line) => {
28
37
  return wholeMunicipalityStartPattern.test(line);
29
38
  };
30
- export const getWholeMunicipality = (line) => {
39
+ export const getWholeMunicipality = (line) => __awaiter(void 0, void 0, void 0, function* () {
31
40
  const match = wholeMunicipalityPattern.exec(line);
32
41
  if (match === null) {
33
42
  return {
@@ -42,11 +51,11 @@ export const getWholeMunicipality = (line) => {
42
51
  };
43
52
  }
44
53
  const { type, name } = match.groups;
45
- return getMunicipalityResult(type, name, line);
46
- };
47
- const getMunicipalityResult = (type, name, line) => {
54
+ return yield getMunicipalityResult(type, name, line);
55
+ });
56
+ const getMunicipalityResult = (type, name, line) => __awaiter(void 0, void 0, void 0, function* () {
48
57
  const typeValue = getMunicipalityType(type);
49
- const { municipality, errors } = findMunicipalityByNameAndType(name, typeValue);
58
+ const { municipality, errors } = yield findMunicipalityByNameAndType(name, typeValue);
50
59
  const startOffset = line.indexOf(name);
51
60
  const endOffset = startOffset + name.length + 1;
52
61
  return {
@@ -54,7 +63,7 @@ const getMunicipalityResult = (type, name, line) => {
54
63
  errors: errors.map((error) => (Object.assign(Object.assign({}, error), { startOffset,
55
64
  endOffset }))),
56
65
  };
57
- };
66
+ });
58
67
  const getMunicipalityType = (type) => {
59
68
  switch (type) {
60
69
  case "městské části":
@@ -25,11 +25,14 @@ export const parseLine = (text, showDebug = false) => {
25
25
  smdParser.input = lexingResult.tokens;
26
26
  const smdLines = smdParser.street();
27
27
  if (smdParser.errors.length > 0) {
28
- errors.push(...smdParser.errors.map((error) => ({
29
- message: `Nesprávný zápis pravidla - nevynechali jste něco?`,
30
- startOffset: error.token.startOffset,
31
- endOffset: error.token.endOffset,
32
- })));
28
+ errors.push(...smdParser.errors.map((error) => {
29
+ var _a;
30
+ return ({
31
+ message: `Nesprávný zápis pravidla - nevynechali jste něco?`,
32
+ startOffset: error.token.startOffset,
33
+ endOffset: (_a = error.token.endOffset) !== null && _a !== void 0 ? _a : text.length,
34
+ });
35
+ }));
33
36
  if (showDebug) {
34
37
  printLexingResultInfo(lexingResult);
35
38
  }
@@ -1,11 +1,10 @@
1
1
  import { Founder } from "../db/types";
2
- import { OpenDataSyncOptionsPartial } from "../utils/helpers";
3
2
  import { ErrorCallbackParams, Municipality, MunicipalityWithFounder, ProcessLineCallbackParams, School, SmdError, SmdState } from "./types";
4
- export declare const parseOrdinanceToAddressPoints: (lines: string[], options?: OpenDataSyncOptionsPartial, initialState?: Partial<SmdState>, onError?: (params: ErrorCallbackParams) => void, onWarning?: (params: ErrorCallbackParams) => void, onProcessedLine?: (params: ProcessLineCallbackParams) => void) => Municipality[];
3
+ export declare const parseOrdinanceToAddressPoints: (lines: string[], initialState?: Partial<SmdState>, onError?: (params: ErrorCallbackParams) => void, onWarning?: (params: ErrorCallbackParams) => void, onProcessedLine?: (params: ProcessLineCallbackParams) => void) => Promise<Municipality[]>;
5
4
  export declare const convertMunicipality: (municipality: MunicipalityWithFounder) => Municipality;
6
- export declare const getNewMunicipality: (name: string, options?: OpenDataSyncOptionsPartial) => {
5
+ export declare const getNewMunicipality: (name: string) => Promise<{
7
6
  municipality: MunicipalityWithFounder;
8
7
  errors: SmdError[];
9
- };
10
- export declare const getNewSchool: (name: string, founder: Founder | null, lineNumber: number, onError: (params: ErrorCallbackParams) => void, options?: OpenDataSyncOptionsPartial) => School;
8
+ }>;
9
+ export declare const getNewSchool: (name: string, founder: Founder | null, lineNumber: number, onError: (params: ErrorCallbackParams) => void) => Promise<School>;
11
10
  export declare const wholeLineError: (message: string, line: string) => SmdError;
@@ -1,43 +1,54 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { checkStreetExists, findAddressPoints, getAddressPointById, } from "../db/address-points";
2
- import { setDbConfig } from "../db/db";
11
+ import { disconnectKnex } from "../db/db";
3
12
  import { findFounder } from "../db/founders";
4
13
  import { findSchool } from "../db/schools";
5
14
  import { founderToMunicipality } from "../db/types";
6
- import { prepareOptions } from "../utils/helpers";
7
15
  import { getSwitchMunicipality, getWholeMunicipality, isMunicipalitySwitch, isWholeMunicipality, } from "./municipality";
8
16
  import { parseLine } from "./smd-line-parser";
9
17
  import { isAddressPoint, } from "./types";
10
- export const parseOrdinanceToAddressPoints = (lines, options = {}, initialState = {}, onError = () => { }, onWarning = () => { }, onProcessedLine = () => { }) => {
11
- const readyOptions = prepareOptions(options);
12
- setDbConfig({
13
- filePath: readyOptions.dbFilePath,
14
- initFilePath: readyOptions.dbInitFilePath,
15
- });
16
- const state = Object.assign({ currentMunicipality: null, currentFilterMunicipality: null, currentSchool: null, municipalities: [] }, initialState);
17
- let lineNumber = 1;
18
- for (const rawLine of lines) {
19
- const line = cleanLine(rawLine);
20
- processOneLine({
21
- line,
22
- state,
23
- lineNumber,
24
- onError,
25
- onWarning,
26
- });
27
- onProcessedLine({ lineNumber, line });
28
- lineNumber++;
29
- }
30
- if (state.currentSchool != null) {
31
- if (state.currentMunicipality == null) {
32
- return [];
18
+ export const parseOrdinanceToAddressPoints = (lines, initialState = {}, onError = () => { }, onWarning = () => { }, onProcessedLine = () => { }) => __awaiter(void 0, void 0, void 0, function* () {
19
+ try {
20
+ const state = Object.assign({ currentMunicipality: null, currentFilterMunicipality: null, currentSchool: null, municipalities: [] }, initialState);
21
+ let lineNumber = 1;
22
+ for (const rawLine of lines) {
23
+ const line = cleanLine(rawLine);
24
+ yield processOneLine({
25
+ line,
26
+ state,
27
+ lineNumber,
28
+ onError,
29
+ onWarning,
30
+ });
31
+ onProcessedLine({ lineNumber, line });
32
+ lineNumber++;
33
+ }
34
+ if (state.currentSchool != null) {
35
+ if (state.currentMunicipality == null) {
36
+ return [];
37
+ }
38
+ state.currentMunicipality.schools.push(mapSchoolForExport(state.currentSchool));
39
+ }
40
+ if (state.currentMunicipality != null) {
41
+ state.municipalities.push(convertMunicipality(state.currentMunicipality));
33
42
  }
34
- state.currentMunicipality.schools.push(mapSchoolForExport(state.currentSchool));
43
+ return state.municipalities;
35
44
  }
36
- if (state.currentMunicipality != null) {
37
- state.municipalities.push(convertMunicipality(state.currentMunicipality));
45
+ catch (error) {
46
+ console.error(error);
38
47
  }
39
- return state.municipalities;
40
- };
48
+ finally {
49
+ yield disconnectKnex();
50
+ }
51
+ });
41
52
  const filterOutSchoolAddressPoint = (addressPoints, school) => {
42
53
  const schoolPosition = school.position;
43
54
  return schoolPosition && isAddressPoint(schoolPosition)
@@ -50,10 +61,10 @@ export const convertMunicipality = (municipality) => {
50
61
  schools: municipality.schools,
51
62
  };
52
63
  };
53
- const processOneLine = (params) => {
64
+ const processOneLine = (params) => __awaiter(void 0, void 0, void 0, function* () {
54
65
  const { line, state } = params;
55
66
  if (line[0] === "#") {
56
- processMunicipalityLine(params);
67
+ yield processMunicipalityLine(params);
57
68
  return;
58
69
  }
59
70
  if (line === "") {
@@ -65,20 +76,20 @@ const processOneLine = (params) => {
65
76
  return;
66
77
  }
67
78
  if (state.currentSchool === null) {
68
- processSchoolLine(params);
79
+ yield processSchoolLine(params);
69
80
  return;
70
81
  }
71
82
  if (isMunicipalitySwitch(line)) {
72
83
  processMunicipalitySwitchLine(params);
73
84
  }
74
85
  else if (isWholeMunicipality(line)) {
75
- processWholeMunicipalityLine(params);
86
+ yield processWholeMunicipalityLine(params);
76
87
  }
77
88
  else {
78
- processAddressPointLine(params);
89
+ yield processAddressPointLine(params);
79
90
  }
80
- };
81
- const processMunicipalityLine = ({ line, state, onError }) => {
91
+ });
92
+ const processMunicipalityLine = ({ line, state, onError, }) => __awaiter(void 0, void 0, void 0, function* () {
82
93
  if (state.currentSchool !== null) {
83
94
  state.currentMunicipality.schools.push(state.currentSchool);
84
95
  state.currentSchool = null;
@@ -86,18 +97,18 @@ const processMunicipalityLine = ({ line, state, onError }) => {
86
97
  if (state.currentMunicipality !== null) {
87
98
  state.municipalities.push(convertMunicipality(state.currentMunicipality));
88
99
  }
89
- const { municipality, errors } = getNewMunicipality(line);
100
+ const { municipality, errors } = yield getNewMunicipality(line);
90
101
  state.currentMunicipality = municipality;
91
102
  state.currentFilterMunicipality = founderToMunicipality(state.currentMunicipality.founder);
92
103
  state.currentSchool = null;
93
- };
104
+ });
94
105
  const processEmptyLine = ({ state }) => {
95
106
  if (state.currentSchool !== null) {
96
107
  state.currentMunicipality.schools.push(state.currentSchool);
97
108
  state.currentSchool = null;
98
109
  }
99
110
  };
100
- const processSchoolLine = ({ line, lineNumber, state, onError, }) => {
111
+ const processSchoolLine = ({ line, lineNumber, state, onError, }) => __awaiter(void 0, void 0, void 0, function* () {
101
112
  if (state.currentMunicipality === null) {
102
113
  onError({
103
114
  lineNumber,
@@ -108,58 +119,48 @@ const processSchoolLine = ({ line, lineNumber, state, onError, }) => {
108
119
  });
109
120
  return;
110
121
  }
111
- state.currentSchool = getNewSchool(line, state.currentMunicipality.founder, lineNumber, onError);
122
+ state.currentSchool = yield getNewSchool(line, state.currentMunicipality.founder, lineNumber, onError);
112
123
  state.currentFilterMunicipality = founderToMunicipality(state.currentMunicipality.founder);
113
- };
114
- const processMunicipalitySwitchLine = ({ line, state, lineNumber, onError, }) => {
115
- const { municipality, errors } = getSwitchMunicipality(line);
124
+ });
125
+ const processMunicipalitySwitchLine = ({ line, state, lineNumber, onError, }) => __awaiter(void 0, void 0, void 0, function* () {
126
+ const { municipality, errors } = yield getSwitchMunicipality(line);
116
127
  if (errors.length > 0) {
117
128
  onError({ lineNumber, line, errors });
118
129
  }
119
130
  else {
120
131
  state.currentFilterMunicipality = municipality;
121
132
  }
122
- };
123
- const processWholeMunicipalityLine = ({ line, state, lineNumber, onError, }) => {
124
- const { municipality, errors } = getWholeMunicipality(line);
133
+ });
134
+ const processWholeMunicipalityLine = ({ line, state, lineNumber, onError, }) => __awaiter(void 0, void 0, void 0, function* () {
135
+ const { municipality, errors } = yield getWholeMunicipality(line);
125
136
  if (errors.length > 0) {
126
137
  onError({ lineNumber, line, errors });
127
138
  }
128
139
  else {
129
- const addressPoints = findAddressPoints({ wholeMunicipality: true, street: "", numberSpec: [] }, municipality);
140
+ const addressPoints = yield findAddressPoints({ wholeMunicipality: true, street: "", numberSpec: [] }, municipality);
130
141
  state.currentSchool.addresses.push(...filterOutSchoolAddressPoint(addressPoints, state.currentSchool).map(mapAddressPointForExport));
131
142
  }
132
- };
133
- const processAddressPointLine = ({ line, state, lineNumber, onError, onWarning, }) => {
143
+ });
144
+ const processAddressPointLine = ({ line, state, lineNumber, onError, onWarning, }) => __awaiter(void 0, void 0, void 0, function* () {
134
145
  const { smdLines, errors } = parseLine(line);
135
146
  if (errors.length > 0) {
136
147
  onError({ lineNumber, line, errors });
137
148
  }
138
149
  else {
139
- smdLines.forEach((smdLine) => {
140
- const { exists, errors } = checkStreetExists(smdLine.street, state.currentMunicipality.founder);
150
+ for (const smdLine of smdLines) {
151
+ const { exists, errors } = yield checkStreetExists(smdLine.street, state.currentMunicipality.founder);
141
152
  if (errors.length > 0) {
142
153
  onWarning({ lineNumber, line, errors });
143
154
  }
144
155
  if (exists) {
145
- let addressPoints = findAddressPoints(smdLine, state.currentFilterMunicipality);
156
+ let addressPoints = yield findAddressPoints(smdLine, state.currentFilterMunicipality);
146
157
  state.currentSchool.addresses.push(...filterOutSchoolAddressPoint(addressPoints, state.currentSchool).map(mapAddressPointForExport));
147
158
  }
148
- });
149
- }
150
- };
151
- export const getNewMunicipality = (name, options) => {
152
- if (options) {
153
- options = prepareOptions(options);
154
- setDbConfig({
155
- filePath: options.dbFilePath,
156
- initFilePath: options.dbInitFilePath,
157
- });
158
- }
159
- const { founder, errors } = findFounder(name);
160
- if (errors.length > 0) {
161
- errors.forEach(console.error);
159
+ }
162
160
  }
161
+ });
162
+ export const getNewMunicipality = (name) => __awaiter(void 0, void 0, void 0, function* () {
163
+ const { founder, errors } = yield findFounder(name);
163
164
  return {
164
165
  municipality: {
165
166
  municipalityName: founder ? founder.name : "Neznámá obec",
@@ -168,15 +169,8 @@ export const getNewMunicipality = (name, options) => {
168
169
  },
169
170
  errors,
170
171
  };
171
- };
172
- export const getNewSchool = (name, founder, lineNumber, onError, options) => {
173
- if (options) {
174
- options = prepareOptions(options);
175
- setDbConfig({
176
- filePath: options.dbFilePath,
177
- initFilePath: options.dbInitFilePath,
178
- });
179
- }
172
+ });
173
+ export const getNewSchool = (name, founder, lineNumber, onError) => __awaiter(void 0, void 0, void 0, function* () {
180
174
  let exportSchool = {
181
175
  name,
182
176
  izo: "",
@@ -191,7 +185,7 @@ export const getNewSchool = (name, founder, lineNumber, onError, options) => {
191
185
  exportSchool.name = school.name;
192
186
  exportSchool.izo = school.izo || "";
193
187
  if (school.locations.length > 0) {
194
- const position = getAddressPointById(school.locations[0].addressPointId);
188
+ const position = yield getAddressPointById(school.locations[0].addressPointId);
195
189
  if (position !== null) {
196
190
  exportSchool.position = position;
197
191
  }
@@ -199,7 +193,7 @@ export const getNewSchool = (name, founder, lineNumber, onError, options) => {
199
193
  }
200
194
  }
201
195
  return exportSchool;
202
- };
196
+ });
203
197
  const mapAddressPointForExport = (addressPoint) => {
204
198
  return {
205
199
  address: addressPoint.address,
@@ -19,7 +19,6 @@ export interface OpenDataSyncOptions {
19
19
  export type OpenDataSyncOptionsPartial = Partial<OpenDataSyncOptions>;
20
20
  export declare const getAppDataDirPath: () => string;
21
21
  export declare const prepareOptions: (options: OpenDataSyncOptionsPartial) => OpenDataSyncOptions;
22
- export declare const initDb: (options: OpenDataSyncOptions) => void;
23
22
  export declare const extractMunicipalityName: (founder: Founder) => string;
24
23
  export declare const sanitizeMunicipalityName: (name: string) => string;
25
24
  export declare const findClosestString: (str: string, arr: string[]) => string;