From 3b61aec4555bc3ccdf49d92c6cfb43ae717e3f88 Mon Sep 17 00:00:00 2001 From: dinwwwh Date: Tue, 14 May 2024 15:15:06 +0700 Subject: [PATCH] fix: cli and manual install methods (#26) * 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 --- .changeset/great-ducks-add.md | 5 +++ .changeset/wise-foxes-speak.md | 5 +++ .../src/components/installation-guide.astro | 31 ++++++------------- apps/web/tsconfig.json | 6 +++- packages/cli/src/code-parser/index.ts | 3 ++ packages/cli/src/code-parser/parser.ts | 19 ++++++++++++ packages/cli/src/commands/add.ts | 11 +++++-- packages/cli/src/utils/components.ts | 6 ---- 8 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 .changeset/great-ducks-add.md create mode 100644 .changeset/wise-foxes-speak.md create mode 100644 packages/cli/src/code-parser/index.ts create mode 100644 packages/cli/src/code-parser/parser.ts diff --git a/.changeset/great-ducks-add.md b/.changeset/great-ducks-add.md new file mode 100644 index 0000000..7601a11 --- /dev/null +++ b/.changeset/great-ducks-add.md @@ -0,0 +1,5 @@ +--- +"@dinui/cli": patch +--- + +chore: improve user code in cli and manual install methods diff --git a/.changeset/wise-foxes-speak.md b/.changeset/wise-foxes-speak.md new file mode 100644 index 0000000..20f5a7c --- /dev/null +++ b/.changeset/wise-foxes-speak.md @@ -0,0 +1,5 @@ +--- +"@dinui/cli": patch +--- + +fix: relative import when install by cli and manual diff --git a/apps/web/src/components/installation-guide.astro b/apps/web/src/components/installation-guide.astro index 7f9cb59..4ea2406 100644 --- a/apps/web/src/components/installation-guide.astro +++ b/apps/web/src/components/installation-guide.astro @@ -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( '../../../../packages/react-ui/src/**/*.tsx', @@ -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() ---
@@ -75,28 +74,16 @@ const componentDependencies = [

Run the following command in your project

- + - + - + - + @@ -147,7 +134,7 @@ const componentDependencies = [
  • Copy and paste the following code into your project

    - +
  • diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index c5d33d3..68c19f6 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -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", @@ -16,6 +17,9 @@ "references": [ { "path": "../../packages/react-ui/tsconfig.json" + }, + { + "path": "../../packages/cli/tsconfig.json" } ] } diff --git a/packages/cli/src/code-parser/index.ts b/packages/cli/src/code-parser/index.ts new file mode 100644 index 0000000..35a0d13 --- /dev/null +++ b/packages/cli/src/code-parser/index.ts @@ -0,0 +1,3 @@ +import { CodeParser } from './parser' + +export default CodeParser diff --git a/packages/cli/src/code-parser/parser.ts b/packages/cli/src/code-parser/parser.ts new file mode 100644 index 0000000..57f06c0 --- /dev/null +++ b/packages/cli/src/code-parser/parser.ts @@ -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/") + }) + } +} diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index c8b4e6a..244ae03 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -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() @@ -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 { diff --git a/packages/cli/src/utils/components.ts b/packages/cli/src/utils/components.ts index 1b7d7cc..6abedc5 100644 --- a/packages/cli/src/utils/components.ts +++ b/packages/cli/src/utils/components.ts @@ -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') + '/' }