import fs from 'fs' ; import { resolve , join } from 'path' ; import { logger , progress , FlexPluginError , singleLineString } from 'flex-dev-utils' ; import { copyTemplateDir , checkPluginConfigurationExists } from 'flex-dev-utils/dist/fs' ; import { dirSync as tmpDirSync , DirResult as TmpDirResult } from 'tmp' ; import { setupConfiguration , installDependencies , downloadFromGitHub } from './commands' ; import { CLIArguments } from './cli' ; import finalMessage from '../prints/finalMessage' ; import validate from '../utils/validators' ; const templatesRootDir = resolve ( __dirname , '../../templates' ) ; const templateCorePath = resolve ( templatesRootDir , 'core' ) ; const templateJsPath = resolve ( templatesRootDir , 'js' ) ; const templateTsPath = resolve ( templatesRootDir , 'ts' ) ; export interface FlexPluginArguments extends CLIArguments { name : string ; targetDirectory : string ; flexSdkVersion : string ; pluginScriptsVersion : string ; pluginClassName : string ; pluginNamespace : string ; } param config { FlexPluginArguments } private export const _install = async ( config ) : Promise < boolean > => { return progress ( 'Installing dependencies' , async ( ) => { await installDependencies ( config ) ; return true ; } ) ; } ; param config { FlexPluginArguments } private export const _scaffold = async ( config ) : Promise < boolean > => { let dirObject ; const promise = progress ( 'Creating project directory' , async ( ) => { await copyTemplateDir ( templateCorePath , config . targetDirectory , config ) ; let srcPath = templateJsPath ; if ( config . typescript ) { srcPath = templateTsPath ; } if ( config . template ) { dirObject = tmpDirSync ( ) ; await downloadFromGitHub ( config . template , dirObject . name ) ; srcPath = dirObject . name ; } await copyTemplateDir ( srcPath , config . targetDirectory , config ) ; if ( ! dirObject ) { const ext = config . typescript ? 'tsx' : 'js' ; fs . renameSync ( join ( config . targetDirectory , `src/DemoPlugin.${ ext }` ) , join ( config . targetDirectory , `src/${ config . pluginClassName }.${ ext }` ) ) ; } return true ; } ) ; const cleanUp = ( ) => { if ( dirObject ) { dirObject . removeCallback ( ) ; } } ; promise . then ( cleanUp ) . catch ( cleanUp ) ; return promise ; } ; /**
 * Creates a Flex Plugin from the  FlexPluginArguments param config { FlexPluginArguments } export const createFlexPlugin = async ( config ) : Promise < void > => { config = await validate ( config ) ; config = setupConfiguration ( config ) ; if ( fs . existsSync ( config . targetDirectory ) ) { throw new FlexPluginError ( singleLineString ( `Path ${ logger . coloredStrings . link ( config . targetDirectory ) } already exists;` , 'please remove it and try again.' ) ) ; } if ( ! ( await _scaffold ( config ) ) ) { throw new FlexPluginError ( 'Failed to scaffold project' ) ; } await checkPluginConfigurationExists ( config . name , config . targetDirectory ) ; if ( config . install && ! ( await _install ( config ) ) ) { logger . error ( 'Failed to install dependencies. Please run `npm install` manually.' ) ; config . install = false ; } finalMessage ( config ) ; } ; export default createFlexPlugin ;