From eac038e97a673493a249a6c2a5d639f54d816453 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Thu, 18 Jan 2024 15:49:29 +0100 Subject: [PATCH] feat(shadcn-ui): Added `add` executor to add new components --- e2e/shadcn-ui-e2e/tests/shadcn-ui.spec.ts | 9 +++++++ packages/shadcn-ui/executors.json | 10 +++++++ .../shadcn-ui/src/executors/add/add.impl.ts | 26 +++++++++++++++++++ .../shadcn-ui/src/executors/add/compat.ts | 5 ++++ .../shadcn-ui/src/executors/add/schema.json | 22 ++++++++++++++++ .../init/files-utils/index.ts.template | 1 + .../src/generators/init/init.impl.ts | 14 ++++++++-- 7 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 packages/shadcn-ui/src/executors/add/add.impl.ts create mode 100644 packages/shadcn-ui/src/executors/add/compat.ts create mode 100644 packages/shadcn-ui/src/executors/add/schema.json create mode 100644 packages/shadcn-ui/src/generators/init/files-utils/index.ts.template diff --git a/e2e/shadcn-ui-e2e/tests/shadcn-ui.spec.ts b/e2e/shadcn-ui-e2e/tests/shadcn-ui.spec.ts index aaefb142..bc1eb45c 100644 --- a/e2e/shadcn-ui-e2e/tests/shadcn-ui.spec.ts +++ b/e2e/shadcn-ui-e2e/tests/shadcn-ui.spec.ts @@ -19,8 +19,17 @@ describe('shadcn/ui e2e', () => { `${utilsLibName}/src/tailwind.config.js`, `${utilsLibName}/src/global.css`, `${utilsLibName}/src/cn.ts`, + `${utilsLibName}/src/index.ts`, 'components.json' )).not.toThrow() }) + // it('should be able add a button', async () => { + // await runNxCommandAsync(`add ${uiLibName} button`) + // + // expect(() => checkFilesExist( + // `${uiLibName}/src/button.tsx`, + // )).not.toThrow() + // }) + }) diff --git a/packages/shadcn-ui/executors.json b/packages/shadcn-ui/executors.json index 7f6d9dd8..09fa8459 100644 --- a/packages/shadcn-ui/executors.json +++ b/packages/shadcn-ui/executors.json @@ -1,6 +1,16 @@ { "executors": { + "add": { + "implementation": "./src/executors/add/add.impl", + "schema": "./src/executors/add/schema.json", + "description": "add executor" + } }, "builders": { + "add": { + "implementation": "./src/executors/add/add.impl", + "schema": "./src/executors/add/schema.json", + "description": "add executor" + } } } diff --git a/packages/shadcn-ui/src/executors/add/add.impl.ts b/packages/shadcn-ui/src/executors/add/add.impl.ts new file mode 100644 index 00000000..97fb1619 --- /dev/null +++ b/packages/shadcn-ui/src/executors/add/add.impl.ts @@ -0,0 +1,26 @@ +import { ExecutorContext } from '@nx/devkit' +import { buildCommand, execCommand } from '@nx-extend/core' + +export interface ExecutorSchema { + component: string + overwrite?: boolean +} + +export async function addExecutor( + options: ExecutorSchema, + context: ExecutorContext +): Promise<{ success: boolean }> { + const { root } = context.workspace.projects[context.projectName] + + return execCommand(buildCommand([ + 'npx shadcn-ui@latest add', + options.component, + options.overwrite && '--overwrite', + '--path=src', + `--cwd=${root}` + ]),{ + + }) +} + +export default addExecutor diff --git a/packages/shadcn-ui/src/executors/add/compat.ts b/packages/shadcn-ui/src/executors/add/compat.ts new file mode 100644 index 00000000..4e773afa --- /dev/null +++ b/packages/shadcn-ui/src/executors/add/compat.ts @@ -0,0 +1,5 @@ +import { convertNxExecutor } from '@nx/devkit' + +import { addExecutor } from './add.impl' + +export default convertNxExecutor(addExecutor) diff --git a/packages/shadcn-ui/src/executors/add/schema.json b/packages/shadcn-ui/src/executors/add/schema.json new file mode 100644 index 00000000..7d0f93ca --- /dev/null +++ b/packages/shadcn-ui/src/executors/add/schema.json @@ -0,0 +1,22 @@ +{ + "version": 2, + "outputCapture": "direct-nodejs", + "$schema": "http://json-schema.org/schema", + "type": "object", + "title": "add executor", + "description": "", + "properties": { + "component": { + "type": "string", + "description": "", + "$default": { + "$source": "argv", + "index": 0 + }, + "x-prompt": "What component would you like to add?" + } + }, + "required": [ + "component" + ] +} diff --git a/packages/shadcn-ui/src/generators/init/files-utils/index.ts.template b/packages/shadcn-ui/src/generators/init/files-utils/index.ts.template new file mode 100644 index 00000000..c5c1882c --- /dev/null +++ b/packages/shadcn-ui/src/generators/init/files-utils/index.ts.template @@ -0,0 +1 @@ +export * from './cn' diff --git a/packages/shadcn-ui/src/generators/init/init.impl.ts b/packages/shadcn-ui/src/generators/init/init.impl.ts index 9e840cf9..e48c518e 100644 --- a/packages/shadcn-ui/src/generators/init/init.impl.ts +++ b/packages/shadcn-ui/src/generators/init/init.impl.ts @@ -2,9 +2,9 @@ import { addDependenciesToPackageJson, generateFiles, getWorkspaceLayout, - joinPathFragments, + joinPathFragments, readProjectConfiguration, runTasksInSerial, - Tree, writeJson + Tree, updateProjectConfiguration, writeJson } from '@nx/devkit' import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils' import { addTsConfigPath, libraryGenerator } from '@nx/js' @@ -16,6 +16,7 @@ import { devDependencies } from '../../../package.json' function cleanupLib(tree: Tree, libDirectory: string) { // Remove the unneeded files + tree.delete(`${libDirectory}/package.json`) tree.delete(`${libDirectory}/src/index.ts`) const libFiles = tree.children(`${libDirectory}/src/lib`) @@ -86,6 +87,15 @@ export default async function (tree: Tree, options: ShadecnUiSchema) { } }) + updateProjectConfiguration(tree, uiLibOptions.projectName, { + ...readProjectConfiguration(tree, uiLibOptions.projectName), + targets: { + add: { + executor: '@nx-extend/shadcn-ui:add' + } + } + }) + return runTasksInSerial( addDependenciesToPackageJson( tree,