Skip to content

Commit eed636d

Browse files
committed
run tscCheck on preprocessed templates
test both browser and node platform variants
1 parent c3c3424 commit eed636d

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Plugins/PackageToJS/Tests/TemplatesTests.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,47 @@ import Foundation
99
.appendingPathComponent("Templates")
1010

1111
/// `npx tsc -p Templates/tsconfig.json`
12-
@Test func tscCheck() throws {
12+
/// Test both node and browser platform variants
13+
@Test(arguments: ["node", "browser"])
14+
func tscCheck(platform: String) throws {
15+
// Create a temporary directory for preprocessed files
16+
let tempDir = FileManager.default.temporaryDirectory
17+
.appendingPathComponent("JavaScriptKit-tsc-\(platform)-\(UUID().uuidString)")
18+
defer {
19+
try? FileManager.default.removeItem(at: tempDir)
20+
}
21+
22+
// Copy entire templates folder to temp location
23+
try FileManager.default.copyItem(at: Self.templatesPath, to: tempDir)
24+
25+
// Setup preprocessing conditions
26+
let conditions: [String: Bool] = [
27+
"USE_SHARED_MEMORY": false,
28+
"IS_WASI": true,
29+
"USE_WASI_CDN": false,
30+
"HAS_BRIDGE": false,
31+
"HAS_IMPORTS": platform == "browser",
32+
"TARGET_PLATFORM_NODE": platform == "node",
33+
]
34+
let preprocessOptions = PreprocessOptions(conditions: conditions, substitutions: [:])
35+
36+
// Preprocess all JS and TS files in-place
37+
let enumerator = FileManager.default.enumerator(at: tempDir, includingPropertiesForKeys: nil)
38+
while let fileURL = enumerator?.nextObject() as? URL {
39+
guard !fileURL.hasDirectoryPath,
40+
fileURL.pathExtension == "js" || fileURL.pathExtension == "ts" else {
41+
continue
42+
}
43+
44+
let content = try String(contentsOf: fileURL, encoding: .utf8)
45+
let preprocessed = try preprocess(source: content, file: fileURL.path, options: preprocessOptions)
46+
try preprocessed.write(to: fileURL, atomically: true, encoding: .utf8)
47+
}
48+
49+
// Run TypeScript on the preprocessed files
1350
let tsc = Process()
1451
tsc.executableURL = try which("npx")
15-
tsc.arguments = ["tsc", "-p", Self.templatesPath.appending(path: "tsconfig.json").path]
52+
tsc.arguments = ["tsc", "-p", tempDir.appending(path: "tsconfig.json").path]
1653
try tsc.run()
1754
tsc.waitUntilExit()
1855
#expect(tsc.terminationStatus == 0)

0 commit comments

Comments
 (0)