From 5c69b2160c82db9e3965b6d3db7db0170264bf87 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 3 Feb 2024 23:14:10 +0800 Subject: [PATCH] fix(cli): cannot add Carousel component without enabled typescript (#318) --- packages/cli/src/commands/add.ts | 40 +++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index 4abeaa74..279ae085 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -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' @@ -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) }