@opencrvs/toolkit 2.0.0-rc.076ef94 → 2.0.0-rc.078e42f

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,218 +0,0 @@
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/v2.1/index.ts
32
- var v2_exports = {};
33
- __export(v2_exports, {
34
- runUpgrade: () => runUpgrade
35
- });
36
- module.exports = __toCommonJS(v2_exports);
37
-
38
- // src/migrations/v2.1/add-explicit-correction-flags.ts
39
- var import_ts_morph = require("ts-morph");
40
- var import_path = __toESM(require("path"));
41
- var DEFINE_CONFIG_NAME = "defineConfig";
42
- var ACTIONS_PROPERTY_NAME = "actions";
43
- var TYPE_PROPERTY_NAME = "type";
44
- var FLAGS_PROPERTY_NAME = "flags";
45
- var CONDITIONALS_PROPERTY_NAME = "conditionals";
46
- var ACTION_TYPE_ENUM_NAME = "ActionType";
47
- var REQUEST_CORRECTION_TYPE = "REQUEST_CORRECTION";
48
- var CORRECTION_ACTIONS_TO_ADD = [
49
- {
50
- actionType: "APPROVE_CORRECTION",
51
- labelId: "v2.events.correction.approve.label",
52
- defaultMessage: "Approve correction",
53
- description: "Label for the approve correction action"
54
- },
55
- {
56
- actionType: "REJECT_CORRECTION",
57
- labelId: "v2.events.correction.reject.label",
58
- defaultMessage: "Reject correction",
59
- description: "Label for the reject correction action"
60
- }
61
- ];
62
- function isActionOfType(obj, typeName) {
63
- const typeProperty = obj.getProperty(TYPE_PROPERTY_NAME);
64
- if (!typeProperty || !import_ts_morph.Node.isPropertyAssignment(typeProperty)) return false;
65
- const typeInit = typeProperty.getInitializer();
66
- if (!typeInit) return false;
67
- if (import_ts_morph.Node.isPropertyAccessExpression(typeInit)) {
68
- return typeInit.getExpression().getText() === ACTION_TYPE_ENUM_NAME && typeInit.getName() === typeName;
69
- }
70
- if (import_ts_morph.Node.isStringLiteral(typeInit)) {
71
- return typeInit.getLiteralValue() === typeName;
72
- }
73
- return false;
74
- }
75
- function getTypeInitializerStyle(obj) {
76
- const typeProperty = obj.getProperty(TYPE_PROPERTY_NAME);
77
- if (typeProperty && import_ts_morph.Node.isPropertyAssignment(typeProperty)) {
78
- const typeInit = typeProperty.getInitializer();
79
- if (typeInit && import_ts_morph.Node.isPropertyAccessExpression(typeInit)) return "enum";
80
- }
81
- return "string";
82
- }
83
- function getNonEmptyArrayPropertyText(obj, propName) {
84
- const prop = obj.getProperty(propName);
85
- if (!prop || !import_ts_morph.Node.isPropertyAssignment(prop)) return void 0;
86
- const init = prop.getInitializer();
87
- if (!init || !import_ts_morph.Node.isArrayLiteralExpression(init)) return void 0;
88
- if (init.getElements().length === 0) return void 0;
89
- return init.getText();
90
- }
91
- function buildCorrectionActionText(spec, typeStyle, flagsText, conditionalsText) {
92
- const typeText = typeStyle === "enum" ? `${ACTION_TYPE_ENUM_NAME}.${spec.actionType}` : `'${spec.actionType}'`;
93
- const lines = [
94
- `type: ${typeText}`,
95
- `label: {
96
- id: '${spec.labelId}',
97
- defaultMessage: '${spec.defaultMessage}',
98
- description: '${spec.description}'
99
- }`
100
- ];
101
- if (flagsText) {
102
- lines.push(`${FLAGS_PROPERTY_NAME}: ${flagsText}`);
103
- }
104
- if (conditionalsText) {
105
- lines.push(`${CONDITIONALS_PROPERTY_NAME}: ${conditionalsText}`);
106
- }
107
- return `{
108
- ${lines.join(",\n ")}
109
- }`;
110
- }
111
- function processFile(filePath, project) {
112
- const sourceFile = project.getSourceFile(filePath);
113
- if (!sourceFile) return 0;
114
- let changes = 0;
115
- const relPath = import_path.default.relative(process.cwd(), filePath);
116
- const callExpressions = sourceFile.getDescendantsOfKind(
117
- import_ts_morph.SyntaxKind.CallExpression
118
- );
119
- for (const call of callExpressions) {
120
- const expression = call.getExpression();
121
- if (!import_ts_morph.Node.isIdentifier(expression) || expression.getText() !== DEFINE_CONFIG_NAME) {
122
- continue;
123
- }
124
- const args = call.getArguments();
125
- if (args.length === 0) continue;
126
- const configArg = args[0];
127
- if (!import_ts_morph.Node.isObjectLiteralExpression(configArg)) continue;
128
- const actionsProperty = configArg.getProperty(ACTIONS_PROPERTY_NAME);
129
- if (!actionsProperty || !import_ts_morph.Node.isPropertyAssignment(actionsProperty))
130
- continue;
131
- const actionsInitializer = actionsProperty.getInitializer();
132
- if (!actionsInitializer || !import_ts_morph.Node.isArrayLiteralExpression(actionsInitializer))
133
- continue;
134
- const elements = actionsInitializer.getElements();
135
- const requestCorrectionIndex = elements.findIndex(
136
- (el) => import_ts_morph.Node.isObjectLiteralExpression(el) && isActionOfType(el, REQUEST_CORRECTION_TYPE)
137
- );
138
- if (requestCorrectionIndex === -1) continue;
139
- const requestCorrection = elements[requestCorrectionIndex];
140
- const flagsText = getNonEmptyArrayPropertyText(
141
- requestCorrection,
142
- FLAGS_PROPERTY_NAME
143
- );
144
- const conditionalsText = getNonEmptyArrayPropertyText(
145
- requestCorrection,
146
- CONDITIONALS_PROPERTY_NAME
147
- );
148
- if (!flagsText && !conditionalsText) continue;
149
- const typeStyle = getTypeInitializerStyle(requestCorrection);
150
- let insertAt = requestCorrectionIndex + 1;
151
- for (const spec of CORRECTION_ACTIONS_TO_ADD) {
152
- const alreadyPresent = actionsInitializer.getElements().some(
153
- (el) => import_ts_morph.Node.isObjectLiteralExpression(el) && isActionOfType(el, spec.actionType)
154
- );
155
- if (alreadyPresent) continue;
156
- const text = buildCorrectionActionText(
157
- spec,
158
- typeStyle,
159
- flagsText,
160
- conditionalsText
161
- );
162
- actionsInitializer.insertElement(insertAt, text);
163
- insertAt++;
164
- changes++;
165
- console.log(
166
- ` [${relPath}] Added ${spec.actionType} action carrying over REQUEST_CORRECTION's flags/conditionals`
167
- );
168
- }
169
- }
170
- return changes;
171
- }
172
- async function main() {
173
- const srcDir = import_path.default.join(process.cwd(), "src");
174
- console.log(`Scanning for defineConfig calls in: ${srcDir}
175
- `);
176
- const project = new import_ts_morph.Project({
177
- tsConfigFilePath: import_path.default.resolve(srcDir, "../tsconfig.json"),
178
- skipAddingFilesFromTsConfig: false
179
- });
180
- const sourceFiles = project.getSourceFiles().filter((sf) => {
181
- const fp = sf.getFilePath();
182
- return fp.includes("/src/") && !fp.includes("/node_modules/");
183
- });
184
- console.log(`Found ${sourceFiles.length} source file(s) to analyse.
185
- `);
186
- let totalChanges = 0;
187
- const modifiedFiles = [];
188
- for (const sourceFile of sourceFiles) {
189
- const filePath = sourceFile.getFilePath();
190
- const changes = processFile(filePath, project);
191
- if (changes > 0) {
192
- totalChanges += changes;
193
- modifiedFiles.push(filePath);
194
- }
195
- }
196
- if (modifiedFiles.length === 0) {
197
- console.log(
198
- "No REQUEST_CORRECTION actions with flags/conditionals found. Nothing to do."
199
- );
200
- return;
201
- }
202
- console.log(`
203
- Saving ${modifiedFiles.length} modified file(s)...`);
204
- for (const filePath of modifiedFiles) {
205
- const sourceFile = project.getSourceFileOrThrow(filePath);
206
- await sourceFile.save();
207
- console.log(` Saved: ${import_path.default.relative(process.cwd(), filePath)}`);
208
- }
209
- console.log(
210
- `
211
- Done. Added ${totalChanges} correction action config(s) across ${modifiedFiles.length} file(s).`
212
- );
213
- }
214
-
215
- // src/migrations/v2.1/index.ts
216
- async function runUpgrade(dockerSwarm) {
217
- await main();
218
- }