import type { ChildProcess } from 'child_process' ; import { spawn } from 'child_process' ; import execa from 'execa' ; import { join } from 'path' ; import kill from 'tree-kill' ; export const FIXTURES_DIR = join ( __dirname , '../fixtures/' ) ; export const LONG_TIMEOUT_MS = 600000 ; let localRegistryProcess ; const VERSION = `9999.0.1-local-integration-tests` ; const cwd = process . cwd ( ) ; process . env . npm_config_registry = `http://localhost:4872/` ; process . env . YARN_REGISTRY = process . env . npm_config_registry ; export async function spawnLocalRegistry ( ) { localRegistryProcess = spawn ( 'npx' , [ 'verdaccio' , '--config' , './local-registry/config.yml' , '--listen' , '4872' , ] ) ; const collectedOutput = [ ] ; let resolvedOrRejected = false ; setTimeout ( ( ) => { if ( ! resolvedOrRejected ) { console . error ( `Failed to start the npm registry` ) ; console . error ( collectedOutput . join ( '' ) ) ; cleanUp ( 1 ) ; } } , 10000 ) ; await new Promise ( ( res , rej ) => { localRegistryProcess . stdout ! . pipe ( process . stdout ) ; localRegistryProcess . stdout ! . on ( 'data' , ( data ) => { collectedOutput . push ( data . toString ( ) ) ; if ( data . includes ( 'http address' ) ) { resolvedOrRejected = true ; res ( undefined ) ; } } ) ; localRegistryProcess . stderr ! . pipe ( process . stderr ) ; localRegistryProcess . on ( 'error' , ( err ) => { console . error ( collectedOutput . join ( '' ) ) ; resolvedOrRejected = true ; rej ( err ) ; } ) ; } ) ; } export function cleanUp ( code ) { process . chdir ( cwd ) ; try { if ( ! process . env . CI ) { kill ( 0 ) ; } } catch { } try { if ( localRegistryProcess ) localRegistryProcess . kill ( ) ; } catch { } try { if ( ! process . env . CI ) { kill ( 0 , 'SIGKILL' ) ; } } catch { } try { if ( localRegistryProcess ) localRegistryProcess . kill ( 'SIGKILL' ) ; } catch { } process . exit ( code ) ; } export async function publishPackagesToVerdaccio ( ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } const subprocess = execa ( './publish-to-verdaccio.sh' , [ VERSION ] ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; } export async function runNpmInstall ( ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } const subprocess = execa ( 'npm' , [ 'install' ] ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; } export async function runYarnInstall ( ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } const subprocess = execa ( 'yarn' , [ 'install' ] ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; } export async function runNgAdd ( ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } const subprocess = execa ( 'npx' , [ 'ng' , 'add' , `@angular-eslint/schematics@${ VERSION }` , ] ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; } export async function runNgNew ( workspaceName ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } console . log ( process . cwd ( ) ) ; const subprocess = execa ( '../../../node_modules/.bin/ng' , [ 'new' , `--strict=true` , `--package-manager=npm` , `--interactive=false` , workspaceName , ] ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; } export async function runNgGenerate ( args ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } const subprocess = execa ( 'npx' , [ 'ng' , 'generate' , ... args ] ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; } export async function runConvertTSLintToESLint ( additionalArgs ? ) { if ( process . env . npm_config_registry ?. indexOf ( 'http://localhost' ) === - 1 ) { throw Error ( `
      ------------------
      💣 ERROR 💣 => $NPM_REGISTRY does not look like a local registry'
      ------------------
    ` ) ; } let args = [ 'ng' , 'g' , `@angular-eslint/schematics:convert-tslint-to-eslint` ] ; if ( additionalArgs ) { args = [ ... args , ... additionalArgs ] ; } const subprocess = execa ( 'npx' , args ) ; subprocess . stdout ! . pipe ( process . stdout ) ; subprocess . stderr ! . pipe ( process . stderr ) ; return await subprocess ; }