import { externalSchematic , Tree , SchematicContext , Rule , } from "@angular-devkit/schematics" ; import * as fs from "fs" ; import { Observable } from "rxjs" ; import { tap , map } from "rxjs/operators" ; import { getJSON , setJSON , readFile , writeFile , readFileFromFS , } from "../utils/files" ; import { observify , isFunction } from "../utils/helpers" ; import { Schema } from "./schema" ; function resolveTSConfig ( ) { const tsConfigPath = fs . existsSync ( "tsconfig.base.json" ) ? "tsconfig.base.json" : "tsconfig.json" ; let sourceText = readFileFromFS ( tsConfigPath ) ; sourceText = sourceText . replace ( /\/\*.+?\*\/|\/\/.*(?=[\n\r])/g , "" ) ; return { path : tsConfigPath , json : JSON . parse ( sourceText ) } ; } function toTree ( ruleOrTree , tree , context ) { return isFunction ( ruleOrTree ) ? ruleOrTree ( tree , context ) : ( ruleOrTree as any ) ; } function removeLibFiles ( tree ) { tree . actions . filter ( ( action ) => ! ! action . path . match ( /\.component|\.service/ ) ) . forEach ( ( action ) => tree . delete ( action . path ) ) ; return tree ; } function updateTsConfig ( host , name , libPath ) { const tsConfig = resolveTSConfig ( ) ; const paths = { [ name ] : [ `${ libPath }/src/public-api.ts` ] , } ; tsConfig . json . compilerOptions . paths = { ... tsConfig . json . compilerOptions . paths , ... paths , } ; return setJSON ( host , tsConfig . path , tsConfig . json ) ; } function updateKarmaConfig ( host , libPath ) { const karmaPath = `${ libPath }/karma.conf.js` ; if ( host . exists ( karmaPath ) ) { let karma = readFile ( host , karmaPath ) ; karma = karma . replace ( /browsers:.*/ , `browsers: [process.env.CI ? 'ChromeHeadless' : 'Chrome'],` ) ; karma = karma . replace ( /singleRun:.*/ , `singleRun: process.env.CI,` ) ; writeFile ( karmaPath , karma , host ) ; } return host ; } function packageJSONExtensions ( options , scopeWithName ) { const repoUrl = options . repositoryUrl || `https://github.com/${ options . name }` ; const libDistPath = scopeWithName . replace ( "@" , "" ) ; const depthFromRootLib = "../" + scopeWithName . split ( "/" ) . map ( ( ) => ".." ) . join ( "/" ) ; const basicPkgJson = { version : "1.0.0" , keywords : [ "angular" , "angular 2" , options . name ] , license : "MIT" , publishConfig : { access : "public" , } , bugs : { url : `${ repoUrl }/issue` , } , homepage : `${ repoUrl }#readme` , repository : { type : "git" , url : `${ repoUrl }` , } , } ; if ( options . skipSchematics ) { return basicPkgJson ; } return { ... basicPkgJson , schematics : "./schematics/collection.json" , scripts : { build : "tsc -p tsconfig.schematics.json" , "copy:schemas" : `cpx schematics/ng-add ${ depthFromRootLib }/dist/${ libDistPath }/` , "copy:collection" : `cpx schematics/collection.json ${ depthFromRootLib }/dist/${ libDistPath }/schematics/` , postbuild : "npm run copy:schemas && npm run copy:collection" , } , } ; } function updatePackageJSON ( options , libPath , tree , scopeWithName ) { const packageJSONPath = `${ libPath }/package.json` ; if ( ! tree . exists ( packageJSONPath ) ) { return tree ; } const pkg = getJSON ( tree , packageJSONPath ) as Record < string , any > ; return setJSON ( tree , packageJSONPath , { ... pkg , ... packageJSONExtensions ( options , scopeWithName ) , } ) ; } export function createLib ( options , scopeWithName , libPath , tree , context , isNx ) { const libRule = externalSchematic ( isNx ? "@nrwl/angular" : "@schematics/angular" , "library" , { name : scopeWithName , } ) ; const libTree$ = observify < Tree > ( toTree ( libRule ( tree , context ) , tree , context ) ) ; return libTree$ . pipe ( tap ( ( tree ) => updatePackageJSON ( options , libPath , tree , scopeWithName ) ) , map ( removeLibFiles ) , map ( ( libTree ) => updateTsConfig ( libTree , scopeWithName , libPath ) ) , map ( ( libTree ) => updateKarmaConfig ( libTree , libPath ) ) ) ; }