-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add metagenerator for convert-to-inferred
- Loading branch information
Showing
12 changed files
with
282 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
docs/generated/packages/workspace/generators/convert-to-inferred.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "convert-to-inferred", | ||
"factory": "./src/generators/convert-to-inferred/generator", | ||
"schema": { | ||
"$schema": "https://json-schema.org/schema", | ||
"$id": "ConvertToInferred", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The project to convert to use inferred targets.", | ||
"x-priority": "important" | ||
}, | ||
"exclude": { | ||
"type": "array", | ||
"description": "List of plugins whose convert-to-inferred generators should not be run.", | ||
"items": { "type": "string" } | ||
}, | ||
"skipFormat": { | ||
"type": "boolean", | ||
"description": "Whether to format files.", | ||
"default": false | ||
} | ||
}, | ||
"presets": [] | ||
}, | ||
"description": "convert-to-inferred generator", | ||
"implementation": "/packages/workspace/src/generators/convert-to-inferred/generator.ts", | ||
"aliases": [], | ||
"hidden": false, | ||
"path": "/packages/workspace/src/generators/convert-to-inferred/schema.json", | ||
"type": "generator" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/workspace/src/generators/convert-to-inferred/files/src/index.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const variable = "<%= name %>"; |
20 changes: 20 additions & 0 deletions
20
packages/workspace/src/generators/convert-to-inferred/generator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; | ||
import { Tree, readProjectConfiguration } from '@nx/devkit'; | ||
|
||
import { convertToInferredGenerator } from './generator'; | ||
import { ConvertToInferredGeneratorSchema } from './schema'; | ||
|
||
describe('convert-to-inferred generator', () => { | ||
let tree: Tree; | ||
const options: ConvertToInferredGeneratorSchema = { name: 'test' }; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
it('should run successfully', async () => { | ||
await convertToInferredGenerator(tree, options); | ||
const config = readProjectConfiguration(tree, 'test'); | ||
expect(config).toBeDefined(); | ||
}); | ||
}); |
159 changes: 159 additions & 0 deletions
159
packages/workspace/src/generators/convert-to-inferred/generator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { | ||
createProjectGraphAsync, | ||
output, | ||
readNxJson, | ||
readProjectsConfigurationFromProjectGraph, | ||
Tree, | ||
workspaceRoot, | ||
} from '@nx/devkit'; | ||
import { prompt } from 'enquirer'; | ||
import { | ||
GeneratorInformation, | ||
getGeneratorInformation, | ||
} from 'nx/src/command-line/generate/generator-utils'; | ||
import { findInstalledPlugins } from 'nx/src/utils/plugins/installed-plugins'; | ||
|
||
interface Schema { | ||
project?: string; | ||
skipFormat?: boolean; | ||
exclude?: string[]; | ||
} | ||
|
||
export async function convertToInferredGenerator(tree: Tree, options: Schema) { | ||
const generatorChoices = await getPossibleConvertToInferredGenerators( | ||
options | ||
); | ||
|
||
const result = ( | ||
await prompt<{ generatorsToRun: string[] }>({ | ||
type: 'multiselect', | ||
name: 'generatorsToRun', | ||
message: 'Which convert-to-inferred generators should be run?', | ||
choices: Array.from(generatorChoices.keys()), | ||
initial: getInitialGeneratorChoices(generatorChoices, tree, options), | ||
validate: (result: string[]) => { | ||
if (result.length === 0) { | ||
return 'Please select at least one convert-to-inferred generator to run'; | ||
} | ||
return true; | ||
}, | ||
} as any) | ||
).generatorsToRun; | ||
|
||
if (result.length === 0) { | ||
output.error({ | ||
title: 'Please select at least one convert-to-inferred generator to run', | ||
}); | ||
return; | ||
} | ||
|
||
for (const generatorName of result) { | ||
output.log({ | ||
title: `Running ${generatorName}`, | ||
}); | ||
try { | ||
const generator = generatorChoices.get(generatorName); | ||
if (generator) { | ||
const generatorFactory = generator.implementationFactory(); | ||
await generatorFactory(tree, { | ||
project: options.project, | ||
skipFormat: options.skipFormat, | ||
}); | ||
} | ||
} catch (e) { | ||
const collection = | ||
generatorChoices.get(generatorName)?.resolvedCollectionName; | ||
output.error({ | ||
title: `Failed to run ${generatorName}`, | ||
bodyLines: [ | ||
e, | ||
`To rerun this generator without the ${generatorName} generator, use nx g @nx/workspace:convert-to-inferred ${ | ||
options.project ? `--project=${options.project}` : '' | ||
} --exclude=${ | ||
options.exclude | ||
? [...options.exclude, collection].join(', ') | ||
: collection | ||
}`, | ||
], | ||
}); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
async function getPossibleConvertToInferredGenerators(options: Schema) { | ||
const installedCollections = Array.from( | ||
new Set(findInstalledPlugins().map((x) => x.name)) | ||
); | ||
|
||
const projectGraph = await createProjectGraphAsync(); | ||
const projectsConfigurations = | ||
readProjectsConfigurationFromProjectGraph(projectGraph); | ||
|
||
const choices = new Map<string, GeneratorInformation>(); | ||
|
||
for (const collectionName of installedCollections) { | ||
try { | ||
const generator = getGeneratorInformation( | ||
collectionName, | ||
'convert-to-inferred', | ||
workspaceRoot, | ||
projectsConfigurations.projects | ||
); | ||
if ( | ||
generator.generatorConfiguration.hidden || | ||
generator.generatorConfiguration['x-deprecated'] || | ||
options.exclude?.includes(generator.resolvedCollectionName) | ||
) { | ||
continue; | ||
} | ||
const generatorName = `${generator.resolvedCollectionName}:${generator.normalizedGeneratorName}`; | ||
if (generatorName === '@nx/workspace:convert-to-inferred') { | ||
continue; | ||
} | ||
choices.set(generatorName, generator); | ||
} catch { | ||
// this just means that no convert-to-inferred generator exists for a given collection, ignore | ||
} | ||
} | ||
|
||
return choices; | ||
} | ||
|
||
function getInitialGeneratorChoices( | ||
choices: Map<string, GeneratorInformation>, | ||
tree: Tree, | ||
options: Schema | ||
) { | ||
// if a project is specified, we assume the user wants fine-grained contorl over which generators to run | ||
// in this case we won't include any generators by default | ||
if (options.project) { | ||
return []; | ||
} | ||
// we want to exclude generators from the default if they already have a plugin registered in nx.json | ||
// in that case, they're probably already using inferred targets | ||
const nxJson = readNxJson(tree); | ||
|
||
const collectionsWithRegisteredPlugins: Set<string> = new Set(); | ||
|
||
for (const plugin of nxJson.plugins ?? []) { | ||
if (typeof plugin === 'object') { | ||
collectionsWithRegisteredPlugins.add( | ||
plugin.plugin.replace('/plugin', '') | ||
); | ||
} | ||
} | ||
|
||
const initialChoices = new Set(); | ||
for (const [generatorName, generator] of choices) { | ||
if ( | ||
!collectionsWithRegisteredPlugins.has(generator.resolvedCollectionName) | ||
) { | ||
initialChoices.add(generatorName); | ||
} | ||
} | ||
|
||
return Array.from(initialChoices); | ||
} | ||
|
||
export default convertToInferredGenerator; |
25 changes: 25 additions & 0 deletions
25
packages/workspace/src/generators/convert-to-inferred/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "https://json-schema.org/schema", | ||
"$id": "ConvertToInferred", | ||
"title": "", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The project to convert to use inferred targets.", | ||
"x-priority": "important" | ||
}, | ||
"exclude": { | ||
"type": "array", | ||
"description": "List of plugins whose convert-to-inferred generators should not be run.", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"skipFormat": { | ||
"type": "boolean", | ||
"description": "Whether to format files.", | ||
"default": false | ||
} | ||
} | ||
} |