Skip to content

Commit 9cdf5c8

Browse files
authored
feat(cli): add support for comma-separated tools option (#87)
1 parent b7afed8 commit 9cdf5c8

6 files changed

Lines changed: 101 additions & 8 deletions

File tree

src/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ function isEmptyDir(path: string) {
7474
return files.length === 0 || (files.length === 1 && files[0] === '.git');
7575
}
7676

77+
function parseToolsOption(tools: Argv['tools']) {
78+
if (typeof tools === 'undefined') {
79+
return null;
80+
}
81+
82+
const toolsArr = Array.isArray(tools) ? tools : [tools];
83+
84+
return toolsArr
85+
.flatMap((tool) => tool.split(','))
86+
.map((tool) => tool.trim())
87+
.filter(Boolean);
88+
}
89+
7790
export type Argv = {
7891
help?: boolean;
7992
dir?: string;
@@ -110,9 +123,10 @@ async function getTools(
110123
extraTools?: ExtraTool[],
111124
) {
112125
// Check if tools are specified via CLI options
113-
if (tools) {
114-
let toolsArr = Array.isArray(tools) ? tools : [tools];
115-
toolsArr = toolsArr.filter(
126+
const parsedTools = parseToolsOption(tools);
127+
128+
if (parsedTools !== null) {
129+
const toolsArr = parsedTools.filter(
116130
(tool) =>
117131
BUILTIN_TOOLS.includes(tool) ||
118132
extraTools?.some((extraTool) => extraTool.value === tool),
@@ -123,10 +137,6 @@ async function getTools(
123137
if (dir && template) {
124138
return [];
125139
}
126-
// skip tools selection when tools is empty string
127-
if (tools === '') {
128-
return [];
129-
}
130140

131141
const options = [
132142
{ value: 'biome', label: 'Add Biome for code linting and formatting' },

test/cli.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
import { beforeEach, expect, test } from '@rstest/core';
5+
import { create } from '../src';
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
const fixturesDir = path.join(__dirname, 'fixtures', 'cli');
9+
const testDir = path.join(fixturesDir, 'test-temp-output');
10+
11+
beforeEach(() => {
12+
// Clean up test directory before each test
13+
if (fs.existsSync(testDir)) {
14+
fs.rmSync(testDir, { recursive: true });
15+
}
16+
fs.mkdirSync(testDir, { recursive: true });
17+
18+
// Return cleanup function
19+
return () => {
20+
if (fs.existsSync(testDir)) {
21+
fs.rmSync(testDir, { recursive: true });
22+
}
23+
};
24+
});
25+
26+
test('should accept comma separated tools option', async () => {
27+
const projectDir = path.join(testDir, 'comma-separated-tools');
28+
29+
await create({
30+
name: 'test',
31+
root: fixturesDir,
32+
templates: ['vanilla'],
33+
getTemplateName: async () => 'vanilla',
34+
argv: [
35+
'node',
36+
'test',
37+
'--dir',
38+
projectDir,
39+
'--template',
40+
'vanilla',
41+
'--tools',
42+
'eslint,prettier',
43+
],
44+
});
45+
46+
expect(fs.existsSync(path.join(projectDir, 'eslint.config.mjs'))).toBe(true);
47+
expect(fs.existsSync(path.join(projectDir, '.prettierrc'))).toBe(true);
48+
});
49+
50+
test('should skip tools selection', async () => {
51+
const projectDir = path.join(testDir, 'comma-separated-tools');
52+
53+
await create({
54+
name: 'test',
55+
root: fixturesDir,
56+
templates: ['vanilla'],
57+
getTemplateName: async () => 'vanilla',
58+
argv: [
59+
'node',
60+
'test',
61+
'--dir',
62+
projectDir,
63+
'--template',
64+
'vanilla',
65+
'--tools',
66+
'""',
67+
],
68+
});
69+
70+
expect(fs.existsSync(path.join(projectDir, 'eslint.config.mjs'))).toBe(false);
71+
expect(fs.existsSync(path.join(projectDir, '.prettierrc'))).toBe(false);
72+
});

test/fixtures/cli/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "test-fixtures-cli",
3+
"version": "1.0.0"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "test-common",
3+
"version": "1.0.0"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "test-vanilla",
3+
"version": "1.0.0"
4+
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"compilerOptions": {
33
"outDir": "./dist",
4-
"baseUrl": "./",
54
"target": "ES2020",
65
"lib": ["DOM", "ESNext"],
76
"module": "Node16",

0 commit comments

Comments
 (0)