Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -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' },
Expand Down
72 changes: 72 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
4 changes: 4 additions & 0 deletions test/fixtures/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-fixtures-cli",
"version": "1.0.0"
}
4 changes: 4 additions & 0 deletions test/fixtures/cli/template-common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-common",
"version": "1.0.0"
}
4 changes: 4 additions & 0 deletions test/fixtures/cli/template-vanilla/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-vanilla",
"version": "1.0.0"
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "./",
"target": "ES2020",
"lib": ["DOM", "ESNext"],
"module": "Node16",
Expand Down