Skip to content

Commit

Permalink
fix(core): automatically add root to the project.json projects
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Apr 24, 2022
1 parent 8c38b86 commit f528315
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ Object {

exports[`workspace move to nx layout should update project configuration 1`] = `
Object {
"root": "apps/myApp",
"sourceRoot": "apps/myApp/src",
"targets": Object {
"build": Object {
Expand Down
8 changes: 4 additions & 4 deletions packages/devkit/src/generators/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@ function inlineProjectConfigurationsWithTree(
Object.entries(workspaceJson.projects || {}).forEach(([project, config]) => {
if (typeof config === 'string') {
const configFileLocation = joinPathFragments(config, 'project.json');
workspaceJson.projects[project] = readJson<ProjectConfiguration>(
tree,
configFileLocation
);
workspaceJson.projects[project] = {
root: config,
...readJson<ProjectConfiguration>(tree, configFileLocation),
};
}
});
return workspaceJson as WorkspaceJsonConfiguration;
Expand Down
18 changes: 11 additions & 7 deletions packages/devkit/src/utils/invoke-nx-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FileChange, Tree, TreeWriteOptions } from 'nx/src/config/tree';
import { toNewFormat, toOldFormatOrNull } from 'nx/src/config/workspaces';
import { Generator, GeneratorCallback } from 'nx/src/config/misc-interfaces';
import { parseJson, serializeJson } from 'nx/src/utils/json';
import { join, relative } from 'path';
import { join, relative, dirname } from 'path';

class RunCallbackTask {
constructor(private callback: GeneratorCallback) {}
Expand Down Expand Up @@ -167,9 +167,12 @@ class DevkitTreeFromAngularDevkitTree implements Tree {
? this.tree.read(filePath).toString(encoding)
: this.tree.read(filePath);
if (isWorkspaceJsonChange(filePath)) {
const formatted = toNewFormat(
parseJson(Buffer.isBuffer(rawResult) ? rawResult.toString() : rawResult)
);
const formatted = toNewFormat({
root: dirname(filePath),
...parseJson(
Buffer.isBuffer(rawResult) ? rawResult.toString() : rawResult
),
});
return encoding ? serializeJson(formatted) : serializeJson(formatted);
}
return rawResult;
Expand All @@ -192,9 +195,10 @@ class DevkitTreeFromAngularDevkitTree implements Tree {
const w = parseJson(content.toString());
for (const [project, configuration] of Object.entries(w.projects)) {
if (typeof configuration === 'string') {
w.projects[project] = parseJson(
this.tree.read(`${configuration}/project.json`)
);
w.projects[project] = {
root: configuration,
...parseJson(this.tree.read(`${configuration}/project.json`)),
};
w.projects[project].configFilePath = `${configuration}/project.json`;
}
}
Expand Down
8 changes: 5 additions & 3 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ export class NxScopedHost extends virtualFs.ScopedHost<any> {
map((x) => ({
project,
projectConfig: {
root: dirname(configFilePath),
...parseJson(Buffer.from(x).toString()),
configFilePath,
},
Expand Down Expand Up @@ -600,9 +601,10 @@ export class NxScopeHostUsedForWrappedSchematics extends NxScopedHost {
const nxJsonInTree = nxJsonChange
? parseJson(nxJsonChange.content.toString())
: parseJson(this.host.read('nx.json').toString());
const readJsonWithHost = (file) =>
parseJson(this.host.read(file).toString());

const readJsonWithHost = (file) => ({
root: dirname(file),
...parseJson(this.host.read(file).toString()),
});
const staticProjects = buildWorkspaceConfigurationFromGlobs(
nxJsonInTree,
globForProjectFiles(this.host.root).filter(
Expand Down
3 changes: 3 additions & 0 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,9 @@ export function buildWorkspaceConfigurationFromGlobs(
// directory as a package.json should overwrite the inferred package.json
// project configuration.
const configuration = readJson(file);

configuration.root = directory;

let name = configuration.name;
if (!configuration.name) {
name = toProjectName(file, nxJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('convert-to-nx-project', () => {
getProjectConfigurationPath(config)
);

delete config.root;
expect(config).toEqual(newConfigFile);
});

Expand Down Expand Up @@ -112,6 +113,7 @@ describe('convert-to-nx-project', () => {
tree,
getProjectConfigurationPath(config)
);
delete config.root;
expect(config).toEqual(newConfigFile);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ To upgrade change the version number at the top of ${getWorkspacePath(
continue;
}

delete configuration.root;

writeJson(host, configPath, configuration);

updateJson(host, getWorkspacePath(host), (value) => {
Expand Down

0 comments on commit f528315

Please sign in to comment.