import * as Generator from 'yeoman-generator' ; import * as ts from 'typescript' ; import { Project , SyntaxKind } from "ts-morph" ; import { Editor } from 'mem-fs-editor' ; let path = require ( 'path' ) ; const os = require ( "os" ) ; const packagePath = "package.json" ; /**
 * Utility class for the Generator
 */ export class Yotilities { param url public static validateUrl ( url ) { return /(https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ . test ( url ) ; } param filename param options public static fixFileNames ( filename , options ) { if ( filename !== undefined ) { if ( filename [ 0 ] === "_" ) { filename = '.' + filename . substr ( 1 ) ; } var basename = path . basename ( filename ) ; if ( basename [ 0 ] === '_' ) { var filename = '.' + basename . substr ( 1 ) ; var dirname = path . dirname ( filename ) ; filename = path . join ( dirname , filename ) ; console . log ( filename ) ; } for ( var prop in options ) { if ( options . hasOwnProperty ( prop ) && typeof options [ prop ] === 'string' ) { filename = filename . replace ( new RegExp ( "{" + prop + "}" , 'g' ) , options [ prop ] ) ; } } } return filename ; } public static addAdditionalDeps ( dependencies , fs ) { var pkg = fs . readJSON ( packagePath ) ; dependencies . forEach ( dep => { ( < any > pkg . dependencies ) [ dep [ 0 ] ] = dep [ 1 ] ; } ) ; fs . writeJSON ( packagePath , pkg ) ; } public static addAdditionalDevDeps ( devDependencies , fs ) { var pkg = fs . readJSON ( packagePath ) ; devDependencies . forEach ( dep => { ( < any > pkg . devDependencies ) [ dep [ 0 ] ] = dep [ 1 ] ; } ) ; fs . writeJSON ( packagePath , pkg ) ; } public static addScript ( name , script , fs ) { var pkg = fs . readJSON ( packagePath ) ; pkg . scripts [ name ] = script ; fs . writeJSON ( packagePath , pkg ) ; } public static addNode ( name , node , fs ) { var pkg = fs . readJSON ( packagePath ) ; pkg [ name ] = node ; fs . writeJSON ( packagePath , pkg ) ; } public static addOrUpdateEnv ( fileName , key , value , fs ) { const envFile = fs . read ( fileName ) ; let added = false ; let output = envFile . split ( os . EOL ) . map ( line => { if ( line . startsWith ( key ) ) { added = true ; return `${ key }=${ value }` ; } return line ; } ) ; if ( ! added ) { output = output . concat ( `${ key }=${ value }` ) ; } fs . write ( fileName , output . join ( os . EOL ) ) ; } public static insertTsExportDeclaration ( fileName , literal , comment , fs ) { let clientTs = fs . read ( fileName ) ; const src = ts . createSourceFile ( fileName , clientTs , ts . ScriptTarget . ES5 , true , ts . ScriptKind . TS ) ; const exp = ts . createExportDeclaration ( undefined , undefined , undefined , ts . createLiteral ( literal ) ) ; if ( comment !== undefined ) { const cmt = ts . addSyntheticLeadingComment ( exp , ts . SyntaxKind . SingleLineCommentTrivia , ` ${ comment }` ) ; } const update = ts . updateSourceFileNode ( src , [ ... src . statements , exp ] ) ; const printer = ts . createPrinter ( { newLine : ts . NewLineKind . LineFeed , removeComments : false , } ) ; fs . write ( fileName , printer . printFile ( update ) ) ; } public static insertImportDeclaration ( fileName , identifier , literal , comment , fs ) { let clientTs = fs . read ( fileName ) ; const src = ts . createSourceFile ( fileName , clientTs , ts . ScriptTarget . ES5 , true , ts . ScriptKind . TS ) ; const imp = ts . createImportDeclaration ( undefined , undefined , ts . createImportClause ( ts . createIdentifier ( identifier ) , undefined ) , ts . createLiteral ( literal ) ) ; if ( comment !== undefined ) { const cmt = ts . addSyntheticLeadingComment ( imp , ts . SyntaxKind . SingleLineCommentTrivia , ` ${ comment }` ) ; } const update = ts . updateSourceFileNode ( src , [ imp , ... src . statements ] ) ; const printer = ts . createPrinter ( { newLine : ts . NewLineKind . LineFeed , removeComments : false , } ) ; fs . write ( fileName , printer . printFile ( update ) ) ; } public static getLibraryNameFromWebpackConfig ( ) { const project = new Project ( ) ; project . addSourceFilesAtPaths ( "webpack.config.js" ) ; const src = project . getSourceFileOrThrow ( "webpack.config.js" ) ; let retVal = undefined ; const config = src . getVariableDeclarationOrThrow ( "config" ) ; const arr = config . getFirstChildByKind ( SyntaxKind . ArrayLiteralExpression ) ; if ( arr ) { arr . getChildrenOfKind ( SyntaxKind . SyntaxList ) . forEach ( sl => { sl . getChildrenOfKind ( SyntaxKind . ObjectLiteralExpression ) . forEach ( lit => { const output = lit . getProperty ( "output" ) ; if ( output ) { output . getChildrenOfKind ( SyntaxKind . ObjectLiteralExpression ) . forEach ( x => { const lib = x . getProperty ( "library" ) ; if ( lib ) { const prop = lib . getFirstChildByKind ( SyntaxKind . StringLiteral ) ; if ( prop ) { retVal = prop . getLiteralValue ( ) ; } } } ) ; } } ) ; } ) ; } return retVal ; } }