Skip to content

Commit

Permalink
feat(server): Use angular-cli's built in parser (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmeku authored Nov 26, 2019
1 parent 79ecd4c commit c43c7a1
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 426 deletions.
2 changes: 1 addition & 1 deletion apps/vscode/src/app/ng-task/ng-task-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function selectNgCliCommandAndPromptForFlags(command: string) {
return; // Do not execute a command if user clicks out of VSCode UI.
}

const { validBuilder, schema } = verifyBuilderDefinition(
const { validBuilder, schema } = await verifyBuilderDefinition(
selection.projectName,
command,
json
Expand Down
2 changes: 1 addition & 1 deletion apps/vscode/src/app/ng-task/select-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function promptForFlagValue(flagToSet: NgTaskFlagQuickPickItem) {
placeHolder
});
} else if (flagToSet.schema.enum && flagToSet.schema.enum.length) {
return window.showQuickPick([...flagToSet.schema.enum], {
return window.showQuickPick([...flagToSet.schema.enum.map(String)], {
placeHolder
});
} else {
Expand Down
6 changes: 3 additions & 3 deletions apps/vscode/src/app/select-schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { readAllSchematicCollections } from '@angular-console/server';
import { QuickPickItem, window } from 'vscode';
import { join } from 'path';

export function selectSchematic(workspacePath: string) {
export async function selectSchematic(workspacePath: string) {
interface GenerateQuickPickItem extends QuickPickItem {
collectionName: string;
schematic: Schematic;
}

const schematics = readAllSchematicCollections(
const schematics = (await readAllSchematicCollections(
workspacePath,
join('tools', 'schematics'), // TODO: Make these values auto detectable / configurable
'workspace-schematic' // TODO: Make these values auto detectable / configurable
)
))
.map((c): GenerateQuickPickItem[] =>
c.schematics.map(
(s): GenerateQuickPickItem => ({
Expand Down
15 changes: 11 additions & 4 deletions apps/vscode/src/app/verify-workspace/verify-builder-definition.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Schema } from '@angular-console/schema';
import { readSchema } from '@angular-console/server';
import { readBuilderSchema } from '@angular-console/server';
import { window } from 'vscode';
import { angularJsonTreeProvider } from '../angular-json-tree/angular-json-tree-provider';
import { getTelemetry } from '../telemetry';
import { ngTaskProvider } from '../ng-task/ng-task-provider';

export function verifyBuilderDefinition(
export async function verifyBuilderDefinition(
project: string,
command: string,
angularJson: any
): { validBuilder: boolean; builderName: string; schema: Array<Schema> } {
): Promise<{
validBuilder: boolean;
builderName: string;
schema: Array<Schema>;
}> {
const projects = angularJson.projects || {};
const projectDef = projects[project] || {};
const architectDef = projectDef.architect || {};
Expand Down Expand Up @@ -40,7 +44,10 @@ export function verifyBuilderDefinition(
};
}

const schema = readSchema(ngTaskProvider.getWorkspacePath(), builderName);
const schema = await readBuilderSchema(
ngTaskProvider.getWorkspacePath(),
builderName
);

if (!schema) {
window
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readArchitectDef, readSchema } from '@angular-console/server';
import { readArchitectDef, readBuilderSchema } from '@angular-console/server';
import { TaskExecutionSchema } from '@angular-console/vscode-ui/feature-task-execution-form';
import { window } from 'vscode';

Expand Down Expand Up @@ -40,7 +40,7 @@ export async function getTaskExecutionSchema(

if (!selectedProject) return;

const { validBuilder, schema } = verifyBuilderDefinition(
const { validBuilder, schema } = await verifyBuilderDefinition(
selectedProject.projectName,
command,
json
Expand Down Expand Up @@ -84,12 +84,12 @@ export async function getTaskExecutionSchema(
)
);

return window.showQuickPick(runnableItems).then(selection => {
return window.showQuickPick(runnableItems).then(async selection => {
if (!selection) {
return;
}

const schemaDef = readSchema(
const schemaDef = await readBuilderSchema(
workspacePath,
selection.architectDef.builder
);
Expand Down Expand Up @@ -121,7 +121,6 @@ export async function getTaskExecutionSchema(
}

if (s.name === 'project') {
s.type = 'enum';
s.enum = getProjectEntries()
.map(entry => entry[0])
.sort();
Expand Down
16 changes: 4 additions & 12 deletions libs/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
export interface Schema {
name: string;
type: string;
description: string;
defaultValue?: string;
important?: boolean;
completion?: string;
deprecated?: string;
required: boolean;
positional: boolean;
enum?: string[];
}
import { Option } from '@angular/cli/models/interface';

// tslint:disable-next-line: no-empty-interface
export interface Schema extends Option {}

export interface SchematicCollectionForNgNew {
name: string;
Expand Down
190 changes: 0 additions & 190 deletions libs/server/src/lib/read-schematic-collections.ts

This file was deleted.

40 changes: 8 additions & 32 deletions libs/server/src/lib/utils/read-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,11 @@ function serializeDefaultsForConfig(config: any) {
);
}

export function readBuilder(
export async function readBuilderSchema(
basedir: string,
builder: string
): { schema: Array<Schema> } | undefined {
): Promise<Schema[] | undefined> {
const [npmPackage, builderName] = builder.split(':');
return readBuildersFile(basedir, npmPackage)[builderName];
}

export function readSchema(
basedir: string,
builder: string
): Schema[] | undefined {
const builderDef = readBuilder(basedir, builder);
return builderDef ? builderDef.schema : undefined;
}

function readBuildersFile(
basedir: string,
npmPackage: string
): { [key: string]: any } {
const packageJson = readAndCacheJsonFile(
path.join(npmPackage, 'package.json'),
path.join(basedir, 'node_modules')
Expand All @@ -104,20 +89,11 @@ function readBuildersFile(
path.dirname(packageJson.path)
);

const builders = {} as any;
Object.entries(buildersJson.json.builders).forEach(([k, v]: [any, any]) => {
try {
const builderSchema = readAndCacheJsonFile(
v.schema,
path.dirname(buildersJson.path)
);
builders[k] = {
name: k,
schema: normalizeSchema(builderSchema.json),
description: v.description || ''
};
} catch (e) {}
});
const builderDef = buildersJson.json.builders[builderName];
const builderSchema = readAndCacheJsonFile(
builderDef.schema,
path.dirname(buildersJson.path)
);

return builders;
return await normalizeSchema(builderSchema.json);
}
Loading

0 comments on commit c43c7a1

Please sign in to comment.