import * as fs from 'fs' ; import * as path from 'path' ; import * as yeoman from 'yeoman-environment' ; import { nls } from '../i18n' ; export class CreateUtil { public static checkInputs ( flagValue ) { const alphaRegExp = /^\w+$/ ; if ( ! alphaRegExp . test ( flagValue ) ) { throw new Error ( nls . localize ( 'AlphaNumericNameError' ) ) ; } const letterStartRegExp = /^[A-Za-z]/ ; if ( ! letterStartRegExp . test ( flagValue ) ) { throw new Error ( nls . localize ( 'NameMustStartWithLetterError' ) ) ; } const endUnderscore = /_$/ ; if ( endUnderscore . test ( flagValue ) ) { throw new Error ( nls . localize ( 'EndWithUnderscoreError' ) ) ; } const dblUnderscore = /__/ ; if ( dblUnderscore . test ( flagValue ) ) { throw new Error ( nls . localize ( 'DoubleUnderscoreError' ) ) ; } return '' ; } public static getCommandTemplatesForFiletype ( filetype , command ) { const files = fs . readdirSync ( path . resolve ( __dirname , '..' , 'templates' , command ) ) . filter ( file => filetype . test ( file ) ) . map ( file => { return file . split ( '.' , 1 ) . toString ( ) ; } ) ; return files ; } param command param filetype param subdir return public static getCommandTemplatesInSubdirs ( command , { filetype , subdir } = { } ) { let basedir = path . resolve ( __dirname , '..' , 'templates' , command ) ; if ( subdir ) { basedir = path . join ( basedir , subdir ) ; } const subdirs = fs . readdirSync ( basedir , { withFileTypes : true } ) . filter ( ent => ent . isDirectory ( ) ) . map ( ent => ent . name ) ; if ( filetype ) { return subdirs . filter ( dir => fs . readdirSync ( path . join ( basedir , dir ) , { withFileTypes : true } ) . some ( ent => ent . isFile ( ) && filetype . test ( ent . name ) ) ) ; } return subdirs ; } }