-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from wethegit/feature/cli-add-command
feature(cli): `add` command
- Loading branch information
Showing
15 changed files
with
154 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 43 additions & 13 deletions
56
packages/wethegit-components-cli/src/commands/add/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/wethegit-components-cli/src/commands/add/utils/copyComponentsByName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/wethegit-components-cli/src/commands/add/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./copyComponentsByName"; | ||
export * from "./promptForComponents"; |
25 changes: 25 additions & 0 deletions
25
packages/wethegit-components-cli/src/commands/add/utils/promptForComponents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?`, | ||
}, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters