diff --git a/src/index.ts b/src/index.ts index 211ac0a..60ca632 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,6 +74,19 @@ function isEmptyDir(path: string) { return files.length === 0 || (files.length === 1 && files[0] === '.git'); } +function parseToolsOption(tools: Argv['tools']) { + if (typeof tools === 'undefined') { + return null; + } + + const toolsArr = Array.isArray(tools) ? tools : [tools]; + + return toolsArr + .flatMap((tool) => tool.split(',')) + .map((tool) => tool.trim()) + .filter(Boolean); +} + export type Argv = { help?: boolean; dir?: string; @@ -110,9 +123,10 @@ async function getTools( extraTools?: ExtraTool[], ) { // Check if tools are specified via CLI options - if (tools) { - let toolsArr = Array.isArray(tools) ? tools : [tools]; - toolsArr = toolsArr.filter( + const parsedTools = parseToolsOption(tools); + + if (parsedTools !== null) { + const toolsArr = parsedTools.filter( (tool) => BUILTIN_TOOLS.includes(tool) || extraTools?.some((extraTool) => extraTool.value === tool), @@ -123,10 +137,6 @@ async function getTools( if (dir && template) { return []; } - // skip tools selection when tools is empty string - if (tools === '') { - return []; - } const options = [ { value: 'biome', label: 'Add Biome for code linting and formatting' }, diff --git a/test/cli.test.ts b/test/cli.test.ts new file mode 100644 index 0000000..82987d4 --- /dev/null +++ b/test/cli.test.ts @@ -0,0 +1,72 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { beforeEach, expect, test } from '@rstest/core'; +import { create } from '../src'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const fixturesDir = path.join(__dirname, 'fixtures', 'cli'); +const testDir = path.join(fixturesDir, 'test-temp-output'); + +beforeEach(() => { + // Clean up test directory before each test + if (fs.existsSync(testDir)) { + fs.rmSync(testDir, { recursive: true }); + } + fs.mkdirSync(testDir, { recursive: true }); + + // Return cleanup function + return () => { + if (fs.existsSync(testDir)) { + fs.rmSync(testDir, { recursive: true }); + } + }; +}); + +test('should accept comma separated tools option', async () => { + const projectDir = path.join(testDir, 'comma-separated-tools'); + + await create({ + name: 'test', + root: fixturesDir, + templates: ['vanilla'], + getTemplateName: async () => 'vanilla', + argv: [ + 'node', + 'test', + '--dir', + projectDir, + '--template', + 'vanilla', + '--tools', + 'eslint,prettier', + ], + }); + + expect(fs.existsSync(path.join(projectDir, 'eslint.config.mjs'))).toBe(true); + expect(fs.existsSync(path.join(projectDir, '.prettierrc'))).toBe(true); +}); + +test('should skip tools selection', async () => { + const projectDir = path.join(testDir, 'comma-separated-tools'); + + await create({ + name: 'test', + root: fixturesDir, + templates: ['vanilla'], + getTemplateName: async () => 'vanilla', + argv: [ + 'node', + 'test', + '--dir', + projectDir, + '--template', + 'vanilla', + '--tools', + '""', + ], + }); + + expect(fs.existsSync(path.join(projectDir, 'eslint.config.mjs'))).toBe(false); + expect(fs.existsSync(path.join(projectDir, '.prettierrc'))).toBe(false); +}); diff --git a/test/fixtures/cli/package.json b/test/fixtures/cli/package.json new file mode 100644 index 0000000..538f5a6 --- /dev/null +++ b/test/fixtures/cli/package.json @@ -0,0 +1,4 @@ +{ + "name": "test-fixtures-cli", + "version": "1.0.0" +} diff --git a/test/fixtures/cli/template-common/package.json b/test/fixtures/cli/template-common/package.json new file mode 100644 index 0000000..f4138df --- /dev/null +++ b/test/fixtures/cli/template-common/package.json @@ -0,0 +1,4 @@ +{ + "name": "test-common", + "version": "1.0.0" +} diff --git a/test/fixtures/cli/template-vanilla/package.json b/test/fixtures/cli/template-vanilla/package.json new file mode 100644 index 0000000..65651ce --- /dev/null +++ b/test/fixtures/cli/template-vanilla/package.json @@ -0,0 +1,4 @@ +{ + "name": "test-vanilla", + "version": "1.0.0" +} diff --git a/tsconfig.json b/tsconfig.json index d3f524c..4a066f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,6 @@ { "compilerOptions": { "outDir": "./dist", - "baseUrl": "./", "target": "ES2020", "lib": ["DOM", "ESNext"], "module": "Node16",