Skip to content

Commit 011f8b7

Browse files
committed
refactor: moved functions versions to core
1 parent 04dee80 commit 011f8b7

File tree

5 files changed

+84
-95
lines changed

5 files changed

+84
-95
lines changed

schema/swa-cli.config.schema.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@
262262
"type": "boolean"
263263
},
264264
"apiLanguage": {
265-
"description": "The runtime language of the function",
265+
"description": "The runtime language of the API",
266266
"type": "string"
267267
},
268268
"apiVersion": {
269-
"description": "The version of the function runtime language",
269+
"description": "The version of the API runtime language",
270270
"type": "string"
271271
}
272272
},

src/cli/commands/deploy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
configureOptions,
1010
findSWAConfigFile,
1111
getCurrentSwaCliConfigFromFile,
12-
getDefaultVersion,
1312
isUserOption,
1413
isUserOrConfigOption,
1514
logger,
@@ -21,6 +20,7 @@ import {
2120
import { chooseOrCreateProjectDetails, getStaticSiteDeployment } from "../../core/account";
2221
import { cleanUp, getDeployClientPath } from "../../core/deploy-client";
2322
import { swaCLIEnv } from "../../core/env";
23+
import { getDefaultVersion } from "../../core/functions-versions";
2424
import { addSharedLoginOptionsToCommand, login } from "./login";
2525

2626
const packageInfo = require(path.join(__dirname, "..", "..", "..", "package.json"));
@@ -345,7 +345,7 @@ export async function deploy(options: SWACLIConfig) {
345345
else if (line.includes("[31m")) {
346346
if (line.includes("Cannot deploy to the function app because Function language info isn't provided.")) {
347347
line = chalk.red(
348-
`Cannot deploy to the function app because Function language info isn't provided, use flags "--api-language" and "--api-version" or add a "platform.apiRuntime" property to your staticwebapp.config.json file, or create one in ${options.outputLocation!}. Please consult the documentation for more information about staticwebapp.config.json: https://learn.microsoft.com/en-us/azure/static-web-apps/build-configuration?tabs=github-actions#skip-building-the-api`
348+
`Cannot deploy to the function app because Function language info isn't provided, use flags "--api-language" and "--api-version" or add a "platform.apiRuntime" property to your staticwebapp.config.json file, or create one in ${options.outputLocation!}. Please consult the documentation for more information about staticwebapp.config.json: https://learn.microsoft.com/azure/static-web-apps/build-configuration?tabs=github-actions#skip-building-the-api`
349349
);
350350
}
351351

src/cli/commands/init.ts

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import chalk from "chalk";
22
import { Command } from "commander";
33
import path from "path";
44
import process from "process";
5-
import prompts from "prompts";
65
import { promptOrUseDefault } from "../../core/prompts";
76
import {
87
configureOptions,
@@ -15,6 +14,7 @@ import {
1514
writeConfigFile,
1615
} from "../../core/utils";
1716
import { detectProjectFolders, generateConfiguration, isDescendantPath } from "../../core/frameworks";
17+
import { getChoicesForApiLanguage } from "../../core/functions-versions";
1818

1919
export default function registerCommand(program: Command) {
2020
program
@@ -202,6 +202,23 @@ async function promptConfigSettings(disablePrompts: boolean, detectedConfig: Fra
202202
initial: detectedConfig.apiLocation,
203203
format: trimValue,
204204
},
205+
{
206+
type: (prev) => (prev != null ? "select" : null),
207+
name: "apiLanguage",
208+
message: "What's your API language? (optional)",
209+
choices: [
210+
{ title: "Node.js", value: "node" },
211+
{ title: "Python", value: "python" },
212+
{ title: "Dotnet", value: "dotnet" },
213+
{ title: "Dotnet isolated", value: "dotnet-isolated" },
214+
],
215+
},
216+
{
217+
type: (prev) => (prev != null ? "select" : null),
218+
name: "apiVersion",
219+
message: "What's your API version? (optional)",
220+
choices: (prev) => getChoicesForApiLanguage(prev),
221+
},
205222
{
206223
type: "text",
207224
name: "appBuildCommand",
@@ -240,75 +257,9 @@ async function promptConfigSettings(disablePrompts: boolean, detectedConfig: Fra
240257
},
241258
]);
242259

243-
if (response.apiLocation) {
244-
const apiLanguagePrompt = await prompts([
245-
{
246-
type: "select",
247-
name: "apiLanguage",
248-
message: "What's your API language? (optional)",
249-
choices: [
250-
{ title: "Node.js", value: "node" },
251-
{ title: "Python", value: "python" },
252-
{ title: "Dotnet", value: "dotnet" },
253-
{ title: "Dotnet isolated", value: "dotnet-isolated" },
254-
],
255-
},
256-
]);
257-
258-
response.apiLanguage = apiLanguagePrompt.apiLanguage;
259-
260-
const apiVersionPrompt = await prompts([
261-
{
262-
type: "select",
263-
name: "apiVersion",
264-
message: "What's your API version? (optional)",
265-
choices: getChoicesForApiLanguage(response.apiLanguage),
266-
},
267-
]);
268-
269-
response.apiVersion = apiVersionPrompt.apiVersion;
270-
}
271-
272260
return response;
273261
}
274262

275-
function getChoicesForApiLanguage(apiLanguage: string) {
276-
// Refer to this for functions and versions - https://learn.microsoft.com/en-us/azure/static-web-apps/configuration#selecting-the-api-language-runtime-version
277-
let choices = [];
278-
switch (apiLanguage) {
279-
case "node":
280-
choices = [
281-
{ title: "16", value: "16" },
282-
{ title: "14", value: "14" },
283-
{ title: "12", value: "12" },
284-
];
285-
break;
286-
case "python":
287-
choices = [
288-
{ title: "3.8", value: "3.8" },
289-
{ title: "3.9", value: "3.9" },
290-
];
291-
break;
292-
case "dotnet":
293-
choices = [
294-
{ title: "6.0", value: "6.0" },
295-
{ title: "3.1", value: "3.1" },
296-
];
297-
break;
298-
case "dotnet-isolated":
299-
choices = [{ title: "6.0", value: "6.0" }];
300-
break;
301-
default:
302-
choices = [
303-
{ title: "16", value: "16" },
304-
{ title: "14", value: "14" },
305-
{ title: "12", value: "12" },
306-
];
307-
break;
308-
}
309-
return choices;
310-
}
311-
312263
function printFrameworkConfig(config: FrameworkConfig) {
313264
logger.log(chalk.bold("\nDetected configuration for your app:"));
314265
logger.log(`- Framework(s): ${chalk.green(config.name ?? "none")}`);

src/core/frameworks/detect.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { promises as fs } from "fs";
22
import globrex from "globrex";
33
import path from "path";
44
import { DEFAULT_CONFIG } from "../../config";
5-
import { DEFAULT_DOTNET_ISOLATED_VERSION, DEFAULT_DOTNET_VERSION, DEFAULT_NODE_VERSION, DEFAULT_PYTHON_VERSION } from "../../core/constants";
65
import { hasSpaces, logger, removeTrailingPathSep, safeReadFile, safeReadJson } from "../utils";
76
import { apiFrameworks, appFrameworks } from "./frameworks";
87

@@ -392,26 +391,3 @@ export function formatDetectedFolders(folders: DetectedFolder[], type: string):
392391
`- ${folders.map((f) => `${f.rootPath} (${f.frameworks.map((fr) => fr.name).join(", ")})`).join("\n- ")}`
393392
);
394393
}
395-
export function getDefaultVersion(apiLanguage: string | undefined): string {
396-
let apiVersion = "16";
397-
if (!apiLanguage) apiLanguage = "node";
398-
// apiLanguage = apiLanguage.toLowerCase();
399-
switch (apiLanguage) {
400-
case "node":
401-
apiVersion = DEFAULT_NODE_VERSION;
402-
break;
403-
case "python":
404-
apiVersion = DEFAULT_PYTHON_VERSION;
405-
break;
406-
case "dotnet":
407-
apiVersion = DEFAULT_DOTNET_VERSION;
408-
break;
409-
case "dotnetisolated" || "dotnet isolated" || "dotnet-isolated":
410-
apiVersion = DEFAULT_DOTNET_ISOLATED_VERSION;
411-
break;
412-
default:
413-
apiVersion = DEFAULT_NODE_VERSION;
414-
break;
415-
}
416-
return apiVersion;
417-
}

src/core/functions-versions.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { DEFAULT_DOTNET_ISOLATED_VERSION, DEFAULT_DOTNET_VERSION, DEFAULT_NODE_VERSION, DEFAULT_PYTHON_VERSION } from "./constants";
2+
3+
export function getDefaultVersion(apiLanguage: string | undefined): string {
4+
let apiVersion = "16";
5+
if (!apiLanguage) apiLanguage = "node";
6+
// apiLanguage = apiLanguage.toLowerCase();
7+
switch (apiLanguage) {
8+
case "node":
9+
apiVersion = DEFAULT_NODE_VERSION;
10+
break;
11+
case "python":
12+
apiVersion = DEFAULT_PYTHON_VERSION;
13+
break;
14+
case "dotnet":
15+
apiVersion = DEFAULT_DOTNET_VERSION;
16+
break;
17+
case "dotnetisolated" || "dotnet isolated" || "dotnet-isolated":
18+
apiVersion = DEFAULT_DOTNET_ISOLATED_VERSION;
19+
break;
20+
default:
21+
apiVersion = DEFAULT_NODE_VERSION;
22+
break;
23+
}
24+
return apiVersion;
25+
}
26+
27+
export function getChoicesForApiLanguage(apiLanguage: string) {
28+
// Refer to this for functions and versions - https://learn.microsoft.com/azure/static-web-apps/configuration#selecting-the-api-language-runtime-version
29+
let choices = [];
30+
switch (apiLanguage) {
31+
case "node":
32+
choices = [
33+
{ title: "16", value: "16" },
34+
{ title: "14", value: "14" },
35+
{ title: "12", value: "12" },
36+
];
37+
break;
38+
case "python":
39+
choices = [
40+
{ title: "3.8", value: "3.8" },
41+
{ title: "3.9", value: "3.9" },
42+
];
43+
break;
44+
case "dotnet":
45+
choices = [
46+
{ title: "6.0", value: "6.0" },
47+
{ title: "3.1", value: "3.1" },
48+
];
49+
break;
50+
case "dotnet-isolated":
51+
choices = [{ title: "6.0", value: "6.0" }];
52+
break;
53+
default:
54+
choices = [
55+
{ title: "16", value: "16" },
56+
{ title: "14", value: "14" },
57+
{ title: "12", value: "12" },
58+
];
59+
break;
60+
}
61+
return choices;
62+
}

0 commit comments

Comments
 (0)