@opencrvs/toolkit 2.1.1-rc.52250d8 → 2.2.0-rc.3af310b

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 (141) hide show
  1. package/build.sh +2 -12
  2. package/create-countryconfig/package.json +1 -1
  3. package/dist/application-config/index.js +36 -4
  4. package/dist/authentication/index.js +14 -2
  5. package/dist/cli.js +833 -2019
  6. package/dist/commons/api/router.d.ts +1374 -71
  7. package/dist/commons/conditionals/conditionals.d.ts +3 -3
  8. package/dist/commons/events/ActionConfig.d.ts +146 -20
  9. package/dist/commons/events/ActionDocument.d.ts +88 -0
  10. package/dist/commons/events/ActionInput.d.ts +52 -0
  11. package/dist/commons/events/ActionType.d.ts +11 -1
  12. package/dist/commons/events/AdvancedSearchConfig.d.ts +12 -0
  13. package/dist/commons/events/AuditLog.d.ts +4 -0
  14. package/dist/commons/events/CountryConfigQueryInput.d.ts +192 -18
  15. package/dist/commons/events/Draft.d.ts +2 -0
  16. package/dist/commons/events/EventConfig.d.ts +36 -6
  17. package/dist/commons/events/EventDocument.d.ts +88 -0
  18. package/dist/commons/events/EventIndex.d.ts +41 -4
  19. package/dist/commons/events/EventMetadata.d.ts +23 -1
  20. package/dist/commons/events/EventStatus.d.ts +1 -0
  21. package/dist/commons/events/FieldConfig.d.ts +10 -0
  22. package/dist/commons/events/Flag.d.ts +0 -6
  23. package/dist/commons/events/WorkqueueConfig.d.ts +224 -33
  24. package/dist/commons/events/eventConfigValidation.d.ts +26 -1
  25. package/dist/commons/events/notificationFormFallback.d.ts +10 -0
  26. package/dist/commons/events/scopes.d.ts +2 -0
  27. package/dist/commons/events/state/flags.d.ts +1 -1
  28. package/dist/commons/events/state/index.d.ts +10 -2
  29. package/dist/commons/events/state/utils.d.ts +11 -1
  30. package/dist/commons/events/test.utils.d.ts +31 -1
  31. package/dist/commons/events/utils.d.ts +53 -20
  32. package/dist/conditionals/index.js +14 -2
  33. package/dist/environment/derived-variables.d.ts +6 -0
  34. package/dist/environment/derived-variables.d.ts.map +1 -1
  35. package/dist/environment/derived-variables.js +7 -0
  36. package/dist/environment/dockerhub.d.ts +15 -0
  37. package/dist/environment/dockerhub.d.ts.map +1 -0
  38. package/dist/environment/dockerhub.js +104 -0
  39. package/dist/environment/questions.d.ts +1 -1
  40. package/dist/environment/questions.d.ts.map +1 -1
  41. package/dist/environment/questions.js +4 -7
  42. package/dist/environment/setup-environment.d.ts.map +1 -1
  43. package/dist/environment/setup-environment.js +41 -2
  44. package/dist/environment/templates.d.ts.map +1 -1
  45. package/dist/events/index.js +461 -83
  46. package/dist/migrations/index.d.ts +9 -0
  47. package/dist/migrations/index.d.ts.map +1 -0
  48. package/dist/migrations/index.js +847 -0
  49. package/dist/migrations/move-declaration-to-declare-action.d.ts +38 -0
  50. package/dist/migrations/move-declaration-to-declare-action.d.ts.map +1 -0
  51. package/dist/migrations/move-declaration-to-declare-action.js +377 -0
  52. package/dist/migrations/{v2.1 → persistent}/add-translations.d.ts +7 -1
  53. package/dist/migrations/persistent/add-translations.d.ts.map +1 -0
  54. package/dist/migrations/{v2.1 → persistent}/add-translations.js +14 -12
  55. package/dist/migrations/persistent/check-upgrade-path.d.ts +36 -0
  56. package/dist/migrations/persistent/check-upgrade-path.d.ts.map +1 -0
  57. package/dist/migrations/persistent/check-upgrade-path.js +105 -0
  58. package/dist/migrations/persistent/update-toolkit-dependency.d.ts +7 -0
  59. package/dist/migrations/persistent/update-toolkit-dependency.d.ts.map +1 -0
  60. package/dist/migrations/persistent/update-toolkit-dependency.js +77 -0
  61. package/dist/migrations/read-declaration-through-helper.d.ts +39 -0
  62. package/dist/migrations/read-declaration-through-helper.d.ts.map +1 -0
  63. package/dist/migrations/read-declaration-through-helper.js +257 -0
  64. package/dist/migrations/replace-incomplete-flag.d.ts +26 -0
  65. package/dist/migrations/replace-incomplete-flag.d.ts.map +1 -0
  66. package/dist/migrations/replace-incomplete-flag.js +126 -0
  67. package/dist/notification/index.js +401 -100
  68. package/dist/scopes/index.d.ts +29 -39
  69. package/dist/scopes/index.js +14 -2
  70. package/dist/templates/charts-values/dependencies/values.yaml +1 -1
  71. package/dist/templates/charts-values/opencrvs-services/values.yaml +1 -2
  72. package/dist/templates/charts-values/traefik/values.yaml +4 -3
  73. package/opencrvs-toolkit-2.2.0-rc.3af310b.tgz +0 -0
  74. package/package.json +1 -1
  75. package/tsconfig.build.tsbuildinfo +1 -1
  76. package/dist/migrations/v2.1/add-explicit-correction-flags.d.ts +0 -3
  77. package/dist/migrations/v2.1/add-explicit-correction-flags.d.ts.map +0 -1
  78. package/dist/migrations/v2.1/add-explicit-correction-flags.js +0 -234
  79. package/dist/migrations/v2.1/add-recovery-link-notifications.d.ts +0 -3
  80. package/dist/migrations/v2.1/add-recovery-link-notifications.d.ts.map +0 -1
  81. package/dist/migrations/v2.1/add-recovery-link-notifications.js +0 -511
  82. package/dist/migrations/v2.1/add-translations.d.ts.map +0 -1
  83. package/dist/migrations/v2.1/enable-telemetry.d.ts +0 -3
  84. package/dist/migrations/v2.1/enable-telemetry.d.ts.map +0 -1
  85. package/dist/migrations/v2.1/enable-telemetry.js +0 -455
  86. package/dist/migrations/v2.1/index.d.ts +0 -6
  87. package/dist/migrations/v2.1/index.d.ts.map +0 -1
  88. package/dist/migrations/v2.1/index.js +0 -2233
  89. package/dist/migrations/v2.1/migrate-infrastructure-to-assets.d.ts +0 -14
  90. package/dist/migrations/v2.1/migrate-infrastructure-to-assets.d.ts.map +0 -1
  91. package/dist/migrations/v2.1/migrate-infrastructure-to-assets.js +0 -348
  92. package/dist/migrations/v2.1/remove-metabase-package-installs.d.ts +0 -2
  93. package/dist/migrations/v2.1/remove-metabase-package-installs.d.ts.map +0 -1
  94. package/dist/migrations/v2.1/remove-metabase-package-installs.js +0 -161
  95. package/dist/migrations/v2.1/remove-sentry.d.ts +0 -3
  96. package/dist/migrations/v2.1/remove-sentry.d.ts.map +0 -1
  97. package/dist/migrations/v2.1/remove-sentry.js +0 -354
  98. package/dist/migrations/v2.1/remove-typings-from-dockerfile.d.ts +0 -3
  99. package/dist/migrations/v2.1/remove-typings-from-dockerfile.d.ts.map +0 -1
  100. package/dist/migrations/v2.1/remove-typings-from-dockerfile.js +0 -121
  101. package/dist/migrations/v2.1/rename-trigger-paths.d.ts +0 -3
  102. package/dist/migrations/v2.1/rename-trigger-paths.d.ts.map +0 -1
  103. package/dist/migrations/v2.1/rename-trigger-paths.js +0 -216
  104. package/dist/migrations/v2.1/template-files.d.ts +0 -8
  105. package/dist/migrations/v2.1/template-files.d.ts.map +0 -1
  106. package/dist/migrations/v2.1/template-files.js +0 -49
  107. package/dist/migrations/v2.1/upgrade-tilt.d.ts +0 -8
  108. package/dist/migrations/v2.1/upgrade-tilt.d.ts.map +0 -1
  109. package/dist/migrations/v2.1/upgrade-tilt.js +0 -139
  110. package/dist/templates/countryconfig-template/.tiltignore +0 -1
  111. package/dist/templates/countryconfig-template/Dockerfile.assets +0 -7
  112. package/dist/templates/countryconfig-template/Tiltfile +0 -112
  113. package/dist/templates/countryconfig-template/assets/README.md +0 -1
  114. package/dist/templates/countryconfig-template/assets/clear-all-data-dev.sh +0 -61
  115. package/dist/templates/countryconfig-template/assets/deployment/deploy.sh +0 -455
  116. package/dist/templates/countryconfig-template/assets/deployment/reindex.sh +0 -168
  117. package/dist/templates/countryconfig-template/assets/deployment/validate-required-variables-in-compose-files.ts +0 -67
  118. package/dist/templates/countryconfig-template/assets/elasticsearch/reindex.sh +0 -168
  119. package/dist/templates/countryconfig-template/assets/metabase/README.md +0 -34
  120. package/dist/templates/countryconfig-template/assets/metabase/environment-configuration.sql +0 -36
  121. package/dist/templates/countryconfig-template/assets/metabase/initialize-database.sh +0 -46
  122. package/dist/templates/countryconfig-template/assets/metabase/metabase.init.db.sql +0 -27737
  123. package/dist/templates/countryconfig-template/assets/metabase/open-shell.sh +0 -21
  124. package/dist/templates/countryconfig-template/assets/metabase/package.json +0 -13
  125. package/dist/templates/countryconfig-template/assets/metabase/run-dev.sh +0 -108
  126. package/dist/templates/countryconfig-template/assets/metabase/run.sh +0 -120
  127. package/dist/templates/countryconfig-template/assets/metabase/update-database.sh +0 -40
  128. package/dist/templates/countryconfig-template/assets/postgres/setup-analytics.sh +0 -135
  129. package/dist/templates/countryconfig-template/assets/postgres/setup-reference-data.sh +0 -108
  130. package/dist/templates/countryconfig-template/tilt/clear-all-data.ps1 +0 -50
  131. package/dist/templates/countryconfig-template/tilt/clear-all-data.sh +0 -35
  132. package/dist/templates/countryconfig-template/tilt/common.tilt +0 -145
  133. package/dist/templates/countryconfig-template/tilt/examples/dependencies/values.yaml +0 -15
  134. package/dist/templates/countryconfig-template/tilt/examples/opencrvs-services/values.yaml +0 -56
  135. package/dist/templates/countryconfig-template/tilt/examples/traefik/values-minikube.yaml +0 -21
  136. package/dist/templates/countryconfig-template/tilt/examples/traefik/values.yaml +0 -38
  137. package/dist/templates/countryconfig-template/tilt/helm/dependencies/values.yaml +0 -17
  138. package/dist/templates/countryconfig-template/tilt/helm/opencrvs-services/values.yaml +0 -16
  139. package/dist/templates/countryconfig-template/tilt/lib.tilt +0 -95
  140. package/dist/templates/countryconfig-template/tilt/opencrvs.tilt +0 -136
  141. package/opencrvs-toolkit-2.1.1-rc.52250d8.tgz +0 -0
@@ -0,0 +1,77 @@
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.updateToolkitDependency = updateToolkitDependency;
26
+ exports.main = main;
27
+ /**
28
+ * Upgrade step: Point the country config's `@opencrvs/toolkit` dependency at
29
+ * the toolkit running the upgrade.
30
+ *
31
+ * Why:
32
+ * The upgrade checks which version it upgrades from by the toolkit version
33
+ * the country config depends on. Updating it here, rather than by hand before
34
+ * upgrading, keeps that version the one the country config was on until its
35
+ * steps have run.
36
+ *
37
+ * What it does:
38
+ * - Rewrites the version of `@opencrvs/toolkit` in `package.json`, keeping a
39
+ * `^` or `~` in front of it and the rest of the file as written
40
+ * - Leaves a version without a major.minor alone, as in `workspace:*`
41
+ */
42
+ const fs_1 = __importDefault(require("fs"));
43
+ const path_1 = __importDefault(require("path"));
44
+ const check_upgrade_path_1 = require("./check-upgrade-path");
45
+ const DEPENDENCY_PATTERN = new RegExp(`("${check_upgrade_path_1.TOOLKIT_PACKAGE_NAME}"\\s*:\\s*")([^"]*)(")`, 'g');
46
+ /**
47
+ * Returns the `package.json` text with its toolkit dependency set to the
48
+ * version, or undefined when there is nothing to update.
49
+ */
50
+ function updateToolkitDependency(packageJson, toolkitVersion) {
51
+ const updated = packageJson.replace(DEPENDENCY_PATTERN, (match, before, version, after) => {
52
+ var _a;
53
+ var _b;
54
+ if (!(0, check_upgrade_path_1.findMajorMinor)(version)) {
55
+ return match;
56
+ }
57
+ const range = (_b = (_a = /^[\^~]?/.exec(version)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : '';
58
+ return `${before}${range}${toolkitVersion}${after}`;
59
+ });
60
+ return updated === packageJson ? undefined : updated;
61
+ }
62
+ function main(toolkitVersion) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const packageJsonPath = path_1.default.join(process.cwd(), 'package.json');
65
+ if (!fs_1.default.existsSync(packageJsonPath)) {
66
+ console.log(`No package.json found. Skipping ${check_upgrade_path_1.TOOLKIT_PACKAGE_NAME}.`);
67
+ return;
68
+ }
69
+ const updated = updateToolkitDependency(fs_1.default.readFileSync(packageJsonPath, 'utf-8'), toolkitVersion);
70
+ if (!updated) {
71
+ console.log(`${check_upgrade_path_1.TOOLKIT_PACKAGE_NAME} in package.json needs no update. Nothing to do.`);
72
+ return;
73
+ }
74
+ fs_1.default.writeFileSync(packageJsonPath, updated);
75
+ console.log(`Updated ${check_upgrade_path_1.TOOLKIT_PACKAGE_NAME} to ${toolkitVersion} in package.json. Install the dependencies again to use it.`);
76
+ });
77
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Codemod: Read an event configuration's declaration form through
3
+ * `getDeclaration()` instead of the top-level `declaration` property.
4
+ *
5
+ * Usage:
6
+ * ts-node -r tsconfig-paths/register src/migrations/read-declaration-through-helper.ts
7
+ *
8
+ * Why:
9
+ * As of v2.2 the declaration form lives on the DECLARE action. `defineConfig`
10
+ * still accepts a top-level `declaration` and moves it there, but the parsed
11
+ * `EventConfig` no longer has a top-level `declaration` property, so code
12
+ * reading `eventConfig.declaration` stops compiling.
13
+ *
14
+ * What it does:
15
+ * - Scans all TypeScript files under `src/`
16
+ * - Rewrites `x.declaration` to `getDeclaration(x)` wherever `x` is the
17
+ * toolkit's `EventConfig`. Record data (e.g. `event.declaration`) is a
18
+ * different type and is left alone.
19
+ * - Adds `getDeclaration` to the file's `@opencrvs/toolkit/events` import
20
+ * - Logs, without changing, reads it cannot rewrite safely: optional chains
21
+ * (`x?.declaration`), writes to `x.declaration` (`=`, `??=`, `delete`...)
22
+ * and destructuring `{ declaration }` out of an `EventConfig`
23
+ * - Logs object literals that spread an `EventConfig` and set `declaration`
24
+ * next to it, which `defineConfig` now rejects
25
+ * - Leaves a file alone, and logs it, when it already declares its own
26
+ * `getDeclaration`
27
+ * - Saves the modified files in-place
28
+ */
29
+ import { SourceFile } from 'ts-morph';
30
+ /**
31
+ * Rewrites every `x.declaration` read on an `EventConfig` in the file to
32
+ * `getDeclaration(x)`.
33
+ *
34
+ * @returns the number of reads rewritten.
35
+ */
36
+ export declare function rewriteDeclarationReads(sourceFile: SourceFile): number;
37
+ declare function main(): Promise<void>;
38
+ export { main };
39
+ //# sourceMappingURL=read-declaration-through-helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-declaration-through-helper.d.ts","sourceRoot":"","sources":["../../src/migrations/read-declaration-through-helper.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAIL,UAAU,EAGX,MAAM,UAAU,CAAA;AA8LjB;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAqDtE;AAED,iBAAe,IAAI,kBA6ClB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
@@ -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/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
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Codemod: replace the removed `incomplete` flag with the NOTIFIED status.
3
+ *
4
+ * Why:
5
+ * `InherentFlags.INCOMPLETE` mirrored the NOTIFIED status and has been
6
+ * removed. See CHANGELOG.md ("The `incomplete` flag is removed").
7
+ *
8
+ * What it does:
9
+ * - In every query object of the form
10
+ * `{ flags: { anyOf: [InherentFlags.INCOMPLETE, ...] }, ... }`, removes
11
+ * the flag (and `anyOf` / `flags` if left empty) and adds
12
+ * `status: { type: 'exact', term: EventStatus.enum.NOTIFIED }`
13
+ * - Imports `EventStatus` next to `InherentFlags` when missing
14
+ * - Lists any other `InherentFlags.INCOMPLETE` reference for manual update
15
+ */
16
+ import { SourceFile } from 'ts-morph';
17
+ /**
18
+ * Rewrites `flags.anyOf` uses of the incomplete flag in place.
19
+ * Returns the line numbers of references it could not rewrite.
20
+ */
21
+ export declare function replaceIncompleteFlag(sourceFile: SourceFile): {
22
+ changed: boolean;
23
+ remaining: number[];
24
+ };
25
+ export declare function main(): Promise<void>;
26
+ //# sourceMappingURL=replace-incomplete-flag.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replace-incomplete-flag.d.ts","sourceRoot":"","sources":["../../src/migrations/replace-incomplete-flag.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAiB,UAAU,EAAc,MAAM,UAAU,CAAA;AAsBhE;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,UAAU;;;EAuD3D;AAED,wBAAsB,IAAI,kBAwBzB"}
@@ -0,0 +1,126 @@
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.replaceIncompleteFlag = replaceIncompleteFlag;
26
+ exports.main = main;
27
+ /**
28
+ * Codemod: replace the removed `incomplete` flag with the NOTIFIED status.
29
+ *
30
+ * Why:
31
+ * `InherentFlags.INCOMPLETE` mirrored the NOTIFIED status and has been
32
+ * removed. See CHANGELOG.md ("The `incomplete` flag is removed").
33
+ *
34
+ * What it does:
35
+ * - In every query object of the form
36
+ * `{ flags: { anyOf: [InherentFlags.INCOMPLETE, ...] }, ... }`, removes
37
+ * the flag (and `anyOf` / `flags` if left empty) and adds
38
+ * `status: { type: 'exact', term: EventStatus.enum.NOTIFIED }`
39
+ * - Imports `EventStatus` next to `InherentFlags` when missing
40
+ * - Lists any other `InherentFlags.INCOMPLETE` reference for manual update
41
+ */
42
+ const ts_morph_1 = require("ts-morph");
43
+ const path_1 = __importDefault(require("path"));
44
+ const INCOMPLETE = 'InherentFlags.INCOMPLETE';
45
+ const NOTIFIED_STATUS = `{ type: 'exact', term: EventStatus.enum.NOTIFIED }`;
46
+ function ensureEventStatusImport(sourceFile) {
47
+ const importDeclaration = sourceFile
48
+ .getImportDeclarations()
49
+ .find((declaration) => declaration
50
+ .getNamedImports()
51
+ .some((named) => named.getName() === 'InherentFlags'));
52
+ const hasEventStatus = importDeclaration === null || importDeclaration === void 0 ? void 0 : importDeclaration.getNamedImports().some((named) => named.getName() === 'EventStatus');
53
+ if (importDeclaration && !hasEventStatus) {
54
+ importDeclaration.addNamedImport('EventStatus');
55
+ }
56
+ }
57
+ /**
58
+ * Rewrites `flags.anyOf` uses of the incomplete flag in place.
59
+ * Returns the line numbers of references it could not rewrite.
60
+ */
61
+ function replaceIncompleteFlag(sourceFile) {
62
+ let changed = false;
63
+ const anyOfIncomplete = sourceFile
64
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAccessExpression)
65
+ .filter((node) => node.getText() === INCOMPLETE)
66
+ .map((node) => node.getParent())
67
+ .filter(ts_morph_1.Node.isArrayLiteralExpression)
68
+ .filter((array) => {
69
+ var _a;
70
+ const anyOf = array.getParent();
71
+ const flags = (_a = anyOf === null || anyOf === void 0 ? void 0 : anyOf.getParent()) === null || _a === void 0 ? void 0 : _a.getParent();
72
+ return (ts_morph_1.Node.isPropertyAssignment(anyOf) &&
73
+ anyOf.getName() === 'anyOf' &&
74
+ ts_morph_1.Node.isPropertyAssignment(flags) &&
75
+ flags.getName() === 'flags');
76
+ });
77
+ for (const array of anyOfIncomplete) {
78
+ const anyOf = array.getParentIfKindOrThrow(ts_morph_1.SyntaxKind.PropertyAssignment);
79
+ const flagsObject = anyOf.getParentIfKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression);
80
+ const flags = flagsObject.getParentIfKindOrThrow(ts_morph_1.SyntaxKind.PropertyAssignment);
81
+ const query = flags.getParentIfKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression);
82
+ array
83
+ .getElements()
84
+ .filter((element) => element.getText() === INCOMPLETE)
85
+ .forEach((element) => array.removeElement(element));
86
+ if (array.getElements().length === 0)
87
+ anyOf.remove();
88
+ if (flagsObject.getProperties().length === 0)
89
+ flags.remove();
90
+ if (!query.getProperty('status')) {
91
+ query.addPropertyAssignment({
92
+ name: 'status',
93
+ initializer: NOTIFIED_STATUS
94
+ });
95
+ }
96
+ changed = true;
97
+ }
98
+ if (changed)
99
+ ensureEventStatusImport(sourceFile);
100
+ const remaining = sourceFile
101
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAccessExpression)
102
+ .filter((node) => node.getText() === INCOMPLETE)
103
+ .map((node) => node.getStartLineNumber());
104
+ return { changed, remaining };
105
+ }
106
+ function main() {
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ const project = new ts_morph_1.Project({
109
+ tsConfigFilePath: path_1.default.join(process.cwd(), 'tsconfig.json')
110
+ });
111
+ const sourceFiles = project
112
+ .getSourceFiles()
113
+ .filter((sf) => !sf.getFilePath().includes('/node_modules/'));
114
+ for (const sourceFile of sourceFiles) {
115
+ const relPath = path_1.default.relative(process.cwd(), sourceFile.getFilePath());
116
+ const { changed, remaining } = replaceIncompleteFlag(sourceFile);
117
+ if (changed) {
118
+ yield sourceFile.save();
119
+ console.log(` [${relPath}] Replaced the incomplete flag with the NOTIFIED status`);
120
+ }
121
+ for (const line of remaining) {
122
+ console.warn(` [${relPath}:${line}] ${INCOMPLETE} was removed, update this reference to use the NOTIFIED status`);
123
+ }
124
+ }
125
+ });
126
+ }