@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
115 lines • 4.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const devkit_1 = require("@nx/devkit");
const path_1 = require("path");
async function getCurrentProjectConfiguration(projectName) {
return await (await (0, devkit_1.createProjectGraphAsync)()).nodes[projectName].data;
}
async function normalizeOptions(tree, options) {
const { appsDir, libsDir } = (0, devkit_1.getWorkspaceLayout)(tree);
const { root: currentRoot } = await getCurrentProjectConfiguration(options.projectName);
let destinationRoot = options.destination;
if (!options.relativeToRoot) {
if (currentRoot.startsWith(appsDir)) {
destinationRoot = (0, devkit_1.joinPathFragments)(appsDir, options.destination.replace(new RegExp(`^${appsDir}`), ''));
}
else if (currentRoot.startsWith(libsDir)) {
destinationRoot = (0, devkit_1.joinPathFragments)(libsDir, options.destination.replace(new RegExp(`^${libsDir}`), ''));
}
}
return {
currentRoot,
destinationRoot,
currentProject: options.projectName,
destinationProject: (0, devkit_1.names)(options.destination).fileName.replace(/[\\|/]/g, '-'),
};
}
async function default_1(tree, options) {
const normalizedOptions = await normalizeOptions(tree, options);
const writeNewProjectJson = updateProjectJson(tree, normalizedOptions);
renameDirectory(tree, normalizedOptions.currentRoot, normalizedOptions.destinationRoot);
writeNewProjectJson();
updateXmlReferences(tree, normalizedOptions);
await (0, devkit_1.formatFiles)(tree);
}
function updateProjectJson(tree, normalizedOptions) {
try {
const config = (0, devkit_1.readProjectConfiguration)(tree, normalizedOptions.currentProject);
config.root = normalizedOptions.destinationRoot;
config.name = normalizedOptions.destinationProject;
(0, devkit_1.removeProjectConfiguration)(tree, normalizedOptions.currentProject);
return () => {
(0, devkit_1.addProjectConfiguration)(tree, normalizedOptions.destinationProject, transformConfiguration(tree, config, normalizedOptions));
};
}
catch {
// There was no project.json, so dont add one.
return () => {
/* its fine */
};
}
}
function transformConfiguration(tree, config, options) {
const copy = updateReferencesInObject(config, options);
updateSchemaPath(tree, copy, config.root);
return copy;
}
function updateSchemaPath(tree, config, projectRoot) {
const relativeToRoot = (0, devkit_1.offsetFromRoot)(projectRoot);
config.$schema = config.$schema?.replace(/^.*\/node_modules/, (0, devkit_1.joinPathFragments)(relativeToRoot, 'node_modules'));
}
function updateReferencesInObject(object, options) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const newValue = Array.isArray(object)
? []
: {};
for (const key in object) {
if (typeof object[key] === 'string') {
newValue[key] = object[key].replace(options.currentRoot, options.destinationRoot);
}
else if (typeof object[key] === 'object') {
newValue[key] = updateReferencesInObject(object[key], options);
}
else {
newValue[key] = object[key];
}
}
return newValue;
}
function updateXmlReferences(tree, options) {
(0, devkit_1.visitNotIgnoredFiles)(tree, '.', (path) => {
const extension = (0, path_1.extname)(path);
const directory = (0, path_1.dirname)(path);
if (['.csproj', '.vbproj', '.fsproj', '.sln'].includes(extension)) {
const contents = tree.read(path);
if (!contents) {
return;
}
const pathToUpdate = (0, devkit_1.normalizePath)((0, path_1.relative)(directory, options.currentRoot));
const pathToUpdateWithWindowsSeparators = (0, devkit_1.normalizePath)((0, path_1.relative)(directory, options.currentRoot)).replaceAll('/', '\\');
const newPath = (0, devkit_1.normalizePath)((0, path_1.relative)(directory, options.destinationRoot));
tree.write(path, contents
.toString()
.replaceAll(pathToUpdate, newPath)
.replaceAll(pathToUpdateWithWindowsSeparators, newPath));
}
});
}
function renameDirectory(tree, from, to) {
const children = tree.children(from);
for (const child of children) {
const childFrom = (0, devkit_1.joinPathFragments)(from, child);
const childTo = (0, devkit_1.joinPathFragments)(to, child);
if (tree.isFile(childFrom)) {
tree.rename(childFrom, childTo);
}
else {
renameDirectory(tree, childFrom, childTo);
}
}
if (!to.startsWith(from)) {
tree.delete(from);
}
}
//# sourceMappingURL=generator.js.map