import * as updateNotifier from 'update-notifier' ; import * as yargs from 'yargs' ; import * as semver from 'semver' ; import { initGlobalState } from '../utils/global-state' ; import { TransferedRegisterCommand } from '../utils/define' ; import { logFatal } from '../utils/log' ; export async function createCli ( opts ? ) { await checkEnvironment ( ) ; let preSelectPackage = yargs . argv . package as string ; if ( yargs . argv . v === true || yargs . argv . h === true ) { preSelectPackage = 'root' ; } await initGlobalState ( preSelectPackage ) ; const { globalState } = await import ( '../utils/global-state' ) ; const { transferCommandsArrayToMap } = await import ( '../utils/commands' ) ; const { loadPlugins , plugin } = await import ( '../utils/plugins' ) ; const { cleanUncessaryFields } = await import ( './clean-unecessary-files' ) ; if ( ! globalState . sourceConfig . type && yargs . argv . _ [ 0 ] !== 'init' ) { logFatal ( 'You should run "npx pri init" first' ) ; } await cleanUncessaryFields ( ) ; await loadPlugins ( opts && opts . pluginIncludeRoots ) ; const commandRegisterMap = transferCommandsArrayToMap ( plugin . commands ) ; registerYargs ( yargs , commandRegisterMap ) ; yargs . alias ( 'help' , 'h' ) . alias ( 'version' , 'v' ) . argv ; /**
     * Update notify.
     */ updateNotifier ( { pkg : globalState . priPackageJson as updateNotifier . Package , } ) . notify ( ) ; } function registerYargs ( leafYargs , transferedRegisterCommands ) { transferedRegisterCommands . forEach ( commandRegister => { leafYargs . command ( { command : commandRegister . name [ 0 ] , describe : commandRegister . description || '' , aliases : commandRegister . alias , builder : childYargs => { childYargs . option ( 'package' , { description : 'Select package' , demandOption : false , } ) ; if ( commandRegister . options ) { Object . keys ( commandRegister . options ) . forEach ( optionName => { const optionInfo = commandRegister . options [ optionName ] ; childYargs . option ( optionName , { alias : optionInfo . alias , description : optionInfo . description , demandOption : optionInfo . required , } ) ; } ) ; } registerYargs ( childYargs , commandRegister . childs ) ; return childYargs ; } , handler : async ( argv ) => { if ( commandRegister . actions ) { for ( const action of commandRegister . actions ) { await action ( argv ) ; } } } , } ) ; } ) ; } async function checkEnvironment ( ) { if ( semver . lte ( process . version , '8.0.0' ) ) { logFatal ( `Nodejs version should be greater than 8, current is ${ process . version }` ) ; } }