import * as gulp from 'gulp' ; const runSequence = require ( 'run-sequence' ) ; var exec = require ( 'child_process' ) . exec ; import { DIST_COMPONENTS_ROOT } from '../constants' ; import * as path from 'path' ; const replace = require ( 'gulp-string-replace' ) ; const glob = require ( 'glob' ) ; const fs = require ( 'fs' ) ; gulp . task ( ':dist:checknpmuser' , ( cb ) => { exec ( 'npm whoami' , ( error , stdout , stderr ) => { if ( error ) { cb ( error ) ; } const username = stdout . toString ( ) . replace ( /^\s+|\s+$/g , '' ) ; if ( username !== 'angular-mdl' ) { cb ( 'not loged in as npm user angular-mdl' ) ; } console . log ( `npm user is ${ username }` ) ; cb ( ) ; } ) ; } ) ; gulp . task ( ':dist:removemoduleid' , ( ) => { return gulp . src ( path . join ( DIST_COMPONENTS_ROOT , '**/*.js' ) ) . pipe ( replace ( /\s*moduleId:\s*module\.id\s*,?\s*/gm , '' , { logs : { enabled : false } } ) ) . pipe ( gulp . dest ( DIST_COMPONENTS_ROOT ) ) ; } ) ; gulp . task ( ':dist:inlinehtml' , ( done ) => { const files = glob . sync ( path . join ( DIST_COMPONENTS_ROOT , '**/*.js' ) , { } ) ; files . forEach ( ( file ) => { const content = fs . readFileSync ( file , 'utf-8' ) ; const result = content . replace ( /templateUrl:\s*'([^']+?\.html)'/g , ( fullMatch , templateUrl ) => { const templateFile = path . join ( path . dirname ( file ) , templateUrl ) ; const templateContent = fs . readFileSync ( templateFile , 'utf-8' ) ; const shortenedTemplate = templateContent . replace ( /([\n\r]\s*)+/gm , ' ' ) . replace ( /"/g , '\\"' ) ; return `template: "${ shortenedTemplate }"` ; } ) ; fs . writeFileSync ( file , result , 'utf-8' ) ; } ) ; done ( ) ; } ) ; gulp . task ( ':dist:inlinecss' , ( done ) => { const files = glob . sync ( path . join ( DIST_COMPONENTS_ROOT , '**/*.js' ) , { } ) ; files . forEach ( ( file ) => { const content = fs . readFileSync ( file , 'utf-8' ) ; const result = content . replace ( /styleUrls:\s*(\[[\s\S]*?\])/gm , ( fullMatch , styleUrls ) => { const urls = eval ( styleUrls ) ; return 'styles: [' + urls . map ( ( styleUrl ) => { const styleFile = path . join ( path . dirname ( file ) , styleUrl ) ; const styleContent = fs . readFileSync ( styleFile , 'utf-8' ) ; const shortenedStyle = styleContent . replace ( /([\n\r]\s*)+/gm , ' ' ) . replace ( /"/g , '\\"' ) ; return `"${ shortenedStyle }"` ; } ) . join ( ',\n' ) + ']' ; } ) ; fs . writeFileSync ( file , result , 'utf-8' ) ; } ) ; done ( ) ; } ) ; /**
 * publish the package (determinde by the current working dir)
 */ function publishPackage ( ) { var pJson = JSON . parse ( fs . readFileSync ( 'package.json' , 'utf8' ) ) ; console . log ( `publish ${ pJson . name }:${ pJson . version }` ) ; return new Promise < any > ( ( resolve , reject ) => { exec ( 'npm publish --access public' , ( error , stdout , stderr ) => { if ( error ) { console . log ( `error publishing ${ pJson . name }` , error ) ; reject ( error ) ; } resolve ( ) ; } ) ; } ) ; } param componentDir returns { Promise < any > } function getPublishedVersion ( componentDir ) { return new Promise < any > ( ( resolve , reject ) => { var pJson = JSON . parse ( fs . readFileSync ( path . join ( componentDir , 'package.json' ) , 'utf8' ) ) ; exec ( `npm show ${ pJson . name } version` , ( error , stdout , stderr ) => { if ( error ) { reject ( error ) ; } const publishedVersion = stdout . toString ( ) . replace ( /^\s+|\s+$/g , '' ) ; resolve ( publishedVersion ) ; } ) ; } ) ; } function publishComponent ( dir ) { const fullPath = path . join ( DIST_COMPONENTS_ROOT , dir ) ; return Promise . resolve ( ) . then ( ( ) => { process . chdir ( fullPath ) ; return Promise . resolve ( ) ; } ) . then ( ( ) => { return getPublishedVersion ( fullPath ) ; } ) . then ( ( publishedVersion ) => { console . log ( `published version of ${ dir } is: ${ publishedVersion }` ) ; var pJson = JSON . parse ( fs . readFileSync ( path . join ( fullPath , 'package.json' ) , 'utf8' ) ) ; console . log ( `${ dir }: version to publish is: ${ pJson . version }` ) ; return new Promise ( ( resolve , reject ) => { if ( publishedVersion != pJson . version ) { publishPackage ( ) . then ( ( ) => { resolve ( ) ; } ) ; } else { console . log ( 'not published' ) ; resolve ( ) ; } } ) ; } ) ; } gulp . task ( ':dist:publish' , ( ) => { const currentDir = process . cwd ( ) ; const possiblyComponents = fs . readdirSync ( DIST_COMPONENTS_ROOT ) ; const components = possiblyComponents . filter ( ( fileOrDir ) => { const fullPath = path . join ( DIST_COMPONENTS_ROOT , fileOrDir ) ; const stat = fs . statSync ( fullPath ) ; return stat . isDirectory ( ) && fs . existsSync ( path . join ( fullPath , 'package.json' ) ) ; } ) ; let p = components . reduce ( function ( p , item ) { return p . then ( ( ) => { return publishComponent ( item ) ; } ) ; } , Promise . resolve ( ) ) ; return p . then ( ( ) => { console . log ( `set path to ${ currentDir }` ) ; process . chdir ( currentDir ) ; return Promise . resolve ( ) ; } ) ; } ) ; gulp . task ( 'dist:build' , ( done ) => { runSequence ( 'clean' , [ ':build:components:assets' , ':build:components:scss' , ':build:components:ts' , ':build:components:ngc' ] , ':dist:removemoduleid' , ':dist:inlinehtml' , ':dist:inlinecss' , ':build:components:umd' , done ) ; } ) ; gulp . task ( 'dist' , function ( done ) { runSequence ( ':dist:checknpmuser' , 'dist:build' , ':dist:publish' , done ) ; } ) ;