import { appendFileSync } from 'fs' ; import { mkdirpSync } from 'fs-extra' ; import { join } from 'path' ; import { buildConfig } from './build-config' ; import { BuildPackage } from './build-package' ; import { copyFiles } from './copy-files' ; import { createEntryPointPackageJson } from './entry-point-package-json' ; import { inlinePackageMetadataFiles } from './metadata-inlining' ; import { createMetadataReexportFile } from './metadata-reexport' ; import { insertPackageJsonVersionStamp } from './package-version-stamp' ; import { createTypingsReexportFile } from './typings-reexport' ; import { replaceVersionPlaceholders } from './version-placeholders' ; const { packagesDir , outputDir , projectDir } = buildConfig ; /** Directory where all bundles will be created in. */ const bundlesDir = join ( outputDir , 'bundles' ) ; /**
 * Copies different output files into a folder structure that follows the `angular/angular`
 * release folder structure. The output will also contain a README and the according package.json
 * file. Additionally the package will be Closure Compiler and AOT compatible.
 */ export function composeRelease ( buildPackage ) { const { name , sourceDir } = buildPackage ; const packageOut = buildPackage . outputDir ; const releasePath = join ( outputDir , 'releases' , name ) ; const importAsName = `@angular/${ name }` ; inlinePackageMetadataFiles ( packageOut ) ; copyFiles ( packageOut , '**/*.+(d.ts|metadata.json)' , join ( releasePath , 'typings' ) ) ; copyFiles ( bundlesDir , `${ name }?(-*).umd?(.min).js?(.map)` , join ( releasePath , 'bundles' ) ) ; copyFiles ( bundlesDir , `${ name }.es5.js?(.map)` , join ( releasePath , 'esm5' ) ) ; copyFiles ( join ( bundlesDir , name ) , `*.es5.js?(.map)` , join ( releasePath , 'esm5' ) ) ; copyFiles ( bundlesDir , `${ name }.js?(.map)` , join ( releasePath , 'esm2015' ) ) ; copyFiles ( join ( bundlesDir , name ) , `!(*.es5|*.umd).js?(.map)` , join ( releasePath , 'esm2015' ) ) ; copyFiles ( projectDir , 'LICENSE' , releasePath ) ; copyFiles ( packagesDir , 'README.md' , releasePath ) ; copyFiles ( sourceDir , 'package.json' , releasePath ) ; if ( buildPackage . hasSchematics ) { copyFiles ( join ( packageOut , 'schematics' ) , '**/*' , join ( releasePath , 'schematics' ) ) ; } replaceVersionPlaceholders ( releasePath ) ; insertPackageJsonVersionStamp ( join ( releasePath , 'package.json' ) ) ; createTypingsReexportFile ( releasePath , './typings/index' , name ) ; createMetadataReexportFile ( releasePath , './typings/index' , name , importAsName ) ; if ( buildPackage . secondaryEntryPoints . length ) { createFilesForSecondaryEntryPoint ( buildPackage , releasePath ) ; } if ( buildPackage . copySecondaryEntryPointStylesToRoot ) { copySecondaryEntryPointStylesheets ( buildPackage , releasePath ) ; } if ( buildPackage . exportsSecondaryEntryPointsAtRoot ) { const es2015Exports = buildPackage . secondaryEntryPoints . map ( p => `export * from './${ p }';` ) . join ( '\n' ) ; appendFileSync ( join ( releasePath , `${ name }.d.ts` ) , es2015Exports , 'utf-8' ) ; createMetadataReexportFile ( releasePath , buildPackage . secondaryEntryPoints . concat ( [ 'typings/index' ] ) . map ( p => `./${ p }` ) , name , importAsName ) ; } } /** Creates files necessary for a secondary entry-point. */ function createFilesForSecondaryEntryPoint ( buildPackage , releasePath ) { const { name } = buildPackage ; const packageOut = buildPackage . outputDir ; buildPackage . secondaryEntryPoints . forEach ( entryPointName => { const entryPointDir = join ( releasePath , entryPointName ) ; const importAsName = `@angular/${ name }/${ entryPointName }` ; mkdirpSync ( entryPointDir ) ; createEntryPointPackageJson ( entryPointDir , name , entryPointName ) ; copyFiles ( join ( packageOut , entryPointName ) , '**/*.+(d.ts|metadata.json)' , join ( entryPointDir , 'typings' ) ) ; createTypingsReexportFile ( entryPointDir , `./typings/index` , 'index' ) ; createMetadataReexportFile ( entryPointDir , `./typings/index` , 'index' , importAsName ) ; createTypingsReexportFile ( releasePath , `./${ entryPointName }/index` , entryPointName ) ; createMetadataReexportFile ( releasePath , `./${ entryPointName }/index` , entryPointName , importAsName ) ; } ) ; } /** Copies the stylesheets for secondary entry-points that generate one to the release output. */ function copySecondaryEntryPointStylesheets ( buildPackage , releasePath ) { buildPackage . secondaryEntryPoints . forEach ( entryPointName => { const entryPointDir = join ( buildPackage . outputDir , entryPointName ) ; copyFiles ( entryPointDir , `_${ entryPointName }.scss` , releasePath ) ; copyFiles ( entryPointDir , `${ entryPointName }-prebuilt.css` , releasePath ) ; } ) ; }