Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(angular): support migrating angular cli workspaces with multiple…
… projects when keeping the angular cli layout (#9653)
  • Loading branch information
leosvelperez committed Apr 5, 2022
1 parent d1e40e0 commit 5825257
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/workspace/src/generators/init/init.spec.ts
Expand Up @@ -606,7 +606,7 @@ describe('workspace', () => {
});

const nxJson = readJson(tree, '/nx.json');
expect(nxJson.npmScope).toEqual('myproj');
expect(nxJson.npmScope).toEqual('my-app');
});

it('should create decorate-angular-cli.js', async () => {
Expand Down
78 changes: 52 additions & 26 deletions packages/workspace/src/generators/init/init.ts
@@ -1,5 +1,4 @@
import {
addDependenciesToPackageJson,
addProjectConfiguration,
convertNxGenerator,
formatFiles,
Expand Down Expand Up @@ -748,30 +747,18 @@ function checkCanConvertToWorkspace(host: Tree) {
}
}

function createNxJson(host: Tree) {
const json = JSON.parse(host.read('angular.json').toString());
const projects = json.projects || {};
const hasLibraries = Object.keys(projects).find(
(project) =>
projects[project].projectType &&
projects[project].projectType !== 'application'
);

if (Object.keys(projects).length !== 1 || hasLibraries) {
throw new Error(
`The schematic can only be used with Angular CLI workspaces with a single application.`
);
}
const name = Object.keys(projects)[0];
const tsConfigPath = getRootTsConfigPathInTree(host);
function createNxJson(host: Tree, options: Schema) {
const { newProjectRoot = '' } = readJson(host, 'angular.json');
writeJson<NxJsonConfiguration>(host, 'nx.json', {
npmScope: name,
npmScope: options.npmScope,
affected: {
defaultBase: options.defaultBase ?? deduceDefaultBase(),
},
implicitDependencies: {
'package.json': {
dependencies: '*',
devDependencies: '*',
},
[tsConfigPath]: '*',
'.eslintrc.json': '*',
},
tasksRunnerOptions: {
Expand All @@ -782,6 +769,15 @@ function createNxJson(host: Tree) {
},
},
},
targetDependencies: {
build: [
{
target: 'build',
projects: 'dependencies',
},
],
},
workspaceLayout: { appsDir: newProjectRoot, libsDir: newProjectRoot },
});
}

Expand Down Expand Up @@ -857,21 +853,51 @@ function renameDirSyncInTree(tree: Tree, from: string, to: string) {
});
}

function resolveNpmScope(tree: Tree, options: Schema): string {
let npmScope = options.npmScope ?? options.name;
if (npmScope) {
return names(npmScope).fileName;
}

// try get the scope from any library that have one
const projects = getProjects(tree);
for (const [, project] of projects) {
if (
project.projectType === 'application' ||
!tree.exists(joinPathFragments(project.root, 'package.json'))
) {
continue;
}

const { name } = readJson(
tree,
joinPathFragments(project.root, 'package.json')
);
if (name.startsWith('@')) {
return name.split('/')[0].substring(1);
}
}

// use the name (scope if exists) in the root package.json
const { name } = readJson(tree, 'package.json');
return name.startsWith('@') ? name.split('/')[0].substring(1) : name;
}

export async function initGenerator(tree: Tree, schema: Schema) {
const options = { ...schema, npmScope: resolveNpmScope(tree, schema) };

if (schema.preserveAngularCliLayout) {
updateJson(tree, 'package.json', (json) => {
delete json.dependencies?.['@nrwl/workspace'];
json.devDependencies = {
...json.devDependencies,
'@nrwl/workspace': nxVersion,
};
return json;
});
addDependenciesToPackageJson(tree, {}, { '@nrwl/workspace': nxVersion });
createNxJson(tree);
createNxJson(tree, options);
decorateAngularClI(tree);
} else {
const options = {
...schema,
npmScope: names(schema.npmScope || schema.name).fileName,
};

checkCanConvertToWorkspace(tree);
moveExistingFiles(tree, options);
addFiles(tree);
Expand Down

0 comments on commit 5825257

Please sign in to comment.