diff --git a/packages/cli/src/utils/transformers/index.ts b/packages/cli/src/utils/transformers/index.ts index a26534e4..2609ebb2 100644 --- a/packages/cli/src/utils/transformers/index.ts +++ b/packages/cli/src/utils/transformers/index.ts @@ -1,14 +1,11 @@ -import { promises as fs } from 'node:fs' -import { tmpdir } from 'node:os' -import path from 'pathe' -import { Project, ScriptKind, type SourceFile } from 'ts-morph' import type * as z from 'zod' +import { transform as metaTransform } from 'vue-metamorph' +import { transformTwPrefix } from './transform-tw-prefix' import type { Config } from '@/src/utils/get-config' import type { registryBaseColorSchema } from '@/src/utils/registry/schema' import { transformCssVars } from '@/src/utils/transformers/transform-css-vars' import { transformImport } from '@/src/utils/transformers/transform-import' import { transformSFC } from '@/src/utils/transformers/transform-sfc' -import { transformTwPrefixes } from '@/src/utils/transformers/transform-tw-prefix' export interface TransformOpts { filename: string @@ -17,38 +14,12 @@ export interface TransformOpts { baseColor?: z.infer } -export type Transformer = ( - opts: TransformOpts & { - sourceFile: SourceFile - } -) => Promise - -const transformers: Transformer[] = [ - transformCssVars, - transformImport, - transformTwPrefixes, -] - -const project = new Project({ - compilerOptions: {}, -}) - -async function createTempSourceFile(filename: string) { - const dir = await fs.mkdtemp(path.join(tmpdir(), 'shadcn-')) - return path.join(dir, filename) -} - export async function transform(opts: TransformOpts) { - const tempFile = await createTempSourceFile(opts.filename) - const sourceFile = project.createSourceFile(tempFile, opts.raw, { - scriptKind: ScriptKind.Unknown, - }) + const source = await transformSFC(opts) - for (const transformer of transformers) - transformer({ sourceFile, ...opts }) - - return await transformSFC({ - sourceFile, - ...opts, - }) + return metaTransform(source, opts.filename, [ + transformImport(opts), + transformCssVars(opts), + transformTwPrefix(opts), + ]).code } diff --git a/packages/cli/src/utils/transformers/new.ts b/packages/cli/src/utils/transformers/new.ts deleted file mode 100644 index fa18d664..00000000 --- a/packages/cli/src/utils/transformers/new.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { transform as metaTransform } from 'vue-metamorph' -import type { AST, CodemodPlugin } from 'vue-metamorph' -import type * as z from 'zod' -import { splitClassName } from './transform-css-vars' -import type { Config } from '@/src/utils/get-config' -import type { registryBaseColorSchema } from '@/src/utils/registry/schema' - -export interface TransformOpts { - filename: string - raw: string - config: Config - baseColor?: z.infer -} - -function transformTwPrefix(config: Config): CodemodPlugin { - return { - type: 'codemod', - name: 'change string literals to hello, world', - - // eslint-disable-next-line unused-imports/no-unused-vars - transform({ scriptASTs, sfcAST, styleASTs, filename, utils: { traverseScriptAST, traverseTemplateAST } }) { - // codemod plugins self-report the number of transforms it made - // this is only used to print the stats in CLI output - let transformCount = 0 - - // scriptASTs is an array of Program ASTs - // in a js/ts file, this array will only have one item - // in a vue file, this array will have one item for each