Skip to content

Commit

Permalink
Merge pull request #23 from emulsify-ds/feat/install-all-compoments
Browse files Browse the repository at this point in the history
feat: add ability to pass --all to `emulsify component install`
  • Loading branch information
patrickocoffeyo authored Sep 25, 2021
2 parents 5516133 + 05bbea8 commit 30ed635
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
20 changes: 20 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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).
Expand All @@ -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!
55 changes: 45 additions & 10 deletions src/handlers/componentInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const emulsifyConfig = await getEmulsifyConfig();
if (!emulsifyConfig) {
Expand Down Expand Up @@ -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',
Expand All @@ -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<void>][] = [];
if (all) {
components.push(
...variantConf.components.map((component): [string, Promise<void>] => [
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()}`);
}
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/types/handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ declare module '@emulsify-cli/handlers' {

export type InstallComponentHandlerOptions = {
force?: boolean;
all?: boolean;
};
}

0 comments on commit 30ed635

Please sign in to comment.