@ethlete/query 6.0.0-next.3 → 6.0.0-next.31
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/CHANGELOG.md +454 -0
- package/fesm2022/ethlete-query-testing.mjs +282 -5
- package/fesm2022/ethlete-query-testing.mjs.map +1 -1
- package/fesm2022/ethlete-query.mjs +9926 -5013
- package/fesm2022/ethlete-query.mjs.map +1 -1
- package/generators/deprecate-legacy-queries/migration.js +77 -0
- package/generators/deprecate-legacy-queries/schema.json +26 -0
- package/generators/generators.json +15 -0
- package/generators/migrate-query-client-features/migration.js +142 -0
- package/generators/migrate-query-client-features/report.js +136 -0
- package/generators/migrate-query-client-features/schema.json +26 -0
- package/generators/migrate-query-opt-in-features/migration.js +182 -0
- package/generators/migrate-query-opt-in-features/report.js +143 -0
- package/generators/migrate-query-opt-in-features/schema.json +31 -0
- package/generators/migrate-to-query-v3/auth-provider-scaffold.js +131 -0
- package/generators/migrate-to-query-v3/cleanup-migration.js +293 -0
- package/generators/migrate-to-query-v3/http-client-check.js +93 -0
- package/generators/migrate-to-query-v3/legacy-prepare-migration.js +865 -0
- package/generators/migrate-to-query-v3/legacy-query-creator-migration.js +608 -0
- package/generators/migrate-to-query-v3/migration-scope.js +41 -0
- package/generators/migrate-to-query-v3/migration.js +28 -2932
- package/generators/migrate-to-query-v3/module-graph.js +153 -0
- package/generators/migrate-to-query-v3/query-client-migration.js +526 -0
- package/generators/migrate-to-query-v3/rename-symbols.js +193 -0
- package/generators/migrate-to-query-v3/report.js +136 -0
- package/generators/migrate-to-query-v3/schema.json +12 -0
- package/generators/migrate-to-query-v3/shared.js +142 -0
- package/generators/prep-for-query-v3/migration.js +3 -3
- package/package.json +26 -13
- package/types/ethlete-query-testing.d.ts +141 -12
- package/types/ethlete-query.d.ts +6964 -3128
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
export const QUERY_OPT_IN_FEATURES_REPORT_PATH = 'query-opt-in-features-migration-tasks.md';
|
|
2
|
+
export const MIGRATION_TASK_KIND = {
|
|
3
|
+
WARNING: 'warning',
|
|
4
|
+
MANUAL_REVIEW: 'manual-review',
|
|
5
|
+
FOLLOW_UP: 'follow-up',
|
|
6
|
+
};
|
|
7
|
+
const renderTaskLocations = (locations) => {
|
|
8
|
+
if (locations.length === 0) {
|
|
9
|
+
return '- None recorded';
|
|
10
|
+
}
|
|
11
|
+
return locations.map((location) => `- ${location.filePath}${location.line ? `:${location.line}` : ''}`).join('\n');
|
|
12
|
+
};
|
|
13
|
+
export class QueryOptInFeaturesMigrationReport {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.tasks = [];
|
|
16
|
+
}
|
|
17
|
+
addTask(input) {
|
|
18
|
+
const dedupeKey = input.dedupeKey ?? `${input.kind}:${input.title}:${input.summary}:${input.action}`;
|
|
19
|
+
const existing = this.tasks.find((task) => task.dedupeKey === dedupeKey);
|
|
20
|
+
if (existing) {
|
|
21
|
+
existing.locations.push(...(input.locations ?? []));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
this.tasks.push({
|
|
25
|
+
id: `QOF-${String(this.tasks.length + 1).padStart(3, '0')}`,
|
|
26
|
+
kind: input.kind,
|
|
27
|
+
title: input.title,
|
|
28
|
+
summary: input.summary,
|
|
29
|
+
action: input.action,
|
|
30
|
+
locations: input.locations ?? [],
|
|
31
|
+
source: input.source,
|
|
32
|
+
dedupeKey,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
addWarning(input) {
|
|
36
|
+
this.addTask({ ...input, kind: MIGRATION_TASK_KIND.WARNING });
|
|
37
|
+
}
|
|
38
|
+
addManualReview(input) {
|
|
39
|
+
this.addTask({ ...input, kind: MIGRATION_TASK_KIND.MANUAL_REVIEW });
|
|
40
|
+
}
|
|
41
|
+
addFollowUp(input) {
|
|
42
|
+
this.addTask({ ...input, kind: MIGRATION_TASK_KIND.FOLLOW_UP });
|
|
43
|
+
}
|
|
44
|
+
printSummary() {
|
|
45
|
+
if (this.tasks.length === 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
console.warn(`\n⚠️ Generated ${this.tasks.length} follow-up task${this.tasks.length === 1 ? '' : 's'}:`);
|
|
49
|
+
this.tasks.forEach((task) => {
|
|
50
|
+
const locations = task.locations
|
|
51
|
+
.map((location) => `${location.filePath}${location.line ? `:${location.line}` : ''}`)
|
|
52
|
+
.join(', ');
|
|
53
|
+
console.warn(` - ${task.id} [${task.kind}] ${task.title}${locations ? ` (${locations})` : ''}`);
|
|
54
|
+
});
|
|
55
|
+
console.warn(`\n📄 See ${QUERY_OPT_IN_FEATURES_REPORT_PATH} for the AI-ready follow-up list.`);
|
|
56
|
+
}
|
|
57
|
+
writeToTree(tree) {
|
|
58
|
+
tree.write(QUERY_OPT_IN_FEATURES_REPORT_PATH, renderMigrationReportMarkdown(this.tasks));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export const renderMigrationReportMarkdown = (tasks) => {
|
|
62
|
+
const header = [
|
|
63
|
+
'# Query Opt-In Features Migration Follow-Up',
|
|
64
|
+
'',
|
|
65
|
+
'This file is generated by the `migrate-query-opt-in-features` generator.',
|
|
66
|
+
'',
|
|
67
|
+
'## Context',
|
|
68
|
+
'',
|
|
69
|
+
'Two things that used to be on by default are now opt-in features:',
|
|
70
|
+
'',
|
|
71
|
+
'- error-response parsing (HTML error pages, Symfony / class-validator violation lists) and the',
|
|
72
|
+
' default retry policy, now `withEthleteApiErrors()` on `createQueryClient`,',
|
|
73
|
+
'- bearer-auth multi-tab sync and leader election, now `withBearerAuthMultiTabSync()` on',
|
|
74
|
+
' `createBearerAuthProvider` (the `multiTabSync` config option is gone).',
|
|
75
|
+
'',
|
|
76
|
+
'The codemod is **behavior preserving**: it adds the features every call site used to get for free.',
|
|
77
|
+
'That is rarely what an app wants long term — an API that never answers with an HTML error page, or',
|
|
78
|
+
'an app that is only ever open in one tab, ships that code for nothing. Review each added feature',
|
|
79
|
+
'and delete the ones you do not need; `withEthleteApiErrors()` in particular splits into',
|
|
80
|
+
'`withHtmlErrorParsing()`, `withSymfonyErrors()` and `withDefaultRetry()`.',
|
|
81
|
+
'',
|
|
82
|
+
'## AI Instructions',
|
|
83
|
+
'',
|
|
84
|
+
'1. Re-read the referenced files before changing anything. The migration output is only a starting point.',
|
|
85
|
+
'2. Prefer minimal fixes that align with the current runtime APIs in `@ethlete/query`.',
|
|
86
|
+
'3. Update or remove a task only after the code and tests confirm that it is resolved.',
|
|
87
|
+
'4. If a task is still ambiguous after inspection, keep it open and append a short note instead of guessing.',
|
|
88
|
+
'',
|
|
89
|
+
'## Task Format',
|
|
90
|
+
'',
|
|
91
|
+
'- `Status`: always treat generated tasks as `open` until verified manually or by tests.',
|
|
92
|
+
'- `Kind`: `warning`, `manual-review`, or `follow-up`.',
|
|
93
|
+
'- `Source`: which migration phase raised the task.',
|
|
94
|
+
'',
|
|
95
|
+
].join('\n');
|
|
96
|
+
if (tasks.length === 0) {
|
|
97
|
+
return `${header}## Tasks\n\nNo open follow-up tasks were recorded during this migration run.\n`;
|
|
98
|
+
}
|
|
99
|
+
const tasksSection = tasks
|
|
100
|
+
.map((task) => [
|
|
101
|
+
`### ${task.id} - ${task.title}`,
|
|
102
|
+
'',
|
|
103
|
+
'- Status: open',
|
|
104
|
+
`- Kind: ${task.kind}`,
|
|
105
|
+
`- Source: ${task.source}`,
|
|
106
|
+
`- Summary: ${task.summary}`,
|
|
107
|
+
`- Suggested action: ${task.action}`,
|
|
108
|
+
'- Locations:',
|
|
109
|
+
renderTaskLocations(task.locations),
|
|
110
|
+
'',
|
|
111
|
+
].join('\n'))
|
|
112
|
+
.join('');
|
|
113
|
+
const machineSection = [
|
|
114
|
+
'## Machine Summary',
|
|
115
|
+
'',
|
|
116
|
+
'```yaml',
|
|
117
|
+
'tasks:',
|
|
118
|
+
...tasks.flatMap((task) => {
|
|
119
|
+
const nestedLocations = task.locations.flatMap((location) => {
|
|
120
|
+
const lines = [` - filePath: ${location.filePath}`];
|
|
121
|
+
if (location.line) {
|
|
122
|
+
lines.push(` line: ${location.line}`);
|
|
123
|
+
}
|
|
124
|
+
return lines;
|
|
125
|
+
});
|
|
126
|
+
return [
|
|
127
|
+
` - id: ${task.id}`,
|
|
128
|
+
` status: open`,
|
|
129
|
+
` kind: ${task.kind}`,
|
|
130
|
+
` source: ${task.source}`,
|
|
131
|
+
` title: ${task.title}`,
|
|
132
|
+
` summary: ${task.summary}`,
|
|
133
|
+
` action: ${task.action}`,
|
|
134
|
+
` locations:`,
|
|
135
|
+
...(nestedLocations.length > 0 ? nestedLocations : [' []']),
|
|
136
|
+
];
|
|
137
|
+
}),
|
|
138
|
+
'```',
|
|
139
|
+
'',
|
|
140
|
+
].join('\n');
|
|
141
|
+
return `${header}## Tasks\n\n${tasksSection}${machineSection}`;
|
|
142
|
+
};
|
|
143
|
+
//# sourceMappingURL=report.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"$id": "migrate-query-opt-in-features",
|
|
4
|
+
"title": "Migrate query clients and auth providers to opt-in features",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"projects": {
|
|
8
|
+
"type": "array",
|
|
9
|
+
"items": { "type": "string" },
|
|
10
|
+
"description": "Nx projects to migrate. Defaults to every project in the workspace.",
|
|
11
|
+
"alias": "project"
|
|
12
|
+
},
|
|
13
|
+
"include": {
|
|
14
|
+
"type": "array",
|
|
15
|
+
"items": { "type": "string" },
|
|
16
|
+
"description": "Path prefixes to migrate, relative to the workspace root (e.g. 'libs/queries'). Unioned with `projects`. Defaults to the whole workspace.",
|
|
17
|
+
"alias": "includePath"
|
|
18
|
+
},
|
|
19
|
+
"reportOnly": {
|
|
20
|
+
"type": "boolean",
|
|
21
|
+
"description": "Only list the affected call sites in the report file, without changing any code.",
|
|
22
|
+
"default": false
|
|
23
|
+
},
|
|
24
|
+
"skipFormat": {
|
|
25
|
+
"type": "boolean",
|
|
26
|
+
"description": "Skip formatting files after migration",
|
|
27
|
+
"default": false
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"required": []
|
|
31
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { createSourceFile } from './shared.js';
|
|
3
|
+
const REFRESH_CONFIG_KEYS = new Set([
|
|
4
|
+
'queryCreator',
|
|
5
|
+
'responseAdapter',
|
|
6
|
+
'requestArgsAdapter',
|
|
7
|
+
'cookieName',
|
|
8
|
+
'cookieDomain',
|
|
9
|
+
'cookieExpiresInDays',
|
|
10
|
+
'expiresInPropertyName',
|
|
11
|
+
]);
|
|
12
|
+
export const collectV2BearerAuthConfigs = (tree, scope) => {
|
|
13
|
+
const configs = [];
|
|
14
|
+
scope.visit(tree, (filePath) => {
|
|
15
|
+
if (!filePath.endsWith('.ts') || filePath.endsWith('.spec.ts')) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const content = tree.read(filePath, 'utf-8');
|
|
19
|
+
if (!content || !content.includes('V2BearerAuthProvider')) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const sourceFile = createSourceFile(content, filePath);
|
|
23
|
+
const visit = (node) => {
|
|
24
|
+
if (ts.isNewExpression(node) &&
|
|
25
|
+
ts.isIdentifier(node.expression) &&
|
|
26
|
+
node.expression.text === 'V2BearerAuthProvider') {
|
|
27
|
+
const configArgument = node.arguments?.[0];
|
|
28
|
+
if (configArgument && ts.isObjectLiteralExpression(configArgument)) {
|
|
29
|
+
configs.push(readConfig(configArgument, sourceFile, filePath, node));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
ts.forEachChild(node, visit);
|
|
33
|
+
};
|
|
34
|
+
visit(sourceFile);
|
|
35
|
+
});
|
|
36
|
+
return configs;
|
|
37
|
+
};
|
|
38
|
+
const readConfig = (configArgument, sourceFile, filePath, node) => {
|
|
39
|
+
const config = {
|
|
40
|
+
filePath,
|
|
41
|
+
line: sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1,
|
|
42
|
+
};
|
|
43
|
+
const refreshConfig = configArgument.properties.find((property) => ts.isPropertyAssignment(property) && ts.isIdentifier(property.name) && property.name.text === 'refreshConfig');
|
|
44
|
+
if (!refreshConfig ||
|
|
45
|
+
!ts.isPropertyAssignment(refreshConfig) ||
|
|
46
|
+
!ts.isObjectLiteralExpression(refreshConfig.initializer)) {
|
|
47
|
+
return config;
|
|
48
|
+
}
|
|
49
|
+
refreshConfig.initializer.properties.forEach((property) => {
|
|
50
|
+
if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const key = property.name.text;
|
|
54
|
+
if (!REFRESH_CONFIG_KEYS.has(key)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const text = property.initializer.getText(sourceFile);
|
|
58
|
+
if (key === 'queryCreator') {
|
|
59
|
+
if (ts.isIdentifier(property.initializer)) {
|
|
60
|
+
config.refreshCreatorName = property.initializer.text;
|
|
61
|
+
}
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
config[key] = text;
|
|
65
|
+
});
|
|
66
|
+
return config;
|
|
67
|
+
};
|
|
68
|
+
const REFRESH_QUERY_KEY = 'tokenRefresh';
|
|
69
|
+
/**
|
|
70
|
+
* Renders the `queries` / `features` arguments of a v3 provider from a v2 config.
|
|
71
|
+
*
|
|
72
|
+
* The mapping is close to 1:1 - `queryCreator` → `withRefreshQuery`, `responseAdapter` →
|
|
73
|
+
* `extractTokens`, `cookieName` → `withPersistentAuth({ cookie })` - but the adapters are copied
|
|
74
|
+
* verbatim from v2 and their parameter shapes differ, so each one is emitted with a TODO rather
|
|
75
|
+
* than presented as finished.
|
|
76
|
+
*/
|
|
77
|
+
export const renderAuthProviderBody = (config) => {
|
|
78
|
+
if (!config?.refreshCreatorName) {
|
|
79
|
+
return {
|
|
80
|
+
queries: ' queries: [],',
|
|
81
|
+
features: '',
|
|
82
|
+
importsNeeded: [],
|
|
83
|
+
isScaffolded: false,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const refreshEntries = [` queryCreator: ${config.refreshCreatorName},`];
|
|
87
|
+
if (config.responseAdapter) {
|
|
88
|
+
refreshEntries.push(` // TODO(query-v3): v2 responseAdapter. It must return { accessToken, refreshToken } - v2 returned { token, refreshToken }.`, ` extractTokens: ${config.responseAdapter},`);
|
|
89
|
+
}
|
|
90
|
+
if (config.requestArgsAdapter) {
|
|
91
|
+
refreshEntries.push(` // TODO(query-v3): v2 requestArgsAdapter took { token, refreshToken }; buildArgs takes the refresh token alone.`, ` buildArgs: ${config.requestArgsAdapter},`);
|
|
92
|
+
}
|
|
93
|
+
if (config.expiresInPropertyName) {
|
|
94
|
+
refreshEntries.push(` expiresInPropertyName: ${config.expiresInPropertyName},`);
|
|
95
|
+
}
|
|
96
|
+
const queries = [
|
|
97
|
+
' queries: [',
|
|
98
|
+
` // Derived from the v2 BearerAuthProvider in ${config.filePath}:${config.line}. Verify before shipping.`,
|
|
99
|
+
` withRefreshQuery('${REFRESH_QUERY_KEY}', {`,
|
|
100
|
+
...refreshEntries,
|
|
101
|
+
' }),',
|
|
102
|
+
` // TODO(query-v3): add withAuthenticationQuery('login', { queryCreator: … }) for the login query.`,
|
|
103
|
+
' ],',
|
|
104
|
+
].join('\n');
|
|
105
|
+
const importsNeeded = ['withRefreshQuery'];
|
|
106
|
+
if (!config.cookieName) {
|
|
107
|
+
return { queries, features: '', importsNeeded, isScaffolded: true };
|
|
108
|
+
}
|
|
109
|
+
const cookieEntries = [` name: ${config.cookieName},`];
|
|
110
|
+
if (config.cookieDomain)
|
|
111
|
+
cookieEntries.push(` domain: ${config.cookieDomain},`);
|
|
112
|
+
if (config.cookieExpiresInDays)
|
|
113
|
+
cookieEntries.push(` expiresInDays: ${config.cookieExpiresInDays},`);
|
|
114
|
+
const features = [
|
|
115
|
+
' features: [',
|
|
116
|
+
' withPersistentAuth({',
|
|
117
|
+
' cookie: {',
|
|
118
|
+
...cookieEntries,
|
|
119
|
+
' },',
|
|
120
|
+
' autoLogin: {',
|
|
121
|
+
` queryKey: '${REFRESH_QUERY_KEY}',`,
|
|
122
|
+
` // TODO(query-v3): shape the auto-login request from the cookie value.`,
|
|
123
|
+
' buildArgs: (token) => ({ body: { refreshToken: token } }),',
|
|
124
|
+
' },',
|
|
125
|
+
' }),',
|
|
126
|
+
' ],',
|
|
127
|
+
].join('\n');
|
|
128
|
+
importsNeeded.push('withPersistentAuth');
|
|
129
|
+
return { queries, features, importsNeeded, isScaffolded: true };
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=auth-provider-scaffold.js.map
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import { createSourceFile, ensureImportFromQuery, ensureNamedImports, getLineNumberFromPosition } from './shared.js';
|
|
3
|
+
/**
|
|
4
|
+
* Points existing devtools usage at the v3 components instead of deleting it.
|
|
5
|
+
*
|
|
6
|
+
* Both versions render `<et-query-devtools>`, so templates need no change at all - only the provider
|
|
7
|
+
* call and the component's import move. Stripping the markup (as this phase used to) threw away
|
|
8
|
+
* something that would have kept working, and left every migrated app without devtools until someone
|
|
9
|
+
* noticed they were gone.
|
|
10
|
+
*/
|
|
11
|
+
export const migrateDevtoolsUsage = (tree, scope, report) => {
|
|
12
|
+
const updatedFiles = [];
|
|
13
|
+
const filesNeedingComponentsPackage = [];
|
|
14
|
+
scope.visit(tree, (filePath) => {
|
|
15
|
+
if (!filePath.endsWith('.ts')) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const content = tree.read(filePath, 'utf-8');
|
|
19
|
+
if (!content ||
|
|
20
|
+
(!content.includes('provideQueryClientForDevtools') && !content.includes('QueryDevtoolsComponent'))) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const { content: nextContent, importsComponent } = migrateDevtoolsInFile(content);
|
|
24
|
+
if (nextContent === content) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
tree.write(filePath, nextContent);
|
|
28
|
+
updatedFiles.push(filePath);
|
|
29
|
+
if (importsComponent) {
|
|
30
|
+
filesNeedingComponentsPackage.push(filePath);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
if (updatedFiles.length === 0) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
console.log('\n✅ Migrated query devtools usage to v3 in:');
|
|
37
|
+
updatedFiles.forEach((filePath) => console.log(` - ${filePath}`));
|
|
38
|
+
if (filesNeedingComponentsPackage.length > 0) {
|
|
39
|
+
// The component moved packages, which is the one part of this rewrite that can fail outside the
|
|
40
|
+
// file being edited: an app that only ever depended on `@ethlete/query` now needs `@ethlete/components`.
|
|
41
|
+
report.addManualReview({
|
|
42
|
+
title: 'Add @ethlete/components for the query devtools',
|
|
43
|
+
summary: '`QueryDevtoolsComponent` now lives in `@ethlete/components`. The imports were rewritten, but the package may not be a dependency of these projects yet.',
|
|
44
|
+
action: 'Run `yarn add @ethlete/components` where needed, then `yarn install` and re-lint the affected libs so the `@nx/dependency-checks` rule sees the new dependency.',
|
|
45
|
+
locations: filesNeedingComponentsPackage.map((filePath) => ({ filePath })),
|
|
46
|
+
source: 'cleanup-migration',
|
|
47
|
+
dedupeKey: 'devtools-components-dependency',
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
export const replaceAnyQueryWithLegacy = (tree, scope) => {
|
|
52
|
+
const updatedFiles = [];
|
|
53
|
+
scope.visit(tree, (filePath) => {
|
|
54
|
+
if (!filePath.endsWith('.ts') || filePath.endsWith('.spec.ts')) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const content = tree.read(filePath, 'utf-8');
|
|
58
|
+
if (!content) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const nextContent = replaceAnyQueryInFile(content);
|
|
62
|
+
if (nextContent !== content) {
|
|
63
|
+
tree.write(filePath, nextContent);
|
|
64
|
+
updatedFiles.push(filePath);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
if (updatedFiles.length > 0) {
|
|
68
|
+
console.log(`\n✅ Replaced legacy AnyV2Query aliases in ${updatedFiles.length} files`);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export const migrateEmptyPrepareCalls = (tree, scope) => {
|
|
72
|
+
const updatedFiles = [];
|
|
73
|
+
scope.visit(tree, (filePath) => {
|
|
74
|
+
if (!filePath.endsWith('.ts') || filePath.endsWith('.spec.ts')) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const content = tree.read(filePath, 'utf-8');
|
|
78
|
+
if (!content || !content.includes('.prepare()')) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const nextContent = transformEmptyPrepareCalls(content);
|
|
82
|
+
if (nextContent !== content) {
|
|
83
|
+
tree.write(filePath, nextContent);
|
|
84
|
+
updatedFiles.push(filePath);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (updatedFiles.length > 0) {
|
|
88
|
+
console.log(`\n✅ Migrated empty .prepare() calls in ${updatedFiles.length} files`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const migrateDevtoolsInFile = (content) => {
|
|
92
|
+
const importsComponent = importsFromQuery(content, 'QueryDevtoolsComponent');
|
|
93
|
+
let result = replaceDevtoolsProviderCalls(content);
|
|
94
|
+
const hasProvider = result.includes('provideQueryDevtools()');
|
|
95
|
+
result = dropLegacyDevtoolsImports(result);
|
|
96
|
+
if (hasProvider) {
|
|
97
|
+
result = ensureImportFromQuery(result, ['provideQueryDevtools']);
|
|
98
|
+
}
|
|
99
|
+
if (importsComponent) {
|
|
100
|
+
result = ensureNamedImports({
|
|
101
|
+
content: result,
|
|
102
|
+
importsNeeded: ['QueryDevtoolsComponent'],
|
|
103
|
+
moduleSpecifier: '@ethlete/components',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return { content: result, importsComponent };
|
|
107
|
+
};
|
|
108
|
+
const importsFromQuery = (content, name) => {
|
|
109
|
+
const sourceFile = createSourceFile(content);
|
|
110
|
+
return sourceFile.statements.some((node) => ts.isImportDeclaration(node) &&
|
|
111
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
112
|
+
node.moduleSpecifier.text === '@ethlete/query' &&
|
|
113
|
+
!!node.importClause?.namedBindings &&
|
|
114
|
+
ts.isNamedImports(node.importClause.namedBindings) &&
|
|
115
|
+
node.importClause.namedBindings.elements.some((element) => element.name.text === name));
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Turns every `provideQueryClientForDevtools({ client, displayName })` into a single
|
|
119
|
+
* `provideQueryDevtools()`.
|
|
120
|
+
*
|
|
121
|
+
* v3 registers every client and auth provider at once, so N per-client calls collapse to one. The
|
|
122
|
+
* first call site keeps its position - it is already where the app wanted its devtools - and the
|
|
123
|
+
* rest are removed along with the comma that separated them.
|
|
124
|
+
*/
|
|
125
|
+
const replaceDevtoolsProviderCalls = (content) => {
|
|
126
|
+
const sourceFile = createSourceFile(content);
|
|
127
|
+
const calls = [];
|
|
128
|
+
const visit = (node) => {
|
|
129
|
+
if (ts.isCallExpression(node) &&
|
|
130
|
+
ts.isIdentifier(node.expression) &&
|
|
131
|
+
node.expression.text === 'provideQueryClientForDevtools') {
|
|
132
|
+
calls.push(node);
|
|
133
|
+
}
|
|
134
|
+
ts.forEachChild(node, visit);
|
|
135
|
+
};
|
|
136
|
+
visit(sourceFile);
|
|
137
|
+
if (calls.length === 0) {
|
|
138
|
+
return content;
|
|
139
|
+
}
|
|
140
|
+
const replacements = calls.map((call, index) => {
|
|
141
|
+
const start = call.getStart(sourceFile);
|
|
142
|
+
const end = call.getEnd();
|
|
143
|
+
if (index === 0) {
|
|
144
|
+
return { start, end, replacement: 'provideQueryDevtools()' };
|
|
145
|
+
}
|
|
146
|
+
return { ...withSurroundingComma(content, start, end), replacement: '' };
|
|
147
|
+
});
|
|
148
|
+
let result = content;
|
|
149
|
+
replacements.sort((left, right) => right.start - left.start);
|
|
150
|
+
replacements.forEach(({ start, end, replacement }) => {
|
|
151
|
+
result = result.slice(0, start) + replacement + result.slice(end);
|
|
152
|
+
});
|
|
153
|
+
return result;
|
|
154
|
+
};
|
|
155
|
+
/** Widens a range to swallow the comma that separated the element from its neighbours. */
|
|
156
|
+
const withSurroundingComma = (content, start, end) => {
|
|
157
|
+
let nextEnd = end;
|
|
158
|
+
while (nextEnd < content.length && /\s/.test(content[nextEnd]))
|
|
159
|
+
nextEnd += 1;
|
|
160
|
+
if (content[nextEnd] === ',') {
|
|
161
|
+
return { start, end: nextEnd + 1 };
|
|
162
|
+
}
|
|
163
|
+
let nextStart = start;
|
|
164
|
+
while (nextStart > 0 && /\s/.test(content[nextStart - 1]))
|
|
165
|
+
nextStart -= 1;
|
|
166
|
+
if (content[nextStart - 1] === ',') {
|
|
167
|
+
return { start: nextStart - 1, end };
|
|
168
|
+
}
|
|
169
|
+
return { start, end };
|
|
170
|
+
};
|
|
171
|
+
/** Drops the v2 devtools names from the `@ethlete/query` import; both moved or were renamed. */
|
|
172
|
+
const dropLegacyDevtoolsImports = (content) => {
|
|
173
|
+
const sourceFile = createSourceFile(content);
|
|
174
|
+
let importNode;
|
|
175
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
176
|
+
if (ts.isImportDeclaration(node) &&
|
|
177
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
178
|
+
node.moduleSpecifier.text === '@ethlete/query') {
|
|
179
|
+
importNode = node;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
if (!importNode?.importClause?.namedBindings || !ts.isNamedImports(importNode.importClause.namedBindings)) {
|
|
183
|
+
return content;
|
|
184
|
+
}
|
|
185
|
+
const nextElements = importNode.importClause.namedBindings.elements.filter((element) => element.name.text !== 'QueryDevtoolsComponent' && element.name.text !== 'provideQueryClientForDevtools');
|
|
186
|
+
if (nextElements.length === importNode.importClause.namedBindings.elements.length) {
|
|
187
|
+
return content;
|
|
188
|
+
}
|
|
189
|
+
if (nextElements.length === 0) {
|
|
190
|
+
const nextLineEnd = content[importNode.getEnd()] === '\n' ? importNode.getEnd() + 1 : importNode.getEnd();
|
|
191
|
+
return content.slice(0, importNode.getStart(sourceFile)) + content.slice(nextLineEnd);
|
|
192
|
+
}
|
|
193
|
+
const nextImport = `import { ${nextElements.map((element) => element.getText(sourceFile)).join(', ')} } from '@ethlete/query';`;
|
|
194
|
+
return content.slice(0, importNode.getStart(sourceFile)) + nextImport + content.slice(importNode.getEnd());
|
|
195
|
+
};
|
|
196
|
+
const replaceAnyQueryInFile = (content) => {
|
|
197
|
+
const sourceFile = createSourceFile(content);
|
|
198
|
+
const replacements = [];
|
|
199
|
+
const visit = (node) => {
|
|
200
|
+
if (ts.isIdentifier(node)) {
|
|
201
|
+
if (node.text === 'AnyV2Query') {
|
|
202
|
+
replacements.push({
|
|
203
|
+
start: node.getStart(sourceFile),
|
|
204
|
+
end: node.getEnd(),
|
|
205
|
+
replacement: 'AnyLegacyQuery',
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (node.text === 'AnyV2QueryCreator') {
|
|
209
|
+
replacements.push({
|
|
210
|
+
start: node.getStart(sourceFile),
|
|
211
|
+
end: node.getEnd(),
|
|
212
|
+
replacement: 'AnyLegacyQueryCreator',
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
ts.forEachChild(node, visit);
|
|
217
|
+
};
|
|
218
|
+
visit(sourceFile);
|
|
219
|
+
let result = content;
|
|
220
|
+
replacements.sort((left, right) => right.start - left.start);
|
|
221
|
+
replacements.forEach(({ start, end, replacement }) => {
|
|
222
|
+
result = result.slice(0, start) + replacement + result.slice(end);
|
|
223
|
+
});
|
|
224
|
+
return removeAnyQueryFromImports(result);
|
|
225
|
+
};
|
|
226
|
+
const removeAnyQueryFromImports = (content) => {
|
|
227
|
+
const sourceFile = createSourceFile(content);
|
|
228
|
+
let importNode;
|
|
229
|
+
ts.forEachChild(sourceFile, (node) => {
|
|
230
|
+
if (ts.isImportDeclaration(node) &&
|
|
231
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
232
|
+
node.moduleSpecifier.text === '@ethlete/query') {
|
|
233
|
+
importNode = node;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
if (!importNode?.importClause?.namedBindings || !ts.isNamedImports(importNode.importClause.namedBindings)) {
|
|
237
|
+
return content;
|
|
238
|
+
}
|
|
239
|
+
const replaced = new Map([
|
|
240
|
+
['AnyV2Query', 'AnyLegacyQuery'],
|
|
241
|
+
['AnyV2QueryCreator', 'AnyLegacyQueryCreator'],
|
|
242
|
+
]);
|
|
243
|
+
const names = new Set();
|
|
244
|
+
let changed = false;
|
|
245
|
+
importNode.importClause.namedBindings.elements.forEach((element) => {
|
|
246
|
+
const replacement = replaced.get(element.name.text);
|
|
247
|
+
if (!replacement) {
|
|
248
|
+
names.add(element.getText(sourceFile));
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
// Only add the alias that was actually imported. Adding both unconditionally is what put
|
|
252
|
+
// `AnyLegacyQuery` / `AnyLegacyQueryCreator` into hundreds of files that never referenced them.
|
|
253
|
+
changed = true;
|
|
254
|
+
names.add(replacement);
|
|
255
|
+
});
|
|
256
|
+
if (!changed) {
|
|
257
|
+
return content;
|
|
258
|
+
}
|
|
259
|
+
const nextImport = `import { ${Array.from(names).sort().join(', ')} } from '@ethlete/query';`;
|
|
260
|
+
return content.slice(0, importNode.getStart(sourceFile)) + nextImport + content.slice(importNode.getEnd());
|
|
261
|
+
};
|
|
262
|
+
const transformEmptyPrepareCalls = (content) => {
|
|
263
|
+
const sourceFile = createSourceFile(content);
|
|
264
|
+
const replacements = [];
|
|
265
|
+
const visit = (node) => {
|
|
266
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
|
|
267
|
+
const propertyAccess = node.expression;
|
|
268
|
+
if (ts.isIdentifier(propertyAccess.name) &&
|
|
269
|
+
propertyAccess.name.text === 'prepare' &&
|
|
270
|
+
node.arguments.length === 0) {
|
|
271
|
+
const callText = content.slice(node.getStart(sourceFile), node.getEnd());
|
|
272
|
+
replacements.push({
|
|
273
|
+
start: node.getStart(sourceFile),
|
|
274
|
+
end: node.getEnd(),
|
|
275
|
+
replacement: callText.replace(/\.prepare\(\)/, '.prepare({})'),
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
ts.forEachChild(node, visit);
|
|
280
|
+
};
|
|
281
|
+
visit(sourceFile);
|
|
282
|
+
let result = content;
|
|
283
|
+
replacements.sort((left, right) => right.start - left.start);
|
|
284
|
+
replacements.forEach(({ start, end, replacement }) => {
|
|
285
|
+
result = result.slice(0, start) + replacement + result.slice(end);
|
|
286
|
+
});
|
|
287
|
+
return result;
|
|
288
|
+
};
|
|
289
|
+
export const describeTemplateLine = (content, position, suffix) => {
|
|
290
|
+
const lineNumber = getLineNumberFromPosition(content, position);
|
|
291
|
+
return `${lineNumber} (${suffix})`;
|
|
292
|
+
};
|
|
293
|
+
//# sourceMappingURL=cleanup-migration.js.map
|