diff --git a/USAGE.md b/USAGE.md index 4d40b05..6d35b9e 100644 --- a/USAGE.md +++ b/USAGE.md @@ -136,6 +136,8 @@ Commands: ``` +#### Listing components + The `emulsify component list|ls` will return a list of components available within the system you selected for your project. Example usage: @@ -169,6 +171,8 @@ pages -> landing-pages ``` +#### Installing components + Once you've found a component you want to install, you can use the `emulsify component install` command to fetch it into your project. Example usage: @@ -179,8 +183,12 @@ emulsify component install card > Success! The card component has been added to your project. ``` +#### Force installing components + If you attempt to install a component that already exists within your project, you the installation process will throw an error. However, if you wish to overwrite the existing component, pass the `--force` flag. +Example usage: + ```bash emulsify component install card > Error: The component "card" already exists, and force was not passed (--force). @@ -189,4 +197,16 @@ emulsify component install card --force > Success! The card component has been added to your project. ``` +#### Installing all available components + +If you would like to simply install all components available withing the system you've selected, use the `--all` flag. + +Example usage: + +```bash +emulsify component install --all + > Success! The 01-colors component has been added to your project. + > ... +``` + That's pretty much it. Have fun, and please feel free to open issues if you discover a bug, or have an improvement to suggest! diff --git a/src/handlers/componentInstall.ts b/src/handlers/componentInstall.ts index 276c632..fbc5ee3 100644 --- a/src/handlers/componentInstall.ts +++ b/src/handlers/componentInstall.ts @@ -11,13 +11,14 @@ import getEmulsifyConfig from '../util/project/getEmulsifyConfig'; import getJsonFromCachedFile from '../util/cache/getJsonFromCachedFile'; import installComponentFromCache from '../util/project/installComponentFromCache'; import cloneIntoCache from '../util/cache/cloneIntoCache'; +import catchLater from '../util/catchLater'; /** * Handler for the `component install` command. */ export default async function componentInstall( name: string, - { force }: InstallComponentHandlerOptions + { force, all }: InstallComponentHandlerOptions ): Promise { const emulsifyConfig = await getEmulsifyConfig(); if (!emulsifyConfig) { @@ -66,9 +67,6 @@ export default async function componentInstall( ); // If no systemConf is present, error with a helpful message. - // @TODO: We definitely need a mechanism to pull in the system, cache it, and make sure it's - // checked out version is correct. This should likely be done with a HOF that can be applied - // to any command handler: R.compose(withCachedSystem, withEmulsifySystemRequirement)(componentInstall). if (!systemConf) { return log( 'error', @@ -90,13 +88,50 @@ export default async function componentInstall( ); } - try { - await installComponentFromCache(systemConf, variantConf, name, force); + if (!name && !all) { return log( - 'success', - `Success! The ${name} component has been added to your project.` + 'error', + 'Please specify a component to install, or pass --all to install all available components.' ); - } catch (e) { - return log('error', (e as Error).toString()); + } + + // If all components are to be installed, spawn promises for installing all available components. + const components: [string, Promise][] = []; + if (all) { + components.push( + ...variantConf.components.map((component): [string, Promise] => [ + component.name, + catchLater( + installComponentFromCache( + systemConf, + variantConf, + component.name, + // Force install all components. + true + ) + ), + ]) + ); + } + // If there is only one component to install, add one single promise for the single component. + else { + components.push([ + name, + catchLater( + installComponentFromCache(systemConf, variantConf, name, force) + ), + ]); + } + + for (const [cname, promise] of components) { + try { + await promise; + log( + 'success', + `Success! The ${cname} component has been added to your project.` + ); + } catch (e) { + log('error', `Unable to install ${cname}: ${(e as Error).toString()}`); + } } } diff --git a/src/index.ts b/src/index.ts index 6e16d21..6199edd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,6 +83,10 @@ component '-f --force', 'Use this to overwrite a component that is already installed' ) + .option( + '-a --all', + 'Use this to install all available components, rather than specifying a single component to install' + ) .alias('i') .description( "Install a component from within the current project's system and variant" diff --git a/src/types/handlers.d.ts b/src/types/handlers.d.ts index fc9ce34..06d0bc8 100644 --- a/src/types/handlers.d.ts +++ b/src/types/handlers.d.ts @@ -17,5 +17,6 @@ declare module '@emulsify-cli/handlers' { export type InstallComponentHandlerOptions = { force?: boolean; + all?: boolean; }; }