Skip to content

Commit 6f072d1

Browse files
authored
feat(create-rstack): simplify template names (#305)
1 parent 8f3f86b commit 6f072d1

104 files changed

Lines changed: 90 additions & 98 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/create-rstack/README.md

Lines changed: 13 additions & 13 deletions

packages/create-rstack/src/index.ts

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,55 @@ import {
55
create,
66
select,
77
} from '@rstackjs/create-toolkit';
8-
import { appendFile, mkdir, readFile, writeFile } from 'node:fs/promises';
8+
import { access, appendFile, mkdir, readFile, writeFile } from 'node:fs/promises';
99
import path from 'node:path';
1010

1111
const packageRoot = path.join(import.meta.dirname, '..');
1212

13-
const getTemplateName = async ({ template }: Argv): Promise<string> => {
14-
if (typeof template === 'string') {
15-
if (template === 'app' || template.startsWith('app-')) {
16-
const [, framework = 'vanilla', language = 'js'] = template.split('-');
17-
18-
if (framework === 'js' || framework === 'ts') {
19-
return `app-vanilla-${framework}`;
20-
}
21-
22-
return `app-${framework}-${language}`;
23-
}
24-
25-
if (template === 'lib' || template.startsWith('lib-')) {
26-
const [, libraryType = 'node', language = 'js'] = template.split('-');
27-
28-
if (libraryType === 'js' || libraryType === 'ts') {
29-
return `lib-node-${libraryType}`;
30-
}
13+
const templateNames = [
14+
'app-vanilla',
15+
'app-vanilla-ts',
16+
'app-react',
17+
'app-react-ts',
18+
'app-preact',
19+
'app-preact-ts',
20+
'app-vue',
21+
'app-vue-ts',
22+
'app-lit',
23+
'app-lit-ts',
24+
'app-svelte',
25+
'app-svelte-ts',
26+
'app-solid',
27+
'app-solid-ts',
28+
'lib-node',
29+
'lib-node-ts',
30+
'lib-react',
31+
'lib-react-ts',
32+
'lib-vue',
33+
'lib-vue-ts',
34+
'lib-svelte',
35+
'lib-svelte-ts',
36+
'lib-solid',
37+
'lib-solid-ts',
38+
'doc',
39+
'doc-i18n',
40+
];
41+
42+
const resolveTemplateName = (template: string): string => {
43+
if (!templateNames.includes(template)) {
44+
throw new Error(`Invalid input: template "${template}" not found.`);
45+
}
3146

32-
return `lib-${libraryType}-${language}`;
33-
}
47+
return template;
48+
};
3449

35-
if (template === 'doc' || template.startsWith('doc-')) {
36-
const [, documentationType = 'basic'] = template.split('-');
37-
return `doc-${documentationType}`;
50+
const getTemplateName = async ({ template }: Argv): Promise<string> => {
51+
if (typeof template === 'string') {
52+
if (/^(?:app|lib|doc)(?:-|$)/u.test(template)) {
53+
return resolveTemplateName(template);
3854
}
3955

40-
const [type, language = 'js'] = template.split('-');
41-
return `${type}-${language}`;
56+
return template;
4257
}
4358

4459
const projectType = checkCancel<string>(
@@ -72,7 +87,7 @@ const getTemplateName = async ({ template }: Argv): Promise<string> => {
7287
}),
7388
);
7489

75-
return `doc-${documentationType}`;
90+
return resolveTemplateName(documentationType === 'basic' ? 'doc' : 'doc-i18n');
7691
}
7792

7893
const templateType = checkCancel<string>(
@@ -109,7 +124,8 @@ const getTemplateName = async ({ template }: Argv): Promise<string> => {
109124
}),
110125
);
111126

112-
return `${projectType}-${templateType}-${language}`;
127+
const templateName = `${projectType}-${templateType}${language === 'ts' ? '-ts' : ''}`;
128+
return resolveTemplateName(templateName);
113129
};
114130

115131
const getStagedConfig = (templateName: string): string => {
@@ -141,7 +157,10 @@ const injectStagedSetup = async ({
141157
return;
142158
}
143159

144-
const configExtension = templateName.endsWith('-js') ? 'js' : 'ts';
160+
const configExtension = await access(path.join(distFolder, 'rstack.config.ts')).then(
161+
() => 'ts',
162+
() => 'js',
163+
);
145164
const packageJsonPath = path.join(distFolder, 'package.json');
146165
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8')) as {
147166
scripts: Record<string, string>;
@@ -168,34 +187,7 @@ const injectStagedSetup = async ({
168187
await create({
169188
root: packageRoot,
170189
name: 'rstack',
171-
templates: [
172-
'app-vanilla-js',
173-
'app-vanilla-ts',
174-
'app-react-js',
175-
'app-react-ts',
176-
'app-preact-js',
177-
'app-preact-ts',
178-
'app-vue-js',
179-
'app-vue-ts',
180-
'app-lit-js',
181-
'app-lit-ts',
182-
'app-svelte-js',
183-
'app-svelte-ts',
184-
'app-solid-js',
185-
'app-solid-ts',
186-
'lib-node-js',
187-
'lib-node-ts',
188-
'lib-react-js',
189-
'lib-react-ts',
190-
'lib-vue-js',
191-
'lib-vue-ts',
192-
'lib-svelte-js',
193-
'lib-svelte-ts',
194-
'lib-solid-js',
195-
'lib-solid-ts',
196-
'doc-basic',
197-
'doc-i18n',
198-
],
190+
templates: templateNames,
199191
builtinTools: [],
200192
getTemplateName,
201193
onGitResolved: injectStagedSetup,

packages/create-rstack/template-app-lit-js/package.json renamed to packages/create-rstack/template-app-lit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "rstack-app-lit-js",
2+
"name": "rstack-app-lit",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module",

packages/create-rstack/template-app-lit-js/rstack.config.js renamed to packages/create-rstack/template-app-lit/rstack.config.js

File renamed without changes.

packages/create-rstack/template-app-lit-js/src/index.css renamed to packages/create-rstack/template-app-lit/src/index.css

File renamed without changes.

packages/create-rstack/template-app-lit-js/src/index.html renamed to packages/create-rstack/template-app-lit/src/index.html

File renamed without changes.

packages/create-rstack/template-app-lit-js/src/index.js renamed to packages/create-rstack/template-app-lit/src/index.js

File renamed without changes.

packages/create-rstack/template-app-lit-js/src/my-element.js renamed to packages/create-rstack/template-app-lit/src/my-element.js

File renamed without changes.

packages/create-rstack/template-app-lit-js/tests/index.test.js renamed to packages/create-rstack/template-app-lit/tests/index.test.js

File renamed without changes.

packages/create-rstack/template-app-preact-js/package.json renamed to packages/create-rstack/template-app-preact/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "rstack-app-preact-js",
2+
"name": "rstack-app-preact",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module",

0 commit comments

Comments
 (0)