Skip to content

Commit

Permalink
fix: only reads and normalizes selected schematic options
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandi Barr committed May 17, 2021
1 parent 77e42e3 commit fa60821
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion libs/schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface Schematic {
collection: string;
name: string;
description: string;
options: Option[];
options?: Option[];
}

export interface DefaultValue {
Expand Down
31 changes: 24 additions & 7 deletions libs/server/src/lib/select-schematic.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { Schematic } from '@nx-console/schema';
import { TaskExecutionSchema } from '@nx-console/schema';
import { QuickPickItem, window } from 'vscode';
import { readAllSchematicCollections } from './utils/read-schematic-collections';
import {
readAllSchematicCollections,
readSchematicOptions,
} from './utils/read-schematic-collections';

export async function selectSchematic(workspaceJsonPath: string) {
export async function selectSchematic(
workspaceJsonPath: string
): Promise<TaskExecutionSchema | undefined> {
interface GenerateQuickPickItem extends QuickPickItem {
collectionName: string;
schematic: Schematic;
}

const schematics = (await readAllSchematicCollections(workspaceJsonPath))
.filter((c) => c && c.schematics.length)
.map((c): GenerateQuickPickItem[] =>
c.schematics.map(
(s): GenerateQuickPickItem => ({
(s: Schematic): GenerateQuickPickItem => ({
description: s.description,
label: `${c.name} - ${s.name}`,
collectionName: c.name,
Expand All @@ -21,14 +28,24 @@ export async function selectSchematic(workspaceJsonPath: string) {
)
.flat();

return window.showQuickPick(schematics).then((selection) => {
if (schematics) {
const selection = await window.showQuickPick(schematics);
if (selection) {
const schematic = `${selection.schematic.collection}:${selection.schematic.name}`;
const options =
selection.schematic.options ||
(await readSchematicOptions(
workspaceJsonPath,
selection.collectionName,
selection.schematic.name
));
const positional = `${selection.collectionName}:${selection.schematic.name}`;
return {
...selection.schematic,
options,
command: 'generate',
positional: schematic,
positional,
cliName: 'nx',
};
}
});
}
}
69 changes: 35 additions & 34 deletions libs/server/src/lib/utils/read-schematic-collections.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schematic, SchematicCollection } from '@nx-console/schema';
import { Option, Schematic, SchematicCollection } from '@nx-console/schema';
import { basename, dirname, join } from 'path';

import {
Expand Down Expand Up @@ -31,7 +31,7 @@ export async function readAllSchematicCollections(
];
return collections.filter(
(collection): collection is SchematicCollection =>
!!collection && collection!.schematics!.length > 0
collection && collection.schematics.length > 0
);
}

Expand Down Expand Up @@ -102,13 +102,10 @@ async function readSchematicCollectionsFromNodeModules(
}
}
});
const defaults = readWorkspaceJsonDefaults(workspaceJsonPath);

return (
await Promise.all(
schematicCollections.map((c) =>
readCollection(nodeModulesDir, c, defaults)
)
schematicCollections.map((c) => readCollection(nodeModulesDir, c))
)
).filter((c): c is SchematicCollection => Boolean(c));
}
Expand All @@ -125,11 +122,7 @@ async function readWorkspaceSchematicsCollection(
if (fileExistsSync(join(collectionDir, 'collection.json'))) {
const collection = readAndCacheJsonFile('collection.json', collectionDir);

return await readCollectionSchematics(
collectionName,
collection.path,
collection.json
);
return await readCollectionSchematics(collectionName, collection.json);
} else {
const schematics: Schematic[] = await Promise.all(
listFiles(collectionDir)
Expand All @@ -150,8 +143,7 @@ async function readWorkspaceSchematicsCollection(

async function readCollection(
basedir: string,
collectionName: string,
defaults?: any
collectionName: string
): Promise<SchematicCollection | null> {
try {
const packageJson = readAndCacheJsonFile(
Expand All @@ -162,12 +154,7 @@ async function readCollection(
packageJson.json.schematics || packageJson.json.generators,
dirname(packageJson.path)
);
return readCollectionSchematics(
collectionName,
collection.path,
collection.json,
defaults
);
return readCollectionSchematics(collectionName, collection.json);
} catch (e) {
// this happens when package is misconfigured. We decided to ignore such a case.
return null;
Expand All @@ -176,9 +163,7 @@ async function readCollection(

async function readCollectionSchematics(
collectionName: string,
collectionPath: string,
collectionJson: any,
defaults?: any
collectionJson: any
) {
const schematicCollection = {
name: collectionName,
Expand All @@ -190,20 +175,9 @@ async function readCollectionSchematics(
).forEach(async ([k, v]: [any, any]) => {
try {
if (canAdd(k, v)) {
const schematicSchema = readAndCacheJsonFile(
v.schema,
dirname(collectionPath)
);
const projectDefaults =
defaults && defaults[collectionName] && defaults[collectionName][k];

schematicCollection.schematics.push({
name: k,
collection: collectionName,
options: await normalizeSchema(
schematicSchema.json,
projectDefaults
),
description: v.description || '',
});
}
Expand All @@ -221,7 +195,34 @@ async function readCollectionSchematics(
return schematicCollection;
}

export function canAdd(
export async function readSchematicOptions(
workspaceJsonPath: string,
collectionName: string,
schematicName: string
): Promise<Option[]> {
const basedir = join(workspaceJsonPath, '..');
const nodeModulesDir = join(basedir, 'node_modules');
const collectionPackageJson = readAndCacheJsonFile(
join(collectionName, 'package.json'),
nodeModulesDir
);
const collectionJson = readAndCacheJsonFile(
collectionPackageJson.json.schematics || collectionPackageJson.json.generators,
dirname(collectionPackageJson.path)
);
const schematicSchema = readAndCacheJsonFile(
collectionJson.json.schematics[schematicName].schema,
dirname(collectionJson.path)
);
const workspaceDefaults = readWorkspaceJsonDefaults(workspaceJsonPath);
const defaults =
workspaceDefaults &&
workspaceDefaults[collectionName] &&
workspaceDefaults[collectionName][schematicName];
return await normalizeSchema(schematicSchema.json, defaults);
}

function canAdd(
name: string,
s: { hidden: boolean; private: boolean; schema: string; extends: boolean }
): boolean {
Expand Down

0 comments on commit fa60821

Please sign in to comment.