@opencrvs/toolkit 1.9.12-rc.bcebfd3 → 1.9.12-rc.bda22f7

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.
@@ -1,196 +0,0 @@
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.main = main;
26
- /**
27
- * Codemod: Clean up `src/analytics/analytics.ts` by removing the Farajaland
28
- * sample precalculation wiring.
29
- *
30
- * Usage:
31
- * ts-node -r tsconfig-paths/register src/migrations/v2.0/simplify-analytics-precalculations.ts
32
- *
33
- * Runs LAST, after `checkout-upstream-files` has replaced
34
- * `src/analytics/analytics.ts` with the upstream v2.0 version. That upstream
35
- * version hardcodes a Farajaland-specific birth-event precalculation (it
36
- * imports `Event` and `precalculateBirthEvent`), which countries implementing
37
- * their own flows don't want. This codemod strips that out so each country
38
- * starts from a clean placeholder.
39
- *
40
- * What it does (only to `src/analytics/analytics.ts`, if present):
41
- * 1. Removes the named imports `Event` and `precalculateBirthEvent`.
42
- * If an import declaration becomes empty it is removed entirely.
43
- * 2. Rewrites the body of `precalculateAdditionalAnalytics` so it contains
44
- * nothing but `return declaration` — i.e. every statement that
45
- * references `Event` or `precalculateBirthEvent` is dropped, and a
46
- * `return declaration` is ensured at the end.
47
- *
48
- * The codemod is safe to re-run: if the imports are already gone and the
49
- * function body is already `return declaration`, it is a no-op.
50
- */
51
- const ts_morph_1 = require("ts-morph");
52
- const fs_1 = __importDefault(require("fs"));
53
- const path_1 = __importDefault(require("path"));
54
- const _1 = require(".");
55
- const ANALYTICS_FILE_REL = 'src/analytics/analytics.ts';
56
- const FUNCTION_NAME = 'precalculateAdditionalAnalytics';
57
- const IMPORT_NAMES_TO_REMOVE = new Set(['Event', 'precalculateBirthEvent']);
58
- const RETURN_EXPRESSION_TEXT = 'declaration';
59
- // ─── Import cleanup ──────────────────────────────────────────────────────────
60
- /**
61
- * Removes every named import in `sourceFile` whose local binding name is in
62
- * `IMPORT_NAMES_TO_REMOVE`. If a given import declaration is left with no
63
- * named imports, default import or namespace import, the declaration itself
64
- * is removed.
65
- */
66
- function removeTargetedNamedImports(sourceFile) {
67
- const log = [];
68
- for (const importDecl of [...sourceFile.getImportDeclarations()]) {
69
- const namedImports = importDecl.getNamedImports();
70
- if (namedImports.length === 0)
71
- continue;
72
- const matched = namedImports.filter((ni) => IMPORT_NAMES_TO_REMOVE.has(ni.getName()));
73
- if (matched.length === 0)
74
- continue;
75
- const removedNames = matched.map((ni) => ni.getName());
76
- for (const ni of matched)
77
- ni.remove();
78
- log.push(`removed import(s) ${removedNames.join(', ')} from '${importDecl.getModuleSpecifierValue()}'`);
79
- const stillUsed = importDecl.getNamedImports().length > 0 ||
80
- importDecl.getDefaultImport() ||
81
- importDecl.getNamespaceImport();
82
- if (!stillUsed)
83
- importDecl.remove();
84
- }
85
- return log;
86
- }
87
- // ─── Function-body cleanup ───────────────────────────────────────────────────
88
- /**
89
- * Returns the `precalculateAdditionalAnalytics` declaration in `sourceFile`,
90
- * whether it was written as `function precalculateAdditionalAnalytics(...)` or
91
- * as `const precalculateAdditionalAnalytics = (...) => { ... }`.
92
- */
93
- function findPrecalculateFunction(sourceFile) {
94
- for (const fn of sourceFile.getFunctions()) {
95
- if (fn.getName() === FUNCTION_NAME)
96
- return fn;
97
- }
98
- for (const statement of sourceFile.getVariableStatements()) {
99
- for (const decl of statement.getDeclarations()) {
100
- if (decl.getName() === FUNCTION_NAME)
101
- return decl;
102
- }
103
- }
104
- return undefined;
105
- }
106
- /**
107
- * Returns the block body of a function declaration or variable-declared arrow
108
- * / function expression, or undefined if the function has an expression body
109
- * or doesn't exist.
110
- */
111
- function getFunctionBodyBlock(fn) {
112
- if (ts_morph_1.Node.isFunctionDeclaration(fn))
113
- return fn.getBody();
114
- const initializer = fn.getInitializer();
115
- if (!initializer)
116
- return undefined;
117
- if (!ts_morph_1.Node.isArrowFunction(initializer) &&
118
- !ts_morph_1.Node.isFunctionExpression(initializer)) {
119
- return undefined;
120
- }
121
- return initializer.getBody();
122
- }
123
- /**
124
- * Rewrites the body of `precalculateAdditionalAnalytics` so that the only
125
- * statement is `return declaration`. Any statement whose subtree references
126
- * the removed imports (`Event`, `precalculateBirthEvent`) is dropped; a
127
- * trailing `return declaration` is ensured.
128
- */
129
- function simplifyPrecalculateFunction(sourceFile) {
130
- var _a;
131
- const log = [];
132
- const fn = findPrecalculateFunction(sourceFile);
133
- if (!fn)
134
- return log;
135
- const body = getFunctionBodyBlock(fn);
136
- if (!body || !ts_morph_1.Node.isBlock(body))
137
- return log;
138
- // Drop every statement (not just `if`s) whose AST references an identifier
139
- // in IMPORT_NAMES_TO_REMOVE. This handles both the upstream shape
140
- // if (eventConfig.id === Event.Birth) return precalculateBirthEvent(...)
141
- // and any equivalent variant a country might have typed.
142
- for (const statement of [...body.getStatements()]) {
143
- const references = statement
144
- .getDescendantsOfKind(ts_morph_1.SyntaxKind.Identifier)
145
- .some((id) => IMPORT_NAMES_TO_REMOVE.has(id.getText()));
146
- if (references) {
147
- statement.remove();
148
- log.push(`removed statement in ${FUNCTION_NAME} referencing removed imports`);
149
- }
150
- }
151
- // Ensure the function ends with `return declaration`. If nothing changed
152
- // and the body already ends correctly, this is a no-op.
153
- const remaining = body.getStatements();
154
- const last = remaining[remaining.length - 1];
155
- const alreadyCorrect = last &&
156
- ts_morph_1.Node.isReturnStatement(last) &&
157
- ((_a = last.getExpression()) === null || _a === void 0 ? void 0 : _a.getText()) === RETURN_EXPRESSION_TEXT;
158
- if (!alreadyCorrect) {
159
- body.addStatements(`return ${RETURN_EXPRESSION_TEXT}`);
160
- log.push(`ensured '${FUNCTION_NAME}' ends with 'return ${RETURN_EXPRESSION_TEXT}'`);
161
- }
162
- return log;
163
- }
164
- // ─── Entry point ─────────────────────────────────────────────────────────────
165
- function main() {
166
- return __awaiter(this, void 0, void 0, function* () {
167
- const cwd = (0, _1.getCwd)();
168
- const absPath = path_1.default.join(cwd, ANALYTICS_FILE_REL);
169
- if (!fs_1.default.existsSync(absPath)) {
170
- console.log(`[${ANALYTICS_FILE_REL}] does not exist — nothing to clean up.`);
171
- return;
172
- }
173
- const project = new ts_morph_1.Project({
174
- tsConfigFilePath: path_1.default.resolve(cwd, 'tsconfig.json'),
175
- skipAddingFilesFromTsConfig: false
176
- });
177
- const sourceFile = project.getSourceFile(absPath);
178
- if (!sourceFile) {
179
- console.log(`[${ANALYTICS_FILE_REL}] is not part of the tsconfig project. Skipping.`);
180
- return;
181
- }
182
- console.log(`Cleaning up ${ANALYTICS_FILE_REL}...\n`);
183
- const importLogs = removeTargetedNamedImports(sourceFile);
184
- const fnLogs = simplifyPrecalculateFunction(sourceFile);
185
- const allLogs = [...importLogs, ...fnLogs];
186
- if (allLogs.length === 0) {
187
- console.log(`[${ANALYTICS_FILE_REL}] already clean — nothing to do.`);
188
- return;
189
- }
190
- for (const log of allLogs) {
191
- console.log(` [${ANALYTICS_FILE_REL}] ${log}`);
192
- }
193
- yield sourceFile.save();
194
- console.log(`\nSaved ${ANALYTICS_FILE_REL}.`);
195
- });
196
- }