import path from 'path' ; import { isTsProject , getPackageManager , getPackageManagerText , } from '@modern-js/generator-utils' ; import { GeneratorContext , GeneratorCore } from '@modern-js/codesmith' ; import { AppAPI } from '@modern-js/codesmith-api-app' ; import { JsonAPI } from '@modern-js/codesmith-api-json' ; import { DependenceGenerator , i18n as commonI18n , Language , } from '@modern-js/generator-common' ; import { localeKeys , i18n } from './locale' ; const getGeneratorPath = ( generator , distTag ) => { if ( process . env . CODESMITH_ENV === 'development' ) { return path . dirname ( require . resolve ( generator ) ) ; } else if ( distTag ) { return `${ generator }@${ distTag }` ; } return generator ; } ; const handleTemplateFile = async ( context , generator , appApi ) => { const jsonAPI = new JsonAPI ( generator ) ; await jsonAPI . update ( context . materials . default . get ( 'package.json' ) , { query : { } , update : { $set : { 'scripts.test' : 'modern test' } } , } ) ; const appDir = context . materials . default . basePath ; const language = isTsProject ( appDir ) ? Language . TS : Language . JS ; await appApi . forgeTemplate ( 'templates/**/*' , undefined , resourceKey => resourceKey . replace ( 'templates/' , '' ) . replace ( 'language' , language as string ) . replace ( '.handlebars' , '' ) ) ; await appApi . runSubGenerator ( getGeneratorPath ( DependenceGenerator , context . config . distTag ) , undefined , { ... context . config , devDependencies : { ... ( context . config . devDependencies || { } ) , '@types/jest' : '^26.0.9' , } , isSubGenerator : true , } ) ; const packageManager = context . config . packageManager || ( await getPackageManager ( appDir ) ) ; return { packageManager , } ; } ; export default async ( context , generator ) => { const appApi = new AppAPI ( context , generator ) ; const { locale } = context . config ; i18n . changeLanguage ( { locale } ) ; commonI18n . changeLanguage ( { locale } ) ; appApi . i18n . changeLanguage ( { locale } ) ; if ( ! ( await appApi . checkEnvironment ( ) ) ) { process . exit ( 1 ) ; } generator . logger . debug ( `start run @modern-js/test-generator` ) ; generator . logger . debug ( `context=${ JSON . stringify ( context ) }` ) ; generator . logger . debug ( `context.data=${ JSON . stringify ( context . data ) }` ) ; const { packageManager } = await handleTemplateFile ( context , generator , appApi ) ; await appApi . runInstall ( ) ; appApi . showSuccessInfo ( i18n . t ( localeKeys . success , { packageManager : getPackageManagerText ( packageManager ) , } ) ) ; generator . logger . debug ( `forge @modern-js/test-generator succeed ` ) ; } ;