import commandLineArgs , { OptionDefinition } from "command-line-args" ; import fse from "fs-extra" ; import path from "path" ; import { compile } from "../compile" ; import { parseOptions } from "../compilerOptions" ; type CliGenerateComponentsArguments = { config ? : string ; force ? : boolean ; help ? : boolean ; verbose ? : boolean ; } ; function printUsage ( ) { const usage = `Usage:

    component-importer generate [--config] [--force] [--help]

Where:
    [config]      : (optional) Path to the "importer.config.json" file generated by the "component-importer init" command.
                    Default: "importer.config.json".
    [force]       : (optional) Re-writes component files, even if they already exist.
                    Default: false.
    [help]        : (optional) Prints this message.
    [verbose]     : (optional) Increase verbosity of printed out messages.
                    Default: false.

Example:

    component-importer generate --config path/to/importer.config.json --force

` ; console . log ( usage ) ; } function generateFile ( file , source , force ) { if ( ! fse . existsSync ( file ) ) { console . log ( "✅ Generating ....................." , file ) ; fse . ensureDirSync ( path . dirname ( file ) ) ; fse . writeFileSync ( file , source ) ; } if ( fse . existsSync ( file ) && force ) { console . log ( "⚠️  Rewriting file (--force)........" , file ) ; fse . ensureDirSync ( path . dirname ( file ) ) ; fse . writeFileSync ( file , source ) ; } else { console . log ( "⚠️  Skipping existing file ........." , file ) ; } } export async function generateComponents ( args ) { if ( args . help || ! args . config ) { printUsage ( ) ; return ; } const config = parseOptions ( args . config ) ; const outFiles = await compile ( { ... config , verbose : args . verbose } ) ; for ( const outFile of outFiles ) { if ( outFile . type === "component" ) { generateFile ( outFile . emitPath , outFile . outputSource , ! ! args . force ) ; } else if ( outFile . type === "hoc" ) { const resultingFilePath = path . join ( config . out , outFile . fileName ) ; generateFile ( resultingFilePath , outFile . outputSource , ! ! args . force ) ; } else { console . warn ( [ `🐛 Unrecognized EmitResult type ${ outFile . type }. ` , `This likely means that that a new EmitResult ` , `type was introduced but hasn't been correctly configured. ` , ] . join ( "" ) ) ; } } } export async function commandGenerateComponents ( argv ) { const argumentDefinitions = [ { name : "config" , type : String , defaultValue : "importer.config.json" } , { name : "force" , type : Boolean , defaultValue : false } , { name : "help" , type : Boolean , defaultValue : false } , { name : "verbose" , type : Boolean , defaultValue : false } , ] ; const args = commandLineArgs ( argumentDefinitions , { argv } ) as CliGenerateComponentsArguments ; generateComponents ( args ) ; }