diff --git a/packages/core/schematics/migrations/static-queries/index.ts b/packages/core/schematics/migrations/static-queries/index.ts index 505c404bde..971c01f40e 100644 --- a/packages/core/schematics/migrations/static-queries/index.ts +++ b/packages/core/schematics/migrations/static-queries/index.ts @@ -21,9 +21,14 @@ import {QueryTemplateStrategy} from './strategies/template_strategy/template_str import {QueryTestStrategy} from './strategies/test_strategy/test_strategy'; import {TimingStrategy} from './strategies/timing-strategy'; import {QueryUsageStrategy} from './strategies/usage_strategy/usage_strategy'; -import {SELECTED_STRATEGY, promptForMigrationStrategy} from './strategy_prompt'; import {getTransformedQueryCallExpr} from './transform'; +enum SELECTED_STRATEGY { + TEMPLATE, + USAGE, + TESTS, +} + interface AnalyzedProject { program: ts.Program; host: ts.CompilerHost; @@ -50,8 +55,9 @@ async function runMigration(tree: Tree, context: SchematicContext) { const logger = context.logger; logger.info('------ Static Query migration ------'); - logger.info('In preparation for Ivy, developers can now explicitly specify the'); - logger.info('timing of their queries. Read more about this here:'); + logger.info('With Angular version 8, developers need to'); + logger.info('explicitly specify the timing of ViewChild or'); + logger.info('ContentChild queries. Read more about this here:'); logger.info('https://github.com/angular/angular/pull/28810'); if (!buildPaths.length && !testPaths.length) { @@ -63,6 +69,9 @@ async function runMigration(tree: Tree, context: SchematicContext) { const analyzedFiles = new Set(); const buildProjects = new Set(); const failures = []; + const strategy = process.env['NG_STATIC_QUERY_USAGE_STRATEGY'] === 'true' ? + SELECTED_STRATEGY.USAGE : + SELECTED_STRATEGY.TEMPLATE; for (const tsconfigPath of buildPaths) { const project = analyzeProject(tree, tsconfigPath, basePath, analyzedFiles); @@ -71,10 +80,7 @@ async function runMigration(tree: Tree, context: SchematicContext) { } } - // In case there are projects which contain queries that need to be migrated, - // we want to prompt for the migration strategy and run the migration. if (buildProjects.size) { - const strategy = await promptForMigrationStrategy(logger); for (let project of Array.from(buildProjects.values())) { failures.push(...await runStaticQueryMigration(tree, project, strategy, logger)); } diff --git a/packages/core/schematics/migrations/static-queries/strategy_prompt.ts b/packages/core/schematics/migrations/static-queries/strategy_prompt.ts deleted file mode 100644 index e0cbf514a9..0000000000 --- a/packages/core/schematics/migrations/static-queries/strategy_prompt.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @license - * Copyright Google Inc. All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - - -import {logging} from '@angular-devkit/core'; - -import {getInquirer, supportsPrompt} from '../../utils/schematics_prompt'; - -export enum SELECTED_STRATEGY { - TEMPLATE, - USAGE, - TESTS, -} - -/** - * Prompts the user for the migration strategy that should be used. Defaults to the - * template strategy as it provides a migration with rare manual corrections. - * */ -export async function promptForMigrationStrategy(logger: logging.LoggerApi) { - if (supportsPrompt()) { - logger.info('There are two available migration strategies that can be selected:'); - logger.info(' • Template strategy - migration tool (short-term gains, rare corrections)'); - logger.info(' • Usage strategy - best practices (long-term gains, manual corrections)'); - logger.info('For an easy migration, the template strategy is recommended. The usage'); - logger.info('strategy can be used for best practices and a code base that will be more'); - logger.info('flexible to changes going forward.'); - const {strategyName} = await getInquirer().prompt<{strategyName: string}>({ - type: 'list', - name: 'strategyName', - message: 'What migration strategy do you want to use?', - choices: [ - {name: 'Template strategy', value: 'template'}, {name: 'Usage strategy', value: 'usage'} - ], - default: 'template', - }); - logger.info(''); - return strategyName === 'usage' ? SELECTED_STRATEGY.USAGE : SELECTED_STRATEGY.TEMPLATE; - } else { - // In case prompts are not supported, we still want to allow developers to opt - // into the usage strategy by specifying an environment variable. The tests also - // use the environment variable as there is no headless way to select via prompt. - return !!process.env['NG_STATIC_QUERY_USAGE_STRATEGY'] ? SELECTED_STRATEGY.USAGE : - SELECTED_STRATEGY.TEMPLATE; - } -} diff --git a/packages/core/schematics/test/static_queries_migration_usage_spec.ts b/packages/core/schematics/test/static_queries_migration_usage_spec.ts index fa92fa1e0b..c1e065276f 100644 --- a/packages/core/schematics/test/static_queries_migration_usage_spec.ts +++ b/packages/core/schematics/test/static_queries_migration_usage_spec.ts @@ -1506,23 +1506,6 @@ describe('static-queries migration with usage strategy', () => { .toContain(`@${queryType}('test', { static: false }) query: any;`); }); - it(`should not prompt for migration strategy if no @${queryType} query is used`, async() => { - writeFile('/index.ts', ` - import {Component, ${queryType}} from '@angular/core'; - - @Component({template: ''}) - export class NoQueriesDeclared { - } - `); - - const testModule = require('../migrations/static-queries/strategy_prompt'); - spyOn(testModule, 'promptForMigrationStrategy').and.callThrough(); - - await runMigration(); - - expect(testModule.promptForMigrationStrategy).toHaveBeenCalledTimes(0); - }); - it('should support function call with default parameter value', async() => { writeFile('/index.ts', ` import {Component, ${queryType}} from '@angular/core';