Skip to content

Commit

Permalink
Merge pull request #9 from wethegit/feature/cli-add-command
Browse files Browse the repository at this point in the history
feature(cli): `add` command
  • Loading branch information
marlonmarcello authored Oct 19, 2023
2 parents 0bcc28c + 1a178bc commit 7a5c310
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 58 deletions.
11 changes: 11 additions & 0 deletions packages/wethegit-components-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @wethegit/components-cli

## 2.2.0

### Minor Changes

- Adds the add command to the CLI

### Patch Changes

- Updated dependencies
- @wethegit/components@2.1.1

## 2.1.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/wethegit-components-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/components-cli",
"version": "2.1.0",
"version": "2.2.0",
"type": "module",
"exports": "./dist/index.js",
"bin": "./dist/index.js",
Expand Down Expand Up @@ -40,6 +40,6 @@
"resolve-package-path": "^4.0.3"
},
"peerDependencies": {
"@wethegit/components": "2.1.0"
"@wethegit/components": "2.1.1"
}
}
56 changes: 43 additions & 13 deletions packages/wethegit-components-cli/src/commands/add/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,68 @@
import chalk from "chalk";
import appRootPath from "app-root-path";
import resolvePackagePath from "resolve-package-path";
import ora from "ora";

import {
handleError,
logger,
buildAndParseConfig,
ensureCwd,
} from "../../utils";
import { logger, buildAndParseConfig, ensureCwd } from "../../utils";

import { copyComponentsByName, promptForComponents } from "./utils";

interface Options {
root: string;
}

export async function add(options: Options) {
try {
console.log("add");
console.log(appRootPath.toString());
console.log(
resolvePackagePath("@wethegit/components", appRootPath.toString())
const componentsPackageRoot = resolvePackagePath(
"@wethegit/components",
appRootPath.toString()
);
console.log(options);

if (!componentsPackageRoot) {
logger.error(
"Failed to find @wethegit/components package. It should be installed as a dependency automatically, something went wrong with the CLI installation."
);
process.exit(1);
}

const { root } = options;

// different than init if cwd doesnt exist this will throw, user should run init first to ensure deps and other requirements are met
const cwd = await ensureCwd(root);

const config = await buildAndParseConfig(cwd);
// get our config
const { componentsRootDir } = await buildAndParseConfig(cwd);

// ask what components to install
const { selectedComponentNames, proceed } = await promptForComponents();
if (!proceed) process.exit(1);

// copy components
const spinner = ora("Copying components...").start();

const copyPromises = copyComponentsByName(
componentsPackageRoot,
componentsRootDir,
selectedComponentNames
);

try {
await copyPromises;
} catch (e) {
await spinner.fail("Error copying components");
logger.error("");
logger.error(e);
process.exit(1);
}

await spinner.succeed();

logger.info("");
logger.info(`${chalk.green("Success!")} All done!`);
logger.info("");
} catch (error) {
handleError(error);
logger.error("");
logger.error(error);
process.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { resolve } from "node:path";
import fse from "fs-extra";
import ora from "ora";
import chalk from "chalk";

import COMPONENTS_INDEX from "../../../component-index";
import { logger } from "../../../utils";

export function copyComponentsByName(
componentsPackageRoot: string,
componentsRootDir: string,
selectedComponentNames: string[]
) {
const componentsPackageSrcRoot = resolve(componentsPackageRoot, "../src");
const allFilesPromise = [];
for (let componentName of selectedComponentNames) {
const componentSpinner = ora(
`Copying ${chalk.cyan(componentName)}...`
).start();

const { name } = COMPONENTS_INDEX[componentName];

const src = resolve(componentsPackageSrcRoot, name);
const dest = resolve(componentsRootDir, name);

allFilesPromise.push(
fse
.copy(src, dest)
.then(() => componentSpinner.succeed())
.catch((e) => {
logger.error(``);
logger.error(e);

return componentSpinner.fail(
`Error copying ${chalk.cyan(componentName)}`
);
})
);
}

return Promise.all(allFilesPromise);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./copyComponentsByName";
export * from "./promptForComponents";
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import prompts from "prompts";
import chalk from "chalk";

import COMPONENTS_INDEX from "../../../component-index";

export function promptForComponents() {
return prompts([
{
type: "multiselect",
name: "selectedComponentNames",
message: "What components would you like to add?",
choices: Object.values(COMPONENTS_INDEX).map(({ name }) => ({
title: name,
value: name,
})),
},
{
type: "confirm",
name: "proceed",
message: `This operation will ${chalk.yellow(
"overwrite"
)} any existing files, are you sure you want to proceed?`,
},
]);
}
11 changes: 4 additions & 7 deletions packages/wethegit-components-cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import chalk from "chalk";

import {
buildAndParseConfig,
logger,
handleError,
ensureCwd,
} from "../../utils";
import { buildAndParseConfig, logger, ensureCwd } from "../../utils";

interface Options {
root: string;
Expand Down Expand Up @@ -40,6 +35,8 @@ export async function init({ root, skip }: Options) {
logger.info(`${chalk.green("Success!")} Project initialization completed.`);
logger.info("");
} catch (error) {
handleError(error);
logger.error("");
logger.error(error);
process.exit(0);
}
}
Empty file.
6 changes: 5 additions & 1 deletion packages/wethegit-components-cli/src/component-index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export default {
import type { ComponentsIndex } from "./index.d";

const COMPONENTS_INDEX: ComponentsIndex = {
button: {
name: "button",
},
};

export default COMPONENTS_INDEX;
13 changes: 12 additions & 1 deletion packages/wethegit-components-cli/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
export type ComponentDependency = string;

export interface ComponentConfig {
/** Should be the same name as the component directory */
name: string;
dependencies: string[];
dependencies?: ComponentDependency[];
/** A glob pattern to match files to copy from the component to the project
* @default "*"
*/
files?: string;
}

export type ComponentsIndex = {
[key: string]: ComponentConfig;
};

export interface Config {
typescript: boolean;
componentsRootDir: string;
Expand Down
28 changes: 0 additions & 28 deletions packages/wethegit-components-cli/src/utils/handleError.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/wethegit-components-cli/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./consts";
export * from "./ensureConfigPaths";
export * from "./ensureCwd";
export * from "./getPackageManager";
export * from "./handleError";
export * from "./installDependencies";
export * from "./logger";
export * from "./promptForConfig";
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export async function promptForConfig(
{
type: "text",
name: "componentsRootDir",
message: (prev) =>
`What is the ${highlight(
"components"
)} root directory relative to ${prev}?`,
message: `What is your ${highlight("components")} root directory?`,
initial: DEFAULT_CONFIG.componentsRootDir,
},
],
Expand Down
6 changes: 6 additions & 0 deletions packages/wethegit-components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @wethegit/components

## 2.1.1

### Patch Changes

- Adds the add command to the CLI

## 2.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/wethegit-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/components",
"version": "2.1.0",
"version": "2.1.1",
"main": "./src/index.ts",
"sideEffects": false,
"license": "MIT",
Expand Down

0 comments on commit 7a5c310

Please sign in to comment.