Skip to content

Commit

Permalink
feat(shadcn-ui): Added add executor to add new components
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Jan 18, 2024
1 parent 387ac65 commit eac038e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
9 changes: 9 additions & 0 deletions e2e/shadcn-ui-e2e/tests/shadcn-ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
// })

})
10 changes: 10 additions & 0 deletions packages/shadcn-ui/executors.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
26 changes: 26 additions & 0 deletions packages/shadcn-ui/src/executors/add/add.impl.ts
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions packages/shadcn-ui/src/executors/add/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { convertNxExecutor } from '@nx/devkit'

import { addExecutor } from './add.impl'

export default convertNxExecutor(addExecutor)
22 changes: 22 additions & 0 deletions packages/shadcn-ui/src/executors/add/schema.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cn'
14 changes: 12 additions & 2 deletions packages/shadcn-ui/src/generators/init/init.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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`)

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit eac038e

Please sign in to comment.