Skip to content

Commit

Permalink
fix: cli and manual install methods (#26)
Browse files Browse the repository at this point in the history
* remove redundant option on cli install

* Unified code parser for components in installation guide & cli

* remove "import type * as" when cli & manual install

* handle relative import

* changeset
  • Loading branch information
unnoq authored May 14, 2024
1 parent 97904a4 commit 3b61aec
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-ducks-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dinui/cli": patch
---

chore: improve user code in cli and manual install methods
5 changes: 5 additions & 0 deletions .changeset/wise-foxes-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dinui/cli": patch
---

fix: relative import when install by cli and manual
31 changes: 9 additions & 22 deletions apps/web/src/components/installation-guide.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { Tabs, TabItem, Code, Steps } from '@astrojs/starlight/components'
import CodeParser from '@dinui/cli/code-parser'
const globComponentCodeImports = import.meta.glob<string>(
'../../../../packages/react-ui/src/**/*.tsx',
Expand All @@ -25,11 +26,9 @@ const componentCode = await (async () => {
return await loadCode()
})()
const componentDependencies = [
...componentCode.matchAll(/from '((@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*))/g),
]
.map((m) => m[1] as string)
.filter((d) => !['react'].includes(d))
const componentCodeParser = new CodeParser(componentCode)
const componentDependencies = componentCodeParser.getDependencies()
const componentUserCode = componentCodeParser.getUserCode()
---

<div class="pt-4">
Expand Down Expand Up @@ -75,28 +74,16 @@ const componentDependencies = [
<p>Run the following command in your project</p>
<Tabs>
<TabItem label="npm">
<Code
lang="bash"
code={`npx @dinui/cli@latest add -d ./components/ui ${componentName}`}
/>
<Code lang="bash" code={`npx @dinui/cli@latest add ${componentName}`} />
</TabItem>
<TabItem label="yarn">
<Code
lang="bash"
code={`yarn dlx @dinui/cli@latest add -d ./components/ui ${componentName}`}
/>
<Code lang="bash" code={`yarn dlx @dinui/cli@latest add ${componentName}`} />
</TabItem>
<TabItem label="pnpm">
<Code
lang="bash"
code={`pnpm dlx @dinui/cli@latest add -d ./components/ui ${componentName}`}
/>
<Code lang="bash" code={`pnpm dlx @dinui/cli@latest add ${componentName}`} />
</TabItem>
<TabItem label="bun">
<Code
lang="bash"
code={`bunx @dinui/cli@latest add -d ./components/ui ${componentName}`}
/>
<Code lang="bash" code={`bunx @dinui/cli@latest add ${componentName}`} />
</TabItem>
</Tabs>
</li>
Expand Down Expand Up @@ -147,7 +134,7 @@ const componentDependencies = [

<li>
<p>Copy and paste the following code into your project</p>
<Code code={componentCode} lang="tsx" />
<Code code={componentUserCode} lang="tsx" />
</li>

<slot />
Expand Down
6 changes: 5 additions & 1 deletion apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"paths": {
"@web/*": ["./*"],
"@dinui/react/utils": ["../../../packages/react-ui/src/utils"],
"@dinui/react/*": ["../../../packages/react-ui/src/ui/*"]
"@dinui/react/*": ["../../../packages/react-ui/src/ui/*"],
"@dinui/cli/*": ["../../../packages/cli/src/*"]
},
"jsx": "react-jsx",
"jsxImportSource": "react",
Expand All @@ -16,6 +17,9 @@
"references": [
{
"path": "../../packages/react-ui/tsconfig.json"
},
{
"path": "../../packages/cli/tsconfig.json"
}
]
}
3 changes: 3 additions & 0 deletions packages/cli/src/code-parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CodeParser } from './parser'

export default CodeParser
19 changes: 19 additions & 0 deletions packages/cli/src/code-parser/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export class CodeParser {
constructor(public code: string) {}

getDependencies() {
return [
...this.code.matchAll(/from '((@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*))/g),
]
.map((m) => m[1] as string)
.filter((d) => !['react'].includes(d))
}

getUserCode() {
return this.code
.replaceAll(/import type \* as _.*\n/g, '')
.replaceAll(/import .* from '(.)\/.*\n/g, (line) => {
return line.replace("'./", "'@dinui/react/")
})
}
}
11 changes: 8 additions & 3 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { env } from '../env'
import { getPersistedOption, setPersistedOption } from '../utils/options'
import chalk from 'chalk'
import CodeParser from 'code-parser'
import { Command, program } from 'commander'
import fs from 'fs-extra'
import ora from 'ora'
import path from 'path'
import detachPreferredPM from 'preferred-pm'
import prompts from 'prompts'
import { getComponentDependencies, loadComponentCode, loadComponents } from 'utils/components'
import { loadComponentCode, loadComponents } from 'utils/components'
import { $ } from 'zx'

export const add = new Command()
Expand Down Expand Up @@ -118,12 +119,16 @@ export const add = new Command()
if (!answers.override) continue
}

const componentCodeParser = new CodeParser(componentCode)
spinner.start(`Adding "${component}" component...`)
await fs.ensureDir(componentDir)
await fs.writeFile(path.resolve(cwd, directory, `${component}.tsx`), componentCode)
await fs.writeFile(
path.resolve(cwd, directory, `${component}.tsx`),
componentCodeParser.getUserCode(),
)

spinner.text = `Installing dependencies for "${component}" component...`
const componentDependencies = getComponentDependencies({ code: componentCode })
const componentDependencies = componentCodeParser.getDependencies()

if (componentDependencies.length) {
try {
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/src/utils/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ export async function loadComponentCode(opts: { name: string; cwd: string }) {
return await fs.readFile(fullPath, { encoding: 'utf8' })
}

export function getComponentDependencies(opts: { code: string }) {
return [...opts.code.matchAll(/from '((@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*))/g)]
.map((m) => m[1] as string)
.filter((d) => !['react'].includes(d))
}

export function getBaseComponentPath(opts: { cwd: string }) {
return path.resolve(opts.cwd, './node_modules/@dinui/react/src') + '/'
}

0 comments on commit 3b61aec

Please sign in to comment.