Skip to content

Commit

Permalink
fix(cli): cannot add Carousel component without enabled typescript (#318
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Dunqing committed Feb 3, 2024
1 parent ed70e4e commit 5c69b21
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, promises as fs } from 'node:fs'
import { existsSync, promises as fs, rmSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import chalk from 'chalk'
Expand Down Expand Up @@ -159,28 +159,46 @@ export const add = new Command()
}
}

for (const file of item.files) {
const componentDir = path.resolve(targetDir, item.name)
let filePath = path.resolve(
const componentDir = path.resolve(targetDir, item.name)
if (!existsSync(componentDir))
await fs.mkdir(componentDir, { recursive: true })

const files = item.files.map(file => ({
...file,
path: path.resolve(
targetDir,
item.name,
file.name,
)

if (!config.typescript)
filePath = filePath.replace(/\.ts$/, '.js')

if (!existsSync(componentDir))
await fs.mkdir(componentDir, { recursive: true })
),
}))

// We need to write original files to disk if we're not using TypeScript.
// Rewrite or delete added files after transformed
if (!config.typescript) {
for (const file of files)
await fs.writeFile(file.path, file.content)
}

for (const file of files) {
// Run transformers.
const content = await transform({
filename: file.name,
filename: file.path,
raw: file.content,
config,
baseColor,
})

let filePath = file.path

if (!config.typescript) {
// remove original .ts file if we're not using TypeScript.
if (file.path.endsWith('.ts')) {
if (existsSync(file.path))
rmSync(file.path)
}
filePath = file.path.replace(/\.ts$/, '.js')
}

await fs.writeFile(filePath, content)
}

Expand Down

0 comments on commit 5c69b21

Please sign in to comment.