Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 26 additions & 34 deletions packages/core/schematics/migrations/control-flow-migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {Rule, SchematicContext, SchematicsException, Tree} from '@angular-devkit/schematics';
import {join, relative} from 'path';
import ts from 'typescript';

import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host';

Expand All @@ -29,6 +30,12 @@ export function migrate(options: Options): Rule {
const basePath = process.cwd();
let pathToMigrate: string | undefined;
if (options.path) {
if (options.path.startsWith('..')) {
throw new SchematicsException(
'Cannot run control flow migration outside of the current project.',
);
}

pathToMigrate = normalizePath(join(basePath, options.path));
if (pathToMigrate.trim() !== '') {
allPaths.push(pathToMigrate);
Expand All @@ -39,60 +46,45 @@ export function migrate(options: Options): Rule {
}

if (!allPaths.length) {
throw new SchematicsException(
'Could not find any tsconfig file. Cannot run the http providers migration.',
context.logger.warn(
'Could not find any tsconfig file. Cannot run the control flow migration.',
);
return;
}

let errors: string[] = [];
let sourceFilesCount = 0;

for (const tsconfigPath of allPaths) {
const migrateErrors = runControlFlowMigration(
tree,
tsconfigPath,
basePath,
pathToMigrate,
options,
);
const program = createMigrationProgram(tree, tsconfigPath, basePath);
const sourceFiles = program
.getSourceFiles()
.filter(
(sourceFile) =>
(pathToMigrate ? sourceFile.fileName.startsWith(pathToMigrate) : true) &&
canMigrateFile(basePath, sourceFile, program),
);

const migrateErrors = runControlFlowMigration(tree, sourceFiles, basePath, options);
errors = [...errors, ...migrateErrors];
sourceFilesCount += sourceFiles.length;
}

if (errors.length > 0) {
context.logger.warn(`WARNING: ${errors.length} errors occurred during your migration:\n`);
errors.forEach((err: string) => {
context.logger.warn(err);
});
errors.forEach((err) => context.logger.warn(err));
} else if (sourceFilesCount === 0) {
context.logger.warn('Control flow migration did not find any files to migrate');
}
};
}

function runControlFlowMigration(
tree: Tree,
tsconfigPath: string,
sourceFiles: ts.SourceFile[],
basePath: string,
pathToMigrate?: string,
schematicOptions?: Options,
): string[] {
if (schematicOptions?.path?.startsWith('..')) {
throw new SchematicsException(
'Cannot run control flow migration outside of the current project.',
);
}

const program = createMigrationProgram(tree, tsconfigPath, basePath);
const sourceFiles = program
.getSourceFiles()
.filter(
(sourceFile) =>
(pathToMigrate ? sourceFile.fileName.startsWith(pathToMigrate) : true) &&
canMigrateFile(basePath, sourceFile, program),
);

if (sourceFiles.length === 0) {
throw new SchematicsException(
`Could not find any files to migrate under the path ${pathToMigrate}. Cannot run the control flow migration.`,
);
}
const analysis = new Map<string, AnalyzedFile>();
const migrateErrors = new Map<string, MigrateError[]>();

Expand Down
15 changes: 3 additions & 12 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6926,9 +6926,7 @@ describe('control flow migration (ng generate)', () => {
});

describe('path', () => {
it('should throw an error if no files match the passed-in path', async () => {
let error: string | null = null;

it('should warn if no files match the passed-in path', async () => {
writeFile(
'dir.ts',
`
Expand All @@ -6938,15 +6936,8 @@ describe('control flow migration (ng generate)', () => {
`,
);

try {
await runMigration('./foo');
} catch (e: any) {
error = e.message;
}

expect(error).toMatch(
/Could not find any files to migrate under the path .*\/foo\. Cannot run the control flow migration/,
);
await runMigration('./foo');
expect(warnOutput).toContain('Control flow migration did not find any files to migrate');
});

it('should throw an error if a path outside of the project is passed in', async () => {
Expand Down
Loading