UNPKG

@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

96 lines 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initGenerator = initGenerator; const devkit_1 = require("@nx/devkit"); const dotnet_1 = require("@nx-dotnet/dotnet"); const utils_1 = require("@nx-dotnet/utils"); const path = require("path"); const semver_1 = require("semver"); const dotnet_new_1 = require("../utils/dotnet-new"); const noop = () => void 0; async function initGenerator(host, _, // Nx will populate this with options, which are currently unused. dotnetClient = new dotnet_1.DotNetClient((0, dotnet_1.dotnetFactory)())) { const tasks = []; // Prior to Nx 17, nx-dotnet had a custom config file. if ((0, semver_1.major)(devkit_1.NX_VERSION) < 17) { const configObject = host.isFile(utils_1.CONFIG_FILE_PATH) ? (0, devkit_1.readJson)(host, utils_1.CONFIG_FILE_PATH) : { nugetPackages: {}, }; configObject.nugetPackages = configObject.nugetPackages || {}; host.write(utils_1.CONFIG_FILE_PATH, JSON.stringify(configObject, null, 2)); } const nxJson = (0, devkit_1.readNxJson)(host); // Adds a `dotnet restore` operation to the prepare script. addPrepareScript(host); // Adds @nx-dotnet/core to nxJson updateNxJson(host, nxJson); // Setups up the .config/dotnet-tools.json for managing local .NET tools. initToolManifest(host, dotnetClient); // Creates Directory.Build.* to customize default C# builds. initBuildCustomization(host); // Adds @nx/js to package.json tasks.push(installNpmPackages(host)); return async () => { for (const task of tasks) { await task(); } }; } exports.default = initGenerator; function installNpmPackages(host) { if (host.exists('package.json')) { return (0, devkit_1.addDependenciesToPackageJson)(host, {}, { '@nx/js': devkit_1.NX_VERSION, }); } else { return noop; } } function hasPluginInNxJson(nxJson) { return !!nxJson?.plugins?.some((x) => { const plugin = typeof x === 'string' ? x : x.plugin; return plugin === '@nx-dotnet/core'; }); } function updateNxJson(host, nxJson) { if (nxJson && !hasPluginInNxJson(nxJson)) { nxJson.plugins = nxJson.plugins || []; nxJson.plugins.push('@nx-dotnet/core'); (0, devkit_1.writeJson)(host, 'nx.json', nxJson); } } function initToolManifest(host, dotnetClient) { const initialized = host.exists('.config/dotnet-tools.json'); if (!initialized) { devkit_1.logger.log('Tool Manifest created for managing local .NET tools'); (0, dotnet_new_1.runDotnetNew)(host, dotnetClient, 'tool-manifest', {}); } } function addPrepareScript(host) { if (host.exists('package.json')) { const packageJson = (0, devkit_1.readJson)(host, 'package.json'); const prepareSteps = packageJson.scripts?.prepare?.split('&&').map((x) => x.trim()) ?? []; const restoreScript = 'nx g @nx-dotnet/core:restore'; if (!prepareSteps.includes(restoreScript)) { prepareSteps.push(restoreScript); } packageJson.scripts ?? (packageJson.scripts = {}); packageJson.scripts.prepare = prepareSteps.join(' && '); (0, devkit_1.writeJson)(host, 'package.json', packageJson); } } function initBuildCustomization(host) { const initialized = host.exists('Directory.Build.props'); if (!initialized) { const checkModuleBoundariesScriptPath = (0, devkit_1.normalizePath)(path.relative(host.root, (0, utils_1.resolve)('@nx-dotnet/core/src/tasks/check-module-boundaries'))); (0, devkit_1.generateFiles)(host, path.join(__dirname, 'templates/root'), '.', { tmpl: '', checkModuleBoundariesScriptPath, }); } } //# sourceMappingURL=generator.js.map