@nx-dotnet/core
Version:
- Have an existing nx workspace. For creating this, see [nrwl's documentation](https://nx.dev/latest/angular/getting-started/nx-setup). - .NET SDK is installed, and `dotnet` is available on the path. For help on this, see [Microsoft's documentation](https
88 lines • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SWAGGER_CLI_TOOL = void 0;
exports.default = runExecutor;
const devkit_1 = require("@nx/devkit");
const path_1 = require("path");
const dotnet_1 = require("@nx-dotnet/dotnet");
const utils_1 = require("@nx-dotnet/utils");
const get_path_to_startup_assembly_1 = require("../../generators/utils/get-path-to-startup-assembly");
const fs_1 = require("fs");
const child_process_1 = require("child_process");
exports.SWAGGER_CLI_TOOL = 'Swashbuckle.AspNetCore.Cli';
function normalizeOptions(opts, project, csProjFilePath, projectName) {
return {
output: (0, path_1.resolve)(devkit_1.workspaceRoot, opts.output ?? `dist/swagger/${project.root}/swagger.json`),
startupAssembly: opts.startupAssembly
? (0, path_1.resolve)(devkit_1.workspaceRoot, opts.startupAssembly)
: (0, path_1.resolve)((0, get_path_to_startup_assembly_1.buildStartupAssemblyPath)(projectName, project, csProjFilePath)),
swaggerDoc: opts.swaggerDoc ?? 'v1',
skipInstall: opts.skipInstall ?? false,
};
}
async function readSwashbuckleVersion(projectFilePath) {
const configuredVersion = (0, utils_1.readConfig)().nugetPackages?.['Swashbuckle.AspNetCore'];
if (configuredVersion && configuredVersion !== 'ALLOW_MISMATCH') {
return configuredVersion;
}
const xml = (0, utils_1.readXml)(projectFilePath);
let v;
await (0, utils_1.iterateChildrenByPath)(xml, 'ItemGroup.PackageReference', async (reference) => {
const pkg = reference.attr['Include'];
const version = reference.attr['Version'];
if (pkg === 'Swashbuckle.AspNetCore') {
v = version;
}
});
if (!v) {
throw new Error('Unable to resolve Swashbuckle.AspNetCore for ' + projectFilePath);
}
return v;
}
async function runExecutor(schema, context, dotnetClient = new dotnet_1.DotNetClient((0, dotnet_1.dotnetFactory)(), devkit_1.workspaceRoot)) {
const nxProjectConfiguration = (0, utils_1.getExecutedProjectConfiguration)(context);
const csProjFilePath = await (0, utils_1.getProjectFileForNxProject)(nxProjectConfiguration);
const projectDirectory = (0, path_1.resolve)(devkit_1.workspaceRoot, nxProjectConfiguration.root);
dotnetClient.cwd = projectDirectory;
const options = normalizeOptions(schema, nxProjectConfiguration, csProjFilePath, context.projectName);
const outputDirectory = (0, path_1.dirname)(options.output);
if (!(0, fs_1.existsSync)(outputDirectory)) {
(0, fs_1.mkdirSync)(outputDirectory, { recursive: true });
}
if (!options.skipInstall) {
ensureSwaggerToolInstalled(context, dotnetClient, await readSwashbuckleVersion(csProjFilePath));
}
dotnetClient.runTool('swagger', [
'tofile',
'--output',
options.output,
options.startupAssembly,
options.swaggerDoc,
]);
try {
const isInstalled = require.resolve('prettier');
if (isInstalled) {
(0, child_process_1.execSync)(`npx -y prettier --write ${options.output}`, {
windowsHide: true,
});
}
}
catch {
// Its not a huge deal if prettier isn't installed or fails...
// We'll just leave the file as is and let the user decide what to do.
}
return {
success: true,
};
}
function ensureSwaggerToolInstalled(context, dotnetClient, version) {
const installedSwaggerVersion = (0, utils_1.readInstalledDotnetToolVersion)(exports.SWAGGER_CLI_TOOL);
if (installedSwaggerVersion) {
if (installedSwaggerVersion === version) {
return;
}
devkit_1.logger.warn(`Swagger CLI was found, but the version "${installedSwaggerVersion}" does not match the expected version "${version}" of Swashbuckle.AspNetCore in ${context.projectName}. We reinstalled it such that the version matches, but you may want to review the changes made.`);
}
dotnetClient.installTool(exports.SWAGGER_CLI_TOOL, version);
}
//# sourceMappingURL=executor.js.map