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: hyphenate arguments for ng workspaces #1304

Merged
merged 2 commits into from
Jul 4, 2022
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
2 changes: 1 addition & 1 deletion libs/server/src/lib/utils/get-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function readWorkspaceGeneratorsCollection(
data: {
name,
collection: collectionName,
options: await normalizeSchema(schemaJson.json),
options: await normalizeSchema(schemaJson.json, 'nx'),
description: schemaJson.json.description ?? '',
type,
},
Expand Down
37 changes: 37 additions & 0 deletions libs/server/src/lib/utils/ng-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { workspaceDependencyPath } from '@nx-console/npm';
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';

declare function __non_webpack_require__(importPath: string): any;

let ngPackageJson: { version: string };
let loadedNgPackage = false;
export async function ngVersion(): Promise<number> {
if (!loadedNgPackage) {
const workspacePath = WorkspaceConfigurationStore.instance.get(
'nxWorkspacePath',
''
);

const packagePath = await workspaceDependencyPath(
workspacePath,
'@angular/cli'
);

if (!packagePath) {
return 0;
}

ngPackageJson = __non_webpack_require__(packagePath + '/package.json');
loadedNgPackage = true;
}

if (!ngPackageJson) {
return 0;
}
const ngPackageVersion = ngPackageJson.version;
const majorVersion = ngPackageVersion.split('.')[0];
if (!majorVersion) {
return 0;
}
return +majorVersion;
}
7 changes: 6 additions & 1 deletion libs/server/src/lib/utils/read-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function readDefaultValues(configurations: any, name: string): DefaultValue[] {
export async function readBuilderSchema(
basedir: string,
builder: string,
workspaceType: 'ng' | 'nx',
projectDefaults?: { [name: string]: string }
): Promise<Option[] | undefined> {
try {
Expand Down Expand Up @@ -87,7 +88,11 @@ export async function readBuilderSchema(
path.dirname(buildersJson.path)
);

return await normalizeSchema(builderSchema.json, projectDefaults);
return await normalizeSchema(
builderSchema.json,
workspaceType,
projectDefaults
);
} catch (e) {
// todo: make this a utility function to be used in more places.
const stringifiedError = e.toString ? e.toString() : JSON.stringify(e);
Expand Down
49 changes: 29 additions & 20 deletions libs/server/src/lib/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ describe('utils', () => {
options: Schema['properties'],
required: string[] = []
): Promise<Option[]> => {
const r = await normalizeSchema({
properties: { ...options },
required,
});
const r = await normalizeSchema(
{
properties: { ...options },
required,
},
'nx'
);
return r;
};

Expand All @@ -41,28 +44,34 @@ describe('utils', () => {
});

it('should sort positional arguments by ascending order', async () => {
const r = await normalizeSchema({
properties: {
a: { $default: { $source: 'argv', index: 0 } },
b: { $default: { $source: 'argv', index: 2 } },
c: { $default: { $source: 'argv', index: 1 } },
const r = await normalizeSchema(
{
properties: {
a: { $default: { $source: 'argv', index: 0 } },
b: { $default: { $source: 'argv', index: 2 } },
c: { $default: { $source: 'argv', index: 1 } },
},
required: [],
},
required: [],
});
'nx'
);
expect(r.map((x) => x.name)).toEqual(['a', 'c', 'b']);
});

it('should sort required arguments', async () => {
const r = await normalizeSchema({
properties: {
a: { $default: { $source: 'argv', index: 1 } },
b: {},
c: {},
d: { $default: { $source: 'argv', index: 0 } },
e: {},
const r = await normalizeSchema(
{
properties: {
a: { $default: { $source: 'argv', index: 1 } },
b: {},
c: {},
d: { $default: { $source: 'argv', index: 0 } },
e: {},
},
required: ['c', 'e'],
},
required: ['c', 'e'],
});
'nx'
);
expect(r.map((x) => x.name)).toMatchInlineSnapshot(`
Array [
"d",
Expand Down
15 changes: 11 additions & 4 deletions libs/server/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
WorkspaceJsonConfiguration,
NxJsonConfiguration,
} from '@nrwl/devkit';
import { names } from '@nrwl/devkit';

import {
ItemsWithEnum,
Expand All @@ -27,6 +28,7 @@ import { getOutputChannel } from './output-channel';
import { toNewFormat } from 'nx/src/config/workspaces';
import { PosixFS, ZipOpenFS } from '@yarnpkg/fslib';
import { getLibzipSync as libzip } from '@yarnpkg/libzip';
import { ngVersion } from './ng-version';

const zipOpenFs = new ZipOpenFS({ libzip });
export const crossFs = new PosixFS(zipOpenFs);
Expand Down Expand Up @@ -175,9 +177,11 @@ export async function readAndCacheJsonFile(

export async function normalizeSchema(
s: Schema,
workspaceType: 'ng' | 'nx',
projectDefaults?: GeneratorDefaults
): Promise<Option[]> {
const options = schemaToOptions(s);
const hyphenate = workspaceType === 'ng' && (await ngVersion()) >= 14;
const options = schemaToOptions(s, { hyphenate });
const requiredFields = new Set(s.required || []);

const nxOptions = options.map((option) => {
Expand Down Expand Up @@ -322,7 +326,10 @@ export function toWorkspaceFormat(
return newFormat;
}

function schemaToOptions(schema: Schema): CliOption[] {
function schemaToOptions(
schema: Schema,
config?: { hyphenate: boolean }
): CliOption[] {
return Object.keys(schema.properties || {}).reduce<CliOption[]>(
(cliOptions, option) => {
const currentProperty = schema.properties[option];
Expand All @@ -336,9 +343,9 @@ function schemaToOptions(schema: Schema): CliOption[] {
if (!visible) {
return cliOptions;
}

const name = config?.hyphenate ? names(option).fileName : option;
cliOptions.push({
name: option,
name,
positional,
...currentProperty,
});
Expand Down
3 changes: 2 additions & 1 deletion libs/vscode/tasks/src/lib/cli-task-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ async function selectCliCommandAndPromptForFlags(
const builderDefinition = await verifyBuilderDefinition(
projectName,
target,
json
json,
workspaceType
);
const {
validBuilder,
Expand Down
8 changes: 5 additions & 3 deletions libs/vscode/tasks/src/lib/select-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ export async function getGeneratorOptions(
workspacePath: string,
collectionName: string,
generatorName: string,
generatorPath: string
generatorPath: string,
workspaceType: 'ng' | 'nx'
): Promise<Option[]> {
const generatorSchema = await readAndCacheJsonFile(generatorPath);
const workspaceDefaults = await readWorkspaceJsonDefaults(workspacePath);
const defaults =
workspaceDefaults &&
workspaceDefaults[collectionName] &&
workspaceDefaults[collectionName][generatorName];
return await normalizeSchema(generatorSchema.json, defaults);
return await normalizeSchema(generatorSchema.json, workspaceType, defaults);
}

export async function selectGenerator(
Expand Down Expand Up @@ -120,7 +121,8 @@ export async function selectGenerator(
workspacePath,
selection.collectionName,
selection.generator.name,
selection.collectionPath
selection.collectionPath,
workspaceType
));
const positional = `${selection.collectionName}:${selection.generator.name}`;
return {
Expand Down
4 changes: 3 additions & 1 deletion libs/vscode/verify/src/lib/verify-builder-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const RUN_ONE_OPTIONS = [
export async function verifyBuilderDefinition(
project: string,
command: string,
workspaceJson: WorkspaceJsonConfiguration
workspaceJson: WorkspaceJsonConfiguration,
workspaceType: 'ng' | 'nx'
): Promise<{
validBuilder: boolean;
builderName: string;
Expand Down Expand Up @@ -89,6 +90,7 @@ export async function verifyBuilderDefinition(
const options = await readBuilderSchema(
workspacePath(),
executorName,
workspaceType,
commandDef.options
);

Expand Down
6 changes: 4 additions & 2 deletions libs/vscode/webview/src/lib/get-task-execution-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export async function getTaskExecutionSchema(
const { validBuilder, options } = await verifyBuilderDefinition(
selection.projectName,
selection.command,
json
json,
workspaceType
);
if (!validBuilder) {
return;
Expand Down Expand Up @@ -136,7 +137,8 @@ export async function getTaskExecutionSchema(
const { validBuilder, options } = await verifyBuilderDefinition(
selectedProject.projectName,
command,
json
json,
workspaceType
);
if (!validBuilder) {
return;
Expand Down