import { v4 as uuidv4 } from "uuid" ; import path from "path" ; import fs from "fs" ; import { exec } from "child-process-promise" ; import { LinterConfigs } from "parser/linter-config-parser/types" ; import { ClinterModeInfo , ClinterSettings , FormatterInfo , FrontFrameworkInfo , TestFrameworkInfo , TypescriptInfo , } from "types" ; export interface TestService { loadInputConfig : ( config : ClinterSettings ) => void ; loadInitialLinterConfig : ( config : string ) => void ; loadInitialParsedLinterConfig : ( config : LinterConfigs ) => void ; installPackages : ( packages : string [ ] ) => Promise < void > ; runClinter : ( ) => Promise < void > ; getParsedOutputConfig : ( ) => LinterConfigs ; getOutputConfig : ( ) => string ; getInstalledPackages : ( ) => string [ ] ; loadInitialTSConfig : ( config : Record < string , any > ) => void ; } export function makeTestService ( outputFileName = ".eslintrc.json" ) { const id = uuidv4 ( ) ; const dirPath = `/tmp/${ id }/` ; const inputConfigPath = path . join ( dirPath , "inputConfig.json" ) ; const tsconfigPath = path . join ( dirPath , "tsconfig.json" ) ; const packagePath = path . join ( dirPath , "package.json" ) ; const outputConfigPath = path . join ( dirPath , outputFileName ) ; fs . mkdirSync ( dirPath ) ; const loadInputConfig = ( config ) => { fs . writeFileSync ( inputConfigPath , JSON . stringify ( config , null , 2 ) ) ; } ; const loadInitialLinterConfig = ( config ) => { fs . writeFileSync ( outputConfigPath , config ) ; } ; const loadInitialParsedLinterConfig = ( config ) => { fs . writeFileSync ( outputConfigPath , JSON . stringify ( config , null , 2 ) ) ; } ; const loadInitialTSConfig = ( config ) => { fs . writeFileSync ( tsconfigPath , JSON . stringify ( config , null , 2 ) ) ; } ; const installPackages = ( packages ) => { return Promise . resolve ( ) ; } ; const runClinter = ( ) => { return exec ( `node ${ path . join ( process . cwd ( ) , "build/index.js" ) } --path ${ dirPath } --inputFile ${ inputConfigPath }` ) . then ( ( ) => Promise . resolve ( ) ) . catch ( ( error ) => { throw new Error ( error ) ; } ) ; } ; const getOutputConfig = ( ) : string => { return fs . readFileSync ( outputConfigPath , "utf-8" ) ; } ; const getParsedOutputConfig = ( ) : LinterConfigs => { return JSON . parse ( fs . readFileSync ( outputConfigPath , "utf-8" ) ) as LinterConfigs ; } ; const getInstalledPackages = ( ) => { return [ ] ; } ; return { getInstalledPackages , getParsedOutputConfig , getOutputConfig , installPackages , loadInitialLinterConfig , loadInitialParsedLinterConfig , loadInputConfig , runClinter , loadInitialTSConfig , } ; } export const defaultInputConfig = { modeConfig : { mode : ClinterModeInfo . Generator , } , generatorConfig : { typescript : TypescriptInfo . None , env : [ ] , formatter : FormatterInfo . None , frontFramework : FrontFrameworkInfo . None , test : TestFrameworkInfo . None , } , migrationModeConfig : { migration : false , } , dependenciesConfig : { upgradeDependencies : false , } , } ;