|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | | -import type { LoadBootResourceCallback, JsModuleExports, JsAsset, AssemblyAsset, PdbAsset, WasmAsset, IcuAsset, EmscriptenModuleInternal } from "./types"; |
| 4 | +import type { LoadBootResourceCallback, JsModuleExports, JsAsset, AssemblyAsset, PdbAsset, WasmAsset, IcuAsset, EmscriptenModuleInternal, LoaderConfig, DotnetHostBuilder } from "./types"; |
5 | 5 |
|
6 | 6 | import { dotnetAssert, dotnetGetInternals, dotnetBrowserHostExports, dotnetUpdateInternals } from "./cross-module"; |
7 | 7 | import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL } from "./per-module"; |
8 | 8 | import { getLoaderConfig } from "./config"; |
9 | 9 | import { BrowserHost_InitializeCoreCLR } from "./run"; |
10 | 10 | import { createPromiseController } from "./promise-controller"; |
| 11 | +import { node_fs, node_url } from "./polyfills"; |
11 | 12 |
|
12 | 13 | const scriptUrlQuery = /*! webpackIgnore: true */import.meta.url; |
13 | 14 | const queryIndex = scriptUrlQuery.indexOf("?"); |
@@ -143,3 +144,49 @@ function isPathAbsolute(path: string): boolean { |
143 | 144 | // windows http://C:/x.json |
144 | 145 | return protocolRx.test(path); |
145 | 146 | } |
| 147 | + |
| 148 | +export function isShellHosted(): boolean { |
| 149 | + return ENVIRONMENT_IS_SHELL && typeof (globalThis as any).arguments !== "undefined"; |
| 150 | +} |
| 151 | + |
| 152 | +export function isNodeHosted(): boolean { |
| 153 | + if (!ENVIRONMENT_IS_NODE || globalThis.process.argv.length < 3) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + const argv1 = globalThis.process.argv[1].toLowerCase(); |
| 157 | + const argScript = normalizeFileUrl("file:///" + locateFile(argv1)); |
| 158 | + const importScript = normalizeFileUrl(locateFile(scriptUrl.toLowerCase())); |
| 159 | + |
| 160 | + return argScript === importScript; |
| 161 | +} |
| 162 | + |
| 163 | +export async function findResources(dotnet: DotnetHostBuilder): Promise<void> { |
| 164 | + if (!ENVIRONMENT_IS_NODE) { |
| 165 | + return; |
| 166 | + } |
| 167 | + const fs = await node_fs(); |
| 168 | + const url = await node_url(); |
| 169 | + const scriptDir = url.fileURLToPath(scriptDirectory).replace(windowsAbsoluteRx, "/").replace(/\\/g, "/"); |
| 170 | + const files: string[] = await fs.promises.readdir(scriptDir); |
| 171 | + const assemblies = files |
| 172 | + // TODO-WASM: webCIL |
| 173 | + .filter(file => file.endsWith(".dll")) |
| 174 | + .map(filename => { |
| 175 | + // filename without path |
| 176 | + const name = filename.substring(filename.lastIndexOf("/") + 1); |
| 177 | + return { virtualPath: scriptDir + filename, name }; |
| 178 | + }); |
| 179 | + const config: LoaderConfig = { |
| 180 | + mainAssemblyName: globalThis.process.argv[2], |
| 181 | + virtualWorkingDirectory: scriptDir, |
| 182 | + resources: { |
| 183 | + jsModuleNative: [{ name: "dotnet.native.js" }], |
| 184 | + jsModuleRuntime: [{ name: "dotnet.runtime.js" }], |
| 185 | + wasmNative: [{ name: "dotnet.native.wasm", }], |
| 186 | + coreAssembly: [{ virtualPath: scriptDir + "System.Private.CoreLib.dll", name: "System.Private.CoreLib.dll" },], |
| 187 | + assembly: assemblies, |
| 188 | + } |
| 189 | + }; |
| 190 | + dotnet.withConfig(config); |
| 191 | + dotnet.withApplicationArguments(...globalThis.process.argv.slice(3)); |
| 192 | +} |
0 commit comments