import { apply , applyTemplates , HostTree , mergeWith , move , Tree , url } from '@angular-devkit/schematics' ; import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ; import * as fs from 'fs' ; import * as path from 'path' ; export class TestWorkspaceFactory { private hostTree = new HostTree ( ) ; private tree : UnitTestTree ; constructor ( public runner ) { } async create ( options ? ) { this . tree = await this . runner . runExternalSchematicAsync ( '@schematics/angular' , 'workspace' , { name : 'test-workspace' , version : '9.0.0' , newProjectRoot : 'projects' , ... options } , this . hostTree ) . toPromise ( ) ; return this . tree ; } async addApplication ( options ) { this . tree = await this . runner . runExternalSchematicAsync ( '@schematics/angular' , 'application' , options , this . tree ) . toPromise ( ) ; return this . tree ; } async addLibrary ( options ) { this . tree = await this . runner . runExternalSchematicAsync ( '@schematics/angular' , 'library' , options , this . tree ) . toPromise ( ) ; return this . tree ; } addNewFile ( templatePath , content ) { this . hostTree . create ( templatePath , content ) ; return this . tree ; } description param from param to copyFolder ( from , to , overwrite = false ) { if ( ! fs . existsSync ( from ) ) { throw new Error ( `${ from } folder not found` ) ; } const list = fs . readdirSync ( from ) ; const toAddList = [ { parentFromPath : from , parentToPath : to , children : list } ] ; while ( toAddList . length ) { const item = toAddList . pop ( ) ; for ( let i = 0 ; i < item . children . length ; i ++ ) { const element = item . children [ i ] ; const absolutePath = path . resolve ( item . parentFromPath , element ) ; const stat = fs . statSync ( absolutePath ) ; if ( stat . isDirectory ( ) ) { toAddList . push ( { parentFromPath : absolutePath , parentToPath : path . join ( item . parentToPath , element ) , children : fs . readdirSync ( absolutePath ) } ) ; } else { if ( this . hostTree . exists ( path . join ( item . parentToPath , element ) ) ) { if ( overwrite ) { this . hostTree . overwrite ( path . join ( item . parentToPath , element ) , fs . readFileSync ( absolutePath ) ) ; } continue ; } this . hostTree . create ( path . join ( item . parentToPath , element ) , fs . readFileSync ( absolutePath ) ) ; } } } return this . tree ; } getTree ( ) { return this . tree ; } } export function createTestWorkspaceFactory ( runner ) { return new TestWorkspaceFactory ( runner ) ; }