@tricoteuses/senat 1.1.2 → 1.3.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.
Files changed (155) hide show
  1. package/README.md +53 -15
  2. package/lib/config.d.ts +1 -0
  3. package/lib/config.js +14 -45
  4. package/lib/databases.d.ts +9 -0
  5. package/lib/databases.js +90 -117
  6. package/lib/datasets.js +78 -83
  7. package/lib/index.d.ts +8 -7
  8. package/lib/index.js +42 -523
  9. package/lib/legislatures.json +38 -0
  10. package/lib/loaders.d.ts +64 -0
  11. package/lib/loaders.js +177 -0
  12. package/lib/model/ameli.d.ts +105 -4
  13. package/lib/model/ameli.js +88 -166
  14. package/lib/model/debats.d.ts +0 -4
  15. package/lib/model/debats.js +0 -122
  16. package/lib/model/dosleg.d.ts +227 -29
  17. package/lib/model/dosleg.js +205 -839
  18. package/lib/model/index.d.ts +4 -5
  19. package/lib/model/index.js +12 -47
  20. package/lib/model/questions.d.ts +88 -2
  21. package/lib/model/questions.js +75 -51
  22. package/lib/model/sens.d.ts +524 -2
  23. package/lib/model/sens.js +449 -56
  24. package/lib/model/texte.d.ts +7 -0
  25. package/lib/model/texte.js +251 -0
  26. package/lib/model/util.d.ts +7 -1
  27. package/lib/model/util.js +26 -59
  28. package/lib/raw_types/ameli.d.ts +912 -538
  29. package/lib/raw_types/ameli.js +3 -42
  30. package/lib/raw_types/debats.d.ts +204 -124
  31. package/lib/raw_types/debats.js +3 -21
  32. package/lib/raw_types/dosleg.d.ts +3533 -2024
  33. package/lib/raw_types/dosleg.js +3 -95
  34. package/lib/raw_types/questions.d.ts +404 -220
  35. package/lib/raw_types/questions.js +3 -21
  36. package/lib/raw_types/sens.d.ts +4391 -2706
  37. package/lib/raw_types/sens.js +3 -111
  38. package/lib/raw_types_schemats/ameli.d.ts +541 -0
  39. package/lib/raw_types_schemats/ameli.js +3 -0
  40. package/lib/raw_types_schemats/debats.d.ts +127 -0
  41. package/lib/raw_types_schemats/debats.js +3 -0
  42. package/lib/raw_types_schemats/dosleg.d.ts +2029 -0
  43. package/lib/raw_types_schemats/dosleg.js +3 -0
  44. package/lib/raw_types_schemats/questions.d.ts +233 -0
  45. package/lib/raw_types_schemats/questions.js +3 -0
  46. package/lib/raw_types_schemats/sens.d.ts +2709 -0
  47. package/lib/raw_types_schemats/sens.js +3 -0
  48. package/lib/scripts/convert_data.js +184 -275
  49. package/lib/scripts/datautil.d.ts +5 -0
  50. package/lib/scripts/datautil.js +21 -0
  51. package/lib/scripts/parse_textes.js +49 -0
  52. package/lib/scripts/retrieve_documents.js +255 -0
  53. package/lib/scripts/retrieve_open_data.js +211 -360
  54. package/lib/scripts/retrieve_senateurs_photos.js +144 -257
  55. package/lib/scripts/shared/cli_helpers.d.ts +44 -0
  56. package/lib/scripts/shared/cli_helpers.js +35 -0
  57. package/lib/scripts/shared/util.d.ts +3 -0
  58. package/lib/scripts/shared/util.js +36 -0
  59. package/lib/strings.js +20 -45
  60. package/lib/types/ameli.d.ts +2 -2
  61. package/lib/types/ameli.js +14 -25
  62. package/lib/types/debats.d.ts +2 -2
  63. package/lib/types/debats.js +3 -21
  64. package/lib/types/dosleg.d.ts +3 -3
  65. package/lib/types/dosleg.js +152 -119
  66. package/lib/types/questions.d.ts +2 -2
  67. package/lib/types/questions.js +1 -13
  68. package/lib/types/sens.d.ts +2 -4
  69. package/lib/types/sens.js +1 -16
  70. package/lib/types/sessions.d.ts +42 -0
  71. package/lib/types/sessions.js +46 -0
  72. package/lib/types/texte.d.ts +65 -0
  73. package/lib/types/texte.js +19 -0
  74. package/lib/validators/config.js +47 -111
  75. package/lib/validators/senat.js +1 -5
  76. package/package.json +35 -32
  77. package/lib/aggregates.d.ts +0 -54
  78. package/lib/aggregates.js +0 -1122
  79. package/lib/aggregates.mjs +0 -802
  80. package/lib/aggregates.ts +0 -947
  81. package/lib/config.mjs +0 -16
  82. package/lib/config.ts +0 -26
  83. package/lib/databases.mjs +0 -26
  84. package/lib/databases.ts +0 -32
  85. package/lib/datasets.mjs +0 -78
  86. package/lib/datasets.ts +0 -118
  87. package/lib/fields.d.ts +0 -10
  88. package/lib/fields.js +0 -68
  89. package/lib/fields.mjs +0 -22
  90. package/lib/fields.ts +0 -29
  91. package/lib/index.mjs +0 -7
  92. package/lib/index.ts +0 -63
  93. package/lib/inserters.d.ts +0 -102
  94. package/lib/inserters.js +0 -518
  95. package/lib/inserters.mjs +0 -376
  96. package/lib/inserters.ts +0 -545
  97. package/lib/model/ameli.mjs +0 -57
  98. package/lib/model/ameli.ts +0 -86
  99. package/lib/model/debats.mjs +0 -43
  100. package/lib/model/debats.ts +0 -68
  101. package/lib/model/dosleg.mjs +0 -337
  102. package/lib/model/dosleg.ts +0 -558
  103. package/lib/model/index.mjs +0 -5
  104. package/lib/model/index.ts +0 -11
  105. package/lib/model/questions.mjs +0 -8
  106. package/lib/model/questions.ts +0 -14
  107. package/lib/model/sens.mjs +0 -9
  108. package/lib/model/sens.ts +0 -18
  109. package/lib/model/util.mjs +0 -10
  110. package/lib/model/util.ts +0 -16
  111. package/lib/raw_types/ameli.mjs +0 -2
  112. package/lib/raw_types/ameli.ts +0 -601
  113. package/lib/raw_types/debats.mjs +0 -2
  114. package/lib/raw_types/debats.ts +0 -145
  115. package/lib/raw_types/dosleg.mjs +0 -2
  116. package/lib/raw_types/dosleg.ts +0 -2193
  117. package/lib/raw_types/questions.mjs +0 -2
  118. package/lib/raw_types/questions.ts +0 -249
  119. package/lib/raw_types/sens.mjs +0 -2
  120. package/lib/raw_types/sens.ts +0 -2907
  121. package/lib/scripts/convert_data.mjs +0 -146
  122. package/lib/scripts/convert_data.ts +0 -182
  123. package/lib/scripts/fix_db.js +0 -144
  124. package/lib/scripts/fix_db.mjs +0 -64
  125. package/lib/scripts/fix_db.ts +0 -75
  126. package/lib/scripts/images/transparent_150x192.jpg +0 -0
  127. package/lib/scripts/images/transparent_155x225.jpg +0 -0
  128. package/lib/scripts/retrieve_open_data.mjs +0 -218
  129. package/lib/scripts/retrieve_open_data.ts +0 -258
  130. package/lib/scripts/retrieve_senateurs_photos.mjs +0 -164
  131. package/lib/scripts/retrieve_senateurs_photos.ts +0 -200
  132. package/lib/scripts/retrieve_textes.js +0 -179
  133. package/lib/scripts/retrieve_textes.mjs +0 -77
  134. package/lib/scripts/retrieve_textes.ts +0 -95
  135. package/lib/strings.mjs +0 -18
  136. package/lib/strings.ts +0 -26
  137. package/lib/types/ameli.mjs +0 -13
  138. package/lib/types/ameli.ts +0 -21
  139. package/lib/types/debats.mjs +0 -2
  140. package/lib/types/debats.ts +0 -6
  141. package/lib/types/dosleg.mjs +0 -151
  142. package/lib/types/dosleg.ts +0 -284
  143. package/lib/types/questions.mjs +0 -1
  144. package/lib/types/questions.ts +0 -3
  145. package/lib/types/sens.mjs +0 -23
  146. package/lib/types/sens.ts +0 -36
  147. package/lib/typings/windows-1252.d.js +0 -2
  148. package/lib/typings/windows-1252.d.mjs +0 -2
  149. package/lib/typings/windows-1252.d.ts +0 -11
  150. package/lib/validators/config.mjs +0 -54
  151. package/lib/validators/config.ts +0 -79
  152. package/lib/validators/senat.mjs +0 -24
  153. package/lib/validators/senat.ts +0 -26
  154. /package/lib/scripts/{fix_db.d.ts → parse_textes.d.ts} +0 -0
  155. /package/lib/scripts/{retrieve_textes.d.ts → retrieve_documents.d.ts} +0 -0
@@ -1,75 +0,0 @@
1
- import commandLineArgs from 'command-line-args'
2
- import { execSync } from 'child_process'
3
- import assert from 'assert'
4
-
5
- import { getTables } from '../model'
6
- import { Dataset, datasets } from '../datasets'
7
- import config from '../config'
8
-
9
- const optionsDefinitions = [
10
- {
11
- help: 'move database tables to public schema',
12
- name: 'updateSchema',
13
- type: Boolean,
14
- },
15
- {
16
- alias: 's',
17
- help: 'don\'t log anything',
18
- name: 'silent',
19
- type: Boolean,
20
- },
21
- {
22
- alias: "v",
23
- help: "verbose logs",
24
- name: "verbose",
25
- type: Boolean,
26
- },
27
- ]
28
- const options = commandLineArgs(optionsDefinitions)
29
-
30
- async function fixDatabase () {
31
- process.env = {
32
- ...process.env,
33
- PGHOST: process.env.PGHOST || config.db.host,
34
- PGPORT: process.env.PGPORT || config.db.port,
35
- PGUSER: process.env.PGUSER || config.db.user,
36
- PGPASSWORD: process.env.PGPASSWORD || config.db.password
37
- }
38
- assert(process.env.PGHOST
39
- && process.env.PGPORT
40
- && process.env.PGUSER
41
- && process.env.PGPASSWORD,
42
- 'Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file'
43
- )
44
-
45
- const choosenDatasets: Dataset[] = [datasets.questions]
46
- for (const dataset of choosenDatasets) {
47
- if (!options.silent) {
48
- console.log(`Fixing database ${dataset.database}…`)
49
- }
50
-
51
- if (options.updateSchema && dataset.schema != "public") {
52
- const tables = await getTables(dataset.database, dataset.schema)
53
- for (const table of tables) {
54
- if (!options.silent) {
55
- console.log(`Move table ${table} from schema "${dataset.schema}" to "public"…`)
56
- }
57
- execSync(
58
- `psql -d ${dataset.database} -c "ALTER TABLE ${dataset.schema}.${table} SET SCHEMA public"`,
59
- {
60
- env: process.env,
61
- encoding: 'utf-8',
62
- // stdio: ["ignore", "ignore", "pipe"],
63
- },
64
- )
65
- }
66
- }
67
- }
68
- }
69
-
70
- fixDatabase()
71
- .then(() => process.exit(0))
72
- .catch((error) => {
73
- console.log(error)
74
- process.exit(1)
75
- })
@@ -1,218 +0,0 @@
1
- import assert from "assert";
2
- import { execSync } from "child_process";
3
- import commandLineArgs from "command-line-args";
4
- import fs from "fs-extra";
5
- // import fetch from "node-fetch"
6
- import path from "path";
7
- // import stream from "stream"
8
- import StreamZip from "node-stream-zip";
9
- import readline from "readline";
10
- // import util from "util"
11
- import windows1252 from "windows-1252";
12
- import config from "../config";
13
- import { getChosenFromEnabledDatasets } from '../datasets';
14
- const badWindows1252CharacterRegex = /[\u0080-\u009f]/g;
15
- const optionsDefinitions = [
16
- {
17
- alias: 'k',
18
- defaultValue: ['All'],
19
- help: 'categories of datasets to reorganize',
20
- multiple: true,
21
- name: 'categories',
22
- type: String,
23
- },
24
- {
25
- alias: "a",
26
- help: "all options: fetch, unzip, repair-encoding, import, schema",
27
- name: "all",
28
- type: Boolean,
29
- },
30
- {
31
- alias: "c",
32
- help: "create TypeScript interfaces from databases schemas into src/raw_types directory",
33
- name: "schema",
34
- type: Boolean,
35
- },
36
- {
37
- alias: "e",
38
- help: "repair Windows CP 1252 encoding of SQL dumps",
39
- name: "repair-encoding",
40
- type: Boolean,
41
- },
42
- {
43
- alias: "f",
44
- help: "fetch datasets instead of retrieving them from files",
45
- name: "fetch",
46
- type: Boolean,
47
- },
48
- {
49
- alias: "i",
50
- help: "import SQL dumps into a freshly (re-)created database",
51
- name: "import",
52
- type: Boolean,
53
- },
54
- {
55
- alias: "z",
56
- help: "unzip SQL files",
57
- name: "unzip",
58
- type: Boolean,
59
- },
60
- {
61
- alias: "s",
62
- help: "don't log anything",
63
- name: "silent",
64
- type: Boolean,
65
- },
66
- {
67
- defaultOption: true,
68
- help: "directory containing Sénat open data files",
69
- name: "dataDir",
70
- type: String,
71
- },
72
- ];
73
- const options = commandLineArgs(optionsDefinitions);
74
- // const pipeline = util.promisify(stream.pipeline)
75
- async function retrieveDataset(dataDir, dataset) {
76
- const zipFilename = dataset.url.substring(dataset.url.lastIndexOf("/") + 1);
77
- const zipFilePath = path.join(dataDir, zipFilename);
78
- if (options.all || options.fetch) {
79
- // Fetch & save ZIP file.
80
- if (!options.silent) {
81
- console.log(`Loading ${dataset.title}: ${zipFilename}…`);
82
- }
83
- // Fetch fails with OpenSSL error: dh key too small.
84
- // (so does "curl").
85
- // const response = await fetch(dataset.url)
86
- // if (!response.ok) {
87
- // console.error(response.status, response.statusText)
88
- // console.error(await response.text())
89
- // throw new Error(`Fetch failed: ${dataset.url}`)
90
- // }
91
- // await pipeline(response.body!, fs.createWriteStream(zipFilePath))
92
- fs.removeSync(zipFilePath);
93
- execSync(`wget --quiet ${dataset.url}`, {
94
- cwd: dataDir,
95
- env: process.env,
96
- encoding: "utf-8",
97
- // stdio: ["ignore", "ignore", "pipe"],
98
- });
99
- }
100
- const sqlFilename = `${dataset.database}.sql`;
101
- const sqlFilePath = path.join(dataDir, sqlFilename);
102
- if (options.all || options.unzip) {
103
- if (!options.silent) {
104
- console.log(`Unzipping ${dataset.title}: ${zipFilename}…`);
105
- }
106
- fs.removeSync(sqlFilePath);
107
- const zip = new StreamZip({
108
- file: zipFilePath,
109
- storeEntries: true,
110
- });
111
- await new Promise((resolve, reject) => {
112
- zip.on("ready", () => {
113
- zip.extract(null, dataDir, (err, _count) => {
114
- zip.close();
115
- if (err) {
116
- reject(err);
117
- }
118
- else {
119
- resolve(null);
120
- }
121
- });
122
- });
123
- });
124
- if (dataset.repairZip !== undefined) {
125
- if (!options.silent) {
126
- console.log(`Repairing Zip path ${dataset.title}: ${sqlFilename}…`);
127
- }
128
- dataset.repairZip(dataset, dataDir);
129
- }
130
- }
131
- if ((options.all || options["repair-encoding"]) && dataset.repairEncoding) {
132
- if (!options.silent) {
133
- console.log(`Repairing Windows CP1252 encoding of ${dataset.title}: ${sqlFilename}…`);
134
- }
135
- const repairedSqlFilePath = sqlFilePath + ".repaired";
136
- const repairedSqlWriter = fs.createWriteStream(repairedSqlFilePath, {
137
- encoding: "utf8",
138
- });
139
- const lineReader = readline.createInterface({
140
- input: fs.createReadStream(sqlFilePath, { encoding: "utf8" }),
141
- crlfDelay: Infinity,
142
- });
143
- for await (const line of lineReader) {
144
- repairedSqlWriter.write(line.replace(badWindows1252CharacterRegex, (match) => windows1252.decode(match, { mode: "fatal" })) + "\n");
145
- }
146
- repairedSqlWriter.end();
147
- await fs.move(repairedSqlFilePath, sqlFilePath, { overwrite: true });
148
- }
149
- if (options.all || options.import) {
150
- if (!options.silent) {
151
- console.log(`Importing ${dataset.title}: ${sqlFilename}…`);
152
- }
153
- execSync(`psql -c "DROP DATABASE IF EXISTS ${dataset.database}"`, {
154
- cwd: dataDir,
155
- env: process.env,
156
- encoding: "utf-8",
157
- // stdio: ["ignore", "ignore", "pipe"],
158
- });
159
- execSync(`psql -c "CREATE DATABASE ${dataset.database} WITH OWNER opendata"`, {
160
- cwd: dataDir,
161
- env: process.env,
162
- encoding: "utf-8",
163
- // stdio: ["ignore", "ignore", "pipe"],
164
- });
165
- execSync(`psql -f ${sqlFilename} ${dataset.database}`, {
166
- cwd: dataDir,
167
- env: process.env,
168
- encoding: "utf-8",
169
- // stdio: ["ignore", "ignore", "pipe"],
170
- });
171
- }
172
- if (options.schema) {
173
- const definitionsDir = path.resolve("src", "raw_types");
174
- assert(fs.statSync(definitionsDir).isDirectory());
175
- if (!options.silent) {
176
- console.log(`Creating TypeScript definitions from schema of database ${dataset.database}…`);
177
- }
178
- const dbConnectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${dataset.database}`;
179
- const definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`);
180
- execSync(`npx schemats generate -c ${dbConnectionString} -s public -o ${definitionFilePath}`, {
181
- // cwd: dataDir,
182
- env: process.env,
183
- encoding: "utf-8",
184
- // stdio: ["ignore", "ignore", "pipe"],
185
- });
186
- const definition = fs.readFileSync(definitionFilePath, { encoding: "utf8" });
187
- const definitionRepaired = definition
188
- .replace(/\r\n/g, "\n")
189
- .replace(/AUTO-GENERATED FILE @ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, "AUTO-GENERATED FILE");
190
- fs.writeFileSync(definitionFilePath, definitionRepaired);
191
- }
192
- }
193
- async function retrieveOpenData() {
194
- const dataDir = options.dataDir;
195
- assert(dataDir, "Missing argument: data directory");
196
- process.env = {
197
- ...process.env,
198
- PGHOST: process.env.PGHOST || config.db.host,
199
- PGPORT: process.env.PGPORT || config.db.port,
200
- PGUSER: process.env.PGUSER || config.db.user,
201
- PGPASSWORD: process.env.PGPASSWORD || config.db.password
202
- };
203
- assert(process.env.PGHOST
204
- && process.env.PGPORT
205
- && process.env.PGUSER
206
- && process.env.PGPASSWORD, 'Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file');
207
- const choosenDatasets = getChosenFromEnabledDatasets(options.categories);
208
- // await Promise.all(choosenDatasets.map(dataset => retrieveDataset(dataDir, dataset)))
209
- for (const dataset of choosenDatasets) {
210
- await retrieveDataset(dataDir, dataset);
211
- }
212
- }
213
- retrieveOpenData()
214
- .then(() => process.exit(0))
215
- .catch((error) => {
216
- console.log(error);
217
- process.exit(1);
218
- });
@@ -1,258 +0,0 @@
1
- import assert from "assert"
2
- import { execSync } from "child_process"
3
- import commandLineArgs from "command-line-args"
4
- import fs from "fs-extra"
5
- // import fetch from "node-fetch"
6
- import path from "path"
7
- // import stream from "stream"
8
- import StreamZip from "node-stream-zip"
9
- import readline from "readline"
10
- // import util from "util"
11
- import windows1252 from "windows-1252"
12
-
13
- import config from "../config"
14
- import {
15
- Dataset,
16
- getChosenFromEnabledDatasets
17
- } from '../datasets'
18
-
19
- const badWindows1252CharacterRegex = /[\u0080-\u009f]/g
20
- const optionsDefinitions = [
21
- {
22
- alias: 'k',
23
- defaultValue: ['All'],
24
- help: 'categories of datasets to reorganize',
25
- multiple: true,
26
- name: 'categories',
27
- type: String,
28
- },
29
- {
30
- alias: "a",
31
- help: "all options: fetch, unzip, repair-encoding, import, schema",
32
- name: "all",
33
- type: Boolean,
34
- },
35
- {
36
- alias: "c",
37
- help:
38
- "create TypeScript interfaces from databases schemas into src/raw_types directory",
39
- name: "schema",
40
- type: Boolean,
41
- },
42
- {
43
- alias: "e",
44
- help: "repair Windows CP 1252 encoding of SQL dumps",
45
- name: "repair-encoding",
46
- type: Boolean,
47
- },
48
- {
49
- alias: "f",
50
- help: "fetch datasets instead of retrieving them from files",
51
- name: "fetch",
52
- type: Boolean,
53
- },
54
- {
55
- alias: "i",
56
- help: "import SQL dumps into a freshly (re-)created database",
57
- name: "import",
58
- type: Boolean,
59
- },
60
- {
61
- alias: "z",
62
- help: "unzip SQL files",
63
- name: "unzip",
64
- type: Boolean,
65
- },
66
- {
67
- alias: "s",
68
- help: "don't log anything",
69
- name: "silent",
70
- type: Boolean,
71
- },
72
- {
73
- defaultOption: true,
74
- help: "directory containing Sénat open data files",
75
- name: "dataDir",
76
- type: String,
77
- },
78
- ]
79
- const options = commandLineArgs(optionsDefinitions)
80
-
81
- // const pipeline = util.promisify(stream.pipeline)
82
-
83
- async function retrieveDataset(
84
- dataDir: string,
85
- dataset: Dataset,
86
- ): Promise<void> {
87
- const zipFilename = dataset.url.substring(dataset.url.lastIndexOf("/") + 1)
88
- const zipFilePath = path.join(dataDir, zipFilename)
89
- if (options.all || options.fetch) {
90
- // Fetch & save ZIP file.
91
- if (!options.silent) {
92
- console.log(`Loading ${dataset.title}: ${zipFilename}…`)
93
- }
94
- // Fetch fails with OpenSSL error: dh key too small.
95
- // (so does "curl").
96
- // const response = await fetch(dataset.url)
97
- // if (!response.ok) {
98
- // console.error(response.status, response.statusText)
99
- // console.error(await response.text())
100
- // throw new Error(`Fetch failed: ${dataset.url}`)
101
- // }
102
- // await pipeline(response.body!, fs.createWriteStream(zipFilePath))
103
- fs.removeSync(zipFilePath)
104
- execSync(`wget --quiet ${dataset.url}`, {
105
- cwd: dataDir,
106
- env: process.env,
107
- encoding: "utf-8",
108
- // stdio: ["ignore", "ignore", "pipe"],
109
- })
110
- }
111
-
112
- const sqlFilename = `${dataset.database}.sql`
113
- const sqlFilePath = path.join(dataDir, sqlFilename)
114
- if (options.all || options.unzip) {
115
- if (!options.silent) {
116
- console.log(`Unzipping ${dataset.title}: ${zipFilename}…`)
117
- }
118
- fs.removeSync(sqlFilePath)
119
- const zip = new StreamZip({
120
- file: zipFilePath,
121
- storeEntries: true,
122
- })
123
- await new Promise((resolve, reject) => {
124
- zip.on("ready", () => {
125
- zip.extract(null, dataDir, (err?: any, _count?: number) => {
126
- zip.close()
127
- if (err) {
128
- reject(err)
129
- } else {
130
- resolve(null)
131
- }
132
- })
133
- })
134
- })
135
- if (dataset.repairZip !== undefined) {
136
- if (!options.silent) {
137
- console.log(`Repairing Zip path ${dataset.title}: ${sqlFilename}…`)
138
- }
139
- dataset.repairZip(dataset, dataDir)
140
- }
141
- }
142
-
143
- if ((options.all || options["repair-encoding"]) && dataset.repairEncoding) {
144
- if (!options.silent) {
145
- console.log(
146
- `Repairing Windows CP1252 encoding of ${dataset.title}: ${sqlFilename}…`,
147
- )
148
- }
149
- const repairedSqlFilePath = sqlFilePath + ".repaired"
150
- const repairedSqlWriter = fs.createWriteStream(repairedSqlFilePath, {
151
- encoding: "utf8",
152
- })
153
- const lineReader = readline.createInterface({
154
- input: fs.createReadStream(sqlFilePath, { encoding: "utf8" }),
155
- crlfDelay: Infinity,
156
- })
157
- for await (const line of lineReader) {
158
- repairedSqlWriter.write(
159
- line.replace(badWindows1252CharacterRegex, (match) =>
160
- windows1252.decode(match, { mode: "fatal" }),
161
- ) + "\n",
162
- )
163
- }
164
- repairedSqlWriter.end()
165
- await fs.move(repairedSqlFilePath, sqlFilePath, { overwrite: true })
166
- }
167
-
168
- if (options.all || options.import) {
169
- if (!options.silent) {
170
- console.log(`Importing ${dataset.title}: ${sqlFilename}…`)
171
- }
172
- execSync(`psql -c "DROP DATABASE IF EXISTS ${dataset.database}"`, {
173
- cwd: dataDir,
174
- env: process.env,
175
- encoding: "utf-8",
176
- // stdio: ["ignore", "ignore", "pipe"],
177
- })
178
- execSync(
179
- `psql -c "CREATE DATABASE ${dataset.database} WITH OWNER opendata"`,
180
- {
181
- cwd: dataDir,
182
- env: process.env,
183
- encoding: "utf-8",
184
- // stdio: ["ignore", "ignore", "pipe"],
185
- },
186
- )
187
- execSync(`psql -f ${sqlFilename} ${dataset.database}`, {
188
- cwd: dataDir,
189
- env: process.env,
190
- encoding: "utf-8",
191
- // stdio: ["ignore", "ignore", "pipe"],
192
- })
193
- }
194
-
195
- if (options.schema) {
196
- const definitionsDir = path.resolve("src", "raw_types")
197
- assert(fs.statSync(definitionsDir).isDirectory())
198
- if (!options.silent) {
199
- console.log(
200
- `Creating TypeScript definitions from schema of database ${dataset.database}…`,
201
- )
202
- }
203
- const dbConnectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${dataset.database}`
204
- const definitionFilePath = path.join(
205
- definitionsDir,
206
- `${dataset.database}.ts`,
207
- )
208
- execSync(
209
- `npx schemats generate -c ${dbConnectionString} -s public -o ${definitionFilePath}`,
210
- {
211
- // cwd: dataDir,
212
- env: process.env,
213
- encoding: "utf-8",
214
- // stdio: ["ignore", "ignore", "pipe"],
215
- },
216
- )
217
- const definition = fs.readFileSync(definitionFilePath, { encoding: "utf8" })
218
- const definitionRepaired = definition
219
- .replace(/\r\n/g, "\n")
220
- .replace(
221
- /AUTO-GENERATED FILE @ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/,
222
- "AUTO-GENERATED FILE",
223
- )
224
- fs.writeFileSync(definitionFilePath, definitionRepaired)
225
- }
226
- }
227
-
228
- async function retrieveOpenData(): Promise<void> {
229
- const dataDir = options.dataDir
230
- assert(dataDir, "Missing argument: data directory")
231
-
232
- process.env = {
233
- ...process.env,
234
- PGHOST: process.env.PGHOST || config.db.host,
235
- PGPORT: process.env.PGPORT || config.db.port,
236
- PGUSER: process.env.PGUSER || config.db.user,
237
- PGPASSWORD: process.env.PGPASSWORD || config.db.password
238
- }
239
- assert(process.env.PGHOST
240
- && process.env.PGPORT
241
- && process.env.PGUSER
242
- && process.env.PGPASSWORD,
243
- 'Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file'
244
- )
245
-
246
- const choosenDatasets: Dataset[] = getChosenFromEnabledDatasets(options.categories)
247
- // await Promise.all(choosenDatasets.map(dataset => retrieveDataset(dataDir, dataset)))
248
- for (const dataset of choosenDatasets) {
249
- await retrieveDataset(dataDir, dataset)
250
- }
251
- }
252
-
253
- retrieveOpenData()
254
- .then(() => process.exit(0))
255
- .catch((error) => {
256
- console.log(error)
257
- process.exit(1)
258
- })