@opencrvs/toolkit 2.1.2 → 2.2.0-rc.19ec10b
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.
- package/build.sh +9 -14
- package/create-countryconfig/package.json +1 -1
- package/dist/application-config/index.js +57 -14
- package/dist/authentication/index.d.ts +2 -1
- package/dist/authentication/index.js +34 -14
- package/dist/cli.js +1491 -2020
- package/dist/commons/api/router.d.ts +1564 -248
- package/dist/commons/application-config/index.d.ts +3 -3
- package/dist/commons/conditionals/conditionals.d.ts +58 -7
- package/dist/commons/conditionals/index.d.ts +1 -0
- package/dist/commons/conditionals/validate.d.ts +6 -1
- package/dist/commons/events/ActionConfig.d.ts +146 -20
- package/dist/commons/events/ActionDocument.d.ts +89 -0
- package/dist/commons/events/ActionInput.d.ts +56 -4
- package/dist/commons/events/ActionStatus.d.ts +2 -2
- package/dist/commons/events/ActionType.d.ts +13 -1
- package/dist/commons/events/AdvancedSearchConfig.d.ts +15 -0
- package/dist/commons/events/AuditLog.d.ts +4 -0
- package/dist/commons/events/CompositeFieldValue.d.ts +5 -1
- package/dist/commons/events/CountryConfigQueryInput.d.ts +192 -18
- package/dist/commons/events/Draft.d.ts +36 -1
- package/dist/commons/events/EventConfig.d.ts +473 -6
- package/dist/commons/events/EventDocument.d.ts +90 -0
- package/dist/commons/events/EventIndex.d.ts +44 -6
- package/dist/commons/events/EventInput.d.ts +1 -0
- package/dist/commons/events/EventMetadata.d.ts +24 -1
- package/dist/commons/events/EventStatus.d.ts +1 -0
- package/dist/commons/events/FieldConfig.d.ts +11 -1
- package/dist/commons/events/FieldTypeMapping.d.ts +17 -17
- package/dist/commons/events/Flag.d.ts +1 -7
- package/dist/commons/events/PlainDate.d.ts +6 -0
- package/dist/commons/events/WorkqueueConfig.d.ts +224 -33
- package/dist/commons/events/defineConfig.d.ts +7 -2
- package/dist/commons/events/event.d.ts +20 -1
- package/dist/commons/events/eventConfigValidation.d.ts +26 -1
- package/dist/commons/events/field.d.ts +37 -0
- package/dist/commons/events/index.d.ts +3 -0
- package/dist/commons/events/locations.d.ts +1 -1
- package/dist/commons/events/scopes.d.ts +4 -1
- package/dist/commons/events/state/flags.d.ts +1 -1
- package/dist/commons/events/state/index.d.ts +11 -2
- package/dist/commons/events/state/utils.d.ts +11 -1
- package/dist/commons/events/test.utils.d.ts +89 -7
- package/dist/commons/events/toNotificationForm.d.ts +17 -0
- package/dist/commons/events/utils.d.ts +90 -10
- package/dist/commons/users/Role.d.ts +23 -0
- package/dist/commons/users/User.d.ts +263 -0
- package/dist/commons/users/authentication.d.ts +67 -0
- package/dist/commons/users/index.d.ts +5 -0
- package/dist/commons/users/roleValidations.d.ts +15 -0
- package/dist/commons/users/scopes.d.ts +1402 -0
- package/dist/commons/users/userReferences.d.ts +176 -0
- package/dist/conditionals/index.js +29 -9
- package/dist/environment/custom-types.d.ts +1 -0
- package/dist/environment/custom-types.d.ts.map +1 -1
- package/dist/environment/derived-variables.d.ts +24 -4
- package/dist/environment/derived-variables.d.ts.map +1 -1
- package/dist/environment/derived-variables.js +27 -4
- package/dist/environment/questions.d.ts +42 -9
- package/dist/environment/questions.d.ts.map +1 -1
- package/dist/environment/questions.js +71 -48
- package/dist/environment/registry.d.ts +15 -0
- package/dist/environment/registry.d.ts.map +1 -0
- package/dist/environment/registry.js +112 -0
- package/dist/environment/setup-environment.d.ts.map +1 -1
- package/dist/environment/setup-environment.js +165 -35
- package/dist/environment/templates.d.ts.map +1 -1
- package/dist/events/index.js +2050 -1156
- package/dist/migrations/add-notify-config.d.ts +40 -0
- package/dist/migrations/add-notify-config.d.ts.map +1 -0
- package/dist/migrations/add-notify-config.js +249 -0
- package/dist/migrations/index.d.ts +9 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/migrations/index.js +1306 -0
- package/dist/migrations/move-declaration-to-declare-action.d.ts +38 -0
- package/dist/migrations/move-declaration-to-declare-action.d.ts.map +1 -0
- package/dist/migrations/move-declaration-to-declare-action.js +260 -0
- package/dist/migrations/{v2.1 → persistent}/add-translations.d.ts +7 -1
- package/dist/migrations/persistent/add-translations.d.ts.map +1 -0
- package/dist/migrations/{v2.1 → persistent}/add-translations.js +14 -12
- package/dist/migrations/persistent/check-upgrade-path.d.ts +36 -0
- package/dist/migrations/persistent/check-upgrade-path.d.ts.map +1 -0
- package/dist/migrations/persistent/check-upgrade-path.js +105 -0
- package/dist/migrations/persistent/update-toolkit-dependency.d.ts +7 -0
- package/dist/migrations/persistent/update-toolkit-dependency.d.ts.map +1 -0
- package/dist/migrations/persistent/update-toolkit-dependency.js +77 -0
- package/dist/migrations/read-declaration-through-helper.d.ts +39 -0
- package/dist/migrations/read-declaration-through-helper.d.ts.map +1 -0
- package/dist/migrations/read-declaration-through-helper.js +199 -0
- package/dist/migrations/replace-incomplete-flag.d.ts +26 -0
- package/dist/migrations/replace-incomplete-flag.d.ts.map +1 -0
- package/dist/migrations/replace-incomplete-flag.js +126 -0
- package/dist/migrations/resolve-versioned-event-config.d.ts +56 -0
- package/dist/migrations/resolve-versioned-event-config.d.ts.map +1 -0
- package/dist/migrations/resolve-versioned-event-config.js +353 -0
- package/dist/migrations/utils.d.ts +61 -0
- package/dist/migrations/utils.d.ts.map +1 -0
- package/dist/migrations/utils.js +218 -0
- package/dist/notification/index.js +1081 -209
- package/dist/roles/index.d.ts +2 -0
- package/dist/roles/index.d.ts.map +1 -0
- package/dist/roles/index.js +730 -0
- package/dist/scopes/index.d.ts +83 -100
- package/dist/scopes/index.js +29 -13
- package/dist/templates/charts-values/dependencies/values.yaml +6 -3
- package/dist/templates/charts-values/opencrvs-services/values.yaml +5 -10
- package/dist/templates/charts-values/traefik/values.yaml +4 -3
- package/dist/verify/endpoints.d.ts +11 -0
- package/dist/verify/endpoints.d.ts.map +1 -1
- package/dist/verify/endpoints.js +21 -27
- package/opencrvs-toolkit-2.2.0-rc.19ec10b.tgz +0 -0
- package/package.json +5 -3
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +3 -5
- package/dist/migrations/v2.1/add-explicit-correction-flags.d.ts +0 -3
- package/dist/migrations/v2.1/add-explicit-correction-flags.d.ts.map +0 -1
- package/dist/migrations/v2.1/add-explicit-correction-flags.js +0 -234
- package/dist/migrations/v2.1/add-recovery-link-notifications.d.ts +0 -3
- package/dist/migrations/v2.1/add-recovery-link-notifications.d.ts.map +0 -1
- package/dist/migrations/v2.1/add-recovery-link-notifications.js +0 -511
- package/dist/migrations/v2.1/add-translations.d.ts.map +0 -1
- package/dist/migrations/v2.1/enable-telemetry.d.ts +0 -3
- package/dist/migrations/v2.1/enable-telemetry.d.ts.map +0 -1
- package/dist/migrations/v2.1/enable-telemetry.js +0 -455
- package/dist/migrations/v2.1/index.d.ts +0 -6
- package/dist/migrations/v2.1/index.d.ts.map +0 -1
- package/dist/migrations/v2.1/index.js +0 -2170
- package/dist/migrations/v2.1/migrate-infrastructure-to-assets.d.ts +0 -14
- package/dist/migrations/v2.1/migrate-infrastructure-to-assets.d.ts.map +0 -1
- package/dist/migrations/v2.1/migrate-infrastructure-to-assets.js +0 -348
- package/dist/migrations/v2.1/remove-metabase-package-installs.d.ts +0 -2
- package/dist/migrations/v2.1/remove-metabase-package-installs.d.ts.map +0 -1
- package/dist/migrations/v2.1/remove-metabase-package-installs.js +0 -161
- package/dist/migrations/v2.1/remove-sentry.d.ts +0 -3
- package/dist/migrations/v2.1/remove-sentry.d.ts.map +0 -1
- package/dist/migrations/v2.1/remove-sentry.js +0 -354
- package/dist/migrations/v2.1/rename-trigger-paths.d.ts +0 -3
- package/dist/migrations/v2.1/rename-trigger-paths.d.ts.map +0 -1
- package/dist/migrations/v2.1/rename-trigger-paths.js +0 -216
- package/dist/migrations/v2.1/template-files.d.ts +0 -8
- package/dist/migrations/v2.1/template-files.d.ts.map +0 -1
- package/dist/migrations/v2.1/template-files.js +0 -49
- package/dist/migrations/v2.1/upgrade-tilt.d.ts +0 -8
- package/dist/migrations/v2.1/upgrade-tilt.d.ts.map +0 -1
- package/dist/migrations/v2.1/upgrade-tilt.js +0 -139
- package/dist/templates/countryconfig-template/.tiltignore +0 -1
- package/dist/templates/countryconfig-template/Dockerfile.assets +0 -7
- package/dist/templates/countryconfig-template/Tiltfile +0 -112
- package/dist/templates/countryconfig-template/assets/README.md +0 -1
- package/dist/templates/countryconfig-template/assets/clear-all-data-dev.sh +0 -61
- package/dist/templates/countryconfig-template/assets/deployment/deploy.sh +0 -455
- package/dist/templates/countryconfig-template/assets/deployment/reindex.sh +0 -168
- package/dist/templates/countryconfig-template/assets/deployment/validate-required-variables-in-compose-files.ts +0 -67
- package/dist/templates/countryconfig-template/assets/elasticsearch/reindex.sh +0 -168
- package/dist/templates/countryconfig-template/assets/metabase/README.md +0 -34
- package/dist/templates/countryconfig-template/assets/metabase/environment-configuration.sql +0 -36
- package/dist/templates/countryconfig-template/assets/metabase/initialize-database.sh +0 -46
- package/dist/templates/countryconfig-template/assets/metabase/metabase.init.db.sql +0 -27737
- package/dist/templates/countryconfig-template/assets/metabase/open-shell.sh +0 -21
- package/dist/templates/countryconfig-template/assets/metabase/package.json +0 -13
- package/dist/templates/countryconfig-template/assets/metabase/run-dev.sh +0 -108
- package/dist/templates/countryconfig-template/assets/metabase/run.sh +0 -120
- package/dist/templates/countryconfig-template/assets/metabase/update-database.sh +0 -40
- package/dist/templates/countryconfig-template/assets/postgres/setup-analytics.sh +0 -135
- package/dist/templates/countryconfig-template/assets/postgres/setup-reference-data.sh +0 -108
- package/dist/templates/countryconfig-template/tilt/clear-all-data.ps1 +0 -50
- package/dist/templates/countryconfig-template/tilt/clear-all-data.sh +0 -35
- package/dist/templates/countryconfig-template/tilt/common.tilt +0 -145
- package/dist/templates/countryconfig-template/tilt/examples/dependencies/values.yaml +0 -15
- package/dist/templates/countryconfig-template/tilt/examples/opencrvs-services/values.yaml +0 -56
- package/dist/templates/countryconfig-template/tilt/examples/traefik/values-minikube.yaml +0 -21
- package/dist/templates/countryconfig-template/tilt/examples/traefik/values.yaml +0 -38
- package/dist/templates/countryconfig-template/tilt/helm/dependencies/values.yaml +0 -17
- package/dist/templates/countryconfig-template/tilt/helm/opencrvs-services/values.yaml +0 -16
- package/dist/templates/countryconfig-template/tilt/lib.tilt +0 -95
- package/dist/templates/countryconfig-template/tilt/opencrvs.tilt +0 -136
- package/opencrvs-toolkit-2.1.2.tgz +0 -0
|
@@ -0,0 +1,1306 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/migrations/index.ts
|
|
32
|
+
var migrations_exports = {};
|
|
33
|
+
__export(migrations_exports, {
|
|
34
|
+
runUpgrade: () => runUpgrade
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(migrations_exports);
|
|
37
|
+
|
|
38
|
+
// src/migrations/persistent/add-translations.ts
|
|
39
|
+
var import_path = __toESM(require("path"));
|
|
40
|
+
|
|
41
|
+
// src/csv.ts
|
|
42
|
+
var import_fs = require("fs");
|
|
43
|
+
function parseCsvLine(line) {
|
|
44
|
+
const values = [];
|
|
45
|
+
let value = "";
|
|
46
|
+
let quoted = false;
|
|
47
|
+
for (let index = 0; index < line.length; index++) {
|
|
48
|
+
const character = line[index];
|
|
49
|
+
if (quoted) {
|
|
50
|
+
if (character !== '"') {
|
|
51
|
+
value += character;
|
|
52
|
+
} else if (line[index + 1] === '"') {
|
|
53
|
+
value += '"';
|
|
54
|
+
index++;
|
|
55
|
+
} else {
|
|
56
|
+
quoted = false;
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (character === '"') {
|
|
61
|
+
quoted = true;
|
|
62
|
+
} else if (character === ",") {
|
|
63
|
+
values.push(value);
|
|
64
|
+
value = "";
|
|
65
|
+
} else {
|
|
66
|
+
value += character;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
values.push(value);
|
|
70
|
+
return values;
|
|
71
|
+
}
|
|
72
|
+
function toCsvLine(values) {
|
|
73
|
+
return values.map(
|
|
74
|
+
(value) => /["\n,]/.test(value) ? `"${value.replace(/"/g, '""')}"` : value
|
|
75
|
+
).join(",");
|
|
76
|
+
}
|
|
77
|
+
function idOf(line) {
|
|
78
|
+
const comma = line.indexOf(",");
|
|
79
|
+
return comma === -1 ? line : line.slice(0, comma);
|
|
80
|
+
}
|
|
81
|
+
function readCsvFile(path9) {
|
|
82
|
+
if (!(0, import_fs.existsSync)(path9)) {
|
|
83
|
+
return void 0;
|
|
84
|
+
}
|
|
85
|
+
const contents = (0, import_fs.readFileSync)(path9, "utf8");
|
|
86
|
+
const newline = contents.includes("\r\n") ? "\r\n" : "\n";
|
|
87
|
+
const trailingNewline = /\r?\n$/.test(contents);
|
|
88
|
+
const lines = contents.replace(/\r?\n$/, "").split(/\r?\n/);
|
|
89
|
+
const body = lines.slice(1).filter((line) => line !== "");
|
|
90
|
+
return { header: lines[0], body, newline, trailingNewline };
|
|
91
|
+
}
|
|
92
|
+
function writeCsvFile(path9, file) {
|
|
93
|
+
(0, import_fs.writeFileSync)(
|
|
94
|
+
path9,
|
|
95
|
+
[file.header, ...file.body].join(file.newline) + (file.trailingNewline ? file.newline : "")
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
function addRows(file, rows) {
|
|
99
|
+
const isSorted = file.body.every(
|
|
100
|
+
(line, index) => index === 0 || idOf(file.body[index - 1]) <= idOf(line)
|
|
101
|
+
);
|
|
102
|
+
const added = [];
|
|
103
|
+
for (const row of rows) {
|
|
104
|
+
const id = idOf(row);
|
|
105
|
+
if (file.body.some((line) => idOf(line) === id)) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const insertAt2 = isSorted ? file.body.findIndex((line) => idOf(line) > id) : -1;
|
|
109
|
+
if (insertAt2 === -1) {
|
|
110
|
+
file.body.push(row);
|
|
111
|
+
} else {
|
|
112
|
+
file.body.splice(insertAt2, 0, row);
|
|
113
|
+
}
|
|
114
|
+
added.push(id);
|
|
115
|
+
}
|
|
116
|
+
return added;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/translations/template.ts
|
|
120
|
+
var REPOSITORY = "opencrvs/opencrvs-core";
|
|
121
|
+
var TEMPLATE_TRANSLATIONS = "packages/countryconfig-template/src/translations";
|
|
122
|
+
async function matchingRefs(namespace, prefix) {
|
|
123
|
+
const url = `https://api.github.com/repos/${REPOSITORY}/git/matching-refs/${namespace}/${prefix}`;
|
|
124
|
+
const response = await fetch(url, {
|
|
125
|
+
headers: { accept: "application/vnd.github+json" }
|
|
126
|
+
});
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`GitHub answered ${response.status} ${response.statusText} for ${url}`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
const refs = await response.json();
|
|
133
|
+
const patchOf = (ref) => Number(ref.split(".").pop()) || 0;
|
|
134
|
+
return refs.map(({ ref }) => ref.replace(/^refs\/(heads|tags)\//, "")).sort((a, b) => patchOf(b) - patchOf(a));
|
|
135
|
+
}
|
|
136
|
+
async function candidateRefs(version) {
|
|
137
|
+
return [
|
|
138
|
+
...await matchingRefs("heads", `release/${version}.`),
|
|
139
|
+
...await matchingRefs("tags", `v${version}.`),
|
|
140
|
+
"develop"
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
async function fetchTemplate(refs, application) {
|
|
144
|
+
for (const ref of refs) {
|
|
145
|
+
const url = `https://raw.githubusercontent.com/${REPOSITORY}/${ref}/${TEMPLATE_TRANSLATIONS}/${application}.csv`;
|
|
146
|
+
const response = await fetch(url);
|
|
147
|
+
if (response.ok) {
|
|
148
|
+
return { ref, contents: await response.text() };
|
|
149
|
+
}
|
|
150
|
+
if (response.status !== 404) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`GitHub answered ${response.status} ${response.statusText} for ${url}`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return void 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/migrations/persistent/add-translations.ts
|
|
160
|
+
var APPLICATIONS = ["client", "login"];
|
|
161
|
+
var skipped = [];
|
|
162
|
+
function warnSkipped(message) {
|
|
163
|
+
skipped.push(message);
|
|
164
|
+
console.warn(` \u26A0\uFE0F ${message}`);
|
|
165
|
+
}
|
|
166
|
+
function rowsToAdd(local, template) {
|
|
167
|
+
const localColumns = parseCsvLine(local.header);
|
|
168
|
+
const templateColumns = parseCsvLine(template.header);
|
|
169
|
+
return template.body.filter((line) => line !== "").map((line) => {
|
|
170
|
+
const values = parseCsvLine(line);
|
|
171
|
+
return toCsvLine(
|
|
172
|
+
localColumns.map((column) => {
|
|
173
|
+
const index = templateColumns.indexOf(column);
|
|
174
|
+
return index === -1 ? "" : values[index] ?? "";
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async function updateApplication(cwd, targetVersion, refs, application) {
|
|
180
|
+
const relativePath = `src/translations/${application}.csv`;
|
|
181
|
+
const local = readCsvFile(import_path.default.join(cwd, relativePath));
|
|
182
|
+
if (!local) {
|
|
183
|
+
warnSkipped(`${relativePath} not found; translations not added`);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const fetched = await fetchTemplate(refs, application);
|
|
187
|
+
if (!fetched) {
|
|
188
|
+
warnSkipped(
|
|
189
|
+
`No ${application}.csv found in the ${targetVersion} country config template on GitHub; ${application}.csv not updated`
|
|
190
|
+
);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const template = readTemplate(fetched.contents);
|
|
194
|
+
const added = addRows(local, rowsToAdd(local, template));
|
|
195
|
+
if (added.length === 0) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
writeCsvFile(import_path.default.join(cwd, relativePath), local);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
warnSkipped(
|
|
202
|
+
`Could not write ${relativePath} (${error.message}); translations not added`
|
|
203
|
+
);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
for (const id of added) {
|
|
207
|
+
console.log(` \u2713 ${relativePath}: ${id}`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function readTemplate(contents) {
|
|
211
|
+
const lines = contents.replace(/\r?\n$/, "").split(/\r?\n/);
|
|
212
|
+
return {
|
|
213
|
+
header: lines[0],
|
|
214
|
+
body: lines.slice(1),
|
|
215
|
+
newline: "\n",
|
|
216
|
+
trailingNewline: true
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async function listRefs(targetVersion) {
|
|
220
|
+
try {
|
|
221
|
+
return await candidateRefs(targetVersion);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
warnSkipped(
|
|
224
|
+
`Could not list the ${targetVersion} refs on GitHub (${error.message}); translations not added`
|
|
225
|
+
);
|
|
226
|
+
return void 0;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async function main(targetVersion) {
|
|
230
|
+
const cwd = process.cwd();
|
|
231
|
+
console.log("Adding the translation keys core gained this version...\n");
|
|
232
|
+
const refs = await listRefs(targetVersion);
|
|
233
|
+
if (refs) {
|
|
234
|
+
for (const application of APPLICATIONS) {
|
|
235
|
+
try {
|
|
236
|
+
await updateApplication(cwd, targetVersion, refs, application);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
warnSkipped(
|
|
239
|
+
`Could not read ${application}.csv from the country config template on GitHub (${error.message}); ${application}.csv not updated`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (skipped.length > 0) {
|
|
245
|
+
console.warn(
|
|
246
|
+
`
|
|
247
|
+
\u26A0\uFE0F ${skipped.length} step(s) were skipped. Add the missing translations by hand before upgrading:`
|
|
248
|
+
);
|
|
249
|
+
for (const message of skipped) {
|
|
250
|
+
console.warn(` - ${message}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/migrations/move-declaration-to-declare-action.ts
|
|
256
|
+
var import_ts_morph2 = require("ts-morph");
|
|
257
|
+
var import_path3 = __toESM(require("path"));
|
|
258
|
+
|
|
259
|
+
// src/migrations/utils.ts
|
|
260
|
+
var import_ts_morph = require("ts-morph");
|
|
261
|
+
var import_path2 = __toESM(require("path"));
|
|
262
|
+
var DEFINE_CONFIG_NAME = "defineConfig";
|
|
263
|
+
var TOOLKIT_EVENTS_MODULE = "@opencrvs/toolkit/events";
|
|
264
|
+
function location(node) {
|
|
265
|
+
const relPath = import_path2.default.relative(
|
|
266
|
+
process.cwd(),
|
|
267
|
+
node.getSourceFile().getFilePath()
|
|
268
|
+
);
|
|
269
|
+
return `${relPath}:${node.getStartLineNumber()}`;
|
|
270
|
+
}
|
|
271
|
+
function isToolkitDefineConfigCall(call) {
|
|
272
|
+
const symbol = call.getExpression().getSymbol();
|
|
273
|
+
const resolved = symbol?.getAliasedSymbol() ?? symbol;
|
|
274
|
+
if (resolved?.getName() !== DEFINE_CONFIG_NAME) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
return resolved.getDeclarations().some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
278
|
+
}
|
|
279
|
+
function findProperty(literal, name) {
|
|
280
|
+
const property = literal.getProperty(name);
|
|
281
|
+
return import_ts_morph.Node.isPropertyAssignment(property) || import_ts_morph.Node.isShorthandPropertyAssignment(property) ? property : void 0;
|
|
282
|
+
}
|
|
283
|
+
function mayHaveActionType(actionType, node, expectedActionType) {
|
|
284
|
+
const typeProperty = actionType.getProperty("type");
|
|
285
|
+
if (!typeProperty) {
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
const type = typeProperty.getTypeAtLocation(node);
|
|
289
|
+
const values = type.isUnion() ? type.getUnionTypes() : [type];
|
|
290
|
+
return !values.every(
|
|
291
|
+
(value) => value.isStringLiteral() && value.getLiteralValue() !== expectedActionType
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
function mayBeAction(node, actionType) {
|
|
295
|
+
if (import_ts_morph.Node.isObjectLiteralExpression(node)) {
|
|
296
|
+
return isAction(node, actionType);
|
|
297
|
+
}
|
|
298
|
+
if (import_ts_morph.Node.isSpreadElement(node)) {
|
|
299
|
+
const arrayType = node.getExpression().getType();
|
|
300
|
+
const elementTypes = arrayType.isTuple() ? arrayType.getTupleElements() : [arrayType.getArrayElementType()];
|
|
301
|
+
return elementTypes.some(
|
|
302
|
+
(elementType) => !elementType || mayHaveActionType(elementType, node, actionType)
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
return mayHaveActionType(node.getType(), node, actionType);
|
|
306
|
+
}
|
|
307
|
+
function isAction(literal, actionType) {
|
|
308
|
+
const type = findProperty(literal, "type");
|
|
309
|
+
return import_ts_morph.Node.isPropertyAssignment(type) && type.getInitializerOrThrow().getType().getLiteralValue() === actionType;
|
|
310
|
+
}
|
|
311
|
+
function indentationOf(node) {
|
|
312
|
+
const text = node.getSourceFile().getFullText();
|
|
313
|
+
const lineStart = text.lastIndexOf("\n", node.getStart() - 1) + 1;
|
|
314
|
+
return /^[ \t]*/.exec(text.slice(lineStart))?.[0] ?? "";
|
|
315
|
+
}
|
|
316
|
+
function restOfLine(text, position) {
|
|
317
|
+
const lineEnd = text.indexOf("\n", position);
|
|
318
|
+
return text.slice(position, lineEnd === -1 ? text.length : lineEnd);
|
|
319
|
+
}
|
|
320
|
+
function isOnlyComments(text) {
|
|
321
|
+
return /^\s*(?:\/\/.*|\/\*.*?\*\/\s*)*$/.test(text);
|
|
322
|
+
}
|
|
323
|
+
function layoutOf(property) {
|
|
324
|
+
const text = property.getSourceFile().getFullText();
|
|
325
|
+
const next = property.getNextSibling();
|
|
326
|
+
const hasComma = next?.getKind() === import_ts_morph.SyntaxKind.CommaToken;
|
|
327
|
+
const end = next && hasComma ? next.getEnd() : property.getEnd();
|
|
328
|
+
const trailing = restOfLine(text, end);
|
|
329
|
+
const lineBreak = text.slice(property.getFullStart(), property.getStart()).indexOf("\n");
|
|
330
|
+
if (lineBreak === -1 || !isOnlyComments(trailing)) {
|
|
331
|
+
return {
|
|
332
|
+
ownsLines: false,
|
|
333
|
+
start: property.getStart(true),
|
|
334
|
+
end,
|
|
335
|
+
hasComma,
|
|
336
|
+
trailingComment: ""
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
const firstLine = property.getFullStart() + lineBreak + 1;
|
|
340
|
+
return {
|
|
341
|
+
ownsLines: true,
|
|
342
|
+
start: firstLine + (/^\s*/.exec(text.slice(firstLine))?.[0].length ?? 0),
|
|
343
|
+
end,
|
|
344
|
+
hasComma,
|
|
345
|
+
trailingComment: trailing.trimEnd()
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function insertAt(position, text) {
|
|
349
|
+
return { start: position, end: position, text };
|
|
350
|
+
}
|
|
351
|
+
function applyTextEdits(sourceFile, edits) {
|
|
352
|
+
const text = [...edits].sort((a, b) => b.start - a.start).reduce(
|
|
353
|
+
(result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end),
|
|
354
|
+
sourceFile.getFullText()
|
|
355
|
+
);
|
|
356
|
+
sourceFile.replaceWithText(text);
|
|
357
|
+
}
|
|
358
|
+
function findToolkitImport(sourceFile) {
|
|
359
|
+
return sourceFile.getImportDeclaration(
|
|
360
|
+
(declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE && !declaration.isTypeOnly() && !declaration.getNamespaceImport()
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
function findToolkitImportName(sourceFile, name) {
|
|
364
|
+
const imported = findToolkitImport(sourceFile)?.getNamedImports().find((namedImport) => namedImport.getName() === name);
|
|
365
|
+
if (imported) {
|
|
366
|
+
return imported.getAliasNode()?.getText() ?? name;
|
|
367
|
+
}
|
|
368
|
+
if (sourceFile.getLocal(name)) {
|
|
369
|
+
return void 0;
|
|
370
|
+
}
|
|
371
|
+
return name;
|
|
372
|
+
}
|
|
373
|
+
function addToolkitImport(sourceFile, name) {
|
|
374
|
+
const toolkitImport = findToolkitImport(sourceFile);
|
|
375
|
+
if (!toolkitImport) {
|
|
376
|
+
const imports = sourceFile.getImportDeclarations();
|
|
377
|
+
const lastImport = imports[imports.length - 1];
|
|
378
|
+
sourceFile.insertStatements(
|
|
379
|
+
lastImport ? lastImport.getChildIndex() + 1 : 0,
|
|
380
|
+
`import { ${name} } from '${TOOLKIT_EVENTS_MODULE}'`
|
|
381
|
+
);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const alreadyImported = toolkitImport.getNamedImports().some((namedImport) => namedImport.getName() === name);
|
|
385
|
+
if (alreadyImported) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const namedImports = toolkitImport.getNamedImports();
|
|
389
|
+
if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
|
|
390
|
+
toolkitImport.addNamedImport(name);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const names = [
|
|
394
|
+
...namedImports.map((namedImport) => namedImport.getText()),
|
|
395
|
+
name
|
|
396
|
+
];
|
|
397
|
+
const specifiers = toolkitImport.getText().includes("\n") ? `{
|
|
398
|
+
${names.join(",\n ")}
|
|
399
|
+
}` : `{ ${names.join(", ")} }`;
|
|
400
|
+
toolkitImport.replaceWithText(
|
|
401
|
+
`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE}'`
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/migrations/move-declaration-to-declare-action.ts
|
|
406
|
+
var DEFINE_CONFIG_NAME2 = "defineConfig";
|
|
407
|
+
var DECLARATION_PROPERTY_NAME = "declaration";
|
|
408
|
+
var ACTIONS_PROPERTY_NAME = "actions";
|
|
409
|
+
var DECLARE_ACTION_TYPE = "DECLARE";
|
|
410
|
+
function findDeclareAction(config) {
|
|
411
|
+
const actions = findProperty(config, ACTIONS_PROPERTY_NAME);
|
|
412
|
+
const actionsValue = import_ts_morph2.Node.isPropertyAssignment(actions) ? actions.getInitializer() : void 0;
|
|
413
|
+
if (!import_ts_morph2.Node.isArrayLiteralExpression(actionsValue)) {
|
|
414
|
+
console.warn(
|
|
415
|
+
` [${location(config)}] Skipped: \`${ACTIONS_PROPERTY_NAME}\` is not an array literal. Move \`${DECLARATION_PROPERTY_NAME}\` onto the ${DECLARE_ACTION_TYPE} action by hand.`
|
|
416
|
+
);
|
|
417
|
+
return void 0;
|
|
418
|
+
}
|
|
419
|
+
const candidates = actionsValue.getElements().filter((element) => mayBeAction(element, DECLARE_ACTION_TYPE));
|
|
420
|
+
if (candidates.length !== 1 || !import_ts_morph2.Node.isObjectLiteralExpression(candidates[0])) {
|
|
421
|
+
console.warn(
|
|
422
|
+
` [${location(config)}] Skipped: could not find exactly one ${DECLARE_ACTION_TYPE} action written as an object literal in \`${ACTIONS_PROPERTY_NAME}\`. Move \`${DECLARATION_PROPERTY_NAME}\` onto it by hand.`
|
|
423
|
+
);
|
|
424
|
+
return void 0;
|
|
425
|
+
}
|
|
426
|
+
const declareAction = candidates[0];
|
|
427
|
+
if (findProperty(declareAction, DECLARATION_PROPERTY_NAME)) {
|
|
428
|
+
console.warn(
|
|
429
|
+
` [${location(config)}] Skipped: \`${DECLARATION_PROPERTY_NAME}\` is set both at the top level and on the ${DECLARE_ACTION_TYPE} action. Keep only the one on the action.`
|
|
430
|
+
);
|
|
431
|
+
return void 0;
|
|
432
|
+
}
|
|
433
|
+
return declareAction;
|
|
434
|
+
}
|
|
435
|
+
function insertIntoDeclareAction(declareAction, property) {
|
|
436
|
+
const anchor = declareAction.getProperty("label") ?? declareAction.getPropertyOrThrow("type");
|
|
437
|
+
const text = property.getSourceFile().getFullText();
|
|
438
|
+
const anchorLayout = layoutOf(anchor);
|
|
439
|
+
const layout = layoutOf(property);
|
|
440
|
+
const indent = indentationOf(anchor);
|
|
441
|
+
const oldIndent = indentationOf(property);
|
|
442
|
+
const reindent = (lines) => lines.split("\n").map(
|
|
443
|
+
(line, index) => index > 0 && line.startsWith(oldIndent) ? indent + line.slice(oldIndent.length) : line
|
|
444
|
+
).join("\n");
|
|
445
|
+
if (anchorLayout.ownsLines) {
|
|
446
|
+
const moved = reindent(text.slice(layout.start, property.getEnd()));
|
|
447
|
+
const position = anchorLayout.end + restOfLine(text, anchorLayout.end).length;
|
|
448
|
+
const insertion = `
|
|
449
|
+
${indent}${moved}${anchorLayout.hasComma ? "," : ""}${layout.trailingComment}`;
|
|
450
|
+
if (anchorLayout.hasComma) {
|
|
451
|
+
return [insertAt(position, insertion)];
|
|
452
|
+
}
|
|
453
|
+
return position === anchor.getEnd() ? [insertAt(position, `,${insertion}`)] : [insertAt(anchor.getEnd(), ","), insertAt(position, insertion)];
|
|
454
|
+
}
|
|
455
|
+
const droppedComments = text.slice(layout.start, property.getStart(true)).trim() !== "" || layout.trailingComment !== "";
|
|
456
|
+
if (droppedComments) {
|
|
457
|
+
console.warn(
|
|
458
|
+
` [${location(property)}] The comments next to \`${DECLARATION_PROPERTY_NAME}\` were not moved with it. Move them onto the ${DECLARE_ACTION_TYPE} action by hand.`
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
const isMultiLine = declareAction.getText().includes("\n");
|
|
462
|
+
const separator = isMultiLine ? `,
|
|
463
|
+
${indent}` : ", ";
|
|
464
|
+
return [
|
|
465
|
+
insertAt(anchor.getEnd(), `${separator}${reindent(property.getText(true))}`)
|
|
466
|
+
];
|
|
467
|
+
}
|
|
468
|
+
function removeFromConfig(property) {
|
|
469
|
+
const text = property.getSourceFile().getFullText();
|
|
470
|
+
const layout = layoutOf(property);
|
|
471
|
+
const previousComma = property.getPreviousSiblings().find((sibling) => !isOnlyComments(sibling.getText()))?.asKind(import_ts_morph2.SyntaxKind.CommaToken);
|
|
472
|
+
if (layout.ownsLines) {
|
|
473
|
+
const lines = {
|
|
474
|
+
start: text.lastIndexOf("\n", layout.start - 1),
|
|
475
|
+
end: layout.end + restOfLine(text, layout.end).length,
|
|
476
|
+
text: ""
|
|
477
|
+
};
|
|
478
|
+
return !layout.hasComma && previousComma ? [
|
|
479
|
+
lines,
|
|
480
|
+
{
|
|
481
|
+
start: previousComma.getStart(),
|
|
482
|
+
end: previousComma.getEnd(),
|
|
483
|
+
text: ""
|
|
484
|
+
}
|
|
485
|
+
] : [lines];
|
|
486
|
+
}
|
|
487
|
+
if (layout.hasComma) {
|
|
488
|
+
return [{ start: property.getFullStart(), end: layout.end, text: "" }];
|
|
489
|
+
}
|
|
490
|
+
return [
|
|
491
|
+
{
|
|
492
|
+
start: (previousComma ?? property).getStart(),
|
|
493
|
+
end: property.getEnd(),
|
|
494
|
+
text: ""
|
|
495
|
+
}
|
|
496
|
+
];
|
|
497
|
+
}
|
|
498
|
+
function findConfigLiteral(call) {
|
|
499
|
+
const config = call.getArguments()[0];
|
|
500
|
+
if (!config || import_ts_morph2.Node.isObjectLiteralExpression(config)) {
|
|
501
|
+
return config;
|
|
502
|
+
}
|
|
503
|
+
if (config.getType().getProperty(DECLARATION_PROPERTY_NAME)) {
|
|
504
|
+
console.warn(
|
|
505
|
+
` [${location(config)}] Skipped: \`${DEFINE_CONFIG_NAME2}\` is not given an object literal. If its config has a top-level \`${DECLARATION_PROPERTY_NAME}\`, move it onto the ${DECLARE_ACTION_TYPE} action by hand.`
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
return void 0;
|
|
509
|
+
}
|
|
510
|
+
function moveDeclarationToDeclareAction(sourceFile) {
|
|
511
|
+
const configs = sourceFile.getDescendantsOfKind(import_ts_morph2.SyntaxKind.CallExpression).filter(isToolkitDefineConfigCall).map(findConfigLiteral).filter(import_ts_morph2.Node.isObjectLiteralExpression);
|
|
512
|
+
const edits = [];
|
|
513
|
+
let changes = 0;
|
|
514
|
+
for (const config of configs) {
|
|
515
|
+
const declaration = findProperty(config, DECLARATION_PROPERTY_NAME);
|
|
516
|
+
if (!declaration) {
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
const declareAction = findDeclareAction(config);
|
|
520
|
+
if (!declareAction) {
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
console.log(
|
|
524
|
+
` [${location(declaration)}] Moved \`${declaration.getText()}\` onto the ${DECLARE_ACTION_TYPE} action`
|
|
525
|
+
);
|
|
526
|
+
edits.push(
|
|
527
|
+
...insertIntoDeclareAction(declareAction, declaration),
|
|
528
|
+
...removeFromConfig(declaration)
|
|
529
|
+
);
|
|
530
|
+
changes++;
|
|
531
|
+
}
|
|
532
|
+
if (changes === 0) {
|
|
533
|
+
return 0;
|
|
534
|
+
}
|
|
535
|
+
applyTextEdits(sourceFile, edits);
|
|
536
|
+
return changes;
|
|
537
|
+
}
|
|
538
|
+
async function main2() {
|
|
539
|
+
const srcDir = import_path3.default.join(process.cwd(), "src");
|
|
540
|
+
console.log(`Scanning for top-level event declarations in: ${srcDir}
|
|
541
|
+
`);
|
|
542
|
+
const project = new import_ts_morph2.Project({
|
|
543
|
+
tsConfigFilePath: import_path3.default.resolve(srcDir, "../tsconfig.json"),
|
|
544
|
+
skipAddingFilesFromTsConfig: false
|
|
545
|
+
});
|
|
546
|
+
const sourceFiles = project.getSourceFiles().filter((sf) => {
|
|
547
|
+
const fp = sf.getFilePath();
|
|
548
|
+
return fp.includes("/src/") && !fp.includes("/node_modules/");
|
|
549
|
+
});
|
|
550
|
+
console.log(`Found ${sourceFiles.length} source file(s) to analyse.
|
|
551
|
+
`);
|
|
552
|
+
let totalChanges = 0;
|
|
553
|
+
const modifiedFiles = [];
|
|
554
|
+
for (const sourceFile of sourceFiles) {
|
|
555
|
+
const changes = moveDeclarationToDeclareAction(sourceFile);
|
|
556
|
+
if (changes > 0) {
|
|
557
|
+
totalChanges += changes;
|
|
558
|
+
modifiedFiles.push(sourceFile);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (modifiedFiles.length === 0) {
|
|
562
|
+
console.log("No top-level event declarations found. Nothing to do.");
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
console.log(`
|
|
566
|
+
Saving ${modifiedFiles.length} modified file(s)...`);
|
|
567
|
+
for (const sourceFile of modifiedFiles) {
|
|
568
|
+
await sourceFile.save();
|
|
569
|
+
console.log(
|
|
570
|
+
` Saved: ${import_path3.default.relative(process.cwd(), sourceFile.getFilePath())}`
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
console.log(
|
|
574
|
+
`
|
|
575
|
+
Done. Moved ${totalChanges} declaration(s) across ${modifiedFiles.length} file(s).`
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// src/migrations/add-notify-config.ts
|
|
580
|
+
var import_ts_morph3 = require("ts-morph");
|
|
581
|
+
var import_path4 = __toESM(require("path"));
|
|
582
|
+
var DEFINE_CONFIG_NAME3 = "defineConfig";
|
|
583
|
+
var ACTIONS_PROPERTY_NAME2 = "actions";
|
|
584
|
+
var DECLARATION_PROPERTY_NAME2 = "declaration";
|
|
585
|
+
var NOTIFY_ACTION_TYPE = "NOTIFY";
|
|
586
|
+
var DECLARE_ACTION_TYPE2 = "DECLARE";
|
|
587
|
+
var NOTIFY_PROPERTIES = [
|
|
588
|
+
{
|
|
589
|
+
name: "notificationForm",
|
|
590
|
+
helper: "toNotificationForm",
|
|
591
|
+
declareProperty: DECLARATION_PROPERTY_NAME2
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
name: "review",
|
|
595
|
+
helper: "toNotificationReview",
|
|
596
|
+
declareProperty: "review"
|
|
597
|
+
}
|
|
598
|
+
];
|
|
599
|
+
function referenceOf(property) {
|
|
600
|
+
if (import_ts_morph3.Node.isShorthandPropertyAssignment(property)) {
|
|
601
|
+
return property.getName();
|
|
602
|
+
}
|
|
603
|
+
const value = property?.getInitializer();
|
|
604
|
+
return import_ts_morph3.Node.isIdentifier(value) || import_ts_morph3.Node.isPropertyAccessExpression(value) ? value.getText() : void 0;
|
|
605
|
+
}
|
|
606
|
+
function findConfigLiteral2(call) {
|
|
607
|
+
const config = call.getArguments()[0];
|
|
608
|
+
if (import_ts_morph3.Node.isObjectLiteralExpression(config)) {
|
|
609
|
+
return config;
|
|
610
|
+
}
|
|
611
|
+
console.warn(
|
|
612
|
+
` [${location(call)}] Skipped: \`${DEFINE_CONFIG_NAME3}\` is not given an object literal. If the event has a NOTIFY action, give it a \`notificationForm\` and \`review\` by hand.`
|
|
613
|
+
);
|
|
614
|
+
return void 0;
|
|
615
|
+
}
|
|
616
|
+
function findNotifyAction(config, actions) {
|
|
617
|
+
const candidates = actions.filter(
|
|
618
|
+
(action) => mayBeAction(action, NOTIFY_ACTION_TYPE)
|
|
619
|
+
);
|
|
620
|
+
if (candidates.length === 0) {
|
|
621
|
+
console.warn(
|
|
622
|
+
` [${location(config)}] No ${NOTIFY_ACTION_TYPE} action: the event cannot be notified. If a role has \`record.notify\` for it, add a ${NOTIFY_ACTION_TYPE} action with a \`notificationForm\` and \`review\`.`
|
|
623
|
+
);
|
|
624
|
+
return void 0;
|
|
625
|
+
}
|
|
626
|
+
if (candidates.length !== 1 || !import_ts_morph3.Node.isObjectLiteralExpression(candidates[0])) {
|
|
627
|
+
console.warn(
|
|
628
|
+
` [${location(config)}] Skipped: could not find exactly one ${NOTIFY_ACTION_TYPE} action written as an object literal in \`${ACTIONS_PROPERTY_NAME2}\`. Give it a \`notificationForm\` and \`review\` by hand.`
|
|
629
|
+
);
|
|
630
|
+
return void 0;
|
|
631
|
+
}
|
|
632
|
+
return candidates[0];
|
|
633
|
+
}
|
|
634
|
+
function insertIntoAction(action, properties) {
|
|
635
|
+
const anchor = action.getProperty("label") ?? action.getPropertyOrThrow("type");
|
|
636
|
+
const text = action.getSourceFile().getFullText();
|
|
637
|
+
const anchorLayout = layoutOf(anchor);
|
|
638
|
+
const indent = indentationOf(anchor);
|
|
639
|
+
if (anchorLayout.ownsLines) {
|
|
640
|
+
const position = anchorLayout.end + restOfLine(text, anchorLayout.end).length;
|
|
641
|
+
const lines = properties.map((property) => `
|
|
642
|
+
${indent}${property}`);
|
|
643
|
+
if (anchorLayout.hasComma) {
|
|
644
|
+
return [insertAt(position, `${lines.join(",")},`)];
|
|
645
|
+
}
|
|
646
|
+
return position === anchor.getEnd() ? [insertAt(position, `,${lines.join(",")}`)] : [insertAt(anchor.getEnd(), ","), insertAt(position, lines.join(","))];
|
|
647
|
+
}
|
|
648
|
+
const separator = action.getText().includes("\n") ? `,
|
|
649
|
+
${indent}` : ", ";
|
|
650
|
+
return [
|
|
651
|
+
insertAt(
|
|
652
|
+
anchor.getEnd(),
|
|
653
|
+
properties.map((property) => `${separator}${property}`).join("")
|
|
654
|
+
)
|
|
655
|
+
];
|
|
656
|
+
}
|
|
657
|
+
function addNotifyConfig(sourceFile) {
|
|
658
|
+
const configs = sourceFile.getDescendantsOfKind(import_ts_morph3.SyntaxKind.CallExpression).filter(isToolkitDefineConfigCall).map(findConfigLiteral2).filter(import_ts_morph3.Node.isObjectLiteralExpression);
|
|
659
|
+
const helperNames = Object.fromEntries(
|
|
660
|
+
NOTIFY_PROPERTIES.map(({ helper }) => [
|
|
661
|
+
helper,
|
|
662
|
+
findToolkitImportName(sourceFile, helper)
|
|
663
|
+
])
|
|
664
|
+
);
|
|
665
|
+
const edits = [];
|
|
666
|
+
const usedHelpers = /* @__PURE__ */ new Set();
|
|
667
|
+
let changes = 0;
|
|
668
|
+
for (const config of configs) {
|
|
669
|
+
const actions = findProperty(config, ACTIONS_PROPERTY_NAME2);
|
|
670
|
+
const actionsValue = import_ts_morph3.Node.isPropertyAssignment(actions) ? actions.getInitializer() : void 0;
|
|
671
|
+
if (!import_ts_morph3.Node.isArrayLiteralExpression(actionsValue)) {
|
|
672
|
+
console.warn(
|
|
673
|
+
` [${location(config)}] Skipped: \`${ACTIONS_PROPERTY_NAME2}\` is not an array literal. If the event has a ${NOTIFY_ACTION_TYPE} action, give it a \`notificationForm\` and \`review\` by hand.`
|
|
674
|
+
);
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
const notifyAction = findNotifyAction(config, actionsValue.getElements());
|
|
678
|
+
if (!notifyAction) {
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
const declareAction = actionsValue.getElements().find(
|
|
682
|
+
(action) => import_ts_morph3.Node.isObjectLiteralExpression(action) && isAction(action, DECLARE_ACTION_TYPE2)
|
|
683
|
+
);
|
|
684
|
+
const properties = NOTIFY_PROPERTIES.filter(
|
|
685
|
+
({ name }) => !findProperty(notifyAction, name)
|
|
686
|
+
).flatMap(({ name, helper, declareProperty }) => {
|
|
687
|
+
const helperName = helperNames[helper];
|
|
688
|
+
const reference = referenceOf(
|
|
689
|
+
(declareAction && findProperty(declareAction, declareProperty)) ?? (declareProperty === DECLARATION_PROPERTY_NAME2 ? findProperty(config, declareProperty) : void 0)
|
|
690
|
+
);
|
|
691
|
+
if (!helperName) {
|
|
692
|
+
console.warn(
|
|
693
|
+
` [${location(notifyAction)}] Skipped \`${name}\`: the file already declares its own \`${helper}\`. Add \`${name}\` to the ${NOTIFY_ACTION_TYPE} action by hand, using the toolkit's \`${helper}\`.`
|
|
694
|
+
);
|
|
695
|
+
return [];
|
|
696
|
+
}
|
|
697
|
+
if (!reference) {
|
|
698
|
+
console.warn(
|
|
699
|
+
` [${location(notifyAction)}] Skipped \`${name}\`: the ${DECLARE_ACTION_TYPE2} action's \`${declareProperty}\` is not given as a name. Add \`${name}: ${helper}(<${DECLARE_ACTION_TYPE2} ${declareProperty}>)\` to the ${NOTIFY_ACTION_TYPE} action by hand.`
|
|
700
|
+
);
|
|
701
|
+
return [];
|
|
702
|
+
}
|
|
703
|
+
if (helperName === helper) {
|
|
704
|
+
usedHelpers.add(helper);
|
|
705
|
+
}
|
|
706
|
+
return [`${name}: ${helperName}(${reference})`];
|
|
707
|
+
});
|
|
708
|
+
if (properties.length === 0) {
|
|
709
|
+
continue;
|
|
710
|
+
}
|
|
711
|
+
console.log(
|
|
712
|
+
` [${location(notifyAction)}] Added \`${properties.join("`, `")}\` to the ${NOTIFY_ACTION_TYPE} action`
|
|
713
|
+
);
|
|
714
|
+
edits.push(...insertIntoAction(notifyAction, properties));
|
|
715
|
+
changes++;
|
|
716
|
+
}
|
|
717
|
+
if (changes === 0) {
|
|
718
|
+
return 0;
|
|
719
|
+
}
|
|
720
|
+
applyTextEdits(sourceFile, edits);
|
|
721
|
+
for (const helper of usedHelpers) {
|
|
722
|
+
addToolkitImport(sourceFile, helper);
|
|
723
|
+
}
|
|
724
|
+
return changes;
|
|
725
|
+
}
|
|
726
|
+
async function main3() {
|
|
727
|
+
const srcDir = import_path4.default.join(process.cwd(), "src");
|
|
728
|
+
console.log(`Scanning for NOTIFY actions in: ${srcDir}
|
|
729
|
+
`);
|
|
730
|
+
const project = new import_ts_morph3.Project({
|
|
731
|
+
tsConfigFilePath: import_path4.default.resolve(srcDir, "../tsconfig.json"),
|
|
732
|
+
skipAddingFilesFromTsConfig: false
|
|
733
|
+
});
|
|
734
|
+
const sourceFiles = project.getSourceFiles().filter((sf) => {
|
|
735
|
+
const fp = sf.getFilePath();
|
|
736
|
+
return fp.includes("/src/") && !fp.includes("/node_modules/");
|
|
737
|
+
});
|
|
738
|
+
console.log(`Found ${sourceFiles.length} source file(s) to analyse.
|
|
739
|
+
`);
|
|
740
|
+
let totalChanges = 0;
|
|
741
|
+
const modifiedFiles = [];
|
|
742
|
+
for (const sourceFile of sourceFiles) {
|
|
743
|
+
const changes = addNotifyConfig(sourceFile);
|
|
744
|
+
if (changes > 0) {
|
|
745
|
+
totalChanges += changes;
|
|
746
|
+
modifiedFiles.push(sourceFile);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
if (modifiedFiles.length === 0) {
|
|
750
|
+
console.log("No NOTIFY actions to update. Nothing to do.");
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
console.log(`
|
|
754
|
+
Saving ${modifiedFiles.length} modified file(s)...`);
|
|
755
|
+
for (const sourceFile of modifiedFiles) {
|
|
756
|
+
await sourceFile.save();
|
|
757
|
+
console.log(
|
|
758
|
+
` Saved: ${import_path4.default.relative(process.cwd(), sourceFile.getFilePath())}`
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
console.log(
|
|
762
|
+
`
|
|
763
|
+
Done. Updated ${totalChanges} NOTIFY action(s) across ${modifiedFiles.length} file(s).`
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
// src/migrations/read-declaration-through-helper.ts
|
|
768
|
+
var import_ts_morph4 = require("ts-morph");
|
|
769
|
+
var import_path5 = __toESM(require("path"));
|
|
770
|
+
var EVENT_CONFIG_TYPE_NAME = "EventConfig";
|
|
771
|
+
var DECLARATION_PROPERTY_NAME3 = "declaration";
|
|
772
|
+
var GET_DECLARATION_NAME = "getDeclaration";
|
|
773
|
+
function isToolkitEventConfig(type) {
|
|
774
|
+
const nonNullableType = type.getNonNullableType();
|
|
775
|
+
const symbol = nonNullableType.getAliasSymbol() ?? nonNullableType.getSymbol();
|
|
776
|
+
if (symbol?.getName() !== EVENT_CONFIG_TYPE_NAME) {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
return symbol.getDeclarations().some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
780
|
+
}
|
|
781
|
+
function isDeclarationReadOnEventConfig(node) {
|
|
782
|
+
return node.getName() === DECLARATION_PROPERTY_NAME3 && isToolkitEventConfig(node.getExpression().getType());
|
|
783
|
+
}
|
|
784
|
+
function isAssignmentOperator(kind) {
|
|
785
|
+
return kind >= import_ts_morph4.SyntaxKind.FirstAssignment && kind <= import_ts_morph4.SyntaxKind.LastAssignment;
|
|
786
|
+
}
|
|
787
|
+
function isWriteTarget(node) {
|
|
788
|
+
const parent = node.getParent();
|
|
789
|
+
if (import_ts_morph4.Node.isDeleteExpression(parent)) {
|
|
790
|
+
return true;
|
|
791
|
+
}
|
|
792
|
+
return import_ts_morph4.Node.isBinaryExpression(parent) && parent.getLeft() === node && isAssignmentOperator(parent.getOperatorToken().getKind());
|
|
793
|
+
}
|
|
794
|
+
function location2(node) {
|
|
795
|
+
const relPath = import_path5.default.relative(
|
|
796
|
+
process.cwd(),
|
|
797
|
+
node.getSourceFile().getFilePath()
|
|
798
|
+
);
|
|
799
|
+
return `${relPath}:${node.getStartLineNumber()}`;
|
|
800
|
+
}
|
|
801
|
+
function warnDestructuredDeclarations(sourceFile) {
|
|
802
|
+
for (const pattern of sourceFile.getDescendantsOfKind(
|
|
803
|
+
import_ts_morph4.SyntaxKind.ObjectBindingPattern
|
|
804
|
+
)) {
|
|
805
|
+
const destructuresDeclaration = pattern.getElements().some(
|
|
806
|
+
(element) => (element.getPropertyNameNode()?.getText() ?? element.getName()) === DECLARATION_PROPERTY_NAME3
|
|
807
|
+
);
|
|
808
|
+
if (destructuresDeclaration && isToolkitEventConfig(pattern.getType())) {
|
|
809
|
+
console.warn(
|
|
810
|
+
` [${location2(pattern)}] Destructures \`${DECLARATION_PROPERTY_NAME3}\` from an EventConfig. Rewrite it to use \`${GET_DECLARATION_NAME}(config)\` by hand.`
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
function warnSpreadDeclarations(sourceFile) {
|
|
816
|
+
for (const literal of sourceFile.getDescendantsOfKind(
|
|
817
|
+
import_ts_morph4.SyntaxKind.ObjectLiteralExpression
|
|
818
|
+
)) {
|
|
819
|
+
const properties = literal.getProperties();
|
|
820
|
+
const spreadsEventConfig = properties.some(
|
|
821
|
+
(property) => import_ts_morph4.Node.isSpreadAssignment(property) && isToolkitEventConfig(property.getExpression().getType())
|
|
822
|
+
);
|
|
823
|
+
const setsDeclaration = properties.some(
|
|
824
|
+
(property) => (import_ts_morph4.Node.isPropertyAssignment(property) || import_ts_morph4.Node.isShorthandPropertyAssignment(property)) && property.getName() === DECLARATION_PROPERTY_NAME3
|
|
825
|
+
);
|
|
826
|
+
if (spreadsEventConfig && setsDeclaration) {
|
|
827
|
+
console.warn(
|
|
828
|
+
` [${location2(literal)}] Spreads an EventConfig and sets \`${DECLARATION_PROPERTY_NAME3}\` next to it. The spread config already has a \`${DECLARATION_PROPERTY_NAME3}\` on its DECLARE action, so replace that one by hand.`
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
function rewriteDeclarationReads(sourceFile) {
|
|
834
|
+
warnDestructuredDeclarations(sourceFile);
|
|
835
|
+
warnSpreadDeclarations(sourceFile);
|
|
836
|
+
const reads = sourceFile.getDescendantsOfKind(import_ts_morph4.SyntaxKind.PropertyAccessExpression).filter(isDeclarationReadOnEventConfig).filter((node) => {
|
|
837
|
+
if (node.hasQuestionDotToken()) {
|
|
838
|
+
console.warn(
|
|
839
|
+
` [${location2(node)}] Skipped optional chain \`${node.getText()}\`. Rewrite it to use \`${GET_DECLARATION_NAME}()\` by hand.`
|
|
840
|
+
);
|
|
841
|
+
return false;
|
|
842
|
+
}
|
|
843
|
+
if (isWriteTarget(node)) {
|
|
844
|
+
console.warn(
|
|
845
|
+
` [${location2(node)}] Skipped write to \`${node.getText()}\`. Set \`${DECLARATION_PROPERTY_NAME3}\` on the DECLARE action instead.`
|
|
846
|
+
);
|
|
847
|
+
return false;
|
|
848
|
+
}
|
|
849
|
+
return true;
|
|
850
|
+
}).sort((a, b) => b.getStart() - a.getStart());
|
|
851
|
+
if (reads.length === 0) {
|
|
852
|
+
return 0;
|
|
853
|
+
}
|
|
854
|
+
const getDeclarationName = findToolkitImportName(
|
|
855
|
+
sourceFile,
|
|
856
|
+
GET_DECLARATION_NAME
|
|
857
|
+
);
|
|
858
|
+
if (!getDeclarationName) {
|
|
859
|
+
console.warn(
|
|
860
|
+
` [${location2(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.`
|
|
861
|
+
);
|
|
862
|
+
return 0;
|
|
863
|
+
}
|
|
864
|
+
for (const read of reads) {
|
|
865
|
+
console.log(
|
|
866
|
+
` [${location2(read)}] ${read.getText()} -> ${getDeclarationName}(${read.getExpression().getText()})`
|
|
867
|
+
);
|
|
868
|
+
read.replaceWithText(
|
|
869
|
+
`${getDeclarationName}(${read.getExpression().getText()})`
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
addToolkitImport(sourceFile, GET_DECLARATION_NAME);
|
|
873
|
+
return reads.length;
|
|
874
|
+
}
|
|
875
|
+
async function main4() {
|
|
876
|
+
const srcDir = import_path5.default.join(process.cwd(), "src");
|
|
877
|
+
console.log(`Scanning for EventConfig declaration reads in: ${srcDir}
|
|
878
|
+
`);
|
|
879
|
+
const project = new import_ts_morph4.Project({
|
|
880
|
+
tsConfigFilePath: import_path5.default.resolve(srcDir, "../tsconfig.json"),
|
|
881
|
+
skipAddingFilesFromTsConfig: false
|
|
882
|
+
});
|
|
883
|
+
const sourceFiles = project.getSourceFiles().filter((sf) => {
|
|
884
|
+
const fp = sf.getFilePath();
|
|
885
|
+
return fp.includes("/src/") && !fp.includes("/node_modules/");
|
|
886
|
+
});
|
|
887
|
+
console.log(`Found ${sourceFiles.length} source file(s) to analyse.
|
|
888
|
+
`);
|
|
889
|
+
let totalChanges = 0;
|
|
890
|
+
const modifiedFiles = [];
|
|
891
|
+
for (const sourceFile of sourceFiles) {
|
|
892
|
+
const changes = rewriteDeclarationReads(sourceFile);
|
|
893
|
+
if (changes > 0) {
|
|
894
|
+
totalChanges += changes;
|
|
895
|
+
modifiedFiles.push(sourceFile);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
if (modifiedFiles.length === 0) {
|
|
899
|
+
console.log("No EventConfig declaration reads found. Nothing to do.");
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
console.log(`
|
|
903
|
+
Saving ${modifiedFiles.length} modified file(s)...`);
|
|
904
|
+
for (const sourceFile of modifiedFiles) {
|
|
905
|
+
await sourceFile.save();
|
|
906
|
+
console.log(
|
|
907
|
+
` Saved: ${import_path5.default.relative(process.cwd(), sourceFile.getFilePath())}`
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
console.log(
|
|
911
|
+
`
|
|
912
|
+
Done. Rewrote ${totalChanges} declaration read(s) across ${modifiedFiles.length} file(s).`
|
|
913
|
+
);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// src/migrations/resolve-versioned-event-config.ts
|
|
917
|
+
var import_ts_morph5 = require("ts-morph");
|
|
918
|
+
var import_path6 = __toESM(require("path"));
|
|
919
|
+
var EVENT_CONFIG_TYPE_NAME2 = "EventConfig";
|
|
920
|
+
var VERSIONED_EVENT_CONFIG_TYPE_NAME = "VersionedEventConfig";
|
|
921
|
+
var RESOLVE_EVENT_CONFIG_NAME = "resolveEventConfig";
|
|
922
|
+
var TOOLKIT_EVENTS_MODULE2 = "@opencrvs/toolkit/events";
|
|
923
|
+
function isToolkitType(type, name) {
|
|
924
|
+
const nonNullableType = type.getNonNullableType();
|
|
925
|
+
const symbol = nonNullableType.getAliasSymbol() ?? nonNullableType.getSymbol();
|
|
926
|
+
if (symbol?.getName() !== name) {
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
return symbol.getDeclarations().some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
930
|
+
}
|
|
931
|
+
function isVersionedEventConfig(type) {
|
|
932
|
+
return isToolkitType(type, VERSIONED_EVENT_CONFIG_TYPE_NAME);
|
|
933
|
+
}
|
|
934
|
+
function isEventConfig(type) {
|
|
935
|
+
return isToolkitType(type, EVENT_CONFIG_TYPE_NAME2);
|
|
936
|
+
}
|
|
937
|
+
function isArrayOf(type, isElement) {
|
|
938
|
+
const elementType = type.getNonNullableType().getArrayElementType();
|
|
939
|
+
return elementType !== void 0 && isElement(elementType);
|
|
940
|
+
}
|
|
941
|
+
function isVersionedField(type, name) {
|
|
942
|
+
return type.getNonNullableType().getProperty(name) !== void 0;
|
|
943
|
+
}
|
|
944
|
+
function isAssignmentOperator2(kind) {
|
|
945
|
+
return kind >= import_ts_morph5.SyntaxKind.FirstAssignment && kind <= import_ts_morph5.SyntaxKind.LastAssignment;
|
|
946
|
+
}
|
|
947
|
+
function isWriteTarget2(node) {
|
|
948
|
+
const parent = node.getParent();
|
|
949
|
+
if (import_ts_morph5.Node.isDeleteExpression(parent)) {
|
|
950
|
+
return true;
|
|
951
|
+
}
|
|
952
|
+
return import_ts_morph5.Node.isBinaryExpression(parent) && parent.getLeft() === node && isAssignmentOperator2(parent.getOperatorToken().getKind());
|
|
953
|
+
}
|
|
954
|
+
function location3(node) {
|
|
955
|
+
const relPath = import_path6.default.relative(
|
|
956
|
+
process.cwd(),
|
|
957
|
+
node.getSourceFile().getFilePath()
|
|
958
|
+
);
|
|
959
|
+
return `${relPath}:${node.getStartLineNumber()}`;
|
|
960
|
+
}
|
|
961
|
+
function warnDestructuredVersionFields(sourceFile) {
|
|
962
|
+
for (const pattern of sourceFile.getDescendantsOfKind(
|
|
963
|
+
import_ts_morph5.SyntaxKind.ObjectBindingPattern
|
|
964
|
+
)) {
|
|
965
|
+
const type = pattern.getType();
|
|
966
|
+
if (!isVersionedEventConfig(type)) {
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
const destructuresVersionField = pattern.getElements().some(
|
|
970
|
+
(element) => !element.getDotDotDotToken() && !isVersionedField(
|
|
971
|
+
type,
|
|
972
|
+
element.getPropertyNameNode()?.getText() ?? element.getName()
|
|
973
|
+
)
|
|
974
|
+
);
|
|
975
|
+
if (destructuresVersionField) {
|
|
976
|
+
console.warn(
|
|
977
|
+
` [${location3(pattern)}] Destructures a field of a version from a VersionedEventConfig. Destructure it from \`${RESOLVE_EVENT_CONFIG_NAME}(config)\` by hand.`
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
function findVersionFieldReads(sourceFile) {
|
|
983
|
+
return sourceFile.getDescendantsOfKind(import_ts_morph5.SyntaxKind.PropertyAccessExpression).filter((node) => {
|
|
984
|
+
const type = node.getExpression().getType();
|
|
985
|
+
return isVersionedEventConfig(type) && !isVersionedField(type, node.getName());
|
|
986
|
+
}).filter((node) => {
|
|
987
|
+
if (node.hasQuestionDotToken()) {
|
|
988
|
+
console.warn(
|
|
989
|
+
` [${location3(node)}] Skipped optional chain \`${node.getText()}\`. Rewrite it to use \`${RESOLVE_EVENT_CONFIG_NAME}()\` by hand.`
|
|
990
|
+
);
|
|
991
|
+
return false;
|
|
992
|
+
}
|
|
993
|
+
if (isWriteTarget2(node)) {
|
|
994
|
+
console.warn(
|
|
995
|
+
` [${location3(node)}] Skipped write to \`${node.getText()}\`. Set it in the version passed to \`defineConfig\` instead.`
|
|
996
|
+
);
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
return true;
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
function isHandedOver(node) {
|
|
1003
|
+
const parent = node.getParent();
|
|
1004
|
+
if (import_ts_morph5.Node.isCallExpression(parent) || import_ts_morph5.Node.isNewExpression(parent)) {
|
|
1005
|
+
return parent.getArguments().includes(node);
|
|
1006
|
+
}
|
|
1007
|
+
return (import_ts_morph5.Node.isVariableDeclaration(parent) || import_ts_morph5.Node.isPropertyAssignment(parent)) && parent.getInitializer() === node || import_ts_morph5.Node.isReturnStatement(parent);
|
|
1008
|
+
}
|
|
1009
|
+
function findValuesExpectingEventConfig(sourceFile) {
|
|
1010
|
+
return sourceFile.getDescendants().filter((node) => import_ts_morph5.Node.isExpression(node)).filter(isHandedOver).map((node) => {
|
|
1011
|
+
const type = node.getType();
|
|
1012
|
+
const expectedType = node.getContextualType();
|
|
1013
|
+
if (!expectedType) {
|
|
1014
|
+
return void 0;
|
|
1015
|
+
}
|
|
1016
|
+
if (isVersionedEventConfig(type) && isEventConfig(expectedType)) {
|
|
1017
|
+
return { node, isArray: false };
|
|
1018
|
+
}
|
|
1019
|
+
if (isArrayOf(type, isVersionedEventConfig) && isArrayOf(expectedType, isEventConfig)) {
|
|
1020
|
+
return { node, isArray: true };
|
|
1021
|
+
}
|
|
1022
|
+
return void 0;
|
|
1023
|
+
}).filter((value) => value !== void 0).filter(({ node }) => {
|
|
1024
|
+
if (node.getType().isNullable()) {
|
|
1025
|
+
console.warn(
|
|
1026
|
+
` [${location3(node)}] Skipped \`${node.getText()}\`, which may be undefined where an EventConfig is expected. Narrow it and pass \`${RESOLVE_EVENT_CONFIG_NAME}(...)\` by hand.`
|
|
1027
|
+
);
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
return true;
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
function findToolkitImport2(sourceFile) {
|
|
1034
|
+
return sourceFile.getImportDeclaration(
|
|
1035
|
+
(declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE2 && !declaration.isTypeOnly() && !declaration.getNamespaceImport()
|
|
1036
|
+
);
|
|
1037
|
+
}
|
|
1038
|
+
function findResolveEventConfigName(sourceFile) {
|
|
1039
|
+
const imported = findToolkitImport2(sourceFile)?.getNamedImports().find((namedImport) => namedImport.getName() === RESOLVE_EVENT_CONFIG_NAME);
|
|
1040
|
+
if (imported) {
|
|
1041
|
+
return imported.getAliasNode()?.getText() ?? RESOLVE_EVENT_CONFIG_NAME;
|
|
1042
|
+
}
|
|
1043
|
+
if (sourceFile.getLocal(RESOLVE_EVENT_CONFIG_NAME)) {
|
|
1044
|
+
return void 0;
|
|
1045
|
+
}
|
|
1046
|
+
return RESOLVE_EVENT_CONFIG_NAME;
|
|
1047
|
+
}
|
|
1048
|
+
function addResolveEventConfigImport(sourceFile) {
|
|
1049
|
+
const toolkitImport = findToolkitImport2(sourceFile);
|
|
1050
|
+
if (!toolkitImport) {
|
|
1051
|
+
const imports = sourceFile.getImportDeclarations();
|
|
1052
|
+
const lastImport = imports[imports.length - 1];
|
|
1053
|
+
sourceFile.insertStatements(
|
|
1054
|
+
lastImport ? lastImport.getChildIndex() + 1 : 0,
|
|
1055
|
+
`import { ${RESOLVE_EVENT_CONFIG_NAME} } from '${TOOLKIT_EVENTS_MODULE2}'`
|
|
1056
|
+
);
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
const alreadyImported = toolkitImport.getNamedImports().some((namedImport) => namedImport.getName() === RESOLVE_EVENT_CONFIG_NAME);
|
|
1060
|
+
if (alreadyImported) {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
const namedImports = toolkitImport.getNamedImports();
|
|
1064
|
+
if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
|
|
1065
|
+
toolkitImport.addNamedImport(RESOLVE_EVENT_CONFIG_NAME);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
const names = [
|
|
1069
|
+
...namedImports.map((namedImport) => namedImport.getText()),
|
|
1070
|
+
RESOLVE_EVENT_CONFIG_NAME
|
|
1071
|
+
];
|
|
1072
|
+
const specifiers = toolkitImport.getText().includes("\n") ? `{
|
|
1073
|
+
${names.join(",\n ")}
|
|
1074
|
+
}` : `{ ${names.join(", ")} }`;
|
|
1075
|
+
toolkitImport.replaceWithText(
|
|
1076
|
+
`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE2}'`
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
function withToolkitEventsAt(compilerOptions, toolkitEventsPath) {
|
|
1080
|
+
return {
|
|
1081
|
+
...compilerOptions,
|
|
1082
|
+
paths: {
|
|
1083
|
+
...compilerOptions.paths,
|
|
1084
|
+
[TOOLKIT_EVENTS_MODULE2]: [toolkitEventsPath]
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
function rewriteVersionedEventConfigUses(sourceFile) {
|
|
1089
|
+
warnDestructuredVersionFields(sourceFile);
|
|
1090
|
+
const uses = [
|
|
1091
|
+
...findVersionFieldReads(sourceFile).map((node) => ({
|
|
1092
|
+
node,
|
|
1093
|
+
rewrite: (name) => `${name}(${node.getExpression().getText()}).${node.getName()}`
|
|
1094
|
+
})),
|
|
1095
|
+
...findValuesExpectingEventConfig(sourceFile).map(({ node, isArray }) => ({
|
|
1096
|
+
node,
|
|
1097
|
+
rewrite: (name) => isArray ? `${node.getText()}.map(${name})` : `${name}(${node.getText()})`
|
|
1098
|
+
}))
|
|
1099
|
+
].sort(
|
|
1100
|
+
(a, b) => b.node.getStart() - a.node.getStart() || a.node.getEnd() - b.node.getEnd()
|
|
1101
|
+
);
|
|
1102
|
+
if (uses.length === 0) {
|
|
1103
|
+
return 0;
|
|
1104
|
+
}
|
|
1105
|
+
const resolveEventConfigName = findResolveEventConfigName(sourceFile);
|
|
1106
|
+
if (!resolveEventConfigName) {
|
|
1107
|
+
console.warn(
|
|
1108
|
+
` [${location3(uses[uses.length - 1].node)}] Skipped ${uses.length} use(s): the file already declares its own \`${RESOLVE_EVENT_CONFIG_NAME}\`. Rewrite them to use the toolkit's \`${RESOLVE_EVENT_CONFIG_NAME}()\` by hand.`
|
|
1109
|
+
);
|
|
1110
|
+
return 0;
|
|
1111
|
+
}
|
|
1112
|
+
for (const { node, rewrite } of uses) {
|
|
1113
|
+
const before = node.getText();
|
|
1114
|
+
const after = rewrite(resolveEventConfigName);
|
|
1115
|
+
console.log(` [${location3(node)}] ${before} -> ${after}`);
|
|
1116
|
+
node.replaceWithText(after);
|
|
1117
|
+
}
|
|
1118
|
+
addResolveEventConfigImport(sourceFile);
|
|
1119
|
+
return uses.length;
|
|
1120
|
+
}
|
|
1121
|
+
async function main5() {
|
|
1122
|
+
const srcDir = import_path6.default.join(process.cwd(), "src");
|
|
1123
|
+
console.log(
|
|
1124
|
+
`Scanning for VersionedEventConfig used as an EventConfig in: ${srcDir}
|
|
1125
|
+
`
|
|
1126
|
+
);
|
|
1127
|
+
const tsConfigFilePath = import_path6.default.resolve(srcDir, "../tsconfig.json");
|
|
1128
|
+
const project = new import_ts_morph5.Project({
|
|
1129
|
+
tsConfigFilePath,
|
|
1130
|
+
skipAddingFilesFromTsConfig: false,
|
|
1131
|
+
// The events module of this toolkit, next to the migrations in `dist`
|
|
1132
|
+
compilerOptions: withToolkitEventsAt(
|
|
1133
|
+
(0, import_ts_morph5.getCompilerOptionsFromTsConfig)(tsConfigFilePath).options,
|
|
1134
|
+
import_path6.default.resolve(__dirname, "../events")
|
|
1135
|
+
)
|
|
1136
|
+
});
|
|
1137
|
+
const sourceFiles = project.getSourceFiles().filter((sf) => {
|
|
1138
|
+
const fp = sf.getFilePath();
|
|
1139
|
+
return fp.includes("/src/") && !fp.includes("/node_modules/");
|
|
1140
|
+
});
|
|
1141
|
+
console.log(`Found ${sourceFiles.length} source file(s) to analyse.
|
|
1142
|
+
`);
|
|
1143
|
+
let totalChanges = 0;
|
|
1144
|
+
const modifiedFiles = [];
|
|
1145
|
+
for (const sourceFile of sourceFiles) {
|
|
1146
|
+
const changes = rewriteVersionedEventConfigUses(sourceFile);
|
|
1147
|
+
if (changes > 0) {
|
|
1148
|
+
totalChanges += changes;
|
|
1149
|
+
modifiedFiles.push(sourceFile);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
if (modifiedFiles.length === 0) {
|
|
1153
|
+
console.log(
|
|
1154
|
+
"No VersionedEventConfig used as an EventConfig found. Nothing to do."
|
|
1155
|
+
);
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
console.log(`
|
|
1159
|
+
Saving ${modifiedFiles.length} modified file(s)...`);
|
|
1160
|
+
for (const sourceFile of modifiedFiles) {
|
|
1161
|
+
await sourceFile.save();
|
|
1162
|
+
console.log(
|
|
1163
|
+
` Saved: ${import_path6.default.relative(process.cwd(), sourceFile.getFilePath())}`
|
|
1164
|
+
);
|
|
1165
|
+
}
|
|
1166
|
+
console.log(
|
|
1167
|
+
`
|
|
1168
|
+
Done. Rewrote ${totalChanges} use(s) across ${modifiedFiles.length} file(s).`
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// src/migrations/persistent/update-toolkit-dependency.ts
|
|
1173
|
+
var import_fs2 = __toESM(require("fs"));
|
|
1174
|
+
var import_path7 = __toESM(require("path"));
|
|
1175
|
+
|
|
1176
|
+
// src/migrations/persistent/check-upgrade-path.ts
|
|
1177
|
+
var TOOLKIT_PACKAGE_NAME = "@opencrvs/toolkit";
|
|
1178
|
+
function findMajorMinor(version) {
|
|
1179
|
+
const match = /(\d+)\.(\d+)/.exec(version);
|
|
1180
|
+
return match ? { major: Number(match[1]), minor: Number(match[2]) } : void 0;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
// src/migrations/persistent/update-toolkit-dependency.ts
|
|
1184
|
+
var DEPENDENCY_PATTERN = new RegExp(
|
|
1185
|
+
`("${TOOLKIT_PACKAGE_NAME}"\\s*:\\s*")([^"]*)(")`,
|
|
1186
|
+
"g"
|
|
1187
|
+
);
|
|
1188
|
+
function updateToolkitDependency(packageJson, toolkitVersion) {
|
|
1189
|
+
const updated = packageJson.replace(
|
|
1190
|
+
DEPENDENCY_PATTERN,
|
|
1191
|
+
(match, before, version, after) => {
|
|
1192
|
+
if (!findMajorMinor(version)) {
|
|
1193
|
+
return match;
|
|
1194
|
+
}
|
|
1195
|
+
const range = /^[\^~]?/.exec(version)?.[0] ?? "";
|
|
1196
|
+
return `${before}${range}${toolkitVersion}${after}`;
|
|
1197
|
+
}
|
|
1198
|
+
);
|
|
1199
|
+
return updated === packageJson ? void 0 : updated;
|
|
1200
|
+
}
|
|
1201
|
+
async function main6(toolkitVersion) {
|
|
1202
|
+
const packageJsonPath = import_path7.default.join(process.cwd(), "package.json");
|
|
1203
|
+
if (!import_fs2.default.existsSync(packageJsonPath)) {
|
|
1204
|
+
console.log(`No package.json found. Skipping ${TOOLKIT_PACKAGE_NAME}.`);
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
const updated = updateToolkitDependency(
|
|
1208
|
+
import_fs2.default.readFileSync(packageJsonPath, "utf-8"),
|
|
1209
|
+
toolkitVersion
|
|
1210
|
+
);
|
|
1211
|
+
if (!updated) {
|
|
1212
|
+
console.log(
|
|
1213
|
+
`${TOOLKIT_PACKAGE_NAME} in package.json needs no update. Nothing to do.`
|
|
1214
|
+
);
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
import_fs2.default.writeFileSync(packageJsonPath, updated);
|
|
1218
|
+
console.log(
|
|
1219
|
+
`Updated ${TOOLKIT_PACKAGE_NAME} to ${toolkitVersion} in package.json. Install the dependencies again to use it.`
|
|
1220
|
+
);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
// src/migrations/replace-incomplete-flag.ts
|
|
1224
|
+
var import_ts_morph6 = require("ts-morph");
|
|
1225
|
+
var import_path8 = __toESM(require("path"));
|
|
1226
|
+
var INCOMPLETE = "InherentFlags.INCOMPLETE";
|
|
1227
|
+
var NOTIFIED_STATUS = `{ type: 'exact', term: EventStatus.enum.NOTIFIED }`;
|
|
1228
|
+
function ensureEventStatusImport(sourceFile) {
|
|
1229
|
+
const importDeclaration = sourceFile.getImportDeclarations().find(
|
|
1230
|
+
(declaration) => declaration.getNamedImports().some((named) => named.getName() === "InherentFlags")
|
|
1231
|
+
);
|
|
1232
|
+
const hasEventStatus = importDeclaration?.getNamedImports().some((named) => named.getName() === "EventStatus");
|
|
1233
|
+
if (importDeclaration && !hasEventStatus) {
|
|
1234
|
+
importDeclaration.addNamedImport("EventStatus");
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
function replaceIncompleteFlag(sourceFile) {
|
|
1238
|
+
let changed = false;
|
|
1239
|
+
const anyOfIncomplete = sourceFile.getDescendantsOfKind(import_ts_morph6.SyntaxKind.PropertyAccessExpression).filter((node) => node.getText() === INCOMPLETE).map((node) => node.getParent()).filter(import_ts_morph6.Node.isArrayLiteralExpression).filter((array) => {
|
|
1240
|
+
const anyOf = array.getParent();
|
|
1241
|
+
const flags = anyOf?.getParent()?.getParent();
|
|
1242
|
+
return import_ts_morph6.Node.isPropertyAssignment(anyOf) && anyOf.getName() === "anyOf" && import_ts_morph6.Node.isPropertyAssignment(flags) && flags.getName() === "flags";
|
|
1243
|
+
});
|
|
1244
|
+
for (const array of anyOfIncomplete) {
|
|
1245
|
+
const anyOf = array.getParentIfKindOrThrow(import_ts_morph6.SyntaxKind.PropertyAssignment);
|
|
1246
|
+
const flagsObject = anyOf.getParentIfKindOrThrow(
|
|
1247
|
+
import_ts_morph6.SyntaxKind.ObjectLiteralExpression
|
|
1248
|
+
);
|
|
1249
|
+
const flags = flagsObject.getParentIfKindOrThrow(
|
|
1250
|
+
import_ts_morph6.SyntaxKind.PropertyAssignment
|
|
1251
|
+
);
|
|
1252
|
+
const query = flags.getParentIfKindOrThrow(
|
|
1253
|
+
import_ts_morph6.SyntaxKind.ObjectLiteralExpression
|
|
1254
|
+
);
|
|
1255
|
+
array.getElements().filter((element) => element.getText() === INCOMPLETE).forEach((element) => array.removeElement(element));
|
|
1256
|
+
if (array.getElements().length === 0) anyOf.remove();
|
|
1257
|
+
if (flagsObject.getProperties().length === 0) flags.remove();
|
|
1258
|
+
if (!query.getProperty("status")) {
|
|
1259
|
+
query.addPropertyAssignment({
|
|
1260
|
+
name: "status",
|
|
1261
|
+
initializer: NOTIFIED_STATUS
|
|
1262
|
+
});
|
|
1263
|
+
}
|
|
1264
|
+
changed = true;
|
|
1265
|
+
}
|
|
1266
|
+
if (changed) ensureEventStatusImport(sourceFile);
|
|
1267
|
+
const remaining = sourceFile.getDescendantsOfKind(import_ts_morph6.SyntaxKind.PropertyAccessExpression).filter((node) => node.getText() === INCOMPLETE).map((node) => node.getStartLineNumber());
|
|
1268
|
+
return { changed, remaining };
|
|
1269
|
+
}
|
|
1270
|
+
async function main7() {
|
|
1271
|
+
const project = new import_ts_morph6.Project({
|
|
1272
|
+
tsConfigFilePath: import_path8.default.join(process.cwd(), "tsconfig.json")
|
|
1273
|
+
});
|
|
1274
|
+
const sourceFiles = project.getSourceFiles().filter((sf) => !sf.getFilePath().includes("/node_modules/"));
|
|
1275
|
+
for (const sourceFile of sourceFiles) {
|
|
1276
|
+
const relPath = import_path8.default.relative(process.cwd(), sourceFile.getFilePath());
|
|
1277
|
+
const { changed, remaining } = replaceIncompleteFlag(sourceFile);
|
|
1278
|
+
if (changed) {
|
|
1279
|
+
await sourceFile.save();
|
|
1280
|
+
console.log(
|
|
1281
|
+
` [${relPath}] Replaced the incomplete flag with the NOTIFIED status`
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
for (const line of remaining) {
|
|
1285
|
+
console.warn(
|
|
1286
|
+
` [${relPath}:${line}] ${INCOMPLETE} was removed, update this reference to use the NOTIFIED status`
|
|
1287
|
+
);
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
// src/migrations/index.ts
|
|
1293
|
+
async function runUpgrade(toolkitVersion) {
|
|
1294
|
+
const [major, minor] = toolkitVersion.split(".");
|
|
1295
|
+
await main2();
|
|
1296
|
+
await main3();
|
|
1297
|
+
await main5();
|
|
1298
|
+
await main4();
|
|
1299
|
+
await main(`${major}.${minor}`);
|
|
1300
|
+
await main7();
|
|
1301
|
+
await main6(toolkitVersion);
|
|
1302
|
+
}
|
|
1303
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1304
|
+
0 && (module.exports = {
|
|
1305
|
+
runUpgrade
|
|
1306
|
+
});
|