Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support nx.json extends property #1124

Merged
merged 3 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/vscode/src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@
{
"when": "!isNxWorkspace && isAngularWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "ng.generate.ui.fileexplorer",
"group": "explorerContext"
"group": "2_workspace"
},
{
"when": "!isNxWorkspace && isAngularWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "ng.run.fileexplorer",
"group": "explorerContext"
"group": "2_workspace"
},
{
"when": "isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "nx.generate.ui.fileexplorer",
"group": "explorerContext"
"group": "2_workspace"
},
{
"when": "isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "nx.run.fileexplorer",
"group": "explorerContext"
"group": "2_workspace"
}
],
"view/title": [
Expand Down
2 changes: 2 additions & 0 deletions libs/vscode/nx-workspace/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './lib/find-workspace-json-target';
export * from './lib/reveal-workspace-json';
export * from './lib/workspace-codelens-provider';
export * from './lib/verify-workspace';
export * from './lib/get-nx-config';
export * from './lib/get-nx-workspace-config';
21 changes: 21 additions & 0 deletions libs/vscode/nx-workspace/src/lib/get-nx-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { cacheJson, readAndCacheJsonFile } from '@nx-console/server';
import { NxJsonConfiguration } from '@nrwl/devkit';
import { join } from 'path';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';

export function getNxConfig(baseDir: string): NxJsonConfiguration {
try {
let cachedNxJson = cacheJson('nx.json', baseDir).json;

if (!cachedNxJson) {
const nxJson = getNxWorkspacePackageFileUtils().readNxJson(
join(baseDir, 'nx.json')
);

cachedNxJson = cacheJson('nx.json', baseDir, nxJson).json;
}
return cachedNxJson;
} catch (e) {
return readAndCacheJsonFile('nx.json', baseDir).json;
}
}
23 changes: 23 additions & 0 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readAndCacheJsonFile, cacheJson } from '@nx-console/server';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';
import type { WorkspaceJsonConfiguration } from '@nrwl/devkit';

export function getNxWorkspaceConfig(
basedir: string,
workspaceJsonPath: string
): WorkspaceJsonConfiguration {
// try and use the workspace version of nx
try {
let cachedWorkspaceJson = cacheJson(workspaceJsonPath).json;
if (!cachedWorkspaceJson) {
const workspace = getNxWorkspacePackageFileUtils().readWorkspaceConfig({
format: 'nx',
path: basedir,
} as any);
cachedWorkspaceJson = cacheJson(workspaceJsonPath, '', workspace).json;
}
return cachedWorkspaceJson;
} catch (e) {
return readAndCacheJsonFile(workspaceJsonPath).json;
}
}
33 changes: 6 additions & 27 deletions libs/vscode/nx-workspace/src/lib/verify-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import {
fileExistsSync,
getOutputChannel,
getTelemetry,
cacheJson,
readAndCacheJsonFile,
toWorkspaceFormat,
} from '@nx-console/server';
import { window } from 'vscode';
import { dirname, join } from 'path';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { getNxWorkspaceConfig } from './get-nx-workspace-config';

export function verifyWorkspace(): {
validWorkspaceJson: boolean;
Expand All @@ -27,24 +25,21 @@ export function verifyWorkspace(): {

try {
if (fileExistsSync(workspaceJsonPath)) {
readNxWorkspaceConfig(workspacePath, workspaceJsonPath);
return {
validWorkspaceJson: true,
// TODO(cammisuli): change all instances to use the new version - basically reverse this to the new format
json: toWorkspaceFormat(
/**
* We would get the value from the `readWorkspaceConfig` call if that was successful.
* Otherwise, we manually read the workspace.json file
*/
readAndCacheJsonFile(workspaceJsonPath).json
getNxWorkspaceConfig(workspacePath, angularJsonPath)
),
workspaceType: 'nx',
configurationFilePath: workspaceJsonPath,
};
} else if (fileExistsSync(angularJsonPath)) {
readNxWorkspaceConfig(workspacePath, angularJsonPath);
return {
validWorkspaceJson: true,
json: toWorkspaceFormat(readAndCacheJsonFile(angularJsonPath).json),
json: toWorkspaceFormat(
getNxWorkspaceConfig(workspacePath, angularJsonPath)
),
workspaceType: 'ng',
configurationFilePath: angularJsonPath,
};
Expand Down Expand Up @@ -79,19 +74,3 @@ export function verifyWorkspace(): {
};
}
}

function readNxWorkspaceConfig(basedir: string, workspaceJsonPath: string) {
// try and use the workspace version of nx
try {
const cachedWorkspaceJson = cacheJson(workspaceJsonPath).json;
if (!cachedWorkspaceJson) {
const workspace = getNxWorkspacePackageFileUtils().readWorkspaceConfig({
format: 'nx',
path: basedir,
} as any);
cacheJson(workspaceJsonPath, '', workspace);
}
} catch (e) {
// noop - will use the old way
}
}
10 changes: 4 additions & 6 deletions libs/vscode/webview/src/lib/get-task-execution-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { Option, TaskExecutionSchema } from '@nx-console/schema';
import {
getOutputChannel,
getTelemetry,
readAndCacheJsonFile,
readTargetDef,
selectSchematic,
} from '@nx-console/server';
import { verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { getNxConfig, verifyWorkspace } from '@nx-console/vscode/nx-workspace';
import { verifyBuilderDefinition } from '@nx-console/vscode/verify';
import { Uri, window } from 'vscode';
import { WorkspaceRouteTitle } from '@nx-console/vscode/nx-run-target-view';
Expand Down Expand Up @@ -187,10 +186,9 @@ function getConfigValuesFromContextMenuUri(
.replace(workspacePath, '')
.replace(/\\/g, '/')
.replace(/^\//, '');

const nxConfig = readAndCacheJsonFile('nx.json', workspacePath);
const appsDir = nxConfig.json.workspaceLayout?.appsDir ?? 'apps';
const libsDir = nxConfig.json.workspaceLayout?.libsDir ?? 'libs';
const nxConfig = getNxConfig(workspacePath);
const appsDir = nxConfig.workspaceLayout?.appsDir ?? 'apps';
const libsDir = nxConfig.workspaceLayout?.libsDir ?? 'libs';
if (
(appsDir && schematic.name === 'application') ||
schematic.name === 'app'
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@
"@angular/platform-browser": "12.2.2",
"@angular/platform-browser-dynamic": "12.2.2",
"@babel/core": "7.5.4",
"@nrwl/angular": "12.8.0-beta.0",
"@nrwl/cli": "12.8.0-beta.0",
"@nrwl/cypress": "12.8.0-beta.0",
"@nrwl/eslint-plugin-nx": "12.8.0-beta.0",
"@nrwl/jest": "12.8.0-beta.0",
"@nrwl/linter": "12.8.0-beta.0",
"@nrwl/node": "12.8.0-beta.0",
"@nrwl/angular": "12.8.0",
"@nrwl/cli": "12.8.0",
"@nrwl/cypress": "12.8.0",
"@nrwl/eslint-plugin-nx": "12.8.0",
"@nrwl/jest": "12.8.0",
"@nrwl/linter": "12.8.0",
"@nrwl/node": "12.8.0",
"@nrwl/nx-cloud": "12.3.10",
"@nrwl/storybook": "12.8.0-beta.0",
"@nrwl/tao": "12.8.0-beta.0",
"@nrwl/workspace": "12.8.0-beta.0",
"@nrwl/storybook": "12.8.0",
"@nrwl/tao": "12.8.0",
"@nrwl/workspace": "12.8.0",
"@storybook/addon-essentials": "~6.3.0",
"@storybook/addon-knobs": "~6.3.0",
"@storybook/angular": "~6.3.0",
Expand Down Expand Up @@ -100,3 +100,4 @@
"zone.js": "~0.11.4"
}
}

Loading