Skip to content

Optimize NSwag NPM Script nswag.js #5231

@KaterSchnurz

Description

@KaterSchnurz

The nswag.js logic passes the argument /runtime:* to the executable or dll which causes an errormessage like that:

"Unrecognised arguments are present: [/runtime:Net80]"

NSwag NPM Version 11.5.1 is installed and used in VS Code on Windows 11

.Net Runtime 8 und 9 are present, also .Framework 4.6.*

Just run nswag run /runtime:Net80 (or any other supported variant)

As the .Net Framework Version and the .NetCore Version of Nswag does not like this parameter, it should not be passed, and only used to decide which Version should be used.

i modified this script to work like i would expect it to work.

Maybe consider to include that.

#!/usr/bin/env node

"use strict";

 

var defaultCoreVersion = "Net80";

var supportedCoreVersions = [

    { ver: '8.0', dir: "Net80", },

    { ver: '9.0', dir: "Net90", },

];

 

const path = require('path');

var c = require('child_process');

 

// Initialize

process.title = 'nswag';

console.log("NSwag NPM CLI");

 

let args = process.argv.slice(2);

 

// Legacy support

args = args.map(arg => arg === '--x86' ? '/runtime:WinX86' : arg);

args = args.map(arg => arg === '--core' ? '/runtime:' + defaultCoreVersion : arg);

args = args.map(arg => arg === '--core 8.0' ? '/runtime:Net80' : arg);

args = args.map(arg => arg === '--core 9.0' ? '/runtime:Net90' : arg);

 

// Remove /runtime:* parameter from args, but remember its value

let runtimeIndices = [];

let runtimeValue = null;

args.forEach((arg, idx) => {

    const match = arg.match(/^\/runtime:(.+)$/i);

    if (match) {

        runtimeIndices.push(idx);

        runtimeValue = match[1];

    }

});

if (runtimeIndices.length > 1) {

    console.error("Error: Multiple /runtime:* arguments detected. Please specify only one. Maybe remove the legacy --core argument?");

    process.exit(1);

} else if (runtimeIndices.length === 1) {

    args.splice(runtimeIndices[0], 1);

}

 

if (runtimeValue) {

    if (runtimeValue.toLowerCase() === "netcore") {

        // detect latest installed NetCore Version

        console.debug("Trying to detect latest installed NetCore Version.");

        var infoCmd = "dotnet --version";

        try {

            var stdout = c.execSync(infoCmd, { encoding: 'utf8' });

            var coreVersion = stdout.trim();

            const version = supportedCoreVersions.find(v => coreVersion.startsWith(v.ver));

            if (!version) {

                console.error("Error: Detected .NET Core version '" + coreVersion + "' is not supported.");

                process.exit(1);

            }

            console.info("Using supported .NET Core version: " + version.dir);

            runtimeValue = version.dir;

        } catch (error) {

            console.error("Error: Could not detect .NET Core version.");

            console.debug(error);

            process.exit(1);

        }

    }

 

    if(!runtimeValue.toLowerCase().startsWith("win")) {

        const isSupported = supportedCoreVersions.some(v => v.dir.toLowerCase() === runtimeValue.toLowerCase());

        if (!isSupported) {

            console.error("Error: Unsupported /runtime: argument '" + runtimeValue + "'.");

            process.exit(1);

        }

    }

}

 

var hasFullDotNet = false;

if (runtimeValue && runtimeValue.toLowerCase().startsWith("win")) {

    // Search for full .NET installation

    var fs = require('fs');

    if (process.env["windir"]) {

        try {

            var stats = fs.lstatSync(process.env["windir"] + '/Microsoft.NET');

            if (stats.isDirectory())

            {

                hasFullDotNet = true;

            }

        }

        catch (e) {

            console.log("Could not verify the presence of the full .NET Framework installation.");

        }

    }

}

 

let childResult = null;

 

if (hasFullDotNet && runtimeValue && runtimeValue.toLowerCase().startsWith("win")) {

    // Run full .NET version

    if (runtimeValue.toLowerCase() === "winx86") {

        var exePath = path.join(__dirname, 'binaries', 'Win', 'nswag.x86.exe');

    } else {

        var exePath = path.join(__dirname, 'binaries', 'Win', 'nswag.exe');

    }

    try {

        childResult = c.spawnSync(exePath, args, { stdio: 'inherit' });

    } catch (error) {

        if (error.status !== undefined) {

            process.exit(error.status);

        } else {

            console.error(error);

            process.exit(1);

        }

    }

} else {

    // No Runtime specified or full .Net Installation not found, run default command

    if (!runtimeValue || runtimeValue.toLowerCase().startsWith("win")) {

        runtimeValue = defaultCoreVersion;

    }

 

    console.log("Using runtime: " + runtimeValue);

    var coreCmd = 'dotnet "' + path.join(__dirname, 'binaries', runtimeValue, 'dotnet-nswag.dll') + '" ';

 

    if (args.length > 0) {

        coreCmd += args.join(' ');

    }

 

    try {

        childResult = c.execSync(coreCmd, { stdio: [0, 1, 2] });

 

    } catch (error) {

        if (typeof error.status === 'number') {

            process.exit(error.status);

        } else {

            console.error(error);

            process.exit(1);

        }

    }

}

 

console.log("NSwag NPM CLI execution completed.");

 

// Exit with the same exit code as the child process

if (childResult && typeof childResult.status === 'number') {

    process.exit(childResult.status);

}

// End of script

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions