-
Notifications
You must be signed in to change notification settings - Fork 871
Expand file tree
/
Copy pathTypeScriptLanguageSupport.cs
More file actions
286 lines (245 loc) · 10.6 KB
/
TypeScriptLanguageSupport.cs
File metadata and controls
286 lines (245 loc) · 10.6 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.Json;
using System.Text.Json.Nodes;
using Aspire.Shared;
using Aspire.TypeSystem;
namespace Aspire.Hosting.CodeGeneration.TypeScript;
/// <summary>
/// Provides language support for TypeScript AppHosts.
/// Implements scaffolding, detection, and runtime configuration.
/// </summary>
internal sealed class TypeScriptLanguageSupport : ILanguageSupport
{
/// <summary>
/// The language/runtime identifier for TypeScript with Node.js.
/// Format: {language}/{runtime} to support multiple runtimes (e.g., typescript/bun, typescript/deno).
/// </summary>
private const string LanguageId = "typescript/nodejs";
/// <summary>
/// The code generation target language. This maps to the ICodeGenerator.Language property.
/// </summary>
private const string CodeGenTarget = "TypeScript";
private const string LanguageDisplayName = "TypeScript (Node.js)";
private const string AppHostFileName = "apphost.ts";
private const string PackageJsonFileName = "package.json";
private const string AppHostTsConfigFileName = "tsconfig.apphost.json";
/// <summary>
/// The default content for tsconfig.apphost.json, shared between scaffolding and migration.
/// </summary>
private const string AppHostTsConfigContent = """
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "./dist/apphost",
"rootDir": "."
},
"include": ["apphost.ts", ".modules/**/*.ts"],
"exclude": ["node_modules"]
}
""";
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new()
{
WriteIndented = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
private static readonly string[] s_detectionPatterns = ["apphost.ts"];
/// <inheritdoc />
public string Language => LanguageId;
/// <inheritdoc />
public Dictionary<string, string> Scaffold(ScaffoldRequest request)
{
var files = new Dictionary<string, string>();
// Create apphost.ts
files[AppHostFileName] = """
// Aspire TypeScript AppHost
// For more information, see: https://aspire.dev
import { createBuilder } from './.modules/aspire.js';
async function main() {
const builder = await createBuilder();
// Add your resources here, for example:
// const redis = await builder.addContainer("cache", "redis:latest");
// const postgres = await builder.addPostgres("db");
await builder.build().run();
}
main().catch((error: unknown) => {
console.error(error);
process.exitCode = 1;
});
""";
files[PackageJsonFileName] = CreatePackageJson(request);
// Create eslint.config.mjs for catching unawaited promises in apphost.ts
files["eslint.config.mjs"] = """
// @ts-check
import { defineConfig } from 'eslint/config';
import tseslint from 'typescript-eslint';
export default defineConfig({
files: ['apphost.ts'],
extends: [tseslint.configs.base],
languageOptions: {
parserOptions: {
projectService: true,
},
},
rules: {
'@typescript-eslint/no-floating-promises': ['error', { checkThenables: true }],
},
});
""";
// Create an apphost-specific tsconfig so existing brownfield TypeScript settings are preserved.
files[AppHostTsConfigFileName] = AppHostTsConfigContent;
// Create apphost.run.json with random ports
// Use PortSeed if provided (for testing), otherwise use random
var random = request.PortSeed.HasValue
? new Random(request.PortSeed.Value)
: Random.Shared;
var httpsPort = random.Next(10000, 65000);
var httpPort = random.Next(10000, 65000);
var otlpPort = random.Next(10000, 65000);
var resourceServicePort = random.Next(10000, 65000);
files["apphost.run.json"] = $$"""
{
"profiles": {
"https": {
"applicationUrl": "https://localhost:{{httpsPort}};http://localhost:{{httpPort}}",
"environmentVariables": {
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:{{otlpPort}}",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:{{resourceServicePort}}"
}
}
}
}
""";
return files;
}
private static string CreatePackageJson(ScaffoldRequest request)
{
// Build scaffold output with only Aspire-desired content. We intentionally do NOT
// read the existing package.json here — the CLI-side PackageJsonMerger handles all
// combining with on-disk content. Including existing entries in the scaffold output
// would cause a double-merge where correctness depends on JsonObject iteration order.
var packageJson = new JsonObject();
var packageJsonPath = Path.Combine(request.TargetPath, PackageJsonFileName);
if (!File.Exists(packageJsonPath))
{
// Greenfield: include root metadata so the scaffold output is a complete package.json.
var packageName = request.ProjectName?.ToLowerInvariant() ?? "aspire-apphost";
packageJson["name"] = packageName;
packageJson["version"] = "1.0.0";
packageJson["private"] = true;
packageJson["type"] = "module";
}
// NOTE: The engines.node constraint must match ESLint 10's own requirement
// (^20.19.0 || ^22.13.0 || >=24) to avoid install/runtime failures on unsupported Node versions.
// This is set for both greenfield and brownfield scenarios — the user is opting into Aspire
// which requires these Node versions. The CLI-side MergeEngines also enforces this during merge.
var engines = EnsureObject(packageJson, "engines");
engines["node"] = "^20.19.0 || ^22.13.0 || >=24";
var scripts = EnsureObject(packageJson, "scripts");
scripts["aspire:lint"] = "eslint apphost.ts";
scripts["aspire:start"] = "aspire run";
scripts["aspire:build"] = $"tsc -p {AppHostTsConfigFileName}";
scripts["aspire:dev"] = $"tsc --watch -p {AppHostTsConfigFileName}";
EnsureDependency(packageJson, "dependencies", "vscode-jsonrpc", "^8.2.0");
EnsureDependency(packageJson, "devDependencies", "@types/node", "^22.0.0");
EnsureDependency(packageJson, "devDependencies", "eslint", "^10.0.3");
EnsureDependency(packageJson, "devDependencies", "nodemon", "^3.1.14");
EnsureDependency(packageJson, "devDependencies", "tsx", "^4.21.0");
EnsureDependency(packageJson, "devDependencies", "typescript", "^5.9.3");
EnsureDependency(packageJson, "devDependencies", "typescript-eslint", "^8.57.1");
return packageJson.ToJsonString(s_jsonSerializerOptions);
}
private static void EnsureDependency(JsonObject packageJson, string sectionName, string packageName, string version)
{
var section = EnsureObject(packageJson, sectionName);
var existingVersion = GetStringValue(section[packageName]);
if (existingVersion is null)
{
section[packageName] = version;
return;
}
if (NpmVersionHelper.ShouldUpgrade(existingVersion, version))
{
section[packageName] = version;
}
}
private static JsonObject EnsureObject(JsonObject parent, string propertyName)
{
if (parent[propertyName] is JsonObject obj)
{
return obj;
}
obj = new JsonObject();
parent[propertyName] = obj;
return obj;
}
private static string? GetStringValue(JsonNode? node)
{
return node is JsonValue value && value.TryGetValue<string>(out var stringValue) ? stringValue : null;
}
/// <inheritdoc />
public DetectionResult Detect(string directoryPath)
{
// Check for apphost.ts
var appHostPath = Path.Combine(directoryPath, AppHostFileName);
if (!File.Exists(appHostPath))
{
return DetectionResult.NotFound;
}
// Check for package.json (required for TypeScript/Node.js projects)
var packageJsonPath = Path.Combine(directoryPath, PackageJsonFileName);
if (!File.Exists(packageJsonPath))
{
return DetectionResult.NotFound;
}
// Note: .csproj precedence is handled by the CLI, not here.
// Language support should only check for its own language markers.
return DetectionResult.Found(LanguageId, AppHostFileName);
}
/// <inheritdoc />
public RuntimeSpec GetRuntimeSpec()
{
return new RuntimeSpec
{
Language = LanguageId,
DisplayName = LanguageDisplayName,
CodeGenLanguage = CodeGenTarget,
DetectionPatterns = s_detectionPatterns,
ExtensionLaunchCapability = "node",
InstallDependencies = new CommandSpec
{
Command = "npm",
Args = ["install"]
},
Execute = new CommandSpec
{
Command = "npx",
Args = ["--no-install", "tsx", "--tsconfig", AppHostTsConfigFileName, "{appHostFile}"]
},
WatchExecute = new CommandSpec
{
Command = "npx",
Args = [
"--no-install",
"nodemon",
"--signal", "SIGTERM",
"--watch", ".",
"--ext", "ts",
"--ignore", "node_modules/",
"--ignore", ".modules/",
"--exec", $"npx --no-install tsx --tsconfig {AppHostTsConfigFileName} {{appHostFile}}"
]
},
MigrationFiles = new Dictionary<string, string>
{
[AppHostTsConfigFileName] = AppHostTsConfigContent
}
};
}
}