Currently there are two available migration strategies for the `static-query` schematic. Both have benefits and negatives which depend on what the developer prefers. Since we can't decide which migration strategy is the best for a given project, the developer should be able to select a specific strategy through a simple choice prompt. In order to be able to use prompts in a migration schematic, we need to take advantage of the "inquirer" package which is also used by the CLI schematic prompts (schematic prompts are usually only statically defined in the schema). Additionally the schematic needs to be made "async" because with prompts the schematic can no longer execute synchronously without implementing some logic that blocks the execution. PR Close #29876
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
/**
|
|
* @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
|
|
*/
|
|
|
|
type Inquirer = typeof import('inquirer');
|
|
|
|
let resolvedInquirerModule: Inquirer|null;
|
|
|
|
try {
|
|
// "inquirer" is the prompt module also used by the devkit schematics CLI
|
|
// in order to show prompts for schematics. We transitively depend on this
|
|
// module, but don't want to throw an exception if the module is not
|
|
// installed for some reason. In that case prompts are just not supported.
|
|
resolvedInquirerModule = require('inquirer');
|
|
} catch (e) {
|
|
resolvedInquirerModule = null;
|
|
}
|
|
|
|
/** Whether prompts are currently supported. */
|
|
export function supportsPrompt(): boolean {
|
|
return !!resolvedInquirerModule && !!process.stdin.isTTY;
|
|
}
|
|
|
|
/**
|
|
* Gets the resolved instance of "inquirer" which can be used to programmatically
|
|
* create prompts.
|
|
*/
|
|
export function getInquirer(): Inquirer {
|
|
return resolvedInquirerModule !;
|
|
}
|