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

fix: strip appsDir and libsDir from path if app|application or lib|library #1082

Merged
merged 2 commits into from
Jun 14, 2021
Merged
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
59 changes: 43 additions & 16 deletions libs/vscode/webview/src/lib/get-task-execution-schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TaskExecutionSchema } from '@nx-console/schema';
import { Option, TaskExecutionSchema } from '@nx-console/schema';
import {
getOutputChannel,
getTelemetry,
readAndCacheJsonFile,
readArchitectDef,
selectSchematic,
} from '@nx-console/server';
Expand Down Expand Up @@ -120,18 +121,15 @@ export async function getTaskExecutionSchema(
return;
}

schematic.options.forEach((s) => {
schematic.options.forEach((option) => {
// TODO: mixup between items and enum has been a source for recent bugs,
// util.ts normalizeSchema sets items from enum.
if (s.enum) {
if (option.enum) {
return;
}

if (
s.name === 'project' ||
(s.$default && s.$default.$source === 'projectName')
) {
s.enum = s.items = cliTaskProvider
if (isProjectOption(option)) {
option.enum = option.items = cliTaskProvider
.getProjectEntries()
.map((entry) => entry[0])
.sort();
Expand All @@ -140,6 +138,7 @@ export async function getTaskExecutionSchema(

const contextValues = contextMenuUri
? getConfigValuesFromContextMenuUri(
schematic,
contextMenuUri,
cliTaskProvider
)
Expand Down Expand Up @@ -170,21 +169,49 @@ export async function getTaskExecutionSchema(

// Get information about where the user clicked if invoked through right click in the explorer context menu
function getConfigValuesFromContextMenuUri(
schematic: TaskExecutionSchema,
contextMenuUri: Uri | undefined,
cliTaskProvider: CliTaskProvider
): { path: string; directory: string; project?: string } | undefined {
): { path?: string; directory?: string; project?: string, projectName?: string } | undefined {
if (contextMenuUri) {
const project = cliTaskProvider.projectForPath(contextMenuUri.fsPath);
const projectName = (project && project.name) || undefined;
const path = contextMenuUri.fsPath
.replace(cliTaskProvider.getWorkspacePath(), '')

const workspacePath = cliTaskProvider.getWorkspacePath();
let path = contextMenuUri.fsPath
.replace(workspacePath, '')
.replace(/\\/g, '/')
.replace(/^\//, '');

return {
path,
directory: path,
project: projectName,
};
const nxConfig = readAndCacheJsonFile('nx.json', workspacePath);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this with workspaceLayout() using a deep import from nx/workspace, but it wasn't giving me the configured path with a workspace I was testing on for some reason.

const appsDir = nxConfig.json.workspaceLayout?.appsDir ?? 'apps';
const libsDir = nxConfig.json.workspaceLayout?.libsDir ?? 'libs';
if (appsDir && schematic.name === 'application' || schematic.name === 'app') {
path = path.replace(appsDir, '')
.replace(/^\//, '');
}
if (libsDir && schematic.name === 'library' || schematic.name === 'lib') {
path = path.replace(libsDir, '')
.replace(/^\//, '');
}

if (projectName && schematic.options.some(isProjectOption)) {
return {
project: projectName,
projectName,
}
} else {
return {
path,
directory: path,
};
}
}
}

function isProjectOption(option: Option) {
return (
option.name === 'project' || option.name === 'projectName' ||
(option.$default && option.$default.$source === 'projectName')
);
}