Skip to content

Commit 5229e0d

Browse files
committed
refactor(@angular/cli): Revise how the schematics runner is invoked
1 parent 115f7a8 commit 5229e0d

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

packages/angular/cli/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ ts_project(
130130
"//:node_modules/@types/yargs",
131131
"//:node_modules/rxjs",
132132
"//:node_modules/semver",
133+
"//:node_modules/typescript",
133134
"//packages/angular_devkit/schematics/testing",
134135
],
135136
)

packages/angular/cli/src/commands/mcp/tools/modernize.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { NodeWorkflow } from '@angular-devkit/schematics/tools';
77
import { NodeJsSyncHost } from '@angular-devkit/core/node';
88
import { virtualFs } from '@angular-devkit/core';
99

10-
export const SCHEMATICS_ROOT = '../../../../../../@angular/core/schematics';
11-
1210
enum SchematicTarget {
1311
Code,
1412
Template,
@@ -155,9 +153,10 @@ export async function runModernization(
155153
},
156154
);
157155

156+
const angularCorePath = path.dirname(require.resolve('@angular/core/package.json'));
158157
const collectionPaths = {
159-
[SchematicRunner.Migration]: path.join(__dirname, SCHEMATICS_ROOT, 'migrations.json'),
160-
[SchematicRunner.Collection]: path.join(__dirname, SCHEMATICS_ROOT, 'collection.json'),
158+
[SchematicRunner.Migration]: path.join(angularCorePath, 'schematics/migrations.json'),
159+
[SchematicRunner.Collection]: path.join(angularCorePath, 'schematics/collection.json'),
161160
};
162161

163162
const transformationsToRun =
@@ -178,7 +177,7 @@ export async function runModernization(
178177
documentation.add(transformation.documentation);
179178
}
180179

181-
let options: { [key: string]: unknown } = { path: tempDir };
180+
let options: { [key: string]: unknown } = { path: '/' };
182181
if (transformation.name === 'standalone') {
183182
const mode = input.mode ?? 'convert-to-standalone';
184183
options = { ...options, mode };

packages/angular/cli/src/commands/mcp/tools/modernize_spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,53 @@ describe('Modernize Tool', () => {
146146
expect(fileResult?.content).toBe('<app-foo />');
147147
});
148148
});
149+
150+
describe('Integration Tests', () => {
151+
let tempDir: string;
152+
153+
beforeEach(() => {
154+
tempDir = fs.mkdtempSync(path.join(tmpdir(), 'modernize-spec-integration-'));
155+
fs.mkdirSync(path.join(tempDir, 'node_modules'));
156+
fs.symlinkSync(
157+
path.resolve('node_modules/@angular-devkit'),
158+
path.join(tempDir, 'node_modules/@angular-devkit'),
159+
'dir',
160+
);
161+
fs.symlinkSync(
162+
path.resolve('node_modules/typescript'),
163+
path.join(tempDir, 'node_modules/typescript'),
164+
'dir',
165+
);
166+
});
167+
168+
afterEach(() => {
169+
fs.rmSync(tempDir, { recursive: true, force: true });
170+
});
171+
172+
it('should run the self-closing-tags migration', async () => {
173+
const runfilesRoot = path.join(__dirname, '../../../../../../../../');
174+
const outputFile = path.join(runfilesRoot, 'runfiles.log');
175+
console.log('Output file:', outputFile);
176+
fs.writeFileSync(outputFile, '');
177+
fs.readdirSync(runfilesRoot, { recursive: true }).forEach((file) => {
178+
fs.appendFileSync(outputFile, file + '\n');
179+
});
180+
181+
const input: ModernizeInput = {
182+
files: [{ name: 'test.ng.html', content: '<app-foo></app-foo>' }],
183+
transformations: ['self-closing-tags-migration'],
184+
};
185+
186+
const result = await runModernization(input, undefined, tempDir);
187+
188+
if (!('files' in result.structuredContent) || !result.structuredContent.files) {
189+
fail('Expected files to be present in the result');
190+
return;
191+
}
192+
193+
const fileResult = result.structuredContent.files[0];
194+
expect(fileResult?.changed).toBe(true);
195+
expect(fileResult?.content).toBe('<app-foo />');
196+
});
197+
});
149198
});

0 commit comments

Comments
 (0)