Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a282315

Browse files
committed
fix(cli): remote installation path incorrect
1 parent dce05ac commit a282315

File tree

5 files changed

+216
-17
lines changed

5 files changed

+216
-17
lines changed

packages/cli/src/utils/transformers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface TransformOpts {
1414
raw: string
1515
config: Config
1616
baseColor?: z.infer<typeof registryBaseColorSchema>
17+
isRemote?: boolean
1718
}
1819

1920
export async function transform(opts: TransformOpts) {

packages/cli/src/utils/transformers/transform-import.ts

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,25 @@ export function transformImport(opts: TransformOpts): CodemodPlugin {
88

99
transform({ scriptASTs, utils: { traverseScriptAST } }) {
1010
const transformCount = 0
11-
const { config } = opts
11+
const { config, isRemote } = opts
12+
const utilsImport = '@/lib/utils'
1213

1314
for (const scriptAST of scriptASTs) {
1415
traverseScriptAST(scriptAST, {
1516
visitImportDeclaration(path) {
1617
if (typeof path.node.source.value === 'string') {
1718
const sourcePath = path.node.source.value
1819

19-
// Replace @/registry/[style] with the components alias.
20-
if (sourcePath.startsWith('@/registry/')) {
21-
if (config.aliases.ui) {
22-
path.node.source.value = sourcePath.replace(/^@\/registry\/[^/]+\/ui/, config.aliases.ui)
23-
}
24-
else {
25-
path.node.source.value = sourcePath.replace(/^@\/registry\/[^/]+/, config.aliases.components)
26-
}
27-
}
20+
// Alias to `moduleSpecifier`
21+
const updatedImport = updateImportAliases(sourcePath, config, isRemote)
22+
path.node.source.value = updatedImport
2823

2924
// Replace `import { cn } from "@/lib/utils"`
30-
if (sourcePath === '@/lib/utils') {
25+
if (updatedImport === utilsImport) {
3126
const namedImports = path.node.specifiers?.map(node => node.local?.name ?? '') ?? []
32-
// const namedImports = importDeclaration.getNamedImports()
3327
const cnImport = namedImports.find(i => i === 'cn')
3428
if (cnImport) {
35-
path.node.source.value = sourcePath.replace(/^@\/lib\/utils/, config.aliases.utils)
29+
path.node.source.value = updatedImport === utilsImport ? sourcePath.replace(utilsImport, config.aliases.utils) : config.aliases.utils
3630
}
3731
}
3832
}
@@ -45,3 +39,65 @@ export function transformImport(opts: TransformOpts): CodemodPlugin {
4539
},
4640
}
4741
}
42+
43+
function updateImportAliases(
44+
moduleSpecifier: string,
45+
config: TransformOpts['config'],
46+
isRemote: boolean = false,
47+
) {
48+
// Not a local import.
49+
if (!moduleSpecifier.startsWith('@/') && !isRemote) {
50+
return moduleSpecifier
51+
}
52+
53+
// This treats the remote as coming from a faux registry.
54+
if (isRemote && moduleSpecifier.startsWith('@/')) {
55+
moduleSpecifier = moduleSpecifier.replace(/^@\//, `@/registry/new-york/`)
56+
}
57+
58+
// Not a registry import.
59+
if (!moduleSpecifier.startsWith('@/registry/')) {
60+
// We fix the alias and return.
61+
const alias = config.aliases.components.split('/')[0]
62+
return moduleSpecifier.replace(/^@\//, `${alias}/`)
63+
}
64+
65+
if (moduleSpecifier.match(/^@\/registry\/(.+)\/ui/)) {
66+
return moduleSpecifier.replace(
67+
/^@\/registry\/(.+)\/ui/,
68+
config.aliases.ui ?? `${config.aliases.components}/ui`,
69+
)
70+
}
71+
72+
if (
73+
config.aliases.components
74+
&& moduleSpecifier.match(/^@\/registry\/(.+)\/components/)
75+
) {
76+
return moduleSpecifier.replace(
77+
/^@\/registry\/(.+)\/components/,
78+
config.aliases.components,
79+
)
80+
}
81+
82+
if (config.aliases.lib && moduleSpecifier.match(/^@\/registry\/(.+)\/lib/)) {
83+
return moduleSpecifier.replace(
84+
/^@\/registry\/(.+)\/lib/,
85+
config.aliases.lib,
86+
)
87+
}
88+
89+
if (
90+
config.aliases.composables
91+
&& moduleSpecifier.match(/^@\/registry\/(.+)\/composables/)
92+
) {
93+
return moduleSpecifier.replace(
94+
/^@\/registry\/(.+)\/composables/,
95+
config.aliases.composables,
96+
)
97+
}
98+
99+
return moduleSpecifier.replace(
100+
/^@\/registry\/[^/]+/,
101+
config.aliases.components,
102+
)
103+
}

packages/cli/src/utils/updaters/update-files.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export async function updateFiles(
3636
overwrite?: boolean
3737
force?: boolean
3838
silent?: boolean
39+
rootSpinner?: ReturnType<typeof spinner>
40+
isRemote?: boolean
3941
},
4042
) {
4143
if (!files?.length) {
@@ -45,6 +47,7 @@ export async function updateFiles(
4547
overwrite: false,
4648
force: false,
4749
silent: false,
50+
isRemote: false,
4851
...options,
4952
}
5053
const filesCreatedSpinner = spinner(`Updating files.`, {
@@ -168,6 +171,7 @@ export async function updateFiles(
168171
raw: file.content,
169172
config,
170173
baseColor,
174+
isRemote: options.isRemote,
171175
})
172176

173177
await fs.writeFile(filePath, content, 'utf-8')

packages/cli/test/utils/__snapshots__/transform-import.test.ts.snap

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { Button } from '~/src/components/ui/button'
1616
import { Label} from 'ui/label'
1717
import { Box } from '~/src/components/box'
1818
19-
import { cn, foo } from '~/lib'
20-
import { bar } from '@/lib/utils/bar'
19+
import { cn, foo } from '~/lib/utils'
20+
import { bar } from '~/lib/utils/bar'
2121
"
2222
`;
2323

@@ -27,7 +27,51 @@ import { Button } from '~/src/components/ui/button'
2727
import { Label} from 'ui/label'
2828
import { Box } from '~/src/components/box'
2929
30-
import { cn } from '~/src/utils'
31-
import { bar } from '@/lib/utils/bar'
30+
import { cn } from '~/lib/utils'
31+
import { bar } from '~/lib/utils/bar'
32+
"
33+
`;
34+
35+
exports[`transform import 4`] = `
36+
"import { Foo } from 'bar'
37+
import { Button } from '~/src/components/button'
38+
import { Label} from 'ui/label'
39+
import { Box } from '~/src/components/box'
40+
41+
import { cn } from '~/lib/utils'
42+
import { bar } from '~/lib/utils/bar'
43+
"
44+
`;
45+
46+
exports[`transform import 5`] = `
47+
"import { Foo } from 'bar'
48+
import { Button } from '~/src/ui/button'
49+
import { Label} from 'ui/label'
50+
import { Box } from '~/src/components/box'
51+
52+
import { cn } from '~/lib/utils'
53+
import { bar } from '~/lib/utils/bar'
54+
"
55+
`;
56+
57+
exports[`transform import 6`] = `
58+
"import { Foo } from 'bar'
59+
import { Button } from '@custom-alias/components/ui/button'
60+
import { Label} from 'ui/label'
61+
import { Box } from '@custom-alias/components/box'
62+
63+
import { cn } from '@custom-alias/lib/utils'
64+
"
65+
`;
66+
67+
exports[`transform import 7`] = `
68+
"import { Foo } from 'bar'
69+
import { Button } from '@custom-alias/components/ui/button'
70+
import { Label} from 'ui/label'
71+
import { Box } from '@custom-alias/components/box'
72+
import Layout from '@custom-alias/components/layout/Layout.vue'
73+
74+
75+
import { cn } from '@custom-alias/lib/utils'
3276
"
3377
`;

packages/cli/test/utils/transform-import.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,98 @@ it('transform import', async () => {
6767
},
6868
}),
6969
).toMatchSnapshot()
70+
71+
expect(
72+
await transform({
73+
filename: 'app.ts',
74+
raw: `import { Foo } from "bar"
75+
import { Button } from "@/registry/new-york/ui/button"
76+
import { Label} from "ui/label"
77+
import { Box } from "@/registry/new-york/box"
78+
79+
import { cn } from "@/lib/utils"
80+
import { bar } from "@/lib/utils/bar"
81+
`,
82+
config: {
83+
aliases: {
84+
components: '~/src/components',
85+
utils: '~/src/utils',
86+
ui: '~/src/components',
87+
},
88+
typescript: true,
89+
},
90+
}),
91+
).toMatchSnapshot()
92+
93+
expect(
94+
await transform({
95+
filename: 'app.ts',
96+
raw: `import { Foo } from "bar"
97+
import { Button } from "@/registry/new-york/ui/button"
98+
import { Label} from "ui/label"
99+
import { Box } from "@/registry/new-york/box"
100+
101+
import { cn } from "@/lib/utils"
102+
import { bar } from "@/lib/utils/bar"
103+
`,
104+
config: {
105+
aliases: {
106+
components: '~/src/components',
107+
utils: '~/src/utils',
108+
ui: '~/src/ui',
109+
},
110+
typescript: true,
111+
},
112+
}),
113+
).toMatchSnapshot()
114+
115+
expect(
116+
await transform({
117+
filename: 'app.ts',
118+
raw: `import { Foo } from "bar"
119+
import { Button } from "@/components/ui/button"
120+
import { Label} from "ui/label"
121+
import { Box } from "@/registry/new-york/box"
122+
123+
import { cn } from "@/lib/utils"
124+
`,
125+
config: {
126+
tailwind: {
127+
baseColor: 'neutral',
128+
cssVariables: true,
129+
},
130+
aliases: {
131+
components: '@custom-alias/components',
132+
utils: '@custom-alias/lib/utils',
133+
},
134+
typescript: true,
135+
},
136+
}),
137+
).toMatchSnapshot()
138+
139+
expect(
140+
await transform({
141+
filename: 'app.ts',
142+
raw: `import { Foo } from "bar"
143+
import { Button } from "@/components/ui/button"
144+
import { Label} from "ui/label"
145+
import { Box } from "@/registry/new-york/box"
146+
import Layout from "@/registry/blocks/layout/Layout.vue"
147+
148+
149+
import { cn } from "@/lib/utils"
150+
`,
151+
config: {
152+
tailwind: {
153+
baseColor: 'neutral',
154+
cssVariables: true,
155+
},
156+
aliases: {
157+
components: '@custom-alias/components',
158+
utils: '@custom-alias/lib/utils',
159+
},
160+
typescript: true,
161+
},
162+
}),
163+
).toMatchSnapshot()
70164
})

0 commit comments

Comments
 (0)