Skip to content

Commit 41ef20f

Browse files
fix: check for expanded templates when a template is selected from the cli init flow. (#4107)
fixes #4105 ## Description This issue fixes picking the default vanilla template in the cli init-flow. Right now, when a template is selected from the `PROJECT_TEMPLATES` we are directly sending the value to `build`. But we need to check for the `expanded` value of the template in order to pick all the series of templates needed for the build. ## Steps for reproduction - create a project in the editor. - Now, create a share-link for the project and copy it. - Run `webstudio` in your local terminal and then the cli prompts for the URL, parse the url from the above step. - Now, pick `Vanilla` as a template from the list of the templates. - The project should build a vanilla template without any errors. ## Code Review - [ ] hi @istarkov , I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [x] made a self-review - [x] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [x] tested locally and on preview environment (preview dev login: 5de6)
1 parent 5370b7a commit 41ef20f

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

packages/cli/src/build-utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { PROJECT_TEMPLATES, INTERNAL_TEMPLATES } from "./config";
2+
3+
export const mapToTemplatesFromOptions = (values: string[]) => {
4+
const templates: string[] = [];
5+
for (const value of values) {
6+
const template =
7+
PROJECT_TEMPLATES.find((item) => item.value === value) ??
8+
INTERNAL_TEMPLATES.find((item) => item.value === value);
9+
10+
if (template == null) {
11+
templates.push(value);
12+
continue;
13+
}
14+
15+
if ("expand" in template && template.expand != null) {
16+
templates.push(...template.expand);
17+
continue;
18+
}
19+
20+
templates.push(value);
21+
}
22+
23+
return templates;
24+
};

packages/cli/src/commands/build.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import { access } from "node:fs/promises";
22
import { exit } from "node:process";
33
import { log } from "@clack/prompts";
44
import { prebuild } from "../prebuild";
5-
import {
6-
INTERNAL_TEMPLATES,
7-
LOCAL_DATA_FILE,
8-
PROJECT_TEMPLATES,
9-
} from "../config";
5+
import { LOCAL_DATA_FILE, PROJECT_TEMPLATES } from "../config";
106
import type {
117
CommonYargsArgv,
128
StrictYargsOptionsToInterface,
139
} from "./yargs-types";
10+
import { mapToTemplatesFromOptions } from "../build-utils";
1411

1512
export const buildOptions = (yargs: CommonYargsArgv) =>
1613
yargs
@@ -29,28 +26,7 @@ export const buildOptions = (yargs: CommonYargsArgv) =>
2926
string: true,
3027
default: [] as string[],
3128

32-
coerce: (values: string[]) => {
33-
const templates = [];
34-
for (const value of values) {
35-
const template =
36-
PROJECT_TEMPLATES.find((item) => item.value === value) ??
37-
INTERNAL_TEMPLATES.find((item) => item.value === value);
38-
39-
if (template == null) {
40-
templates.push(value);
41-
continue;
42-
}
43-
44-
if ("expand" in template && template.expand != null) {
45-
templates.push(...template.expand);
46-
continue;
47-
}
48-
49-
templates.push(value);
50-
}
51-
52-
return templates;
53-
},
29+
coerce: mapToTemplatesFromOptions,
5430

5531
describe: `Template to use for the build [choices: ${PROJECT_TEMPLATES.map(
5632
(item) => item.value

packages/cli/src/commands/init-flow.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { link, validateShareLink } from "./link";
1717
import { sync } from "./sync";
1818
import { build, buildOptions } from "./build";
1919
import type { StrictYargsOptionsToInterface } from "./yargs-types";
20+
import { mapToTemplatesFromOptions } from "../build-utils";
2021

2122
type ProjectTemplates = (typeof PROJECT_TEMPLATES)[number]["value"];
2223

@@ -103,7 +104,9 @@ export const initFlow = async (
103104

104105
await build({
105106
...options,
106-
...(projectTemplate && { template: [projectTemplate] }),
107+
...(projectTemplate && {
108+
template: mapToTemplatesFromOptions([projectTemplate]),
109+
}),
107110
});
108111

109112
if (shouldInstallDeps === true) {

0 commit comments

Comments
 (0)