@opencrvs/toolkit 2.1.0 → 2.2.0-rc.ab91cce

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 (36) hide show
  1. package/build.sh +2 -2
  2. package/create-countryconfig/package.json +1 -1
  3. package/dist/cli.js +294 -1967
  4. package/dist/commons/events/ActionConfig.d.ts +79 -0
  5. package/dist/commons/events/EventConfig.d.ts +26 -6
  6. package/dist/commons/events/eventConfigValidation.d.ts +17 -1
  7. package/dist/commons/events/test.utils.d.ts +5 -0
  8. package/dist/commons/events/utils.d.ts +14 -14
  9. package/dist/environment/derived-variables.d.ts +18 -0
  10. package/dist/environment/derived-variables.d.ts.map +1 -1
  11. package/dist/environment/derived-variables.js +22 -1
  12. package/dist/environment/setup-environment.d.ts.map +1 -1
  13. package/dist/environment/setup-environment.js +29 -10
  14. package/dist/events/index.js +171 -60
  15. package/dist/migrations/add-translations.d.ts +15 -0
  16. package/dist/migrations/add-translations.d.ts.map +1 -0
  17. package/dist/migrations/{v2.1/add-translations.js → add-translations.js} +16 -14
  18. package/dist/migrations/v2.1/index.js +35 -2155
  19. package/dist/migrations/v2.1/migrate-infrastructure-to-assets.d.ts +3 -0
  20. package/dist/migrations/v2.1/migrate-infrastructure-to-assets.d.ts.map +1 -1
  21. package/dist/migrations/v2.1/upgrade-tilt.d.ts +3 -0
  22. package/dist/migrations/v2.1/upgrade-tilt.d.ts.map +1 -1
  23. package/dist/migrations/v2.2/index.d.ts +6 -0
  24. package/dist/migrations/v2.2/index.d.ts.map +1 -0
  25. package/dist/migrations/v2.2/index.js +456 -0
  26. package/dist/migrations/v2.2/read-declaration-through-helper.d.ts +39 -0
  27. package/dist/migrations/v2.2/read-declaration-through-helper.d.ts.map +1 -0
  28. package/dist/migrations/v2.2/read-declaration-through-helper.js +257 -0
  29. package/dist/notification/index.js +191 -83
  30. package/dist/templates/charts-values/traefik/values.yaml +2 -3
  31. package/opencrvs-toolkit-2.2.0-rc.ab91cce.tgz +0 -0
  32. package/package.json +1 -1
  33. package/tsconfig.build.tsbuildinfo +1 -1
  34. package/dist/migrations/v2.1/add-translations.d.ts +0 -9
  35. package/dist/migrations/v2.1/add-translations.d.ts.map +0 -1
  36. package/opencrvs-toolkit-2.1.0.tgz +0 -0
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ *
7
+ * OpenCRVS is also distributed under the terms of the Civil Registration
8
+ * & Healthcare Disclaimer located at http://opencrvs.org/license.
9
+ *
10
+ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
11
+ */
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.rewriteDeclarationReads = rewriteDeclarationReads;
26
+ exports.main = main;
27
+ /**
28
+ * Codemod: Read an event configuration's declaration form through
29
+ * `getDeclaration()` instead of the top-level `declaration` property.
30
+ *
31
+ * Usage:
32
+ * ts-node -r tsconfig-paths/register src/migrations/v2.2/read-declaration-through-helper.ts
33
+ *
34
+ * Why:
35
+ * As of v2.2 the declaration form lives on the DECLARE action. `defineConfig`
36
+ * still accepts a top-level `declaration` and moves it there, but the parsed
37
+ * `EventConfig` no longer has a top-level `declaration` property, so code
38
+ * reading `eventConfig.declaration` stops compiling.
39
+ *
40
+ * What it does:
41
+ * - Scans all TypeScript files under `src/`
42
+ * - Rewrites `x.declaration` to `getDeclaration(x)` wherever `x` is the
43
+ * toolkit's `EventConfig`. Record data (e.g. `event.declaration`) is a
44
+ * different type and is left alone.
45
+ * - Adds `getDeclaration` to the file's `@opencrvs/toolkit/events` import
46
+ * - Logs, without changing, reads it cannot rewrite safely: optional chains
47
+ * (`x?.declaration`), writes to `x.declaration` (`=`, `??=`, `delete`...)
48
+ * and destructuring `{ declaration }` out of an `EventConfig`
49
+ * - Logs object literals that spread an `EventConfig` and set `declaration`
50
+ * next to it, which `defineConfig` now rejects
51
+ * - Leaves a file alone, and logs it, when it already declares its own
52
+ * `getDeclaration`
53
+ * - Saves the modified files in-place
54
+ */
55
+ const ts_morph_1 = require("ts-morph");
56
+ const path_1 = __importDefault(require("path"));
57
+ const EVENT_CONFIG_TYPE_NAME = 'EventConfig';
58
+ const DECLARATION_PROPERTY_NAME = 'declaration';
59
+ const GET_DECLARATION_NAME = 'getDeclaration';
60
+ const TOOLKIT_EVENTS_MODULE = '@opencrvs/toolkit/events';
61
+ /**
62
+ * Returns true if the type is the `EventConfig` type published by the toolkit,
63
+ * as opposed to a type of the same name declared in the country config itself.
64
+ */
65
+ function isToolkitEventConfig(type) {
66
+ var _a;
67
+ const nonNullableType = type.getNonNullableType();
68
+ const symbol = (_a = nonNullableType.getAliasSymbol()) !== null && _a !== void 0 ? _a : nonNullableType.getSymbol();
69
+ if ((symbol === null || symbol === void 0 ? void 0 : symbol.getName()) !== EVENT_CONFIG_TYPE_NAME) {
70
+ return false;
71
+ }
72
+ return symbol
73
+ .getDeclarations()
74
+ .some((declaration) => declaration.getSourceFile().isDeclarationFile());
75
+ }
76
+ function isDeclarationReadOnEventConfig(node) {
77
+ return (node.getName() === DECLARATION_PROPERTY_NAME &&
78
+ isToolkitEventConfig(node.getExpression().getType()));
79
+ }
80
+ function isAssignmentOperator(kind) {
81
+ return kind >= ts_morph_1.SyntaxKind.FirstAssignment && kind <= ts_morph_1.SyntaxKind.LastAssignment;
82
+ }
83
+ /**
84
+ * Returns true if the node is written to rather than read: the target of `=` or
85
+ * a compound assignment (`??=`, `||=`...), or the operand of `delete`.
86
+ * `getDeclaration(x)` is a call, so rewriting any of these does not compile.
87
+ */
88
+ function isWriteTarget(node) {
89
+ const parent = node.getParent();
90
+ if (ts_morph_1.Node.isDeleteExpression(parent)) {
91
+ return true;
92
+ }
93
+ return (ts_morph_1.Node.isBinaryExpression(parent) &&
94
+ parent.getLeft() === node &&
95
+ isAssignmentOperator(parent.getOperatorToken().getKind()));
96
+ }
97
+ function location(node) {
98
+ const relPath = path_1.default.relative(process.cwd(), node.getSourceFile().getFilePath());
99
+ return `${relPath}:${node.getStartLineNumber()}`;
100
+ }
101
+ function warnDestructuredDeclarations(sourceFile) {
102
+ for (const pattern of sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ObjectBindingPattern)) {
103
+ const destructuresDeclaration = pattern
104
+ .getElements()
105
+ .some((element) => { var _a; var _b; return ((_b = (_a = element.getPropertyNameNode()) === null || _a === void 0 ? void 0 : _a.getText()) !== null && _b !== void 0 ? _b : element.getName()) ===
106
+ DECLARATION_PROPERTY_NAME; });
107
+ if (destructuresDeclaration && isToolkitEventConfig(pattern.getType())) {
108
+ console.warn(` [${location(pattern)}] Destructures \`${DECLARATION_PROPERTY_NAME}\` from an EventConfig. Rewrite it to use \`${GET_DECLARATION_NAME}(config)\` by hand.`);
109
+ }
110
+ }
111
+ }
112
+ function warnSpreadDeclarations(sourceFile) {
113
+ for (const literal of sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)) {
114
+ const properties = literal.getProperties();
115
+ const spreadsEventConfig = properties.some((property) => ts_morph_1.Node.isSpreadAssignment(property) &&
116
+ isToolkitEventConfig(property.getExpression().getType()));
117
+ const setsDeclaration = properties.some((property) => (ts_morph_1.Node.isPropertyAssignment(property) ||
118
+ ts_morph_1.Node.isShorthandPropertyAssignment(property)) &&
119
+ property.getName() === DECLARATION_PROPERTY_NAME);
120
+ if (spreadsEventConfig && setsDeclaration) {
121
+ console.warn(` [${location(literal)}] Spreads an EventConfig and sets \`${DECLARATION_PROPERTY_NAME}\` next to it. The spread config already has a \`${DECLARATION_PROPERTY_NAME}\` on its DECLARE action, so replace that one by hand.`);
122
+ }
123
+ }
124
+ }
125
+ /** Finds the value import of the toolkit's events module, if the file has one. */
126
+ function findToolkitImport(sourceFile) {
127
+ // A type-only import cannot carry a function, and a namespace import cannot
128
+ // be given named imports, so neither is extended
129
+ return sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE &&
130
+ !declaration.isTypeOnly() &&
131
+ !declaration.getNamespaceImport());
132
+ }
133
+ /**
134
+ * Returns the name `getDeclaration` is called by in the file: its local alias
135
+ * when the file already imports it from the toolkit, `getDeclaration` when the
136
+ * name is free, or undefined when the file binds `getDeclaration` to
137
+ * something else.
138
+ */
139
+ function findGetDeclarationName(sourceFile) {
140
+ var _a, _b;
141
+ var _c;
142
+ const imported = (_a = findToolkitImport(sourceFile)) === null || _a === void 0 ? void 0 : _a.getNamedImports().find((namedImport) => namedImport.getName() === GET_DECLARATION_NAME);
143
+ if (imported) {
144
+ return (_c = (_b = imported.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : GET_DECLARATION_NAME;
145
+ }
146
+ if (sourceFile.getLocal(GET_DECLARATION_NAME)) {
147
+ return undefined;
148
+ }
149
+ return GET_DECLARATION_NAME;
150
+ }
151
+ function addGetDeclarationImport(sourceFile) {
152
+ const toolkitImport = findToolkitImport(sourceFile);
153
+ if (!toolkitImport) {
154
+ const imports = sourceFile.getImportDeclarations();
155
+ const lastImport = imports[imports.length - 1];
156
+ // Inserted as text so the import keeps the country config's style
157
+ // (single quotes, no semicolon) rather than ts-morph's defaults
158
+ sourceFile.insertStatements(lastImport ? lastImport.getChildIndex() + 1 : 0, `import { ${GET_DECLARATION_NAME} } from '${TOOLKIT_EVENTS_MODULE}'`);
159
+ return;
160
+ }
161
+ const alreadyImported = toolkitImport
162
+ .getNamedImports()
163
+ .some((namedImport) => namedImport.getName() === GET_DECLARATION_NAME);
164
+ if (alreadyImported) {
165
+ return;
166
+ }
167
+ const namedImports = toolkitImport.getNamedImports();
168
+ // ts-morph's addNamedImport collapses a multi-line import onto one line, so
169
+ // the import is rewritten keeping one name per line when it had them
170
+ if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
171
+ toolkitImport.addNamedImport(GET_DECLARATION_NAME);
172
+ return;
173
+ }
174
+ const names = [
175
+ ...namedImports.map((namedImport) => namedImport.getText()),
176
+ GET_DECLARATION_NAME
177
+ ];
178
+ const specifiers = toolkitImport.getText().includes('\n')
179
+ ? `{\n ${names.join(',\n ')}\n}`
180
+ : `{ ${names.join(', ')} }`;
181
+ toolkitImport.replaceWithText(`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE}'`);
182
+ }
183
+ /**
184
+ * Rewrites every `x.declaration` read on an `EventConfig` in the file to
185
+ * `getDeclaration(x)`.
186
+ *
187
+ * @returns the number of reads rewritten.
188
+ */
189
+ function rewriteDeclarationReads(sourceFile) {
190
+ warnDestructuredDeclarations(sourceFile);
191
+ warnSpreadDeclarations(sourceFile);
192
+ const reads = sourceFile
193
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAccessExpression)
194
+ .filter(isDeclarationReadOnEventConfig)
195
+ .filter((node) => {
196
+ if (node.hasQuestionDotToken()) {
197
+ console.warn(` [${location(node)}] Skipped optional chain \`${node.getText()}\`. Rewrite it to use \`${GET_DECLARATION_NAME}()\` by hand.`);
198
+ return false;
199
+ }
200
+ if (isWriteTarget(node)) {
201
+ console.warn(` [${location(node)}] Skipped write to \`${node.getText()}\`. Set \`${DECLARATION_PROPERTY_NAME}\` on the DECLARE action instead.`);
202
+ return false;
203
+ }
204
+ return true;
205
+ })
206
+ // Replace the innermost reads first, so replacing an outer read does not
207
+ // invalidate nodes still waiting to be replaced.
208
+ .sort((a, b) => b.getStart() - a.getStart());
209
+ if (reads.length === 0) {
210
+ return 0;
211
+ }
212
+ const getDeclarationName = findGetDeclarationName(sourceFile);
213
+ if (!getDeclarationName) {
214
+ console.warn(` [${location(reads[reads.length - 1])}] Skipped ${reads.length} read(s): the file already declares its own \`${GET_DECLARATION_NAME}\`. Rewrite them to use the toolkit's \`${GET_DECLARATION_NAME}()\` by hand.`);
215
+ return 0;
216
+ }
217
+ for (const read of reads) {
218
+ console.log(` [${location(read)}] ${read.getText()} -> ${getDeclarationName}(${read.getExpression().getText()})`);
219
+ read.replaceWithText(`${getDeclarationName}(${read.getExpression().getText()})`);
220
+ }
221
+ addGetDeclarationImport(sourceFile);
222
+ return reads.length;
223
+ }
224
+ function main() {
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ const srcDir = path_1.default.join(process.cwd(), 'src');
227
+ console.log(`Scanning for EventConfig declaration reads in: ${srcDir}\n`);
228
+ const project = new ts_morph_1.Project({
229
+ tsConfigFilePath: path_1.default.resolve(srcDir, '../tsconfig.json'),
230
+ skipAddingFilesFromTsConfig: false
231
+ });
232
+ const sourceFiles = project.getSourceFiles().filter((sf) => {
233
+ const fp = sf.getFilePath();
234
+ return fp.includes('/src/') && !fp.includes('/node_modules/');
235
+ });
236
+ console.log(`Found ${sourceFiles.length} source file(s) to analyse.\n`);
237
+ let totalChanges = 0;
238
+ const modifiedFiles = [];
239
+ for (const sourceFile of sourceFiles) {
240
+ const changes = rewriteDeclarationReads(sourceFile);
241
+ if (changes > 0) {
242
+ totalChanges += changes;
243
+ modifiedFiles.push(sourceFile);
244
+ }
245
+ }
246
+ if (modifiedFiles.length === 0) {
247
+ console.log('No EventConfig declaration reads found. Nothing to do.');
248
+ return;
249
+ }
250
+ console.log(`\nSaving ${modifiedFiles.length} modified file(s)...`);
251
+ for (const sourceFile of modifiedFiles) {
252
+ yield sourceFile.save();
253
+ console.log(` Saved: ${path_1.default.relative(process.cwd(), sourceFile.getFilePath())}`);
254
+ }
255
+ console.log(`\nDone. Rewrote ${totalChanges} declaration read(s) across ${modifiedFiles.length} file(s).`);
256
+ });
257
+ }