Skip to content

Commit 3895d88

Browse files
committed
fix(create-rslib): support repeatable —tools flag
1 parent f210f60 commit 3895d88

File tree

7 files changed

+194
-121
lines changed

7 files changed

+194
-121
lines changed

packages/create-rslib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"test": "rstest"
3030
},
3131
"dependencies": {
32-
"create-rstack": "1.7.4"
32+
"create-rstack": "1.7.6"
3333
},
3434
"devDependencies": {
3535
"@rslib/tsconfig": "workspace:*",

packages/create-rslib/src/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import {
66
type Argv,
7+
BUILTIN_TOOLS,
78
checkCancel,
89
create,
910
type ESLintTemplateName,
@@ -16,15 +17,20 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
1617

1718
type TemplateName = 'react' | 'node' | 'vue';
1819

19-
async function getTemplateName({ template }: Argv) {
20-
if (typeof template === 'string') {
21-
const pair = template.split('-');
22-
const lang = pair[pair.length - 1];
23-
if (lang && ['js', 'ts'].includes(lang)) {
24-
return template;
25-
}
26-
// default to ts
27-
return `${template}-ts`;
20+
async function getTemplateName(argv: Argv) {
21+
if (typeof argv.template === 'string') {
22+
const pair = argv.template.split('-');
23+
const lang = pair[pair.length - 1] ?? 'ts';
24+
const rest = pair.slice(0, pair.length - 1).join('-');
25+
const tools = (
26+
typeof argv.tools === 'string' ? [argv.tools] : (argv.tools ?? [])
27+
).filter((tool) => !BUILTIN_TOOLS.includes(tool));
28+
29+
return composeTemplateName({
30+
template: rest,
31+
lang: lang as Lang,
32+
tools,
33+
});
2834
}
2935

3036
const templateName = checkCancel<TemplateName>(

packages/create-rslib/test/helper.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,7 @@ import { existsSync } from 'node:fs';
33
import path from 'node:path';
44
import { expect } from '@rstest/core';
55
import fse from 'fs-extra';
6-
7-
export const decomposeTemplateName = (
8-
name: string,
9-
): {
10-
template: string;
11-
tools: string[];
12-
lang: string;
13-
} => {
14-
const [template, tools, lang] = name.split('-');
15-
const trimBrackets = (str: string) => str.replace(/^\[|\]$/g, '');
16-
return {
17-
template: trimBrackets(template)!,
18-
tools: tools === '[]' ? [] : trimBrackets(tools).split(','),
19-
lang: lang!,
20-
} as const;
21-
};
6+
import type { Lang } from '../src/helpers';
227

238
export const expectPackageJson = (
249
pkgJson: Record<string, any>,
@@ -30,12 +15,19 @@ export const expectPackageJson = (
3015
expect(pkgJson.devDependencies['@rslib/core']).toBeTruthy();
3116
};
3217

18+
export interface TemplateCase {
19+
template: string;
20+
lang: Lang;
21+
tools: string[];
22+
label: string;
23+
}
24+
3325
export const createAndValidate = (
3426
cwd: string,
35-
template: string,
27+
templateCase: TemplateCase,
3628
{
37-
name = `test-temp-${template}`,
38-
tools = [],
29+
name = `test-temp-${templateCase.label}`,
30+
tools: additionalTools = [],
3931
clean = true,
4032
}: {
4133
name?: string;
@@ -46,9 +38,19 @@ export const createAndValidate = (
4638
const dir = path.join(cwd, name);
4739
fse.removeSync(dir);
4840

49-
let command = `node ../dist/index.js -d ${name} -t ${template}`;
50-
if (tools.length) {
51-
const toolsCmd = tools.map((tool) => `--tools ${tool}`).join(' ');
41+
const templateArg = `${templateCase.template}-${templateCase.lang}`;
42+
43+
let command = `node ../dist/index.js --dir ${name} --template ${templateArg}`;
44+
45+
if (templateCase.tools.length) {
46+
const templateToolsCmd = templateCase.tools
47+
.map((tool) => `--tools ${tool}`)
48+
.join(' ');
49+
command += ` ${templateToolsCmd}`;
50+
}
51+
52+
if (additionalTools.length) {
53+
const toolsCmd = additionalTools.map((tool) => `--tools ${tool}`).join(' ');
5254
command += ` ${toolsCmd}`;
5355
}
5456

@@ -57,33 +59,31 @@ export const createAndValidate = (
5759
const pkgJson = fse.readJSONSync(path.join(dir, 'package.json'));
5860
expectPackageJson(pkgJson, path.basename(name));
5961

60-
const templateData = decomposeTemplateName(template);
61-
6262
// tsconfig
63-
if (templateData.lang === 'ts') {
63+
if (templateCase.lang === 'ts') {
6464
expect(pkgJson.devDependencies.typescript).toBeTruthy();
6565
expect(existsSync(path.join(dir, 'tsconfig.json'))).toBeTruthy();
6666
}
6767

6868
// tool - Vitest
69-
if (templateData.tools.includes('vitest')) {
69+
if (templateCase.tools.includes('vitest')) {
7070
for (const file of [
71-
`vitest.config.${templateData.lang}`,
72-
`tests/index.test.${templateData.lang}${templateData.template === 'react' ? 'x' : ''}`,
71+
`vitest.config.${templateCase.lang}`,
72+
`tests/index.test.${templateCase.lang}${templateCase.template === 'react' ? 'x' : ''}`,
7373
]) {
7474
expect(existsSync(path.join(dir, file))).toBeTruthy();
7575
}
7676
expect(pkgJson.devDependencies.vitest).toBeTruthy();
77-
if (templateData.template === 'react') {
77+
if (templateCase.template === 'react') {
7878
expect(pkgJson.devDependencies['@testing-library/react']).toBeTruthy();
7979
}
8080
}
8181

8282
// tool - Storybook
83-
if (templateData.tools.includes('storybook')) {
83+
if (templateCase.tools.includes('storybook')) {
8484
for (const file of [
85-
`.storybook/main.${templateData.lang}`,
86-
`.storybook/preview.${templateData.lang}`,
85+
`.storybook/main.${templateCase.lang}`,
86+
`.storybook/preview.${templateCase.lang}`,
8787
]) {
8888
expect(existsSync(path.join(dir, file))).toBeTruthy();
8989
}

0 commit comments

Comments
 (0)