@angular-modernizer/adapter-mcp 0.2.0 → 1.0.0
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/README.md +188 -235
- package/dist/cli.js +77 -27
- package/dist/index.d.ts +11 -3
- package/dist/index.js +10 -2
- package/dist/mcp-watcher.d.ts +6 -1
- package/dist/mcp-watcher.js +14 -10
- package/dist/server/limited-stdio-transport.d.ts +68 -0
- package/dist/server/limited-stdio-transport.js +115 -0
- package/dist/server/mcp-server.d.ts +41 -12
- package/dist/server/mcp-server.js +113 -94
- package/dist/server/package-version.d.ts +16 -0
- package/dist/server/package-version.js +37 -0
- package/dist/server/sdk-server.d.ts +70 -0
- package/dist/server/sdk-server.js +113 -0
- package/dist/server/stdio-guard.d.ts +20 -0
- package/dist/server/stdio-guard.js +48 -0
- package/dist/server/tool-registry.d.ts +3 -2
- package/dist/server/tool-registry.js +5 -5
- package/dist/tools/analyze-build-impact.d.ts +346 -14
- package/dist/tools/analyze-build-impact.js +315 -31
- package/dist/tools/analyze-core-to-shared.d.ts +134 -4
- package/dist/tools/analyze-core-to-shared.js +113 -9
- package/dist/tools/analyze-parser-performance.d.ts +200 -7
- package/dist/tools/analyze-parser-performance.js +108 -47
- package/dist/tools/analyze-project.d.ts +267 -5
- package/dist/tools/analyze-project.js +196 -8
- package/dist/tools/check-incremental-compatibility.d.ts +61 -1
- package/dist/tools/check-incremental-compatibility.js +58 -2
- package/dist/tools/collect-feedback.d.ts +199 -10
- package/dist/tools/collect-feedback.js +171 -99
- package/dist/tools/compare-health.d.ts +356 -0
- package/dist/tools/compare-health.js +472 -0
- package/dist/tools/extract-libraries.d.ts +156 -4
- package/dist/tools/extract-libraries.js +115 -10
- package/dist/tools/find-angular-symbol.d.ts +98 -9
- package/dist/tools/find-angular-symbol.js +104 -12
- package/dist/tools/find-component-hosts.d.ts +684 -0
- package/dist/tools/find-component-hosts.js +441 -0
- package/dist/tools/find-usages.d.ts +99 -4
- package/dist/tools/find-usages.js +131 -11
- package/dist/tools/get-bootstrap-execution.d.ts +258 -0
- package/dist/tools/get-bootstrap-execution.js +324 -0
- package/dist/tools/get-call-graph.d.ts +139 -4
- package/dist/tools/get-call-graph.js +123 -13
- package/dist/tools/get-operation-progress-tool.d.ts +114 -2
- package/dist/tools/get-operation-progress-tool.js +114 -7
- package/dist/tools/get-operation-progress.d.ts +1 -1
- package/dist/tools/get-operation-progress.js +5 -5
- package/dist/tools/helpers/analysis-output.d.ts +177 -0
- package/dist/tools/helpers/analysis-output.js +165 -0
- package/dist/tools/helpers/build-schemas.d.ts +36 -0
- package/dist/tools/helpers/build-schemas.js +23 -0
- package/dist/tools/helpers/citation-file-source.d.ts +10 -0
- package/dist/tools/helpers/citation-file-source.js +82 -0
- package/dist/tools/helpers/compact-output.d.ts +61 -0
- package/dist/tools/helpers/compact-output.js +83 -0
- package/dist/tools/helpers/finding-window.d.ts +43 -0
- package/dist/tools/helpers/finding-window.js +64 -0
- package/dist/tools/helpers/health-delta.d.ts +86 -0
- package/dist/tools/helpers/health-delta.js +239 -0
- package/dist/tools/helpers/investigation-schemas.d.ts +93 -0
- package/dist/tools/helpers/investigation-schemas.js +48 -0
- package/dist/tools/helpers/library-imports.d.ts +57 -0
- package/dist/tools/helpers/library-imports.js +151 -0
- package/dist/tools/helpers/load-source-files.d.ts +30 -0
- package/dist/tools/helpers/load-source-files.js +34 -0
- package/dist/tools/helpers/output-contract.d.ts +32 -0
- package/dist/tools/helpers/output-contract.js +32 -0
- package/dist/tools/helpers/platform-root-resolver.d.ts +55 -25
- package/dist/tools/helpers/platform-root-resolver.js +75 -48
- package/dist/tools/helpers/process-output.d.ts +19 -0
- package/dist/tools/helpers/process-output.js +31 -0
- package/dist/tools/helpers/project-cache.d.ts +184 -0
- package/dist/tools/helpers/project-cache.js +331 -0
- package/dist/tools/helpers/project-decorators.d.ts +14 -0
- package/dist/tools/helpers/project-decorators.js +22 -0
- package/dist/tools/helpers/rule-matcher.js +5 -0
- package/dist/tools/helpers/rule-runner.d.ts +72 -0
- package/dist/tools/helpers/rule-runner.js +131 -0
- package/dist/tools/helpers/scan-cache.d.ts +22 -0
- package/dist/tools/helpers/scan-cache.js +34 -0
- package/dist/tools/helpers/scan-plan.d.ts +48 -0
- package/dist/tools/helpers/scan-plan.js +166 -0
- package/dist/tools/helpers/scope-paths.d.ts +17 -0
- package/dist/tools/helpers/scope-paths.js +32 -0
- package/dist/tools/helpers/stack-frame-file-index.d.ts +10 -0
- package/dist/tools/helpers/stack-frame-file-index.js +71 -0
- package/dist/tools/helpers/tool-context.d.ts +60 -0
- package/dist/tools/helpers/tool-context.js +82 -0
- package/dist/tools/helpers/transform-safety.d.ts +40 -0
- package/dist/tools/helpers/transform-safety.js +87 -0
- package/dist/tools/helpers/validation-output.d.ts +53 -0
- package/dist/tools/helpers/validation-output.js +48 -0
- package/dist/tools/inspect-form-group.d.ts +324 -0
- package/dist/tools/inspect-form-group.js +279 -0
- package/dist/tools/inspect-i18n.d.ts +466 -0
- package/dist/tools/inspect-i18n.js +400 -0
- package/dist/tools/inspect-template.d.ts +320 -0
- package/dist/tools/inspect-template.js +324 -0
- package/dist/tools/learn-patterns.d.ts +122 -1
- package/dist/tools/learn-patterns.js +97 -2
- package/dist/tools/list-imports.d.ts +128 -11
- package/dist/tools/list-imports.js +128 -8
- package/dist/tools/orchestrate-migration.d.ts +129 -5
- package/dist/tools/orchestrate-migration.js +166 -12
- package/dist/tools/parse-stack-trace.d.ts +59 -2
- package/dist/tools/parse-stack-trace.js +52 -4
- package/dist/tools/plan-migration.d.ts +418 -1
- package/dist/tools/plan-migration.js +355 -8
- package/dist/tools/resolve-type.d.ts +42 -1
- package/dist/tools/resolve-type.js +37 -4
- package/dist/tools/review-feedback.d.ts +206 -11
- package/dist/tools/review-feedback.js +132 -51
- package/dist/tools/rollback-changes.d.ts +79 -2
- package/dist/tools/rollback-changes.js +66 -14
- package/dist/tools/run-angular-cli.d.ts +45 -2
- package/dist/tools/run-angular-cli.js +67 -7
- package/dist/tools/run-build.d.ts +114 -2
- package/dist/tools/run-build.js +117 -8
- package/dist/tools/run-schematic.d.ts +70 -2
- package/dist/tools/run-schematic.js +104 -10
- package/dist/tools/scan-all.d.ts +24 -16
- package/dist/tools/scan-all.js +145 -137
- package/dist/tools/scan-angular.d.ts +20 -11
- package/dist/tools/scan-angular.js +52 -24
- package/dist/tools/scan-architecture.d.ts +42 -14
- package/dist/tools/scan-architecture.js +206 -163
- package/dist/tools/scan-solid.d.ts +20 -11
- package/dist/tools/scan-solid.js +52 -24
- package/dist/tools/search-codebase.d.ts +85 -1
- package/dist/tools/search-codebase.js +61 -4
- package/dist/tools/server-status.d.ts +370 -0
- package/dist/tools/server-status.js +369 -0
- package/dist/tools/suggest-improvements.d.ts +97 -5
- package/dist/tools/suggest-improvements.js +80 -44
- package/dist/tools/trace-output-flow.d.ts +791 -0
- package/dist/tools/trace-output-flow.js +668 -0
- package/dist/tools/transform-code-batch.d.ts +3 -0
- package/dist/tools/transform-code-batch.js +10 -5
- package/dist/tools/transform-code.d.ts +335 -12
- package/dist/tools/transform-code.js +649 -608
- package/dist/tools/validate-before-transform.d.ts +116 -2
- package/dist/tools/validate-before-transform.js +55 -2
- package/dist/tools/validate-project-compatibility.d.ts +64 -2
- package/dist/tools/validate-project-compatibility.js +67 -4
- package/dist/tools/validate-transformation.d.ts +220 -2
- package/dist/tools/validate-transformation.js +113 -15
- package/dist/tools/verify-citations.d.ts +181 -0
- package/dist/tools/verify-citations.js +218 -0
- package/dist/types.d.ts +45 -1
- package/dist/utils/format-tool-response.d.ts +22 -59
- package/dist/utils/format-tool-response.js +25 -134
- package/package.json +30 -13
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/core/project-analyzer.d.ts.map +0 -1
- package/dist/core/project-analyzer.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mcp-watcher.d.ts.map +0 -1
- package/dist/mcp-watcher.js.map +0 -1
- package/dist/server/json-rpc-handler.d.ts +0 -57
- package/dist/server/json-rpc-handler.d.ts.map +0 -1
- package/dist/server/json-rpc-handler.js +0 -222
- package/dist/server/json-rpc-handler.js.map +0 -1
- package/dist/server/mcp-server.d.ts.map +0 -1
- package/dist/server/mcp-server.js.map +0 -1
- package/dist/server/tool-registry.d.ts.map +0 -1
- package/dist/server/tool-registry.js.map +0 -1
- package/dist/streaming/progress-store.d.ts.map +0 -1
- package/dist/streaming/progress-store.js.map +0 -1
- package/dist/tools/analyze-build-impact.d.ts.map +0 -1
- package/dist/tools/analyze-build-impact.js.map +0 -1
- package/dist/tools/analyze-core-to-shared.d.ts.map +0 -1
- package/dist/tools/analyze-core-to-shared.js.map +0 -1
- package/dist/tools/analyze-parser-performance.d.ts.map +0 -1
- package/dist/tools/analyze-parser-performance.js.map +0 -1
- package/dist/tools/analyze-project.d.ts.map +0 -1
- package/dist/tools/analyze-project.js.map +0 -1
- package/dist/tools/check-incremental-compatibility.d.ts.map +0 -1
- package/dist/tools/check-incremental-compatibility.js.map +0 -1
- package/dist/tools/collect-feedback.d.ts.map +0 -1
- package/dist/tools/collect-feedback.js.map +0 -1
- package/dist/tools/extract-libraries.d.ts.map +0 -1
- package/dist/tools/extract-libraries.js.map +0 -1
- package/dist/tools/find-angular-symbol.d.ts.map +0 -1
- package/dist/tools/find-angular-symbol.js.map +0 -1
- package/dist/tools/find-usages.d.ts.map +0 -1
- package/dist/tools/find-usages.js.map +0 -1
- package/dist/tools/get-call-graph.d.ts.map +0 -1
- package/dist/tools/get-call-graph.js.map +0 -1
- package/dist/tools/get-operation-progress-tool.d.ts.map +0 -1
- package/dist/tools/get-operation-progress-tool.js.map +0 -1
- package/dist/tools/get-operation-progress.d.ts.map +0 -1
- package/dist/tools/get-operation-progress.js.map +0 -1
- package/dist/tools/helpers/feedback-analyzer.d.ts.map +0 -1
- package/dist/tools/helpers/feedback-analyzer.js.map +0 -1
- package/dist/tools/helpers/find-ts-config.d.ts.map +0 -1
- package/dist/tools/helpers/find-ts-config.js.map +0 -1
- package/dist/tools/helpers/platform-root-resolver.d.ts.map +0 -1
- package/dist/tools/helpers/platform-root-resolver.js.map +0 -1
- package/dist/tools/helpers/rule-matcher.d.ts.map +0 -1
- package/dist/tools/helpers/rule-matcher.js.map +0 -1
- package/dist/tools/helpers/suggestion-generator.d.ts.map +0 -1
- package/dist/tools/helpers/suggestion-generator.js.map +0 -1
- package/dist/tools/learn-patterns.d.ts.map +0 -1
- package/dist/tools/learn-patterns.js.map +0 -1
- package/dist/tools/list-imports.d.ts.map +0 -1
- package/dist/tools/list-imports.js.map +0 -1
- package/dist/tools/orchestrate-migration.d.ts.map +0 -1
- package/dist/tools/orchestrate-migration.js.map +0 -1
- package/dist/tools/parse-stack-trace.d.ts.map +0 -1
- package/dist/tools/parse-stack-trace.js.map +0 -1
- package/dist/tools/plan-migration.d.ts.map +0 -1
- package/dist/tools/plan-migration.js.map +0 -1
- package/dist/tools/resolve-type.d.ts.map +0 -1
- package/dist/tools/resolve-type.js.map +0 -1
- package/dist/tools/review-feedback.d.ts.map +0 -1
- package/dist/tools/review-feedback.js.map +0 -1
- package/dist/tools/rollback-changes.d.ts.map +0 -1
- package/dist/tools/rollback-changes.js.map +0 -1
- package/dist/tools/run-angular-cli.d.ts.map +0 -1
- package/dist/tools/run-angular-cli.js.map +0 -1
- package/dist/tools/run-build.d.ts.map +0 -1
- package/dist/tools/run-build.js.map +0 -1
- package/dist/tools/run-schematic.d.ts.map +0 -1
- package/dist/tools/run-schematic.js.map +0 -1
- package/dist/tools/scan-all.d.ts.map +0 -1
- package/dist/tools/scan-all.js.map +0 -1
- package/dist/tools/scan-angular.d.ts.map +0 -1
- package/dist/tools/scan-angular.js.map +0 -1
- package/dist/tools/scan-architecture.d.ts.map +0 -1
- package/dist/tools/scan-architecture.js.map +0 -1
- package/dist/tools/scan-solid.d.ts.map +0 -1
- package/dist/tools/scan-solid.js.map +0 -1
- package/dist/tools/search-codebase.d.ts.map +0 -1
- package/dist/tools/search-codebase.js.map +0 -1
- package/dist/tools/shared/scan-types.d.ts.map +0 -1
- package/dist/tools/shared/scan-types.js.map +0 -1
- package/dist/tools/suggest-improvements.d.ts.map +0 -1
- package/dist/tools/suggest-improvements.js.map +0 -1
- package/dist/tools/transform-code-batch.d.ts.map +0 -1
- package/dist/tools/transform-code-batch.js.map +0 -1
- package/dist/tools/transform-code.d.ts.map +0 -1
- package/dist/tools/transform-code.js.map +0 -1
- package/dist/tools/validate-before-transform.d.ts.map +0 -1
- package/dist/tools/validate-before-transform.js.map +0 -1
- package/dist/tools/validate-project-compatibility.d.ts.map +0 -1
- package/dist/tools/validate-project-compatibility.js.map +0 -1
- package/dist/tools/validate-transformation.d.ts.map +0 -1
- package/dist/tools/validate-transformation.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/dist/utils/format-tool-response.d.ts.map +0 -1
- package/dist/utils/format-tool-response.js.map +0 -1
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* MCP tool for performing surgical code transformations to fix
|
|
5
5
|
* architectural issues, including standalone component migration.
|
|
6
6
|
*/
|
|
7
|
+
import { yieldIfCancellable } from './helpers/tool-context.js';
|
|
7
8
|
import { findTsConfig } from './helpers/find-ts-config.js';
|
|
8
|
-
import { Kernel, FluentKernel,
|
|
9
|
+
import { Kernel, FluentKernel, PreTransformValidator, PostTransformValidator, ValidationSeverity, ValidationType, loadConfig, } from '@angular-modernizer/core';
|
|
9
10
|
import { AngularPlugin } from '@angular-modernizer/plugin-angular';
|
|
10
11
|
import { readFileSync } from 'node:fs';
|
|
11
12
|
import { resolve } from 'node:path';
|
|
@@ -13,15 +14,153 @@ import { randomUUID } from 'node:crypto';
|
|
|
13
14
|
import { executeBatchTransformation, } from './transform-code-batch.js';
|
|
14
15
|
import { ProgressStore } from '../streaming/progress-store.js';
|
|
15
16
|
import { getAvailableRules, findSimilarRules } from './helpers/rule-matcher.js';
|
|
17
|
+
import { createProjectRollbackManager, fileTypeErrors, introducedTypeErrors, } from './helpers/transform-safety.js';
|
|
16
18
|
import { cpus } from 'node:os';
|
|
19
|
+
import { STRING_ARRAY } from './helpers/investigation-schemas.js';
|
|
20
|
+
/** Transformations applied by plugin-angular (its rules check `transformationType`). */
|
|
21
|
+
const PLUGIN_ANGULAR_TRANSFORMATIONS = new Set([
|
|
22
|
+
'constructor-to-inject',
|
|
23
|
+
'interface-extraction',
|
|
24
|
+
'dependency-injection-migration',
|
|
25
|
+
'facade-pattern',
|
|
26
|
+
'form-modernization',
|
|
27
|
+
'constructor-injection-transform',
|
|
28
|
+
'component-inputs-interface-extraction',
|
|
29
|
+
'service-injection-cleanup',
|
|
30
|
+
'container-presentational',
|
|
31
|
+
'core-shared-modules',
|
|
32
|
+
'inheritance-to-composition',
|
|
33
|
+
'promise-service-to-observable',
|
|
34
|
+
'promise-component-to-reactive',
|
|
35
|
+
'promise-cleanup',
|
|
36
|
+
'sequential-await-to-forkjoin',
|
|
37
|
+
'missing-output-transform',
|
|
38
|
+
'subscription-transform',
|
|
39
|
+
'library-extraction',
|
|
40
|
+
'service-bag-transform',
|
|
41
|
+
'any-to-interface',
|
|
42
|
+
'dto-object-literal',
|
|
43
|
+
'interface-duplication',
|
|
44
|
+
'static-class-to-functions',
|
|
45
|
+
'extract-static-from-stateful',
|
|
46
|
+
'type-safety',
|
|
47
|
+
]);
|
|
48
|
+
/**
|
|
49
|
+
* Plugin config of a transformation: the plugins read `transformationType`
|
|
50
|
+
* and the transformation options under their own package name.
|
|
51
|
+
*/
|
|
52
|
+
export function pluginConfigFor(transformation, options) {
|
|
53
|
+
return {
|
|
54
|
+
'@angular-modernizer/plugin-standalone': {
|
|
55
|
+
transformationType: transformation === 'standalone-migration' ? transformation : undefined,
|
|
56
|
+
...options,
|
|
57
|
+
},
|
|
58
|
+
'@angular-modernizer/plugin-angular': {
|
|
59
|
+
transformationType: transformation !== undefined &&
|
|
60
|
+
PLUGIN_ANGULAR_TRANSFORMATIONS.has(transformation)
|
|
61
|
+
? transformation
|
|
62
|
+
: undefined,
|
|
63
|
+
...options,
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** Warnings and failed batch files spelled out in the summary. */
|
|
68
|
+
const SUMMARY_WARNINGS = 5;
|
|
69
|
+
const SUMMARY_FAILED_FILES = 10;
|
|
70
|
+
/** Backup and rollback part of the summary. */
|
|
71
|
+
function describeSafety(result) {
|
|
72
|
+
const lines = [];
|
|
73
|
+
if (result.backup?.created === true) {
|
|
74
|
+
lines.push(`Backup: ${result.backup.backupPath ?? result.backup.strategy}` +
|
|
75
|
+
(result.backup.gitSnapshot === undefined
|
|
76
|
+
? ''
|
|
77
|
+
: ` (git snapshot ${result.backup.gitSnapshot})`) +
|
|
78
|
+
'.');
|
|
79
|
+
}
|
|
80
|
+
if (result.rollback?.performed === true) {
|
|
81
|
+
lines.push(`Rolled back${result.rollback.success === false ? ' (rollback failed)' : ''}: ` +
|
|
82
|
+
`${result.rollback.reason ?? 'no reason given'}.`);
|
|
83
|
+
}
|
|
84
|
+
return lines;
|
|
85
|
+
}
|
|
86
|
+
/** Text summary of a single-file, batch or background transformation. */
|
|
87
|
+
function describeTransformation(result) {
|
|
88
|
+
if (result.operationId !== undefined) {
|
|
89
|
+
return (`${result.transformation} started in the background as operation ${result.operationId}; ` +
|
|
90
|
+
'get-operation-progress (deprecated) returns the result once it is completed.');
|
|
91
|
+
}
|
|
92
|
+
const outcome = result.success ? 'succeeded' : 'failed';
|
|
93
|
+
const lines = [];
|
|
94
|
+
if (result.files !== undefined) {
|
|
95
|
+
const failed = result.files.filter((f) => !f.success);
|
|
96
|
+
const duration = result.orchestration?.duration;
|
|
97
|
+
lines.push(`${result.transformation} ${outcome} on ${result.files.length} file(s): ` +
|
|
98
|
+
`${result.files.length - failed.length} succeeded, ${failed.length} failed` +
|
|
99
|
+
(duration === undefined ? '.' : ` in ${duration} ms.`));
|
|
100
|
+
lines.push(...describeSafety(result));
|
|
101
|
+
lines.push(...failed
|
|
102
|
+
.slice(0, SUMMARY_FAILED_FILES)
|
|
103
|
+
.map((f) => `- ${f.path}${f.error === undefined ? '' : `: ${f.error}`}`));
|
|
104
|
+
return lines.join('\n');
|
|
105
|
+
}
|
|
106
|
+
const c = result.changes;
|
|
107
|
+
const introduced = result.validation.introducedTypeErrors ?? [];
|
|
108
|
+
lines.push(`${result.transformation} ${outcome} on ${result.filePath ?? 'unknown file'}: ` +
|
|
109
|
+
`${c.linesChanged} line(s) changed (+${c.additions}/-${c.deletions}); ` +
|
|
110
|
+
`type check ${result.validation.typeCheckPassed ? 'passed' : 'failed'}` +
|
|
111
|
+
(introduced.length > 0
|
|
112
|
+
? ` (${introduced.length} introduced type error(s))`
|
|
113
|
+
: '') +
|
|
114
|
+
(result.buildValidation === undefined
|
|
115
|
+
? ''
|
|
116
|
+
: `; build ${result.buildValidation.passed ? 'passed' : 'failed'}`) +
|
|
117
|
+
'.');
|
|
118
|
+
lines.push(...describeSafety(result));
|
|
119
|
+
lines.push(...introduced.slice(0, SUMMARY_WARNINGS).map((e) => `- ${e}`));
|
|
120
|
+
const warnings = result.warnings ?? [];
|
|
121
|
+
if (warnings.length > 0) {
|
|
122
|
+
lines.push(`${warnings.length} warning(s):`);
|
|
123
|
+
lines.push(...warnings.slice(0, SUMMARY_WARNINGS).map((w) => `- ${w}`));
|
|
124
|
+
}
|
|
125
|
+
return lines.join('\n');
|
|
126
|
+
}
|
|
17
127
|
/**
|
|
18
128
|
* MCP tool for code transformations.
|
|
19
129
|
*/
|
|
130
|
+
/**
|
|
131
|
+
* Files a batch restores after a failure: all-or-nothing every file,
|
|
132
|
+
* selective only the failed ones, none nothing.
|
|
133
|
+
*/
|
|
134
|
+
function batchFilesToRestore(strategy, allFiles, failedFiles) {
|
|
135
|
+
switch (strategy) {
|
|
136
|
+
case 'all-or-nothing':
|
|
137
|
+
return allFiles;
|
|
138
|
+
case 'selective':
|
|
139
|
+
return failedFiles;
|
|
140
|
+
case 'none':
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** Thrown between the steps of a single-file run when the client cancelled. */
|
|
145
|
+
class TransformCancelledError extends Error {
|
|
146
|
+
constructor() {
|
|
147
|
+
super('Transformation cancelled by the client');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
20
150
|
export class TransformCodeTool {
|
|
21
151
|
name = 'transform-code';
|
|
22
|
-
|
|
152
|
+
title = 'Transform Code';
|
|
153
|
+
annotations = {
|
|
154
|
+
readOnlyHint: false,
|
|
155
|
+
destructiveHint: true,
|
|
156
|
+
idempotentHint: false,
|
|
157
|
+
openWorldHint: false,
|
|
158
|
+
};
|
|
159
|
+
description = 'Apply surgical code transformations to fix architectural issues. ' +
|
|
160
|
+
'summary is a short text with the outcome, the changes, the type check, backup and rollback.';
|
|
23
161
|
inputSchema = {
|
|
24
162
|
type: 'object',
|
|
163
|
+
additionalProperties: false,
|
|
25
164
|
properties: {
|
|
26
165
|
filePath: {
|
|
27
166
|
type: 'string',
|
|
@@ -55,11 +194,18 @@ export class TransformCodeTool {
|
|
|
55
194
|
'missing-output-transform',
|
|
56
195
|
'subscription-transform',
|
|
57
196
|
'library-extraction',
|
|
197
|
+
'service-bag-transform',
|
|
198
|
+
'any-to-interface',
|
|
199
|
+
'dto-object-literal',
|
|
200
|
+
'interface-duplication',
|
|
201
|
+
'static-class-to-functions',
|
|
202
|
+
'extract-static-from-stateful',
|
|
203
|
+
'type-safety',
|
|
58
204
|
],
|
|
59
205
|
},
|
|
60
206
|
options: {
|
|
61
207
|
type: 'object',
|
|
62
|
-
description: 'Transformation-specific options',
|
|
208
|
+
description: 'Transformation-specific options. service-bag-transform: assignments ({ methodName: groupKey }) moves methods into the named group (e.g. uncertain methods from the scan metadata); unassigned uncertain methods stay in the original service',
|
|
63
209
|
},
|
|
64
210
|
validateBeforeTransform: {
|
|
65
211
|
type: 'boolean',
|
|
@@ -73,7 +219,7 @@ export class TransformCodeTool {
|
|
|
73
219
|
},
|
|
74
220
|
validateTransformation: {
|
|
75
221
|
type: 'boolean',
|
|
76
|
-
description: 'Whether to validate transformation results (default: true)',
|
|
222
|
+
description: 'Whether to validate transformation results (default: true): type check of the transformed file (new errors only), plus build and lint when enabled',
|
|
77
223
|
default: true,
|
|
78
224
|
},
|
|
79
225
|
autoRollback: {
|
|
@@ -145,13 +291,13 @@ export class TransformCodeTool {
|
|
|
145
291
|
},
|
|
146
292
|
streaming: {
|
|
147
293
|
type: 'boolean',
|
|
148
|
-
description: '
|
|
149
|
-
default:
|
|
294
|
+
description: 'Deprecated, kept in 1.x (default: false): returns operationId immediately, the transformation runs in the background with the same backup, validation and rollback, and get-operation-progress returns its result once completed. Synchronous calls report progress through MCP progress notifications and roll back when cancelled.',
|
|
295
|
+
default: false,
|
|
150
296
|
},
|
|
151
297
|
diffFormat: {
|
|
152
298
|
type: 'string',
|
|
153
299
|
enum: ['unified', 'structured', 'both'],
|
|
154
|
-
description: '
|
|
300
|
+
description: 'No effect: streaming runs the regular transformation in the background and emits no diff events. Kept so existing calls stay valid.',
|
|
155
301
|
default: 'structured',
|
|
156
302
|
},
|
|
157
303
|
apiStyle: {
|
|
@@ -167,8 +313,13 @@ export class TransformCodeTool {
|
|
|
167
313
|
},
|
|
168
314
|
validateBuild: {
|
|
169
315
|
type: 'boolean',
|
|
170
|
-
description: 'Whether to
|
|
171
|
-
default:
|
|
316
|
+
description: 'Whether to run the project build after transformation (default: false). The transformed file is type-checked either way and rolled back on new type errors; the build runs build.command of .angular-modernizer.json (or the detected build system) and can take minutes on a large app.',
|
|
317
|
+
default: false,
|
|
318
|
+
},
|
|
319
|
+
validateLint: {
|
|
320
|
+
type: 'boolean',
|
|
321
|
+
description: "Whether to run the project's lint script after transformation (default: false). Lints the whole project; the transformed file is type-checked either way and rolled back on new type errors.",
|
|
322
|
+
default: false,
|
|
172
323
|
},
|
|
173
324
|
rollbackOnBuildFailure: {
|
|
174
325
|
type: 'boolean',
|
|
@@ -187,7 +338,153 @@ export class TransformCodeTool {
|
|
|
187
338
|
},
|
|
188
339
|
required: ['transformation'],
|
|
189
340
|
};
|
|
190
|
-
|
|
341
|
+
outputSchema = {
|
|
342
|
+
type: 'object',
|
|
343
|
+
properties: {
|
|
344
|
+
operationId: { type: 'string' },
|
|
345
|
+
success: { type: 'boolean' },
|
|
346
|
+
filePath: { type: 'string' },
|
|
347
|
+
files: {
|
|
348
|
+
type: 'array',
|
|
349
|
+
items: {
|
|
350
|
+
type: 'object',
|
|
351
|
+
properties: {
|
|
352
|
+
path: { type: 'string' },
|
|
353
|
+
success: { type: 'boolean' },
|
|
354
|
+
error: { type: 'string' },
|
|
355
|
+
},
|
|
356
|
+
required: ['path', 'success'],
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
transformation: { type: 'string' },
|
|
360
|
+
changes: {
|
|
361
|
+
type: 'object',
|
|
362
|
+
properties: {
|
|
363
|
+
linesChanged: { type: 'integer' },
|
|
364
|
+
additions: { type: 'integer' },
|
|
365
|
+
deletions: { type: 'integer' },
|
|
366
|
+
},
|
|
367
|
+
required: ['linesChanged', 'additions', 'deletions'],
|
|
368
|
+
},
|
|
369
|
+
preValidation: {
|
|
370
|
+
type: 'object',
|
|
371
|
+
properties: {
|
|
372
|
+
valid: { type: 'boolean' },
|
|
373
|
+
issues: {
|
|
374
|
+
type: 'array',
|
|
375
|
+
items: {
|
|
376
|
+
type: 'object',
|
|
377
|
+
properties: {
|
|
378
|
+
type: { type: 'string' },
|
|
379
|
+
severity: { type: 'string' },
|
|
380
|
+
message: { type: 'string' },
|
|
381
|
+
filePath: { type: 'string' },
|
|
382
|
+
line: { type: 'integer' },
|
|
383
|
+
column: { type: 'integer' },
|
|
384
|
+
},
|
|
385
|
+
required: ['type', 'severity', 'message'],
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
required: ['valid', 'issues'],
|
|
390
|
+
},
|
|
391
|
+
validation: {
|
|
392
|
+
type: 'object',
|
|
393
|
+
properties: {
|
|
394
|
+
syntaxValid: { type: 'boolean' },
|
|
395
|
+
typeCheckPassed: { type: 'boolean' },
|
|
396
|
+
testsPass: { type: 'boolean' },
|
|
397
|
+
introducedTypeErrors: STRING_ARRAY,
|
|
398
|
+
},
|
|
399
|
+
required: ['syntaxValid', 'typeCheckPassed'],
|
|
400
|
+
},
|
|
401
|
+
backup: {
|
|
402
|
+
type: 'object',
|
|
403
|
+
properties: {
|
|
404
|
+
created: { type: 'boolean' },
|
|
405
|
+
backupPath: { type: 'string' },
|
|
406
|
+
strategy: { type: 'string', enum: ['git-stash', 'file-backup'] },
|
|
407
|
+
gitSnapshot: { type: 'string' },
|
|
408
|
+
},
|
|
409
|
+
required: ['created', 'strategy'],
|
|
410
|
+
},
|
|
411
|
+
rollback: {
|
|
412
|
+
type: 'object',
|
|
413
|
+
properties: {
|
|
414
|
+
performed: { type: 'boolean' },
|
|
415
|
+
reason: { type: 'string' },
|
|
416
|
+
success: { type: 'boolean' },
|
|
417
|
+
},
|
|
418
|
+
required: ['performed'],
|
|
419
|
+
},
|
|
420
|
+
warnings: {
|
|
421
|
+
type: 'array',
|
|
422
|
+
items: { type: 'string' },
|
|
423
|
+
description: 'Warnings of the transformation and its validation.',
|
|
424
|
+
},
|
|
425
|
+
orchestration: {
|
|
426
|
+
type: 'object',
|
|
427
|
+
properties: {
|
|
428
|
+
parallel: { type: 'boolean' },
|
|
429
|
+
batchMode: { type: 'boolean' },
|
|
430
|
+
filesProcessed: { type: 'integer' },
|
|
431
|
+
filesSucceeded: { type: 'integer' },
|
|
432
|
+
filesFailed: { type: 'integer' },
|
|
433
|
+
duration: { type: 'number' },
|
|
434
|
+
throughput: { type: 'number' },
|
|
435
|
+
},
|
|
436
|
+
},
|
|
437
|
+
progress: {
|
|
438
|
+
type: 'object',
|
|
439
|
+
properties: {
|
|
440
|
+
current: { type: 'number' },
|
|
441
|
+
total: { type: 'number' },
|
|
442
|
+
percentage: { type: 'number' },
|
|
443
|
+
eta: { type: 'number' },
|
|
444
|
+
phase: { type: 'string' },
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
pipeline: STRING_ARRAY,
|
|
448
|
+
intermediateResults: {
|
|
449
|
+
type: 'object',
|
|
450
|
+
additionalProperties: {
|
|
451
|
+
type: 'object',
|
|
452
|
+
properties: {
|
|
453
|
+
success: { type: 'boolean' },
|
|
454
|
+
violations: { type: 'number' },
|
|
455
|
+
filesProcessed: { type: 'number' },
|
|
456
|
+
message: { type: 'string' },
|
|
457
|
+
},
|
|
458
|
+
required: ['success'],
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
buildValidation: {
|
|
462
|
+
type: 'object',
|
|
463
|
+
properties: {
|
|
464
|
+
passed: { type: 'boolean' },
|
|
465
|
+
success: { type: 'boolean' },
|
|
466
|
+
exitCode: { type: 'integer' },
|
|
467
|
+
errors: STRING_ARRAY,
|
|
468
|
+
warnings: STRING_ARRAY,
|
|
469
|
+
duration: { type: 'number' },
|
|
470
|
+
buildCommand: { type: 'string' },
|
|
471
|
+
buildSystem: { type: 'string' },
|
|
472
|
+
timedOut: { type: 'boolean' },
|
|
473
|
+
},
|
|
474
|
+
required: ['passed', 'success'],
|
|
475
|
+
},
|
|
476
|
+
rolledBack: { type: 'boolean' },
|
|
477
|
+
backupId: { type: 'string' },
|
|
478
|
+
rollbackReason: { type: 'string' },
|
|
479
|
+
summary: { type: 'string' },
|
|
480
|
+
},
|
|
481
|
+
required: ['success', 'transformation', 'changes', 'validation', 'summary'],
|
|
482
|
+
};
|
|
483
|
+
async execute(args, ctx = {}) {
|
|
484
|
+
const result = await this.runTransform(args, ctx);
|
|
485
|
+
return { ...result, summary: describeTransformation(result) };
|
|
486
|
+
}
|
|
487
|
+
async runTransform(args, ctx) {
|
|
191
488
|
const params = args;
|
|
192
489
|
// Validate that either filePath or files is provided
|
|
193
490
|
if (!params.filePath && (!params.files || params.files.length === 0)) {
|
|
@@ -196,8 +493,8 @@ export class TransformCodeTool {
|
|
|
196
493
|
if (params.filePath && params.files && params.files.length > 0) {
|
|
197
494
|
throw new Error('Cannot specify both filePath and files - choose either single file or batch mode');
|
|
198
495
|
}
|
|
199
|
-
//
|
|
200
|
-
const streaming = params.streaming ??
|
|
496
|
+
// Streaming runs the same path in the background (default: false)
|
|
497
|
+
const streaming = params.streaming ?? false;
|
|
201
498
|
if (streaming) {
|
|
202
499
|
// Streaming mode - start operation in background and return operationId
|
|
203
500
|
return this.executeStreaming(params);
|
|
@@ -222,9 +519,9 @@ export class TransformCodeTool {
|
|
|
222
519
|
}
|
|
223
520
|
if (isBatchMode) {
|
|
224
521
|
// Batch mode - delegate to batch transformation handler
|
|
225
|
-
return this.executeBatch(params, orchestration);
|
|
522
|
+
return this.executeBatch(params, orchestration, ctx);
|
|
226
523
|
}
|
|
227
|
-
//
|
|
524
|
+
// Single-file mode
|
|
228
525
|
const filePath = params.filePath;
|
|
229
526
|
const transformation = params.transformation;
|
|
230
527
|
if (!filePath || typeof filePath !== 'string') {
|
|
@@ -235,537 +532,344 @@ export class TransformCodeTool {
|
|
|
235
532
|
const fullConfig = loadConfig(projectRoot);
|
|
236
533
|
const config = fullConfig.tools?.['transform-code'] ?? {};
|
|
237
534
|
const mergedParams = { ...config, ...params };
|
|
238
|
-
const options = mergedParams.options ?? {};
|
|
239
|
-
const validateBeforeTransform = mergedParams.validateBeforeTransform ?? true;
|
|
240
|
-
const createBackup = mergedParams.createBackup ?? true;
|
|
241
|
-
const validateTransformation = mergedParams.validateTransformation ?? true;
|
|
242
|
-
const autoRollback = mergedParams.autoRollback ?? true;
|
|
243
|
-
const backupDir = mergedParams.backupDir;
|
|
244
|
-
const apiStyle = mergedParams.apiStyle ?? 'fluent';
|
|
245
|
-
const buildCheck = mergedParams.buildCheck ?? false;
|
|
246
|
-
const validateBuild = mergedParams.validateBuild ?? true;
|
|
247
|
-
const rollbackOnBuildFailure = mergedParams.rollbackOnBuildFailure ?? true;
|
|
248
|
-
const buildTimeout = mergedParams.buildTimeout ?? 300000;
|
|
249
535
|
if (typeof transformation !== 'string') {
|
|
250
536
|
throw new TypeError('transformation is required and must be a string');
|
|
251
537
|
}
|
|
252
538
|
// Validate that the transformation type exists in the schema
|
|
253
539
|
this.validateTransformationType(transformation);
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
540
|
+
return this.executeSingleFile(filePath, transformation, mergedParams.options ?? {}, {
|
|
541
|
+
apiStyle: mergedParams.apiStyle ?? 'fluent',
|
|
542
|
+
validateBeforeTransform: mergedParams.validateBeforeTransform ?? true,
|
|
543
|
+
createBackup: mergedParams.createBackup ?? true,
|
|
544
|
+
validateTransformation: mergedParams.validateTransformation ?? true,
|
|
545
|
+
autoRollback: mergedParams.autoRollback ?? true,
|
|
546
|
+
backupDir: mergedParams.backupDir,
|
|
547
|
+
buildCheck: mergedParams.buildCheck ?? false,
|
|
548
|
+
validateBuild: mergedParams.validateBuild ?? false,
|
|
549
|
+
validateLint: mergedParams.validateLint ?? false,
|
|
550
|
+
buildTimeout: mergedParams.buildTimeout ?? 300000,
|
|
551
|
+
projectRoot,
|
|
552
|
+
signal: ctx.signal,
|
|
553
|
+
progress: ctx.progress,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Transform one file inside the safety envelope: one kernel load,
|
|
558
|
+
* pre-validation, backup (file plus git snapshot), transformation (fluent
|
|
559
|
+
* or imperative), type check of the file against its errors before the
|
|
560
|
+
* transformation, optional build and lint, rollback on failure.
|
|
561
|
+
*/
|
|
562
|
+
async executeSingleFile(filePath, transformation, options, config) {
|
|
269
563
|
let preValidationResult;
|
|
270
564
|
let backupResult;
|
|
271
|
-
let validationResult;
|
|
272
565
|
let rollbackResult;
|
|
566
|
+
const rollbackManager = createProjectRollbackManager(config.projectRoot, {
|
|
567
|
+
backupDir: config.backupDir,
|
|
568
|
+
});
|
|
273
569
|
try {
|
|
274
|
-
|
|
275
|
-
if (validateBeforeTransform) {
|
|
276
|
-
const tsConfigPath = findTsConfig(projectRoot);
|
|
277
|
-
if (!tsConfigPath) {
|
|
278
|
-
throw new Error('Could not find tsconfig.json in the project root');
|
|
279
|
-
}
|
|
280
|
-
// Create a temporary kernel for pre-validation with caching
|
|
281
|
-
const tempKernel = new Kernel({
|
|
282
|
-
tsConfigPath,
|
|
283
|
-
plugins: [new AngularPlugin()],
|
|
284
|
-
cacheConfig: {
|
|
285
|
-
enabled: true,
|
|
286
|
-
cacheDir: resolve(projectRoot, '.angular-modernizer-cache'),
|
|
287
|
-
maxSize: 100 * 1024 * 1024, // 100MB
|
|
288
|
-
maxAge: 24 * 60 * 60 * 1000, // 24 hours
|
|
289
|
-
compression: true,
|
|
290
|
-
},
|
|
291
|
-
});
|
|
292
|
-
await tempKernel.initialize();
|
|
293
|
-
const project = tempKernel.getProject();
|
|
294
|
-
const sourceFile = project.addSourceFileAtPath(filePath);
|
|
295
|
-
// Create pre-validator and config
|
|
296
|
-
const preValidator = new PreTransformValidator();
|
|
297
|
-
const preValidationConfig = {
|
|
298
|
-
validateSyntax: true,
|
|
299
|
-
validateDependencies: true,
|
|
300
|
-
validateTypes: true,
|
|
301
|
-
validateSchema: true,
|
|
302
|
-
schema: this.getSchemaForTransformation(transformation),
|
|
303
|
-
};
|
|
304
|
-
const result = await preValidator.validate(sourceFile, project, preValidationConfig);
|
|
305
|
-
preValidationResult = {
|
|
306
|
-
valid: result.valid,
|
|
307
|
-
issues: result.issues.map((issue) => ({
|
|
308
|
-
type: issue.type,
|
|
309
|
-
severity: issue.severity,
|
|
310
|
-
message: issue.message,
|
|
311
|
-
filePath: issue.filePath,
|
|
312
|
-
line: issue.line,
|
|
313
|
-
column: issue.column,
|
|
314
|
-
})),
|
|
315
|
-
};
|
|
316
|
-
// If pre-validation fails, stop here (no backup needed)
|
|
317
|
-
if (!result.valid) {
|
|
318
|
-
throw new Error(`Pre-validation failed: ${result.issues.length} issue(s) found. ` +
|
|
319
|
-
`Cannot safely transform file. Please fix these issues first:\n` +
|
|
320
|
-
result.issues
|
|
321
|
-
.map((i) => ` - [${i.severity}] ${i.message} ${i.filePath ? `at ${i.filePath}` : ''}${i.line ? `:${i.line}` : ''}`)
|
|
322
|
-
.join('\n'));
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
// Step 1: Create backup if requested
|
|
326
|
-
if (createBackup) {
|
|
327
|
-
const backupManager = new BackupManager(backupDir);
|
|
328
|
-
backupResult = await backupManager.backup(filePath);
|
|
329
|
-
if (!backupResult.success) {
|
|
330
|
-
throw new Error('Failed to create backup before transformation');
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
// Step 2: Perform transformation
|
|
334
|
-
const tsConfigPath = findTsConfig(projectRoot);
|
|
570
|
+
const tsConfigPath = findTsConfig(config.projectRoot);
|
|
335
571
|
if (!tsConfigPath) {
|
|
336
572
|
throw new Error('Could not find tsconfig.json in the project root');
|
|
337
573
|
}
|
|
338
|
-
//
|
|
339
|
-
//
|
|
574
|
+
// One kernel for pre-validation and transformation: each load parses
|
|
575
|
+
// the whole program.
|
|
340
576
|
const kernel = new Kernel({
|
|
341
577
|
tsConfigPath,
|
|
342
578
|
plugins: [new AngularPlugin()],
|
|
343
579
|
cacheConfig: {
|
|
344
580
|
enabled: true,
|
|
345
|
-
cacheDir: resolve(projectRoot, '.angular-modernizer-cache'),
|
|
581
|
+
cacheDir: resolve(config.projectRoot, '.angular-modernizer-cache'),
|
|
346
582
|
maxSize: 100 * 1024 * 1024, // 100MB
|
|
347
583
|
maxAge: 24 * 60 * 60 * 1000, // 24 hours
|
|
348
584
|
compression: true,
|
|
349
585
|
},
|
|
350
586
|
});
|
|
351
587
|
await kernel.initialize();
|
|
352
|
-
// Get the source file
|
|
353
588
|
const project = kernel.getProject();
|
|
354
589
|
const sourceFile = project.addSourceFileAtPath(filePath);
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
autoSave: true,
|
|
361
|
-
pluginConfig: {
|
|
362
|
-
'@angular-modernizer/plugin-standalone': {
|
|
363
|
-
transformationType: transformation === 'standalone-migration'
|
|
364
|
-
? transformation
|
|
365
|
-
: undefined,
|
|
366
|
-
...options,
|
|
367
|
-
},
|
|
368
|
-
'@angular-modernizer/plugin-angular': {
|
|
369
|
-
transformationType: [
|
|
370
|
-
'constructor-to-inject',
|
|
371
|
-
'interface-extraction',
|
|
372
|
-
'dependency-injection-migration',
|
|
373
|
-
'facade-pattern',
|
|
374
|
-
'form-modernization',
|
|
375
|
-
'constructor-injection-transform',
|
|
376
|
-
'component-inputs-interface-extraction',
|
|
377
|
-
'service-injection-cleanup',
|
|
378
|
-
'container-presentational',
|
|
379
|
-
'core-shared-modules',
|
|
380
|
-
'inheritance-to-composition',
|
|
381
|
-
'promise-service-to-observable',
|
|
382
|
-
'promise-component-to-reactive',
|
|
383
|
-
'promise-cleanup',
|
|
384
|
-
'sequential-await-to-forkjoin',
|
|
385
|
-
'missing-output-transform',
|
|
386
|
-
'subscription-transform',
|
|
387
|
-
'library-extraction',
|
|
388
|
-
].includes(transformation)
|
|
389
|
-
? transformation
|
|
390
|
-
: undefined,
|
|
391
|
-
...options,
|
|
392
|
-
},
|
|
393
|
-
},
|
|
590
|
+
const checkCancelled = async () => {
|
|
591
|
+
await yieldIfCancellable(config.signal);
|
|
592
|
+
if (config.signal?.aborted === true) {
|
|
593
|
+
throw new TransformCancelledError();
|
|
594
|
+
}
|
|
394
595
|
};
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
if (
|
|
398
|
-
|
|
596
|
+
await checkCancelled();
|
|
597
|
+
config.progress?.(1, 3, 'project loaded');
|
|
598
|
+
// Step 0: Pre-validate if requested (no backup needed when it fails)
|
|
599
|
+
if (config.validateBeforeTransform) {
|
|
600
|
+
preValidationResult = await this.preValidate(sourceFile, project, transformation);
|
|
399
601
|
}
|
|
400
|
-
//
|
|
602
|
+
// Step 1: Backup in the project: file copy plus git snapshot
|
|
603
|
+
if (config.createBackup) {
|
|
604
|
+
const backup = await rollbackManager.createBackup([filePath], {
|
|
605
|
+
reason: `transform-code ${transformation}`,
|
|
606
|
+
});
|
|
607
|
+
if (!backup.success) {
|
|
608
|
+
throw new Error(`Failed to create backup before transformation: ${backup.error ?? 'unknown error'}`);
|
|
609
|
+
}
|
|
610
|
+
backupResult = {
|
|
611
|
+
created: true,
|
|
612
|
+
backupPath: backup.metadata?.backupPaths?.[0],
|
|
613
|
+
strategy: 'file-backup',
|
|
614
|
+
gitSnapshot: backup.metadata?.gitStashRef,
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
const typeErrorsBefore = config.validateTransformation
|
|
618
|
+
? fileTypeErrors(sourceFile)
|
|
619
|
+
: [];
|
|
620
|
+
const linesBefore = sourceFile.getFullText().split('\n').length;
|
|
621
|
+
// Step 2: Transform
|
|
622
|
+
const transformed = config.apiStyle === 'fluent'
|
|
623
|
+
? await this.transformFluent(kernel, filePath, transformation, options, config)
|
|
624
|
+
: await this.transformImperative(kernel, sourceFile, transformation, options);
|
|
625
|
+
await sourceFile.save();
|
|
626
|
+
await checkCancelled();
|
|
627
|
+
config.progress?.(2, 3, 'transformed, validating');
|
|
401
628
|
const linesAfter = sourceFile.getFullText().split('\n').length;
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
buildTimeout: buildTimeout,
|
|
415
|
-
testTimeout: 600000,
|
|
416
|
-
lintTimeout: 120000,
|
|
417
|
-
cwd: projectRoot,
|
|
629
|
+
const changes = {
|
|
630
|
+
linesChanged: transformed.linesChanged,
|
|
631
|
+
additions: Math.max(0, linesAfter - linesBefore),
|
|
632
|
+
deletions: Math.max(0, linesBefore - linesAfter),
|
|
633
|
+
};
|
|
634
|
+
// Step 3: Validate
|
|
635
|
+
const checked = config.validateTransformation
|
|
636
|
+
? await this.validateTransformedFile(sourceFile, filePath, typeErrorsBefore, config)
|
|
637
|
+
: {
|
|
638
|
+
valid: true,
|
|
639
|
+
validation: await this.validateTransformation(filePath, transformation),
|
|
640
|
+
issues: [],
|
|
418
641
|
};
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
642
|
+
await checkCancelled();
|
|
643
|
+
config.progress?.(3, 3, 'validated');
|
|
644
|
+
const warnings = [...transformed.warnings];
|
|
645
|
+
// Step 4: Roll back when validation failed
|
|
646
|
+
if (!checked.valid && config.autoRollback && config.createBackup) {
|
|
647
|
+
const attempt = await rollbackManager.rollback([filePath]);
|
|
648
|
+
rollbackResult = {
|
|
649
|
+
performed: true,
|
|
650
|
+
reason: `Validation failed: ${checked.issues.length} issue(s) found`,
|
|
651
|
+
success: attempt.success,
|
|
424
652
|
};
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
backupDir: backupDir ?? undefined,
|
|
429
|
-
gitRoot: projectRoot,
|
|
430
|
-
};
|
|
431
|
-
const rollbackManager = new RollbackManager(rollbackConfig);
|
|
432
|
-
const rollbackAttempt = await rollbackManager.rollback([filePath]);
|
|
433
|
-
rollbackResult = {
|
|
434
|
-
performed: true,
|
|
435
|
-
reason: `Validation failed: ${validationResult.issues.length} issue(s) found`,
|
|
436
|
-
success: rollbackAttempt.success,
|
|
437
|
-
};
|
|
438
|
-
// If rollback succeeded, mark transformation as failed
|
|
439
|
-
if (rollbackAttempt.success) {
|
|
440
|
-
return {
|
|
441
|
-
success: false,
|
|
442
|
-
filePath,
|
|
443
|
-
transformation,
|
|
444
|
-
changes: {
|
|
445
|
-
linesChanged,
|
|
446
|
-
additions,
|
|
447
|
-
deletions,
|
|
448
|
-
},
|
|
449
|
-
preValidation: preValidationResult,
|
|
450
|
-
validation,
|
|
451
|
-
backup: backupResult
|
|
452
|
-
? {
|
|
453
|
-
created: backupResult.success,
|
|
454
|
-
backupPath: backupResult.backupPath,
|
|
455
|
-
strategy: 'file-backup',
|
|
456
|
-
}
|
|
457
|
-
: undefined,
|
|
458
|
-
rollback: rollbackResult,
|
|
459
|
-
warnings: [
|
|
460
|
-
...result.errors.map((e) => e.message),
|
|
461
|
-
`Transformation rolled back due to validation failures`,
|
|
462
|
-
],
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
else {
|
|
468
|
-
// Use basic validation if full validation is disabled
|
|
469
|
-
validation = (await this.validateTransformation(filePath, transformation));
|
|
653
|
+
warnings.push(attempt.success
|
|
654
|
+
? 'Transformation rolled back due to validation failures'
|
|
655
|
+
: `Rollback failed: ${attempt.error ?? 'unknown error'}`);
|
|
470
656
|
}
|
|
471
657
|
return {
|
|
472
|
-
success:
|
|
658
|
+
success: transformed.success && checked.valid,
|
|
473
659
|
filePath,
|
|
474
660
|
transformation,
|
|
475
|
-
changes
|
|
476
|
-
linesChanged,
|
|
477
|
-
additions,
|
|
478
|
-
deletions,
|
|
479
|
-
},
|
|
661
|
+
changes,
|
|
480
662
|
preValidation: preValidationResult,
|
|
481
|
-
validation,
|
|
482
|
-
backup: backupResult
|
|
483
|
-
? {
|
|
484
|
-
created: backupResult.success,
|
|
485
|
-
backupPath: backupResult.backupPath,
|
|
486
|
-
strategy: 'file-backup',
|
|
487
|
-
}
|
|
488
|
-
: undefined,
|
|
663
|
+
validation: checked.validation,
|
|
664
|
+
backup: backupResult,
|
|
489
665
|
rollback: rollbackResult,
|
|
490
|
-
|
|
666
|
+
pipeline: transformed.pipeline,
|
|
667
|
+
intermediateResults: transformed.intermediateResults,
|
|
668
|
+
buildValidation: checked.buildValidation,
|
|
669
|
+
warnings: [...warnings, ...checked.issues],
|
|
491
670
|
};
|
|
492
671
|
}
|
|
493
672
|
catch (error) {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
performed: true,
|
|
505
|
-
reason: `Transformation failed with error: ${error instanceof Error ? error.message : String(error)}`,
|
|
506
|
-
success: rollbackAttempt.success,
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
catch (rollbackError) {
|
|
510
|
-
// Rollback failed, include this in the error
|
|
511
|
-
throw new Error(`Code transformation failed: ${error instanceof Error ? error.message : String(error)}. ` +
|
|
512
|
-
`Rollback also failed: ${rollbackError instanceof Error ? rollbackError.message : String(rollbackError)}`);
|
|
673
|
+
if (error instanceof TransformCancelledError) {
|
|
674
|
+
return this.cancelledResult(filePath, transformation, backupResult, rollbackManager);
|
|
675
|
+
}
|
|
676
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
677
|
+
// Restore the file when it may already have been changed
|
|
678
|
+
if (backupResult && config.autoRollback && !rollbackResult) {
|
|
679
|
+
const attempt = await rollbackManager.rollback([filePath]);
|
|
680
|
+
if (!attempt.success) {
|
|
681
|
+
throw new Error(`Code transformation failed: ${message}. ` +
|
|
682
|
+
`Rollback also failed: ${attempt.error ?? 'unknown error'}`);
|
|
513
683
|
}
|
|
514
684
|
}
|
|
515
|
-
throw new Error(`Code transformation failed: ${
|
|
685
|
+
throw new Error(`Code transformation failed: ${message}`);
|
|
516
686
|
}
|
|
517
687
|
}
|
|
518
688
|
/**
|
|
519
|
-
*
|
|
689
|
+
* Result of a cancelled single-file run. A cancelled run always restores
|
|
690
|
+
* its backup (autoRollback or not): the client asked to stop, a half
|
|
691
|
+
* validated change must not stay. The SDK does not send this result; it
|
|
692
|
+
* is complete for direct callers.
|
|
520
693
|
*/
|
|
521
|
-
async
|
|
522
|
-
|
|
523
|
-
let
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
if (
|
|
528
|
-
|
|
529
|
-
if (!tsConfigPath) {
|
|
530
|
-
throw new Error('Could not find tsconfig.json in the project root');
|
|
531
|
-
}
|
|
532
|
-
const tempKernel = new Kernel({
|
|
533
|
-
tsConfigPath,
|
|
534
|
-
plugins: [new AngularPlugin()],
|
|
535
|
-
cacheConfig: {
|
|
536
|
-
enabled: true,
|
|
537
|
-
cacheDir: resolve(config.projectRoot, '.angular-modernizer-cache'),
|
|
538
|
-
maxSize: 100 * 1024 * 1024,
|
|
539
|
-
maxAge: 24 * 60 * 60 * 1000,
|
|
540
|
-
compression: true,
|
|
541
|
-
},
|
|
542
|
-
});
|
|
543
|
-
await tempKernel.initialize();
|
|
544
|
-
const project = tempKernel.getProject();
|
|
545
|
-
const sourceFile = project.addSourceFileAtPath(filePath);
|
|
546
|
-
const preValidator = new PreTransformValidator();
|
|
547
|
-
const preValidationConfig = {
|
|
548
|
-
validateSyntax: true,
|
|
549
|
-
validateDependencies: true,
|
|
550
|
-
validateTypes: true,
|
|
551
|
-
validateSchema: true,
|
|
552
|
-
schema: this.getSchemaForTransformation(transformation),
|
|
553
|
-
};
|
|
554
|
-
const result = await preValidator.validate(sourceFile, project, preValidationConfig);
|
|
555
|
-
preValidationResult = {
|
|
556
|
-
valid: result.valid,
|
|
557
|
-
issues: result.issues.map((issue) => ({
|
|
558
|
-
type: issue.type,
|
|
559
|
-
severity: issue.severity,
|
|
560
|
-
message: issue.message,
|
|
561
|
-
filePath: issue.filePath,
|
|
562
|
-
line: issue.line,
|
|
563
|
-
column: issue.column,
|
|
564
|
-
})),
|
|
565
|
-
};
|
|
566
|
-
if (!result.valid) {
|
|
567
|
-
throw new Error(`Pre-validation failed: ${result.issues.length} issue(s) found. ` +
|
|
568
|
-
`Cannot safely transform file. Please fix these issues first:\n` +
|
|
569
|
-
result.issues
|
|
570
|
-
.map((i) => ` - [${i.severity}] ${i.message} ${i.filePath ? `at ${i.filePath}` : ''}${i.line ? `:${i.line}` : ''}`)
|
|
571
|
-
.join('\n'));
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
// Step 1: Create backup if requested
|
|
575
|
-
if (config.createBackup) {
|
|
576
|
-
const backupManager = new BackupManager(config.backupDir);
|
|
577
|
-
backupResult = await backupManager.backup(filePath);
|
|
578
|
-
if (!backupResult.success) {
|
|
579
|
-
throw new Error('Failed to create backup before transformation');
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
// Step 2: Create kernel and perform transformation using FluentKernel
|
|
583
|
-
const tsConfigPath = findTsConfig(config.projectRoot);
|
|
584
|
-
if (!tsConfigPath) {
|
|
585
|
-
throw new Error('Could not find tsconfig.json in the project root');
|
|
694
|
+
async cancelledResult(filePath, transformation, backup, rollbackManager) {
|
|
695
|
+
const warnings = [];
|
|
696
|
+
let rollback;
|
|
697
|
+
if (backup !== undefined) {
|
|
698
|
+
const attempt = await rollbackManager.rollback([filePath]);
|
|
699
|
+
rollback = { performed: true, reason: 'Cancelled by the client', success: attempt.success };
|
|
700
|
+
if (!attempt.success) {
|
|
701
|
+
warnings.push(`Rollback failed: ${attempt.error ?? 'unknown error'}`);
|
|
586
702
|
}
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
rollbackResult = {
|
|
666
|
-
performed: true,
|
|
667
|
-
reason: `Validation failed: ${validationResult.issues.length} issue(s) found`,
|
|
668
|
-
success: rollbackAttempt.success,
|
|
669
|
-
};
|
|
670
|
-
if (rollbackAttempt.success) {
|
|
671
|
-
return {
|
|
672
|
-
success: false,
|
|
673
|
-
filePath,
|
|
674
|
-
transformation,
|
|
675
|
-
changes: {
|
|
676
|
-
linesChanged: pipelineResult.transformations.length,
|
|
677
|
-
additions,
|
|
678
|
-
deletions,
|
|
679
|
-
},
|
|
680
|
-
preValidation: preValidationResult,
|
|
681
|
-
validation,
|
|
682
|
-
backup: backupResult
|
|
683
|
-
? {
|
|
684
|
-
created: backupResult.success,
|
|
685
|
-
backupPath: backupResult.backupPath,
|
|
686
|
-
strategy: 'file-backup',
|
|
687
|
-
}
|
|
688
|
-
: undefined,
|
|
689
|
-
rollback: rollbackResult,
|
|
690
|
-
pipeline,
|
|
691
|
-
intermediateResults,
|
|
692
|
-
buildValidation,
|
|
693
|
-
warnings: pipelineResult.errors.map((e) => e.message),
|
|
694
|
-
};
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
else {
|
|
699
|
-
validation = (await this.validateTransformation(filePath, transformation));
|
|
700
|
-
}
|
|
701
|
-
return {
|
|
702
|
-
success: pipelineResult.success,
|
|
703
|
-
filePath,
|
|
704
|
-
transformation,
|
|
705
|
-
changes: {
|
|
706
|
-
linesChanged: pipelineResult.transformations.length,
|
|
707
|
-
additions,
|
|
708
|
-
deletions,
|
|
709
|
-
},
|
|
710
|
-
preValidation: preValidationResult,
|
|
711
|
-
validation,
|
|
712
|
-
backup: backupResult
|
|
713
|
-
? {
|
|
714
|
-
created: backupResult.success,
|
|
715
|
-
backupPath: backupResult.backupPath,
|
|
716
|
-
strategy: 'file-backup',
|
|
717
|
-
}
|
|
718
|
-
: undefined,
|
|
719
|
-
rollback: rollbackResult,
|
|
720
|
-
pipeline,
|
|
721
|
-
intermediateResults,
|
|
722
|
-
buildValidation,
|
|
723
|
-
warnings: pipelineResult.errors.map((e) => e.message),
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
warnings.push('Cancelled by the client before a backup was taken; the file was not changed.');
|
|
706
|
+
}
|
|
707
|
+
return {
|
|
708
|
+
success: false,
|
|
709
|
+
filePath,
|
|
710
|
+
transformation,
|
|
711
|
+
changes: { linesChanged: 0, additions: 0, deletions: 0 },
|
|
712
|
+
validation: { syntaxValid: true, typeCheckPassed: true },
|
|
713
|
+
backup,
|
|
714
|
+
rollback,
|
|
715
|
+
warnings,
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Pre-validate the file on the loaded project; throws when it is invalid.
|
|
720
|
+
*/
|
|
721
|
+
async preValidate(sourceFile, project, transformation) {
|
|
722
|
+
const preValidator = new PreTransformValidator();
|
|
723
|
+
const preValidationConfig = {
|
|
724
|
+
validateSyntax: true,
|
|
725
|
+
validateDependencies: true,
|
|
726
|
+
validateTypes: true,
|
|
727
|
+
validateSchema: true,
|
|
728
|
+
schema: this.getSchemaForTransformation(transformation),
|
|
729
|
+
};
|
|
730
|
+
const result = await preValidator.validate(sourceFile, project, preValidationConfig);
|
|
731
|
+
if (!result.valid) {
|
|
732
|
+
throw new Error(`Pre-validation failed: ${result.issues.length} issue(s) found. ` +
|
|
733
|
+
`Cannot safely transform file. Please fix these issues first:\n` +
|
|
734
|
+
result.issues
|
|
735
|
+
.map((i) => ` - [${i.severity}] ${i.message} ${i.filePath ? `at ${i.filePath}` : ''}${i.line ? `:${i.line}` : ''}`)
|
|
736
|
+
.join('\n'));
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
valid: result.valid,
|
|
740
|
+
issues: result.issues.map((issue) => ({
|
|
741
|
+
type: issue.type,
|
|
742
|
+
severity: issue.severity,
|
|
743
|
+
message: issue.message,
|
|
744
|
+
filePath: issue.filePath,
|
|
745
|
+
line: issue.line,
|
|
746
|
+
column: issue.column,
|
|
747
|
+
})),
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Transform through the Kernel API.
|
|
752
|
+
*/
|
|
753
|
+
async transformImperative(kernel, sourceFile, transformation, options) {
|
|
754
|
+
const result = (await kernel.transformFile(sourceFile, {
|
|
755
|
+
dryRun: false,
|
|
756
|
+
autoSave: true,
|
|
757
|
+
pluginConfig: pluginConfigFor(transformation, options),
|
|
758
|
+
}));
|
|
759
|
+
return {
|
|
760
|
+
success: result.success,
|
|
761
|
+
linesChanged: result.ruleResults.reduce((sum, r) => sum + (r.changeCount ?? 0), 0),
|
|
762
|
+
warnings: result.errors.map((e) => e.message),
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Transform through the FluentKernel pipeline (optional build check stage).
|
|
767
|
+
*/
|
|
768
|
+
async transformFluent(kernel, filePath, transformation, options, config) {
|
|
769
|
+
let fluent = FluentKernel.create(kernel)
|
|
770
|
+
.transform(transformation, [filePath], pluginConfigFor(transformation, options))
|
|
771
|
+
.withMetadata({ transformation, filePath, options });
|
|
772
|
+
if (config.buildCheck) {
|
|
773
|
+
fluent = fluent.buildCheck(config.autoRollback);
|
|
774
|
+
}
|
|
775
|
+
const pipelineResult = await fluent.execute();
|
|
776
|
+
const intermediateResults = {};
|
|
777
|
+
for (const stage of fluent.getStages()) {
|
|
778
|
+
intermediateResults[stage.name] = {
|
|
779
|
+
success: true,
|
|
780
|
+
message: `Stage ${stage.name} completed`,
|
|
724
781
|
};
|
|
725
782
|
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
783
|
+
return {
|
|
784
|
+
success: pipelineResult.success,
|
|
785
|
+
linesChanged: pipelineResult.transformations.length,
|
|
786
|
+
warnings: pipelineResult.errors.map((e) => e.message),
|
|
787
|
+
pipeline: fluent.getStages().map((stage) => stage.name),
|
|
788
|
+
intermediateResults,
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Validate the transformed file: type errors the transformation introduced
|
|
793
|
+
* (from the loaded project, errors the file already had do not count), then
|
|
794
|
+
* build and lint only when requested. `buildValidation` is reported only
|
|
795
|
+
* when a build ran.
|
|
796
|
+
*/
|
|
797
|
+
async validateTransformedFile(sourceFile, filePath, typeErrorsBefore, config) {
|
|
798
|
+
const introduced = introducedTypeErrors(typeErrorsBefore, fileTypeErrors(sourceFile));
|
|
799
|
+
const issues = introduced.map((e) => `Type error introduced: ${e}`);
|
|
800
|
+
let postValid = true;
|
|
801
|
+
let buildValidation;
|
|
802
|
+
if (config.validateBuild || config.validateLint) {
|
|
803
|
+
const validator = new PostTransformValidator();
|
|
804
|
+
const validationConfig = {
|
|
805
|
+
validateBuild: config.validateBuild,
|
|
806
|
+
validateTests: false,
|
|
807
|
+
validateLint: config.validateLint,
|
|
808
|
+
buildTimeout: config.buildTimeout,
|
|
809
|
+
testTimeout: 600000,
|
|
810
|
+
lintTimeout: 120000,
|
|
811
|
+
cwd: config.projectRoot,
|
|
812
|
+
signal: config.signal,
|
|
813
|
+
};
|
|
814
|
+
const result = await validator.validate(filePath, validationConfig);
|
|
815
|
+
const errors = result.issues.filter((i) => i.severity === ValidationSeverity.ERROR);
|
|
816
|
+
postValid = result.valid;
|
|
817
|
+
issues.push(...errors.map((i) => i.message));
|
|
818
|
+
if (config.validateBuild) {
|
|
819
|
+
const buildErrors = errors
|
|
820
|
+
.filter((i) => i.type === ValidationType.BUILD)
|
|
821
|
+
.map((i) => i.message);
|
|
822
|
+
buildValidation = {
|
|
823
|
+
passed: buildErrors.length === 0,
|
|
824
|
+
success: buildErrors.length === 0,
|
|
825
|
+
errors: buildErrors.length > 0 ? buildErrors : undefined,
|
|
826
|
+
};
|
|
746
827
|
}
|
|
747
|
-
throw new Error(`Code transformation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
748
828
|
}
|
|
829
|
+
return {
|
|
830
|
+
valid: introduced.length === 0 && postValid,
|
|
831
|
+
validation: {
|
|
832
|
+
syntaxValid: !introduced.some((e) => /^TS1\d{3}:/.test(e)),
|
|
833
|
+
typeCheckPassed: introduced.length === 0,
|
|
834
|
+
introducedTypeErrors: introduced.length > 0 ? introduced : undefined,
|
|
835
|
+
},
|
|
836
|
+
buildValidation,
|
|
837
|
+
issues,
|
|
838
|
+
};
|
|
749
839
|
}
|
|
750
840
|
/**
|
|
751
|
-
*
|
|
752
|
-
*
|
|
841
|
+
* Streaming mode: runs the synchronous path (same safety envelope, batch
|
|
842
|
+
* included) in the background and returns the operationId at once. The
|
|
843
|
+
* result is stored with setResult before markComplete, so
|
|
844
|
+
* get-operation-progress returns it.
|
|
753
845
|
*/
|
|
754
|
-
|
|
846
|
+
executeStreaming(params) {
|
|
755
847
|
const operationId = randomUUID();
|
|
756
848
|
const store = ProgressStore.getInstance();
|
|
757
|
-
// Create operation in store
|
|
758
849
|
store.createOperation(operationId, {
|
|
759
850
|
operationId,
|
|
760
851
|
type: 'transformation',
|
|
761
852
|
startTime: Date.now(),
|
|
762
853
|
});
|
|
763
|
-
// Start
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
//
|
|
854
|
+
// Start in a later turn of the event loop: the run's synchronous part
|
|
855
|
+
// (project load, seconds on large projects) would otherwise delay the
|
|
856
|
+
// response with the operationId. Once the synchronous part runs, it also
|
|
857
|
+
// delays the next request, so the start waits 50 ms: a client that polls
|
|
858
|
+
// get-operation-progress right after the response is answered first
|
|
859
|
+
// (measured on a large project: setImmediate ~4 s for that poll, a 0 ms
|
|
860
|
+
// timer 0 or ~4 s, 50 ms always 0 to 1 ms).
|
|
861
|
+
setTimeout(() => {
|
|
862
|
+
void this.execute({ ...params, streaming: false }).then((result) => {
|
|
863
|
+
if (store.hasOperation(operationId)) {
|
|
864
|
+
store.setResult(operationId, result);
|
|
865
|
+
store.markComplete(operationId);
|
|
866
|
+
}
|
|
867
|
+
}, (error) => {
|
|
868
|
+
if (store.hasOperation(operationId)) {
|
|
869
|
+
store.markFailed(operationId, error instanceof Error ? error.message : String(error));
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
}, 50);
|
|
769
873
|
return {
|
|
770
874
|
success: true,
|
|
771
875
|
operationId,
|
|
@@ -779,98 +883,15 @@ export class TransformCodeTool {
|
|
|
779
883
|
syntaxValid: true,
|
|
780
884
|
typeCheckPassed: true,
|
|
781
885
|
},
|
|
886
|
+
warnings: [
|
|
887
|
+
'Started in the background: the result is returned by get-operation-progress once the operation is completed.',
|
|
888
|
+
],
|
|
782
889
|
};
|
|
783
890
|
}
|
|
784
|
-
/**
|
|
785
|
-
* Run streaming transformation in background using StreamingKernelWrapper.
|
|
786
|
-
*/
|
|
787
|
-
async runStreamingTransformation(operationId, params) {
|
|
788
|
-
const store = ProgressStore.getInstance();
|
|
789
|
-
const filePath = params.filePath;
|
|
790
|
-
const transformation = params.transformation;
|
|
791
|
-
if (!filePath || typeof filePath !== 'string') {
|
|
792
|
-
throw new Error('filePath is required and must be a string');
|
|
793
|
-
}
|
|
794
|
-
if (!transformation) {
|
|
795
|
-
throw new Error('transformation is required');
|
|
796
|
-
}
|
|
797
|
-
// Find project root
|
|
798
|
-
const projectRoot = this.findProjectRoot(filePath);
|
|
799
|
-
const tsConfigPath = findTsConfig(projectRoot);
|
|
800
|
-
if (!tsConfigPath) {
|
|
801
|
-
throw new Error('Could not find tsconfig.json in the project root');
|
|
802
|
-
}
|
|
803
|
-
// Create kernel
|
|
804
|
-
const kernel = new Kernel({
|
|
805
|
-
tsConfigPath,
|
|
806
|
-
plugins: [new AngularPlugin()],
|
|
807
|
-
cacheConfig: {
|
|
808
|
-
enabled: true,
|
|
809
|
-
cacheDir: resolve(projectRoot, '.angular-modernizer-cache'),
|
|
810
|
-
maxSize: 100 * 1024 * 1024,
|
|
811
|
-
maxAge: 24 * 60 * 60 * 1000,
|
|
812
|
-
compression: true,
|
|
813
|
-
},
|
|
814
|
-
});
|
|
815
|
-
await kernel.initialize();
|
|
816
|
-
// Create StreamingKernelWrapper with diffFormat from params (default: 'structured')
|
|
817
|
-
const wrapper = new StreamingKernelWrapper(kernel, {
|
|
818
|
-
includeDiffs: true,
|
|
819
|
-
diffFormat: params.diffFormat ?? 'structured',
|
|
820
|
-
includeMemory: true,
|
|
821
|
-
validateSyntax: true,
|
|
822
|
-
diffContext: 3,
|
|
823
|
-
});
|
|
824
|
-
// Create transform config
|
|
825
|
-
const transformConfig = {
|
|
826
|
-
dryRun: false,
|
|
827
|
-
autoSave: true,
|
|
828
|
-
pluginConfig: {
|
|
829
|
-
'@angular-modernizer/plugin-standalone': {
|
|
830
|
-
transformationType: transformation === 'standalone-migration'
|
|
831
|
-
? transformation
|
|
832
|
-
: undefined,
|
|
833
|
-
...params.options,
|
|
834
|
-
},
|
|
835
|
-
'@angular-modernizer/plugin-angular': {
|
|
836
|
-
transformationType: [
|
|
837
|
-
'constructor-to-inject',
|
|
838
|
-
'interface-extraction',
|
|
839
|
-
'dependency-injection-migration',
|
|
840
|
-
'facade-pattern',
|
|
841
|
-
'form-modernization',
|
|
842
|
-
'constructor-injection-transform',
|
|
843
|
-
'component-inputs-interface-extraction',
|
|
844
|
-
'service-injection-cleanup',
|
|
845
|
-
'container-presentational',
|
|
846
|
-
'core-shared-modules',
|
|
847
|
-
'inheritance-to-composition',
|
|
848
|
-
'promise-service-to-observable',
|
|
849
|
-
'promise-component-to-reactive',
|
|
850
|
-
'promise-cleanup',
|
|
851
|
-
'sequential-await-to-forkjoin',
|
|
852
|
-
'missing-output-transform',
|
|
853
|
-
'subscription-transform',
|
|
854
|
-
'library-extraction',
|
|
855
|
-
].includes(transformation)
|
|
856
|
-
? transformation
|
|
857
|
-
: undefined,
|
|
858
|
-
...params.options,
|
|
859
|
-
},
|
|
860
|
-
},
|
|
861
|
-
};
|
|
862
|
-
// Use streaming transformation
|
|
863
|
-
for await (const event of wrapper.transformStreaming([], [filePath], transformConfig)) {
|
|
864
|
-
// Add each streaming event to the store
|
|
865
|
-
store.addEvent(operationId, event);
|
|
866
|
-
}
|
|
867
|
-
// Mark complete
|
|
868
|
-
store.markComplete(operationId);
|
|
869
|
-
}
|
|
870
891
|
/**
|
|
871
892
|
* Execute batch transformation (Phase 6a).
|
|
872
893
|
*/
|
|
873
|
-
async executeBatch(params, orchestration) {
|
|
894
|
+
async executeBatch(params, orchestration, ctx) {
|
|
874
895
|
if (!params.files || params.files.length === 0) {
|
|
875
896
|
throw new Error('files array is required for batch mode');
|
|
876
897
|
}
|
|
@@ -882,7 +903,7 @@ export class TransformCodeTool {
|
|
|
882
903
|
if (!firstFile) {
|
|
883
904
|
throw new Error('files array cannot be empty');
|
|
884
905
|
}
|
|
885
|
-
const projectRoot = this.findProjectRoot(firstFile);
|
|
906
|
+
const projectRoot = params.projectRoot ?? this.findProjectRoot(firstFile);
|
|
886
907
|
const fullConfig = loadConfig(projectRoot);
|
|
887
908
|
const config = fullConfig.tools?.['transform-code'] ?? {};
|
|
888
909
|
const mergedParams = { ...config, ...params };
|
|
@@ -893,7 +914,12 @@ export class TransformCodeTool {
|
|
|
893
914
|
options: mergedParams.options,
|
|
894
915
|
parallel: orchestration.parallel ?? false,
|
|
895
916
|
enableProgress: orchestration.enableProgress ?? false,
|
|
896
|
-
signal:
|
|
917
|
+
signal: ctx.signal,
|
|
918
|
+
onProgress: ctx.progress === undefined
|
|
919
|
+
? undefined
|
|
920
|
+
: (done, total) => {
|
|
921
|
+
ctx.progress?.(done, total, `${done}/${total} files transformed`);
|
|
922
|
+
},
|
|
897
923
|
resourceLimits: orchestration.resourceLimits
|
|
898
924
|
? {
|
|
899
925
|
maxMemory: orchestration.resourceLimits.maxMemory,
|
|
@@ -909,12 +935,59 @@ export class TransformCodeTool {
|
|
|
909
935
|
timeout: orchestration.timeout,
|
|
910
936
|
pluginIds: orchestration.pluginIds,
|
|
911
937
|
};
|
|
938
|
+
// Back up all files as one group before any of them changes
|
|
939
|
+
const rollbackManager = createProjectRollbackManager(projectRoot, {
|
|
940
|
+
backupDir: mergedParams.backupDir,
|
|
941
|
+
});
|
|
942
|
+
const createBackup = mergedParams.createBackup ?? true;
|
|
943
|
+
let backup;
|
|
944
|
+
if (createBackup) {
|
|
945
|
+
const backupResult = await rollbackManager.createBackup(batchOptions.files, {
|
|
946
|
+
reason: `transform-code ${batchOptions.transformation}`,
|
|
947
|
+
});
|
|
948
|
+
if (!backupResult.success) {
|
|
949
|
+
throw new Error(`Failed to create backup before transformation: ${backupResult.error ?? 'unknown error'}`);
|
|
950
|
+
}
|
|
951
|
+
backup = {
|
|
952
|
+
created: true,
|
|
953
|
+
backupPath: backupResult.metadata?.backupPaths?.[0],
|
|
954
|
+
strategy: 'file-backup',
|
|
955
|
+
gitSnapshot: backupResult.metadata?.gitStashRef,
|
|
956
|
+
};
|
|
957
|
+
}
|
|
912
958
|
const result = await executeBatchTransformation(batchOptions, async (filePath) => {
|
|
913
959
|
return this.transformSingleFile(filePath, params, projectRoot);
|
|
914
960
|
});
|
|
961
|
+
const failedFiles = result.files
|
|
962
|
+
.filter((f) => !f.success)
|
|
963
|
+
.map((f) => f.path);
|
|
964
|
+
const cancelled = ctx.signal?.aborted === true;
|
|
965
|
+
// A cancelled batch restores every file, whatever the strategy
|
|
966
|
+
const filesToRestore = cancelled
|
|
967
|
+
? batchOptions.files
|
|
968
|
+
: batchFilesToRestore(batchOptions.rollbackStrategy ?? 'all-or-nothing', batchOptions.files, failedFiles);
|
|
969
|
+
let rollback;
|
|
970
|
+
if (cancelled && createBackup) {
|
|
971
|
+
const attempt = await rollbackManager.rollback(filesToRestore);
|
|
972
|
+
rollback = {
|
|
973
|
+
performed: true,
|
|
974
|
+
reason: `Cancelled by the client after ${result.files.length} of ${batchOptions.files.length} file(s)`,
|
|
975
|
+
success: attempt.success,
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
else if (createBackup && failedFiles.length > 0 && filesToRestore.length > 0) {
|
|
979
|
+
const attempt = await rollbackManager.rollback(filesToRestore);
|
|
980
|
+
rollback = {
|
|
981
|
+
performed: true,
|
|
982
|
+
reason: `${failedFiles.length} file(s) failed (${batchOptions.rollbackStrategy})`,
|
|
983
|
+
success: attempt.success,
|
|
984
|
+
};
|
|
985
|
+
}
|
|
915
986
|
// Convert batch result to TransformationResult format
|
|
916
987
|
return {
|
|
917
|
-
success: result.success,
|
|
988
|
+
success: result.success && !cancelled,
|
|
989
|
+
backup,
|
|
990
|
+
rollback,
|
|
918
991
|
files: result.files,
|
|
919
992
|
transformation: params.transformation,
|
|
920
993
|
changes: {
|
|
@@ -954,39 +1027,7 @@ export class TransformCodeTool {
|
|
|
954
1027
|
const transformConfig = {
|
|
955
1028
|
dryRun: false,
|
|
956
1029
|
autoSave: true,
|
|
957
|
-
pluginConfig:
|
|
958
|
-
'@angular-modernizer/plugin-standalone': {
|
|
959
|
-
transformationType: params.transformation === 'standalone-migration'
|
|
960
|
-
? params.transformation
|
|
961
|
-
: undefined,
|
|
962
|
-
...params.options,
|
|
963
|
-
},
|
|
964
|
-
'@angular-modernizer/plugin-angular': {
|
|
965
|
-
transformationType: [
|
|
966
|
-
'constructor-to-inject',
|
|
967
|
-
'interface-extraction',
|
|
968
|
-
'dependency-injection-migration',
|
|
969
|
-
'facade-pattern',
|
|
970
|
-
'form-modernization',
|
|
971
|
-
'constructor-injection-transform',
|
|
972
|
-
'component-inputs-interface-extraction',
|
|
973
|
-
'service-injection-cleanup',
|
|
974
|
-
'container-presentational',
|
|
975
|
-
'core-shared-modules',
|
|
976
|
-
'inheritance-to-composition',
|
|
977
|
-
'promise-service-to-observable',
|
|
978
|
-
'promise-component-to-reactive',
|
|
979
|
-
'promise-cleanup',
|
|
980
|
-
'sequential-await-to-forkjoin',
|
|
981
|
-
'missing-output-transform',
|
|
982
|
-
'subscription-transform',
|
|
983
|
-
'library-extraction',
|
|
984
|
-
].includes(params.transformation ?? '')
|
|
985
|
-
? params.transformation
|
|
986
|
-
: undefined,
|
|
987
|
-
...params.options,
|
|
988
|
-
},
|
|
989
|
-
},
|
|
1030
|
+
pluginConfig: pluginConfigFor(params.transformation, params.options),
|
|
990
1031
|
};
|
|
991
1032
|
const result = (await kernel.transformFile(sourceFile, transformConfig));
|
|
992
1033
|
if (!transformConfig.dryRun && transformConfig.autoSave) {
|