-
Notifications
You must be signed in to change notification settings - Fork 35
/
Webpage.ts
204 lines (178 loc) · 6.25 KB
/
Webpage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as vscode from "vscode";
import {
createFileWatcher,
createRecord,
formatFileName,
formatFolderName,
getPageTemplate,
getParentPagePaths,
getPortalContext,
isNullOrEmpty,
} from "./utils/CommonUtils";
import { QuickPickItem } from "vscode";
import { MultiStepInput } from "../../../common/utilities/MultiStepInput";
import { TableFolder, Tables, YoSubGenerator } from "./CreateOperationConstants";
import path from "path";
import { ITelemetry } from "../../telemetry/ITelemetry";
import { sendTelemetryEvent, UserFileCreateEvent } from "../telemetry";
interface IWebpageInputState {
title: string;
step: number;
totalSteps: number;
pageTemplate: string;
name: string;
parentPage: string;
}
export const createWebpage = async (
context: vscode.ExtensionContext,
selectedWorkspaceFolder: string | undefined,
yoGenPath: string | null,
telemetry: ITelemetry
) => {
try {
if(!selectedWorkspaceFolder) {
return
}
const portalContext = getPortalContext(selectedWorkspaceFolder);
// Get the page templates & webpages from the portal directory
await portalContext.init([Tables.PAGETEMPLATE, Tables.WEBPAGE]);
const { pageTemplateNames, pageTemplateMap } =
getPageTemplate(portalContext);
if (pageTemplateNames.length === 0) {
vscode.window.showErrorMessage(vscode.l10n.t("No page templates found"));
return;
}
const { paths, pathsMap, webpageNames } =
getParentPagePaths(portalContext);
if (paths.length === 0) {
vscode.window.showErrorMessage(vscode.l10n.t("No webpages found"));
return;
}
// Show a quick pick
const webpageInputs = await getWebpageInputs(
pageTemplateNames,
paths,
webpageNames
);
const pageTemplateId = pageTemplateMap.get(webpageInputs.pageTemplate);
const webpageName = webpageInputs.name;
const parentPageId = pathsMap.get(webpageInputs.parentPage);
// Create the webpage using the yo command
if (!isNullOrEmpty(webpageName) && selectedWorkspaceFolder) {
const file = formatFileName(webpageName);
const folder = formatFolderName(webpageName);
const watcherPattern = path.join(
TableFolder.WEBPAGE_FOLDER,
folder,
"content-pages",
`${file}.*.webpage.copy.html`
);
const watcher = createFileWatcher(
context,
selectedWorkspaceFolder,
watcherPattern
);
const command = `"${yoGenPath}" ${YoSubGenerator.WEBPAGE} "${webpageName}" "${parentPageId}" "${pageTemplateId}"`;
await createRecord(
Tables.WEBPAGE,
command,
selectedWorkspaceFolder,
watcher,
telemetry
);
}
} catch (error: any) {
sendTelemetryEvent(telemetry, { methodName: createWebpage.name, eventName: UserFileCreateEvent, fileEntityType:Tables.WEBPAGE, exception: error as Error })
throw new Error(error);
}
};
async function getWebpageInputs(
pageTemplateName: string[],
parentPage: string[],
webpageNames: string[]
) {
const pageTemplates: QuickPickItem[] = pageTemplateName.map((label) => ({
label,
}));
const parentPages: QuickPickItem[] = parentPage.map((label) => ({
label,
}));
const title = vscode.l10n.t("New Webpage");
async function collectInputs() {
const state = {} as Partial<IWebpageInputState>;
await MultiStepInput.run((input) => inputWebpageName(input, state));
return state as IWebpageInputState;
}
async function inputWebpageName(
input: MultiStepInput,
state: Partial<IWebpageInputState>
) {
state.name = await input.showInputBox({
title,
step: 1,
totalSteps: 3,
value: state.name || "",
placeholder: vscode.l10n.t("Enter name"),
validate: validateNameIsUnique,
});
return (input: MultiStepInput) => pickPageTemplate(input, state);
}
async function pickPageTemplate(
input: MultiStepInput,
state: Partial<IWebpageInputState>
) {
const pick = await input.showQuickPick({
title,
step: 2,
totalSteps: 3,
placeholder: vscode.l10n.t("Choose page template"),
items: pageTemplates,
activeItem:
typeof state.pageTemplate !== "string"
? state.pageTemplate
: undefined,
});
state.pageTemplate = pick.label;
return (input: MultiStepInput) => pickParentPage(input, state);
}
async function pickParentPage(
input: MultiStepInput,
state: Partial<IWebpageInputState>
) {
const pick = await input.showQuickPick({
title,
step: 3,
totalSteps: 3,
placeholder: vscode.l10n.t("Choose parent page"),
items: parentPages,
activeItem:
typeof state.parentPage !== "string"
? state.parentPage
: undefined,
});
state.parentPage = pick.label;
}
async function validateNameIsUnique(name: string) {
if (!name) {
return vscode.l10n.t("Please enter a name for the webpage.");
}
if (!/^[A-Za-z0-9-_]+$/.test(name)) {
return vscode.l10n.t("Webpage names should contain only letters, numbers, hyphens, or underscores.");
}
if (
webpageNames
.map((n) => n.toLowerCase())
.includes(name.toLowerCase())
) {
return vscode.l10n.t("A webpage with the same name already exists. Please enter a different name.");
}
return undefined;
}
const state = await collectInputs();
return state;
}