Skip to content

Commit

Permalink
fix: optimize file watches (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli authored Jul 4, 2022
1 parent 8641d03 commit 8caaaaf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 35 deletions.
4 changes: 3 additions & 1 deletion apps/vscode/src/commands/refresh-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { debounceTime, Subject } from 'rxjs';
import { commands } from 'vscode';

export const REFRESH_WORKSPACE = 'nxConsole.refreshWorkspace';

const refresh = new Subject();

refresh.pipe(debounceTime(300)).subscribe(async () => {
Expand All @@ -14,7 +16,7 @@ refresh.pipe(debounceTime(300)).subscribe(async () => {
* Refresh workspace by debouncing multiple calls to only trigger once
*/
export function refreshWorkspace() {
return commands.registerCommand('nxConsole.refreshWorkspace', () => {
return commands.registerCommand(REFRESH_WORKSPACE, () => {
refresh.next(undefined);
});
}
64 changes: 34 additions & 30 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ import {
} from '@nx-console/vscode/json-schema';
import { enableTypeScriptPlugin } from '@nx-console/typescript-plugin';
import { NxConversion } from '@nx-console/vscode/nx-conversion';
import { refreshWorkspace } from './commands/refresh-workspace';
import {
refreshWorkspace,
REFRESH_WORKSPACE,
} from './commands/refresh-workspace';

let runTargetTreeView: TreeView<RunTargetTreeItem>;
let nxProjectTreeView: TreeView<NxProjectTreeItem>;
Expand Down Expand Up @@ -213,13 +216,6 @@ async function setWorkspace(workspacePath: string) {
// Set the NX_WORKSPACE_ROOT_PATH as soon as possible so that the nx utils can get this.
process.env.NX_WORKSPACE_ROOT_PATH = workspacePath;

// const { verifyWorkspace } = await import('@nx-console/vscode/nx-workspace');

// const { validWorkspaceJson } = await verifyWorkspace();
// if (!validWorkspaceJson) {
// return;
// // }

if (!cliTaskProvider) {
cliTaskProvider = new CliTaskProvider();
registerNxCommands(context, cliTaskProvider);
Expand Down Expand Up @@ -280,20 +276,15 @@ async function setWorkspace(workspacePath: string) {
}

async function setApplicationAndLibraryContext(workspacePath: string) {
const { getNxConfig } = await import('@nx-console/vscode/nx-workspace');
const { nxWorkspace } = await import('@nx-console/vscode/nx-workspace');

let nxConfig: AsyncReturnType<typeof getNxConfig>;
try {
nxConfig = await getNxConfig(workspacePath);
} catch {
return;
}
const { workspaceLayout } = await nxWorkspace();

commands.executeCommand('setContext', 'nxAppsDir', [
join(workspacePath, nxConfig.workspaceLayout?.appsDir ?? 'apps'),
join(workspacePath, workspaceLayout.appsDir),
]);
commands.executeCommand('setContext', 'nxLibsDir', [
join(workspacePath, nxConfig.workspaceLayout?.libsDir ?? 'libs'),
join(workspacePath, workspaceLayout.libsDir),
]);

const generatorCollections = await getGenerators(workspacePath);
Expand Down Expand Up @@ -323,25 +314,38 @@ async function setApplicationAndLibraryContext(workspacePath: string) {
);
}

function registerWorkspaceFileWatcher(
async function registerWorkspaceFileWatcher(
context: ExtensionContext,
workspaceJsonPath: string
workspacePath: string
) {
if (workspaceFileWatcher) {
workspaceFileWatcher.dispose();
}

const workspaceDir = dirname(workspaceJsonPath);

workspaceFileWatcher = watchFile(
new RelativePattern(
workspaceDir,
'**/{workspace,angular,project,nx,package}.json'
const { nxWorkspace } = await import('@nx-console/vscode/nx-workspace');

const { workspaceLayout } = await nxWorkspace();
const workspacePackageDirs = new Set<string>();
workspacePackageDirs.add(workspaceLayout.appsDir);
workspacePackageDirs.add(workspaceLayout.libsDir);
workspacePackageDirs.add('packages');
context.subscriptions.push(
watchFile(
new RelativePattern(workspacePath, '{workspace,angular,nx}.json'),
() => {
commands.executeCommand(REFRESH_WORKSPACE);
}
),
() => {
commands.executeCommand('nxConsole.refreshWorkspace');
}
...Array.from(workspacePackageDirs).map((dir) => {
return watchFile(
new RelativePattern(
join(workspacePath, dir),
`**/{project,package}.json`
),
() => {
commands.executeCommand(REFRESH_WORKSPACE);
}
);
})
);

context.subscriptions.push(workspaceFileWatcher);
}
23 changes: 23 additions & 0 deletions libs/vscode/nx-workspace/src/lib/nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ interface Workspace {
workspaceType: 'ng' | 'nx';
configurationFilePath: string;
workspacePath: string;
isLerna: boolean;
workspaceLayout: {
appsDir: string;
libsDir: string;
};
}

const enum Status {
Expand Down Expand Up @@ -85,12 +90,25 @@ async function _workspace(): Promise<Workspace> {
isAngularWorkspace ? 'angularCli' : 'nx'
);

const isLerna = await fileExists(join(workspacePath, 'lerna.json'));

try {
return {
validWorkspaceJson: true,
workspaceType: isAngularWorkspace ? 'ng' : 'nx',
json: toWorkspaceFormat(config.workspaceConfiguration),
configurationFilePath: config.configPath,
isLerna,
workspaceLayout: {
appsDir:
config.workspaceConfiguration.workspaceLayout?.appsDir ?? isLerna
? 'packages'
: 'apps',
libsDir:
config.workspaceConfiguration.workspaceLayout?.libsDir ?? isLerna
? 'packages'
: 'libs',
},
workspacePath,
};
} catch (e) {
Expand All @@ -117,6 +135,11 @@ async function _workspace(): Promise<Workspace> {
},
configurationFilePath: '',
workspacePath,
isLerna: false,
workspaceLayout: {
appsDir: 'apps',
libsDir: 'libs',
},
};
}
}
9 changes: 5 additions & 4 deletions libs/vscode/webview/src/lib/get-task-execution-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getTelemetry,
readTargetDef,
} from '@nx-console/server';
import { getNxConfig, nxWorkspace } from '@nx-console/vscode/nx-workspace';
import { nxWorkspace } from '@nx-console/vscode/nx-workspace';
import { verifyBuilderDefinition } from '@nx-console/vscode/verify';
import { Uri, window } from 'vscode';
import {
Expand Down Expand Up @@ -196,9 +196,10 @@ async function getConfigValuesFromContextMenuUri(
.replace(workspacePath, '')
.replace(/\\/g, '/')
.replace(/^\//, '');
const nxConfig = await getNxConfig(workspacePath);
const appsDir = nxConfig.workspaceLayout?.appsDir ?? 'packages';
const libsDir = nxConfig.workspaceLayout?.libsDir ?? 'packages';
const { workspaceLayout } = await nxWorkspace();

const appsDir = workspaceLayout.appsDir;
const libsDir = workspaceLayout.libsDir;
if (
(appsDir && generator.name === 'application') ||
generator.name === 'app'
Expand Down

0 comments on commit 8caaaaf

Please sign in to comment.