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

121 lines 5.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runDotnetAddProjectToSolution = runDotnetAddProjectToSolution; exports.runDotnetAddProjectReference = runDotnetAddProjectReference; const utils_1 = require("@nx-dotnet/utils"); const fs_1 = require("fs"); const path_1 = require("path"); function runDotnetAddProjectToSolution(tree, dotnetClient, projectRoot, solutionFile) { const relativePath = (0, path_1.relative)(dotnetClient.cwd ?? tree.root, tree.root); const projectFile = (0, utils_1.findProjectFileInPathSync)(projectRoot, tree); const cleanupFns = [ // Restore the original contents of the solution file if it existed. writeFileFromTree(tree, solutionFile), // Writes the file to disk from the tree, and returns a cleanup function. writeFileFromTree(tree, projectFile), ]; dotnetClient.addProjectToSolution((0, path_1.join)(relativePath, solutionFile), (0, path_1.join)(relativePath, projectRoot)); const updatedContents = (0, fs_1.readFileSync)(solutionFile); for (const cleanup of cleanupFns) { cleanup(); } // Writing to the tree **after** cleaning up on-disk files // ensures that the tree changes think its a file creation rather // than a file update. tree.write(solutionFile, updatedContents); } function runDotnetAddProjectReference(tree, hostCsProj, targetCsProj, dotnetClient) { const cleanupFns = hostCsProj === targetCsProj ? [writeFileFromTree(tree, hostCsProj)] : [ writeFileFromTree(tree, hostCsProj), writeFileFromTree(tree, targetCsProj), ]; dotnetClient.addProjectReference(hostCsProj, targetCsProj); const updatedContents = (0, fs_1.readFileSync)((0, path_1.join)(tree.root, hostCsProj)).toString(); for (const cleanup of cleanupFns) { cleanup(); } // Writing to the tree **after** cleaning up on-disk files // ensures that the tree changes think its a file creation rather // than a file update. tree.write(hostCsProj, updatedContents); } function writeFileFromTree(tree, path) { const treeContents = tree.read(path); const diskPath = (0, path_1.join)(tree.root, path); // Restore original contents of the file if it existed when // calling cleanup functions. const onDiskContents = tryReadDiskOrNull(diskPath); const cleanupFns = []; if (treeContents) { if (!(0, fs_1.existsSync)(tree.root)) { (0, fs_1.mkdirSync)(tree.root); cleanupFns.push([ 'remove ' + tree.root, () => { (0, fs_1.rmdirSync)(tree.root); }, ]); } const pathSegments = path.split('/').slice(0, -1); let dirPath = tree.root; while (pathSegments.length) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion dirPath = (0, path_1.join)(dirPath, pathSegments.shift()); if (!(0, fs_1.existsSync)(dirPath)) { (0, fs_1.mkdirSync)(dirPath); cleanupFns.push([ 'remove ' + dirPath, () => { try { (0, fs_1.rmdirSync)(dirPath); } catch (e) { // If the directory is not empty, this will fail. // This is especially likely if two files from different subdirectories // are written into the same directory. e.g. (apps/my-app and apps/other-app). // In this case, if `apps` didn't exist before the first file was written, // we'll try to remove it when running the cleanup functions for the first file. // It'll fail, because the `apps/other-app` directory will still exist. This is fine. } }, ]); } } (0, fs_1.writeFileSync)(diskPath, treeContents); } else { console.log('No contents found in tree for', path, tree.root); } cleanupFns.push([ onDiskContents ? 'restore ' + diskPath : 'remove ' + diskPath, () => { if (onDiskContents) { (0, fs_1.writeFileSync)(diskPath, onDiskContents); } else { (0, fs_1.rmSync)(diskPath); } }, ]); return () => { cleanupFns.reverse(); for (const [label, cleanup] of cleanupFns) { if (process.env.NX_VERBOSE_LOGGING === 'true') { console.log('nx-dotnet cleanup:', label); } cleanup(); } }; } function tryReadDiskOrNull(path) { try { const filePath = (0, path_1.join)(path); return (0, fs_1.readFileSync)(filePath); } catch { return null; } } //# sourceMappingURL=dotnet-add.js.map