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: Add commands to add applications and libraries directly #1128

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/launch.json
Expand Up @@ -17,6 +17,10 @@
"${workspaceFolder}/node_modules"
],
"request": "launch",
"skipFiles": [
"<node_internals>/**",
"/Applications/Visual Studio Code.app/**"
],
"type": "pwa-extensionHost"
},
{
Expand Down
53 changes: 51 additions & 2 deletions apps/vscode/src/main.ts
Expand Up @@ -21,6 +21,7 @@ import {
getOutputChannel,
getTelemetry,
initTelemetry,
readAllGeneratorCollections,
teardownTelemetry,
watchFile,
} from '@nx-console/server';
Expand All @@ -44,6 +45,7 @@ import {
NxProjectTreeProvider,
} from '@nx-console/vscode/nx-project-view';
import {
getNxConfig,
verifyWorkspace,
WorkspaceCodeLensProvider,
} from '@nx-console/vscode/nx-workspace';
Expand All @@ -53,6 +55,7 @@ import {
WorkspaceJsonSchema,
ProjectJsonSchema,
} from '@nx-console/vscode/json-schema';
import { GeneratorType } from '@nx-console/schema';

let runTargetTreeView: TreeView<RunTargetTreeItem>;
let nxProjectTreeView: TreeView<NxProjectTreeItem>;
Expand Down Expand Up @@ -196,10 +199,10 @@ function scanForWorkspace(vscodeWorkspacePath: string) {

while (currentDirectory !== root) {
if (existsSync(join(currentDirectory, 'angular.json'))) {
return setWorkspace(join(currentDirectory, 'angular.json'));
setWorkspace(join(currentDirectory, 'angular.json'));
}
if (existsSync(join(currentDirectory, 'workspace.json'))) {
return setWorkspace(join(currentDirectory, 'workspace.json'));
setWorkspace(join(currentDirectory, 'workspace.json'));
}
currentDirectory = dirname(currentDirectory);
}
Expand Down Expand Up @@ -244,6 +247,8 @@ async function setWorkspace(workspaceJsonPath: string) {
);
}

await setApplicationAndLibraryContext(workspaceJsonPath);

const isNxWorkspace = existsSync(join(workspaceJsonPath, '..', 'nx.json'));
const isAngularWorkspace = workspaceJsonPath.endsWith('angular.json');

Expand Down Expand Up @@ -271,6 +276,50 @@ async function setWorkspace(workspaceJsonPath: string) {
getTelemetry().record('WorkspaceType', { workspaceType });
}

async function setApplicationAndLibraryContext(workspaceJsonPath: string) {
const nxConfig = getNxConfig(dirname(workspaceJsonPath));

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

const generatorCollections = await readAllGeneratorCollections(
workspaceJsonPath
);
let hasApplicationGenerators = false;
let hasLibraryGenerators = false;

generatorCollections.forEach((generatorCollection) => {
generatorCollection.generators.forEach((generator) => {
if (generator.type === 'application') {
hasApplicationGenerators = true;
} else if (generator.type === 'library') {
hasLibraryGenerators = true;
}
});
});

commands.executeCommand(
'setContext',
'nx.hasApplicationGenerators',
hasApplicationGenerators
);
commands.executeCommand(
'setContext',
'nx.hasLibraryGenerators',
hasLibraryGenerators
);
}

function registerWorkspaceFileWatcher(
context: ExtensionContext,
workspaceJsonPath: string
Expand Down
46 changes: 46 additions & 0 deletions apps/vscode/src/package.json
Expand Up @@ -65,6 +65,16 @@
"when": "isNxWorkspace && config.nxConsole.enableGenerateFromContextMenu",
"command": "nx.run.fileexplorer",
"group": "2_workspace"
},
{
"when": "isNxWorkspace && explorerResourceIsFolder && resourcePath in nxAppsDir && config.nxConsole.enableGenerateFromContextMenu && nx.hasApplicationGenerators",
"command": "nx.generate.ui.app.fileexplorer",
"group": "2_workspace"
},
{
"when": "isNxWorkspace && explorerResourceIsFolder && resourcePath in nxLibsDir && config.nxConsole.enableGenerateFromContextMenu && nx.hasLibraryGenerators",
"command": "nx.generate.ui.lib.fileexplorer",
"group": "2_workspace"
}
],
"view/title": [
Expand Down Expand Up @@ -108,6 +118,22 @@
"command": "nx.run.fileexplorer",
"when": "false"
},
{
"command": "nx.generate.ui.app.fileexplorer",
"when": "false"
},
{
"command": "nx.generate.ui.lib.fileexplorer",
"when": "false"
},
{
"command": "nx.generate.ui.app",
"when": "isNxWorkspace && nx.hasApplicationGenerators"
},
{
"command": "nx.generate.ui.lib",
"when": "isNxWorkspace && && nx.hasLibraryGenerators"
},
{
"command": "ng.lint",
"when": "isAngularWorkspace"
Expand Down Expand Up @@ -511,6 +537,26 @@
"category": "Nx",
"title": "Nx run",
"command": "nx.run.fileexplorer"
},
{
"category": "Nx",
"title": "Add application",
"command": "nx.generate.ui.app"
},
{
"category": "Nx",
"title": "Add library",
"command": "nx.generate.ui.lib"
},
{
"category": "Nx",
"title": "Nx add application",
"command": "nx.generate.ui.app.fileexplorer"
},
{
"category": "Nx",
"title": "Nx add library",
"command": "nx.generate.ui.lib.fileexplorer"
}
],
"configuration": {
Expand Down
13 changes: 10 additions & 3 deletions libs/schema/src/index.ts
Expand Up @@ -47,16 +47,23 @@ export interface TaskExecutionSchema {
contextValues?: Record<string, string | number | boolean | undefined>;
}

export interface SchematicCollection {
export interface GeneratorCollection {
name: string;
schematics: Schematic[];
generators: Generator[];
}

export interface Schematic {
export enum GeneratorType {
Application = 'application',
Library = 'library',
Other = 'other',
}

export interface Generator {
collection: string;
name: string;
description: string;
options?: Option[];
type: GeneratorType;
}

export interface DefaultValue {
Expand Down
4 changes: 2 additions & 2 deletions libs/server/src/index.ts
@@ -1,11 +1,11 @@
export * from './lib/abstract-tree-provider';
export * from './lib/extensions';
export * from './lib/stores';
export * from './lib/select-schematic';
export * from './lib/select-generator';
export * from './lib/telemetry';
export * from './lib/utils/output-channel';
export * from './lib/utils/read-projects';
export * from './lib/utils/read-schematic-collections';
export * from './lib/utils/read-generator-collections';
export {
fileExistsSync,
findClosestNg,
Expand Down
58 changes: 58 additions & 0 deletions libs/server/src/lib/select-generator.ts
@@ -0,0 +1,58 @@
import { Generator, GeneratorType } from '@nx-console/schema';
import { TaskExecutionSchema } from '@nx-console/schema';
import { QuickPickItem, window } from 'vscode';
import {
readAllGeneratorCollections,
readGeneratorOptions,
} from './utils/read-generator-collections';

export async function selectGenerator(
workspaceJsonPath: string,
generatorType?: GeneratorType
): Promise<TaskExecutionSchema | undefined> {
interface GenerateQuickPickItem extends QuickPickItem {
collectionName: string;
generator: Generator;
}

let generators = (await readAllGeneratorCollections(workspaceJsonPath))
.filter((c) => c && c.generators.length)
.map((c): GenerateQuickPickItem[] =>
c.generators.map(
(s: Generator): GenerateQuickPickItem => ({
description: s.description,
label: `${c.name} - ${s.name}`,
collectionName: c.name,
generator: s,
})
)
)
.flat();

if (generatorType) {
generators = generators.filter((generator) => {
return generator.generator.type === generatorType;
});
}

if (generators) {
const selection = await window.showQuickPick(generators);
if (selection) {
const options =
selection.generator.options ||
(await readGeneratorOptions(
workspaceJsonPath,
selection.collectionName,
selection.generator.name
));
const positional = `${selection.collectionName}:${selection.generator.name}`;
return {
...selection.generator,
options,
command: 'generate',
positional,
cliName: 'nx',
};
}
}
}
51 changes: 0 additions & 51 deletions libs/server/src/lib/select-schematic.ts

This file was deleted.