@opencrvs/toolkit 2.1.2 → 2.2.0-rc.1a111f2
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 +2 -12
- package/create-countryconfig/package.json +1 -1
- package/dist/application-config/index.js +41 -9
- package/dist/authentication/index.d.ts +1 -0
- package/dist/authentication/index.js +30 -6
- package/dist/cli.js +1489 -2020
- package/dist/commons/api/router.d.ts +1551 -248
- package/dist/commons/application-config/index.d.ts +2 -2
- package/dist/commons/conditionals/conditionals.d.ts +3 -3
- package/dist/commons/conditionals/index.d.ts +1 -0
- package/dist/commons/conditionals/validate.d.ts +5 -0
- 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/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 +412 -6
- package/dist/commons/events/EventDocument.d.ts +88 -0
- package/dist/commons/events/EventIndex.d.ts +43 -6
- package/dist/commons/events/EventMetadata.d.ts +23 -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 +6 -6
- package/dist/commons/events/Flag.d.ts +1 -7
- package/dist/commons/events/PlainDate.d.ts +1 -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 +4 -1
- package/dist/commons/events/eventConfigValidation.d.ts +26 -1
- package/dist/commons/events/field.d.ts +5 -0
- package/dist/commons/events/index.d.ts +3 -0
- package/dist/commons/events/scopes.d.ts +3 -0
- package/dist/commons/events/state/flags.d.ts +1 -1
- package/dist/commons/events/state/index.d.ts +10 -2
- package/dist/commons/events/state/utils.d.ts +11 -1
- package/dist/commons/events/test.utils.d.ts +87 -6
- package/dist/commons/events/toNotificationForm.d.ts +17 -0
- package/dist/commons/events/utils.d.ts +75 -8
- package/dist/conditionals/index.js +16 -4
- 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 +159 -35
- package/dist/environment/templates.d.ts.map +1 -1
- package/dist/events/index.js +944 -214
- 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 +887 -129
- package/dist/scopes/index.d.ts +83 -90
- package/dist/scopes/index.js +26 -6
- package/dist/templates/charts-values/dependencies/values.yaml +3 -2
- package/dist/templates/charts-values/opencrvs-services/values.yaml +5 -7
- 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.1a111f2.tgz +0 -0
- package/package.json +3 -2
- package/tsconfig.build.tsbuildinfo +1 -1
- 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,353 @@
|
|
|
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.withToolkitEventsAt = withToolkitEventsAt;
|
|
26
|
+
exports.rewriteVersionedEventConfigUses = rewriteVersionedEventConfigUses;
|
|
27
|
+
exports.main = main;
|
|
28
|
+
/**
|
|
29
|
+
* Codemod: Read a `VersionedEventConfig` through `resolveEventConfig()` wherever
|
|
30
|
+
* the country config uses it as a flat `EventConfig`.
|
|
31
|
+
*
|
|
32
|
+
* Usage:
|
|
33
|
+
* ts-node -r tsconfig-paths/register src/migrations/resolve-versioned-event-config.ts
|
|
34
|
+
*
|
|
35
|
+
* Why:
|
|
36
|
+
* As of v2.2 an event configuration can have several versions, each with an
|
|
37
|
+
* `effectiveFrom` date. `defineConfig` still accepts a flat configuration and
|
|
38
|
+
* wraps it in a single version, but it now returns a `VersionedEventConfig`:
|
|
39
|
+
* `{ id, label, advancedSearch, versions }`. Fields such as `actions` or
|
|
40
|
+
* `analytics` live on each version, so code that reads them straight off the
|
|
41
|
+
* result of `defineConfig`, or passes it to a function expecting an
|
|
42
|
+
* `EventConfig`, stops compiling.
|
|
43
|
+
*
|
|
44
|
+
* What it does:
|
|
45
|
+
* - Scans all TypeScript files under `src/`
|
|
46
|
+
* - Reads `@opencrvs/toolkit/events` from the toolkit running it, not the one the
|
|
47
|
+
* country config has installed, which predates `VersionedEventConfig`
|
|
48
|
+
* - Rewrites `x.field` to `resolveEventConfig(x).field` wherever `x` is the
|
|
49
|
+
* toolkit's `VersionedEventConfig` and `field` is not one of its own fields
|
|
50
|
+
* (`id`, `label`, `advancedSearch` and `versions` are left alone)
|
|
51
|
+
* - Rewrites `x` to `resolveEventConfig(x)` wherever a `VersionedEventConfig` is
|
|
52
|
+
* passed, assigned or returned where an `EventConfig` is expected, and `xs` to
|
|
53
|
+
* `xs.map(resolveEventConfig)` for arrays of them
|
|
54
|
+
* - Adds `resolveEventConfig` to the file's `@opencrvs/toolkit/events` import
|
|
55
|
+
* - Logs, without changing: optional chains (`x?.actions`), writes
|
|
56
|
+
* (`x.actions = ...`, `delete x.actions`...), destructuring `{ actions }` out
|
|
57
|
+
* of a `VersionedEventConfig`, and a possibly undefined `VersionedEventConfig`
|
|
58
|
+
* passed where an `EventConfig` is expected
|
|
59
|
+
* - Leaves a file alone, and logs it, when it already declares its own
|
|
60
|
+
* `resolveEventConfig`
|
|
61
|
+
* - Saves the modified files in-place
|
|
62
|
+
*
|
|
63
|
+
* Runs before `read-declaration-through-helper`, which then rewrites
|
|
64
|
+
* `resolveEventConfig(x).declaration` to `getDeclaration(resolveEventConfig(x))`.
|
|
65
|
+
*/
|
|
66
|
+
const ts_morph_1 = require("ts-morph");
|
|
67
|
+
const path_1 = __importDefault(require("path"));
|
|
68
|
+
const EVENT_CONFIG_TYPE_NAME = 'EventConfig';
|
|
69
|
+
const VERSIONED_EVENT_CONFIG_TYPE_NAME = 'VersionedEventConfig';
|
|
70
|
+
const RESOLVE_EVENT_CONFIG_NAME = 'resolveEventConfig';
|
|
71
|
+
const TOOLKIT_EVENTS_MODULE = '@opencrvs/toolkit/events';
|
|
72
|
+
/**
|
|
73
|
+
* Returns true if the type is the type of that name published by the toolkit,
|
|
74
|
+
* as opposed to a type of the same name declared in the country config itself.
|
|
75
|
+
*/
|
|
76
|
+
function isToolkitType(type, name) {
|
|
77
|
+
var _a;
|
|
78
|
+
const nonNullableType = type.getNonNullableType();
|
|
79
|
+
const symbol = (_a = nonNullableType.getAliasSymbol()) !== null && _a !== void 0 ? _a : nonNullableType.getSymbol();
|
|
80
|
+
if ((symbol === null || symbol === void 0 ? void 0 : symbol.getName()) !== name) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return symbol
|
|
84
|
+
.getDeclarations()
|
|
85
|
+
.some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
86
|
+
}
|
|
87
|
+
function isVersionedEventConfig(type) {
|
|
88
|
+
return isToolkitType(type, VERSIONED_EVENT_CONFIG_TYPE_NAME);
|
|
89
|
+
}
|
|
90
|
+
function isEventConfig(type) {
|
|
91
|
+
return isToolkitType(type, EVENT_CONFIG_TYPE_NAME);
|
|
92
|
+
}
|
|
93
|
+
function isArrayOf(type, isElement) {
|
|
94
|
+
const elementType = type.getNonNullableType().getArrayElementType();
|
|
95
|
+
return elementType !== undefined && isElement(elementType);
|
|
96
|
+
}
|
|
97
|
+
/** Returns true if `name` is a field of `VersionedEventConfig` itself, e.g. `id` or `versions`. */
|
|
98
|
+
function isVersionedField(type, name) {
|
|
99
|
+
return type.getNonNullableType().getProperty(name) !== undefined;
|
|
100
|
+
}
|
|
101
|
+
function isAssignmentOperator(kind) {
|
|
102
|
+
return kind >= ts_morph_1.SyntaxKind.FirstAssignment && kind <= ts_morph_1.SyntaxKind.LastAssignment;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns true if the node is written to rather than read: the target of `=` or
|
|
106
|
+
* a compound assignment (`??=`, `||=`...), or the operand of `delete`.
|
|
107
|
+
* `resolveEventConfig(x)` gives a separate object, so writing to it is lost.
|
|
108
|
+
*/
|
|
109
|
+
function isWriteTarget(node) {
|
|
110
|
+
const parent = node.getParent();
|
|
111
|
+
if (ts_morph_1.Node.isDeleteExpression(parent)) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return (ts_morph_1.Node.isBinaryExpression(parent) &&
|
|
115
|
+
parent.getLeft() === node &&
|
|
116
|
+
isAssignmentOperator(parent.getOperatorToken().getKind()));
|
|
117
|
+
}
|
|
118
|
+
function location(node) {
|
|
119
|
+
const relPath = path_1.default.relative(process.cwd(), node.getSourceFile().getFilePath());
|
|
120
|
+
return `${relPath}:${node.getStartLineNumber()}`;
|
|
121
|
+
}
|
|
122
|
+
function warnDestructuredVersionFields(sourceFile) {
|
|
123
|
+
for (const pattern of sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ObjectBindingPattern)) {
|
|
124
|
+
const type = pattern.getType();
|
|
125
|
+
if (!isVersionedEventConfig(type)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const destructuresVersionField = pattern
|
|
129
|
+
.getElements()
|
|
130
|
+
.some((element) => { var _a; var _b; return !element.getDotDotDotToken() &&
|
|
131
|
+
!isVersionedField(type, (_b = (_a = element.getPropertyNameNode()) === null || _a === void 0 ? void 0 : _a.getText()) !== null && _b !== void 0 ? _b : element.getName()); });
|
|
132
|
+
if (destructuresVersionField) {
|
|
133
|
+
console.warn(` [${location(pattern)}] Destructures a field of a version from a VersionedEventConfig. Destructure it from \`${RESOLVE_EVENT_CONFIG_NAME}(config)\` by hand.`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Finds `x.field` reads where `x` is a `VersionedEventConfig` and `field` belongs
|
|
139
|
+
* to a version of it, e.g. `actions`.
|
|
140
|
+
*/
|
|
141
|
+
function findVersionFieldReads(sourceFile) {
|
|
142
|
+
return sourceFile
|
|
143
|
+
.getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAccessExpression)
|
|
144
|
+
.filter((node) => {
|
|
145
|
+
const type = node.getExpression().getType();
|
|
146
|
+
return (isVersionedEventConfig(type) && !isVersionedField(type, node.getName()));
|
|
147
|
+
})
|
|
148
|
+
.filter((node) => {
|
|
149
|
+
if (node.hasQuestionDotToken()) {
|
|
150
|
+
console.warn(` [${location(node)}] Skipped optional chain \`${node.getText()}\`. Rewrite it to use \`${RESOLVE_EVENT_CONFIG_NAME}()\` by hand.`);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
if (isWriteTarget(node)) {
|
|
154
|
+
console.warn(` [${location(node)}] Skipped write to \`${node.getText()}\`. Set it in the version passed to \`defineConfig\` instead.`);
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns true if the node is a value handed over to something else: a call
|
|
162
|
+
* argument, a variable initializer, a returned value or an object property
|
|
163
|
+
* value. Only these get a contextual type that `EventConfig` can come from.
|
|
164
|
+
*/
|
|
165
|
+
function isHandedOver(node) {
|
|
166
|
+
const parent = node.getParent();
|
|
167
|
+
if (ts_morph_1.Node.isCallExpression(parent) || ts_morph_1.Node.isNewExpression(parent)) {
|
|
168
|
+
return parent.getArguments().includes(node);
|
|
169
|
+
}
|
|
170
|
+
return (((ts_morph_1.Node.isVariableDeclaration(parent) ||
|
|
171
|
+
ts_morph_1.Node.isPropertyAssignment(parent)) &&
|
|
172
|
+
parent.getInitializer() === node) ||
|
|
173
|
+
ts_morph_1.Node.isReturnStatement(parent));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Finds `VersionedEventConfig` values (or arrays of them) handed over where an
|
|
177
|
+
* `EventConfig` (or an array of them) is expected.
|
|
178
|
+
*/
|
|
179
|
+
function findValuesExpectingEventConfig(sourceFile) {
|
|
180
|
+
return sourceFile
|
|
181
|
+
.getDescendants()
|
|
182
|
+
.filter((node) => ts_morph_1.Node.isExpression(node))
|
|
183
|
+
.filter(isHandedOver)
|
|
184
|
+
.map((node) => {
|
|
185
|
+
const type = node.getType();
|
|
186
|
+
const expectedType = node.getContextualType();
|
|
187
|
+
if (!expectedType) {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
if (isVersionedEventConfig(type) && isEventConfig(expectedType)) {
|
|
191
|
+
return { node, isArray: false };
|
|
192
|
+
}
|
|
193
|
+
if (isArrayOf(type, isVersionedEventConfig) &&
|
|
194
|
+
isArrayOf(expectedType, isEventConfig)) {
|
|
195
|
+
return { node, isArray: true };
|
|
196
|
+
}
|
|
197
|
+
return undefined;
|
|
198
|
+
})
|
|
199
|
+
.filter((value) => value !== undefined)
|
|
200
|
+
.filter(({ node }) => {
|
|
201
|
+
if (node.getType().isNullable()) {
|
|
202
|
+
console.warn(` [${location(node)}] Skipped \`${node.getText()}\`, which may be undefined where an EventConfig is expected. Narrow it and pass \`${RESOLVE_EVENT_CONFIG_NAME}(...)\` by hand.`);
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
return true;
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/** Finds the value import of the toolkit's events module, if the file has one. */
|
|
209
|
+
function findToolkitImport(sourceFile) {
|
|
210
|
+
// A type-only import cannot carry a function, and a namespace import cannot
|
|
211
|
+
// be given named imports, so neither is extended
|
|
212
|
+
return sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE &&
|
|
213
|
+
!declaration.isTypeOnly() &&
|
|
214
|
+
!declaration.getNamespaceImport());
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Returns the name `resolveEventConfig` is called by in the file: its local
|
|
218
|
+
* alias when the file already imports it from the toolkit, `resolveEventConfig`
|
|
219
|
+
* when the name is free, or undefined when the file binds `resolveEventConfig`
|
|
220
|
+
* to something else.
|
|
221
|
+
*/
|
|
222
|
+
function findResolveEventConfigName(sourceFile) {
|
|
223
|
+
var _a, _b;
|
|
224
|
+
var _c;
|
|
225
|
+
const imported = (_a = findToolkitImport(sourceFile)) === null || _a === void 0 ? void 0 : _a.getNamedImports().find((namedImport) => namedImport.getName() === RESOLVE_EVENT_CONFIG_NAME);
|
|
226
|
+
if (imported) {
|
|
227
|
+
return (_c = (_b = imported.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : RESOLVE_EVENT_CONFIG_NAME;
|
|
228
|
+
}
|
|
229
|
+
if (sourceFile.getLocal(RESOLVE_EVENT_CONFIG_NAME)) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
return RESOLVE_EVENT_CONFIG_NAME;
|
|
233
|
+
}
|
|
234
|
+
function addResolveEventConfigImport(sourceFile) {
|
|
235
|
+
const toolkitImport = findToolkitImport(sourceFile);
|
|
236
|
+
if (!toolkitImport) {
|
|
237
|
+
const imports = sourceFile.getImportDeclarations();
|
|
238
|
+
const lastImport = imports[imports.length - 1];
|
|
239
|
+
// Inserted as text so the import keeps the country config's style
|
|
240
|
+
// (single quotes, no semicolon) rather than ts-morph's defaults
|
|
241
|
+
sourceFile.insertStatements(lastImport ? lastImport.getChildIndex() + 1 : 0, `import { ${RESOLVE_EVENT_CONFIG_NAME} } from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const alreadyImported = toolkitImport
|
|
245
|
+
.getNamedImports()
|
|
246
|
+
.some((namedImport) => namedImport.getName() === RESOLVE_EVENT_CONFIG_NAME);
|
|
247
|
+
if (alreadyImported) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const namedImports = toolkitImport.getNamedImports();
|
|
251
|
+
// ts-morph's addNamedImport collapses a multi-line import onto one line, so
|
|
252
|
+
// the import is rewritten keeping one name per line when it had them
|
|
253
|
+
if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
|
|
254
|
+
toolkitImport.addNamedImport(RESOLVE_EVENT_CONFIG_NAME);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const names = [
|
|
258
|
+
...namedImports.map((namedImport) => namedImport.getText()),
|
|
259
|
+
RESOLVE_EVENT_CONFIG_NAME
|
|
260
|
+
];
|
|
261
|
+
const specifiers = toolkitImport.getText().includes('\n')
|
|
262
|
+
? `{\n ${names.join(',\n ')}\n}`
|
|
263
|
+
: `{ ${names.join(', ')} }`;
|
|
264
|
+
toolkitImport.replaceWithText(`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Resolves `@opencrvs/toolkit/events` to `toolkitEventsPath`, keeping the country
|
|
268
|
+
* config's other `paths`. The country config still has the toolkit it upgrades from
|
|
269
|
+
* installed, whose types have no `VersionedEventConfig`, so the codemod reads the
|
|
270
|
+
* types of the toolkit running it instead.
|
|
271
|
+
*/
|
|
272
|
+
function withToolkitEventsAt(compilerOptions, toolkitEventsPath) {
|
|
273
|
+
return Object.assign(Object.assign({}, compilerOptions), { paths: Object.assign(Object.assign({}, compilerOptions.paths), { [TOOLKIT_EVENTS_MODULE]: [toolkitEventsPath] }) });
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Rewrites every use of a `VersionedEventConfig` as a flat `EventConfig` in the
|
|
277
|
+
* file to go through `resolveEventConfig()`.
|
|
278
|
+
*
|
|
279
|
+
* @returns the number of uses rewritten.
|
|
280
|
+
*/
|
|
281
|
+
function rewriteVersionedEventConfigUses(sourceFile) {
|
|
282
|
+
warnDestructuredVersionFields(sourceFile);
|
|
283
|
+
// Types are read before any rewrite, as a rewrite invalidates them
|
|
284
|
+
const uses = [
|
|
285
|
+
...findVersionFieldReads(sourceFile).map((node) => ({
|
|
286
|
+
node,
|
|
287
|
+
rewrite: (name) => `${name}(${node.getExpression().getText()}).${node.getName()}`
|
|
288
|
+
})),
|
|
289
|
+
...findValuesExpectingEventConfig(sourceFile).map(({ node, isArray }) => ({
|
|
290
|
+
node,
|
|
291
|
+
rewrite: (name) => isArray
|
|
292
|
+
? `${node.getText()}.map(${name})`
|
|
293
|
+
: `${name}(${node.getText()})`
|
|
294
|
+
}))
|
|
295
|
+
]
|
|
296
|
+
// Replace the innermost uses first, so replacing an outer use does not
|
|
297
|
+
// invalidate nodes still waiting to be replaced.
|
|
298
|
+
.sort((a, b) => b.node.getStart() - a.node.getStart() ||
|
|
299
|
+
a.node.getEnd() - b.node.getEnd());
|
|
300
|
+
if (uses.length === 0) {
|
|
301
|
+
return 0;
|
|
302
|
+
}
|
|
303
|
+
const resolveEventConfigName = findResolveEventConfigName(sourceFile);
|
|
304
|
+
if (!resolveEventConfigName) {
|
|
305
|
+
console.warn(` [${location(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.`);
|
|
306
|
+
return 0;
|
|
307
|
+
}
|
|
308
|
+
for (const { node, rewrite } of uses) {
|
|
309
|
+
const before = node.getText();
|
|
310
|
+
const after = rewrite(resolveEventConfigName);
|
|
311
|
+
console.log(` [${location(node)}] ${before} -> ${after}`);
|
|
312
|
+
node.replaceWithText(after);
|
|
313
|
+
}
|
|
314
|
+
addResolveEventConfigImport(sourceFile);
|
|
315
|
+
return uses.length;
|
|
316
|
+
}
|
|
317
|
+
function main() {
|
|
318
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
319
|
+
const srcDir = path_1.default.join(process.cwd(), 'src');
|
|
320
|
+
console.log(`Scanning for VersionedEventConfig used as an EventConfig in: ${srcDir}\n`);
|
|
321
|
+
const tsConfigFilePath = path_1.default.resolve(srcDir, '../tsconfig.json');
|
|
322
|
+
const project = new ts_morph_1.Project({
|
|
323
|
+
tsConfigFilePath,
|
|
324
|
+
skipAddingFilesFromTsConfig: false,
|
|
325
|
+
// The events module of this toolkit, next to the migrations in `dist`
|
|
326
|
+
compilerOptions: withToolkitEventsAt((0, ts_morph_1.getCompilerOptionsFromTsConfig)(tsConfigFilePath).options, path_1.default.resolve(__dirname, '../events'))
|
|
327
|
+
});
|
|
328
|
+
const sourceFiles = project.getSourceFiles().filter((sf) => {
|
|
329
|
+
const fp = sf.getFilePath();
|
|
330
|
+
return fp.includes('/src/') && !fp.includes('/node_modules/');
|
|
331
|
+
});
|
|
332
|
+
console.log(`Found ${sourceFiles.length} source file(s) to analyse.\n`);
|
|
333
|
+
let totalChanges = 0;
|
|
334
|
+
const modifiedFiles = [];
|
|
335
|
+
for (const sourceFile of sourceFiles) {
|
|
336
|
+
const changes = rewriteVersionedEventConfigUses(sourceFile);
|
|
337
|
+
if (changes > 0) {
|
|
338
|
+
totalChanges += changes;
|
|
339
|
+
modifiedFiles.push(sourceFile);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (modifiedFiles.length === 0) {
|
|
343
|
+
console.log('No VersionedEventConfig used as an EventConfig found. Nothing to do.');
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
console.log(`\nSaving ${modifiedFiles.length} modified file(s)...`);
|
|
347
|
+
for (const sourceFile of modifiedFiles) {
|
|
348
|
+
yield sourceFile.save();
|
|
349
|
+
console.log(` Saved: ${path_1.default.relative(process.cwd(), sourceFile.getFilePath())}`);
|
|
350
|
+
}
|
|
351
|
+
console.log(`\nDone. Rewrote ${totalChanges} use(s) across ${modifiedFiles.length} file(s).`);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers shared by the codemods, kept apart so a codemod can be removed
|
|
3
|
+
* without breaking the others.
|
|
4
|
+
*/
|
|
5
|
+
import { CallExpression, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph';
|
|
6
|
+
export declare function location(node: Node): string;
|
|
7
|
+
/**
|
|
8
|
+
* Returns true if the call is to the `defineConfig` published by the toolkit,
|
|
9
|
+
* as opposed to a function of the same name declared in the country config.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isToolkitDefineConfigCall(call: CallExpression): boolean;
|
|
12
|
+
/** Finds a property given by name, whether written `key: value` or shorthand. */
|
|
13
|
+
export declare function findProperty(literal: ObjectLiteralExpression, name: string): import("ts-morph").PropertyAssignment | import("ts-morph").ShorthandPropertyAssignment | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if the node may be the action of the given type. An action not
|
|
16
|
+
* written inline (a variable, a call, a spread) may be, unless its type shows
|
|
17
|
+
* it is some other action.
|
|
18
|
+
*/
|
|
19
|
+
export declare function mayBeAction(node: Node, actionType: string): boolean;
|
|
20
|
+
export declare function isAction(literal: ObjectLiteralExpression, actionType: string): boolean;
|
|
21
|
+
/** The whitespace the line the node starts on is indented by. */
|
|
22
|
+
export declare function indentationOf(node: Node): string;
|
|
23
|
+
/** The text from the position to the end of its line, without the line break. */
|
|
24
|
+
export declare function restOfLine(text: string, position: number): string;
|
|
25
|
+
export declare function isOnlyComments(text: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Where a property sits in its object literal. A property on lines of its own
|
|
28
|
+
* owns the comments on the lines above it and the comment after it on its last
|
|
29
|
+
* line, and they move with it.
|
|
30
|
+
*/
|
|
31
|
+
export interface PropertyLayout {
|
|
32
|
+
ownsLines: boolean;
|
|
33
|
+
/** The first comment above the property, or the property itself. */
|
|
34
|
+
start: number;
|
|
35
|
+
/** After the comma following the property, or the property when it has none. */
|
|
36
|
+
end: number;
|
|
37
|
+
hasComma: boolean;
|
|
38
|
+
/** The comment after the property on its last line, with the space before it. */
|
|
39
|
+
trailingComment: string;
|
|
40
|
+
}
|
|
41
|
+
export declare function layoutOf(property: Node): PropertyLayout;
|
|
42
|
+
export interface TextEdit {
|
|
43
|
+
start: number;
|
|
44
|
+
end: number;
|
|
45
|
+
text: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function insertAt(position: number, text: string): TextEdit;
|
|
48
|
+
/**
|
|
49
|
+
* Applies the edits to the file together, as editing a node through ts-morph
|
|
50
|
+
* invalidates nodes still waiting to be edited.
|
|
51
|
+
*/
|
|
52
|
+
export declare function applyTextEdits(sourceFile: SourceFile, edits: TextEdit[]): void;
|
|
53
|
+
/**
|
|
54
|
+
* Returns the name a toolkit export is called by in the file: its local alias
|
|
55
|
+
* when the file already imports it from the toolkit, its own name when the
|
|
56
|
+
* name is free, or undefined when the file binds that name to something else.
|
|
57
|
+
*/
|
|
58
|
+
export declare function findToolkitImportName(sourceFile: SourceFile, name: string): string | undefined;
|
|
59
|
+
/** Adds the toolkit export to the file's toolkit import, unless already imported. */
|
|
60
|
+
export declare function addToolkitImport(sourceFile: SourceFile, name: string): void;
|
|
61
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/migrations/utils.ts"],"names":[],"mappings":"AAWA;;;GAGG;AAEH,OAAO,EACL,cAAc,EACd,IAAI,EACJ,uBAAuB,EACvB,UAAU,EAGX,MAAM,UAAU,CAAA;AAMjB,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,UAMlC;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,cAAc,WAW7D;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,OAAO,EAAE,uBAAuB,EAAE,IAAI,EAAE,MAAM,sGAO1E;AA0BD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,WAkBzD;AAED,wBAAgB,QAAQ,CAAC,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,MAAM,WAO5E;AAED,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,UAKvC;AAED,iFAAiF;AACjF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,UAIxD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,WAE1C;AAED;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAA;IAClB,oEAAoE;IACpE,KAAK,EAAE,MAAM,CAAA;IACb,gFAAgF;IAChF,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAAE,OAAO,CAAA;IACjB,iFAAiF;IACjF,eAAe,EAAE,MAAM,CAAA;CACxB;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,IAAI,GAAG,cAAc,CA+BvD;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;CACb;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,QAAQ,CAEjE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,QAYvE;AAcD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,sBAczE;AAED,qFAAqF;AACrF,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,QA4CpE"}
|
|
@@ -0,0 +1,218 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.location = location;
|
|
17
|
+
exports.isToolkitDefineConfigCall = isToolkitDefineConfigCall;
|
|
18
|
+
exports.findProperty = findProperty;
|
|
19
|
+
exports.mayBeAction = mayBeAction;
|
|
20
|
+
exports.isAction = isAction;
|
|
21
|
+
exports.indentationOf = indentationOf;
|
|
22
|
+
exports.restOfLine = restOfLine;
|
|
23
|
+
exports.isOnlyComments = isOnlyComments;
|
|
24
|
+
exports.layoutOf = layoutOf;
|
|
25
|
+
exports.insertAt = insertAt;
|
|
26
|
+
exports.applyTextEdits = applyTextEdits;
|
|
27
|
+
exports.findToolkitImportName = findToolkitImportName;
|
|
28
|
+
exports.addToolkitImport = addToolkitImport;
|
|
29
|
+
/**
|
|
30
|
+
* Helpers shared by the codemods, kept apart so a codemod can be removed
|
|
31
|
+
* without breaking the others.
|
|
32
|
+
*/
|
|
33
|
+
const ts_morph_1 = require("ts-morph");
|
|
34
|
+
const path_1 = __importDefault(require("path"));
|
|
35
|
+
const DEFINE_CONFIG_NAME = 'defineConfig';
|
|
36
|
+
const TOOLKIT_EVENTS_MODULE = '@opencrvs/toolkit/events';
|
|
37
|
+
function location(node) {
|
|
38
|
+
const relPath = path_1.default.relative(process.cwd(), node.getSourceFile().getFilePath());
|
|
39
|
+
return `${relPath}:${node.getStartLineNumber()}`;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns true if the call is to the `defineConfig` published by the toolkit,
|
|
43
|
+
* as opposed to a function of the same name declared in the country config.
|
|
44
|
+
*/
|
|
45
|
+
function isToolkitDefineConfigCall(call) {
|
|
46
|
+
var _a;
|
|
47
|
+
const symbol = call.getExpression().getSymbol();
|
|
48
|
+
const resolved = (_a = symbol === null || symbol === void 0 ? void 0 : symbol.getAliasedSymbol()) !== null && _a !== void 0 ? _a : symbol;
|
|
49
|
+
if ((resolved === null || resolved === void 0 ? void 0 : resolved.getName()) !== DEFINE_CONFIG_NAME) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return resolved
|
|
53
|
+
.getDeclarations()
|
|
54
|
+
.some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
55
|
+
}
|
|
56
|
+
/** Finds a property given by name, whether written `key: value` or shorthand. */
|
|
57
|
+
function findProperty(literal, name) {
|
|
58
|
+
const property = literal.getProperty(name);
|
|
59
|
+
return ts_morph_1.Node.isPropertyAssignment(property) ||
|
|
60
|
+
ts_morph_1.Node.isShorthandPropertyAssignment(property)
|
|
61
|
+
? property
|
|
62
|
+
: undefined;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns false only if the action type's `type` is known to be some other
|
|
66
|
+
* action: a string literal, or a union of them, other than the given one.
|
|
67
|
+
*/
|
|
68
|
+
function mayHaveActionType(actionType, node, expectedActionType) {
|
|
69
|
+
const typeProperty = actionType.getProperty('type');
|
|
70
|
+
if (!typeProperty) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
const type = typeProperty.getTypeAtLocation(node);
|
|
74
|
+
const values = type.isUnion() ? type.getUnionTypes() : [type];
|
|
75
|
+
return !values.every((value) => value.isStringLiteral() && value.getLiteralValue() !== expectedActionType);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns true if the node may be the action of the given type. An action not
|
|
79
|
+
* written inline (a variable, a call, a spread) may be, unless its type shows
|
|
80
|
+
* it is some other action.
|
|
81
|
+
*/
|
|
82
|
+
function mayBeAction(node, actionType) {
|
|
83
|
+
if (ts_morph_1.Node.isObjectLiteralExpression(node)) {
|
|
84
|
+
return isAction(node, actionType);
|
|
85
|
+
}
|
|
86
|
+
if (ts_morph_1.Node.isSpreadElement(node)) {
|
|
87
|
+
const arrayType = node.getExpression().getType();
|
|
88
|
+
const elementTypes = arrayType.isTuple()
|
|
89
|
+
? arrayType.getTupleElements()
|
|
90
|
+
: [arrayType.getArrayElementType()];
|
|
91
|
+
return elementTypes.some((elementType) => !elementType || mayHaveActionType(elementType, node, actionType));
|
|
92
|
+
}
|
|
93
|
+
return mayHaveActionType(node.getType(), node, actionType);
|
|
94
|
+
}
|
|
95
|
+
function isAction(literal, actionType) {
|
|
96
|
+
const type = findProperty(literal, 'type');
|
|
97
|
+
return (ts_morph_1.Node.isPropertyAssignment(type) &&
|
|
98
|
+
type.getInitializerOrThrow().getType().getLiteralValue() === actionType);
|
|
99
|
+
}
|
|
100
|
+
/** The whitespace the line the node starts on is indented by. */
|
|
101
|
+
function indentationOf(node) {
|
|
102
|
+
var _a;
|
|
103
|
+
var _b;
|
|
104
|
+
const text = node.getSourceFile().getFullText();
|
|
105
|
+
const lineStart = text.lastIndexOf('\n', node.getStart() - 1) + 1;
|
|
106
|
+
return (_b = (_a = /^[ \t]*/.exec(text.slice(lineStart))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : '';
|
|
107
|
+
}
|
|
108
|
+
/** The text from the position to the end of its line, without the line break. */
|
|
109
|
+
function restOfLine(text, position) {
|
|
110
|
+
const lineEnd = text.indexOf('\n', position);
|
|
111
|
+
return text.slice(position, lineEnd === -1 ? text.length : lineEnd);
|
|
112
|
+
}
|
|
113
|
+
function isOnlyComments(text) {
|
|
114
|
+
return /^\s*(?:\/\/.*|\/\*.*?\*\/\s*)*$/.test(text);
|
|
115
|
+
}
|
|
116
|
+
function layoutOf(property) {
|
|
117
|
+
var _a;
|
|
118
|
+
var _b;
|
|
119
|
+
const text = property.getSourceFile().getFullText();
|
|
120
|
+
const next = property.getNextSibling();
|
|
121
|
+
const hasComma = (next === null || next === void 0 ? void 0 : next.getKind()) === ts_morph_1.SyntaxKind.CommaToken;
|
|
122
|
+
const end = next && hasComma ? next.getEnd() : property.getEnd();
|
|
123
|
+
const trailing = restOfLine(text, end);
|
|
124
|
+
// A comment on the line the previous property ends on is that property's
|
|
125
|
+
const lineBreak = text
|
|
126
|
+
.slice(property.getFullStart(), property.getStart())
|
|
127
|
+
.indexOf('\n');
|
|
128
|
+
if (lineBreak === -1 || !isOnlyComments(trailing)) {
|
|
129
|
+
return {
|
|
130
|
+
ownsLines: false,
|
|
131
|
+
start: property.getStart(true),
|
|
132
|
+
end,
|
|
133
|
+
hasComma,
|
|
134
|
+
trailingComment: ''
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const firstLine = property.getFullStart() + lineBreak + 1;
|
|
138
|
+
return {
|
|
139
|
+
ownsLines: true,
|
|
140
|
+
start: firstLine + ((_b = (_a = /^\s*/.exec(text.slice(firstLine))) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0),
|
|
141
|
+
end,
|
|
142
|
+
hasComma,
|
|
143
|
+
trailingComment: trailing.trimEnd()
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function insertAt(position, text) {
|
|
147
|
+
return { start: position, end: position, text };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Applies the edits to the file together, as editing a node through ts-morph
|
|
151
|
+
* invalidates nodes still waiting to be edited.
|
|
152
|
+
*/
|
|
153
|
+
function applyTextEdits(sourceFile, edits) {
|
|
154
|
+
// Applied from the end of the file, so each edit leaves the positions of
|
|
155
|
+
// the ones still to apply untouched
|
|
156
|
+
const text = [...edits]
|
|
157
|
+
.sort((a, b) => b.start - a.start)
|
|
158
|
+
.reduce((result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end), sourceFile.getFullText());
|
|
159
|
+
sourceFile.replaceWithText(text);
|
|
160
|
+
}
|
|
161
|
+
/** Finds the value import of the toolkit's events module, if the file has one. */
|
|
162
|
+
function findToolkitImport(sourceFile) {
|
|
163
|
+
// A type-only import cannot carry a function, and a namespace import cannot
|
|
164
|
+
// be given named imports, so neither is extended
|
|
165
|
+
return sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE &&
|
|
166
|
+
!declaration.isTypeOnly() &&
|
|
167
|
+
!declaration.getNamespaceImport());
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Returns the name a toolkit export is called by in the file: its local alias
|
|
171
|
+
* when the file already imports it from the toolkit, its own name when the
|
|
172
|
+
* name is free, or undefined when the file binds that name to something else.
|
|
173
|
+
*/
|
|
174
|
+
function findToolkitImportName(sourceFile, name) {
|
|
175
|
+
var _a, _b;
|
|
176
|
+
var _c;
|
|
177
|
+
const imported = (_a = findToolkitImport(sourceFile)) === null || _a === void 0 ? void 0 : _a.getNamedImports().find((namedImport) => namedImport.getName() === name);
|
|
178
|
+
if (imported) {
|
|
179
|
+
return (_c = (_b = imported.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : name;
|
|
180
|
+
}
|
|
181
|
+
if (sourceFile.getLocal(name)) {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
return name;
|
|
185
|
+
}
|
|
186
|
+
/** Adds the toolkit export to the file's toolkit import, unless already imported. */
|
|
187
|
+
function addToolkitImport(sourceFile, name) {
|
|
188
|
+
const toolkitImport = findToolkitImport(sourceFile);
|
|
189
|
+
if (!toolkitImport) {
|
|
190
|
+
const imports = sourceFile.getImportDeclarations();
|
|
191
|
+
const lastImport = imports[imports.length - 1];
|
|
192
|
+
// Inserted as text so the import keeps the country config's style
|
|
193
|
+
// (single quotes, no semicolon) rather than ts-morph's defaults
|
|
194
|
+
sourceFile.insertStatements(lastImport ? lastImport.getChildIndex() + 1 : 0, `import { ${name} } from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const alreadyImported = toolkitImport
|
|
198
|
+
.getNamedImports()
|
|
199
|
+
.some((namedImport) => namedImport.getName() === name);
|
|
200
|
+
if (alreadyImported) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const namedImports = toolkitImport.getNamedImports();
|
|
204
|
+
// ts-morph's addNamedImport collapses a multi-line import onto one line, so
|
|
205
|
+
// the import is rewritten keeping one name per line when it had them
|
|
206
|
+
if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
|
|
207
|
+
toolkitImport.addNamedImport(name);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const names = [
|
|
211
|
+
...namedImports.map((namedImport) => namedImport.getText()),
|
|
212
|
+
name
|
|
213
|
+
];
|
|
214
|
+
const specifiers = toolkitImport.getText().includes('\n')
|
|
215
|
+
? `{\n ${names.join(',\n ')}\n}`
|
|
216
|
+
: `{ ${names.join(', ')} }`;
|
|
217
|
+
toolkitImport.replaceWithText(`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
218
|
+
}
|