|
1 | | -import { logger, createProjectGraphAsync, ProjectGraph, Tree } from "@nx/devkit"; |
| 1 | +import { |
| 2 | + logger, |
| 3 | + createProjectGraphAsync, |
| 4 | + ProjectGraph, |
| 5 | + Tree, |
| 6 | +} from '@nx/devkit'; |
2 | 7 | import { prompt } from 'enquirer'; |
3 | 8 |
|
4 | | -import { NormalizedSchema } from "../schema"; |
5 | | -import { addGradleModule, addMavenModule, initGradleParentModule, initMavenParentModule, hasMultiModuleGradleProjectInTree, hasMultiModuleMavenProjectInTree, getAdjustedProjectAndModuleRoot } from "@nxrocks/common-jvm"; |
| 9 | +import { NormalizedSchema } from '../schema'; |
| 10 | +import { |
| 11 | + addGradleModule, |
| 12 | + addMavenModule, |
| 13 | + initGradleParentModule, |
| 14 | + initMavenParentModule, |
| 15 | + hasMultiModuleGradleProjectInTree, |
| 16 | + hasMultiModuleMavenProjectInTree, |
| 17 | + getAdjustedProjectAndModuleRoot, |
| 18 | +} from '@nxrocks/common-jvm'; |
6 | 19 |
|
7 | | -export async function promptForMultiModuleSupport(tree: Tree, options: NormalizedSchema) { |
| 20 | +export async function promptForMultiModuleSupport( |
| 21 | + tree: Tree, |
| 22 | + options: NormalizedSchema |
| 23 | +) { |
| 24 | + const buildSystemName = options.buildSystem === 'MAVEN' ? 'Maven' : 'Gradle'; |
8 | 25 |
|
9 | | - const buildSystemName = options.buildSystem === 'MAVEN' ? 'Maven' : 'Gradle'; |
| 26 | + if ( |
| 27 | + (options.transformIntoMultiModule === undefined || |
| 28 | + options.addToExistingParentModule === undefined) && |
| 29 | + options.parentModuleName === undefined && |
| 30 | + process.env.NX_INTERACTIVE === 'true' |
| 31 | + ) { |
| 32 | + logger.info( |
| 33 | + `⏳ Checking for existing multi-module projects. Please wait...` |
| 34 | + ); |
10 | 35 |
|
11 | | - if ( |
12 | | - (options.transformIntoMultiModule === undefined || options.addToExistingParentModule === undefined) && |
13 | | - options.parentModuleName === undefined && |
14 | | - process.env.NX_INTERACTIVE === 'true' |
15 | | - ) { |
16 | | - logger.info(`⏳ Checking for existing multi-module projects. Please wait...`); |
| 36 | + const projectGraph: ProjectGraph = await createProjectGraphAsync(); |
17 | 37 |
|
18 | | - const projectGraph: ProjectGraph = await createProjectGraphAsync(); |
| 38 | + const multiModuleProjects = Object.values(projectGraph.nodes) |
| 39 | + .map((n) => n.data) |
| 40 | + .filter((project) => |
| 41 | + options.buildSystem === 'MAVEN' |
| 42 | + ? hasMultiModuleMavenProjectInTree(tree, project.root) |
| 43 | + : hasMultiModuleGradleProjectInTree(tree, project.root) |
| 44 | + ); |
19 | 45 |
|
20 | | - const multiModuleProjects = Object.values(projectGraph.nodes).map(n => n.data).filter(project => options.buildSystem === 'MAVEN' ? hasMultiModuleMavenProjectInTree(tree, project.root) : hasMultiModuleGradleProjectInTree(tree, project.root)) |
| 46 | + if (multiModuleProjects.length === 0) { |
| 47 | + options.transformIntoMultiModule = await prompt({ |
| 48 | + name: 'transformIntoMultiModule', |
| 49 | + message: `Would you like to transform the generated project into a ${buildSystemName} multi-module project?`, |
| 50 | + type: 'confirm', |
| 51 | + initial: false, |
| 52 | + }).then((a) => a['transformIntoMultiModule']); |
21 | 53 |
|
22 | | - if (multiModuleProjects.length === 0) { |
23 | | - options.transformIntoMultiModule = await prompt({ |
24 | | - name: 'transformIntoMultiModule', |
25 | | - message: |
26 | | - `Would you like to transform the generated project into a ${buildSystemName} multi-module project?`, |
27 | | - type: 'confirm', |
28 | | - initial: false |
29 | | - }).then((a) => a['transformIntoMultiModule']); |
| 54 | + if (options.transformIntoMultiModule) { |
| 55 | + options.parentModuleName = ( |
| 56 | + await prompt({ |
| 57 | + name: 'parentModuleName', |
| 58 | + message: `What name would you like to use for the ${buildSystemName} multi-module project?`, |
| 59 | + type: 'input', |
| 60 | + initial: `${options.projectName}-parent`, |
| 61 | + }).then((a) => a['parentModuleName']) |
| 62 | + ).replace(/\//g, '-'); |
30 | 63 |
|
31 | | - if (options.transformIntoMultiModule) { |
32 | | - options.parentModuleName = (await prompt({ |
33 | | - name: 'parentModuleName', |
34 | | - message: |
35 | | - `What name would you like to use for the ${buildSystemName} multi-module project?`, |
36 | | - type: 'input', |
37 | | - initial: `${options.projectName}-parent` |
38 | | - }).then((a) => a['parentModuleName'])).replace(/\//g, '-'); |
39 | | - |
40 | | - options.keepProjectLevelWrapper ??= false; |
41 | | - } |
42 | | - } |
43 | | - else { |
44 | | - options.addToExistingParentModule = await prompt({ |
45 | | - name: 'addToExistingParentModule', |
46 | | - message: |
47 | | - `We found ${multiModuleProjects.length} existing ${buildSystemName} multi-module projects in your workaspace${multiModuleProjects.length === 1 ? `('${multiModuleProjects[0].name}')` : ''}.\nWould you like to add this new project ${multiModuleProjects.length === 1 ? 'to it?' : 'into one of them?'}`, |
48 | | - type: 'confirm', |
49 | | - initial: false |
50 | | - }).then((a) => a['addToExistingParentModule']); |
51 | | - |
52 | | - if (options.addToExistingParentModule) { |
53 | | - if (multiModuleProjects.length === 1) { |
54 | | - options.parentModuleName = multiModuleProjects[0].name; |
55 | | - } |
56 | | - else { |
57 | | - options.parentModuleName = await prompt({ |
58 | | - name: 'parentModuleName', |
59 | | - message: |
60 | | - 'Which parent module would you like to add the new project into?', |
61 | | - type: 'select', |
62 | | - choices: multiModuleProjects.map(p => p.name), |
63 | | - }).then((a) => a['parentModuleName']); |
64 | | - } |
65 | | - } |
| 64 | + options.keepProjectLevelWrapper ??= true; |
| 65 | + } |
| 66 | + } else { |
| 67 | + options.addToExistingParentModule = await prompt({ |
| 68 | + name: 'addToExistingParentModule', |
| 69 | + message: `We found ${ |
| 70 | + multiModuleProjects.length |
| 71 | + } existing ${buildSystemName} multi-module projects in your workaspace${ |
| 72 | + multiModuleProjects.length === 1 |
| 73 | + ? `('${multiModuleProjects[0].name}')` |
| 74 | + : '' |
| 75 | + }.\nWould you like to add this new project ${ |
| 76 | + multiModuleProjects.length === 1 ? 'to it?' : 'into one of them?' |
| 77 | + }`, |
| 78 | + type: 'confirm', |
| 79 | + initial: false, |
| 80 | + }).then((a) => a['addToExistingParentModule']); |
66 | 81 |
|
| 82 | + if (options.addToExistingParentModule) { |
| 83 | + if (multiModuleProjects.length === 1) { |
| 84 | + options.parentModuleName = multiModuleProjects[0].name; |
| 85 | + } else { |
| 86 | + options.parentModuleName = await prompt({ |
| 87 | + name: 'parentModuleName', |
| 88 | + message: |
| 89 | + 'Which parent module would you like to add the new project into?', |
| 90 | + type: 'select', |
| 91 | + choices: multiModuleProjects.map((p) => p.name), |
| 92 | + }).then((a) => a['parentModuleName']); |
67 | 93 | } |
| 94 | + } |
68 | 95 | } |
69 | | - if ((options.transformIntoMultiModule || options.addToExistingParentModule) && options.parentModuleName) { |
70 | | - const isMavenProject = options.buildSystem === 'MAVEN'; |
71 | | - const helpComment = `For more information about ${buildSystemName} multi-modules projects, go to: ${isMavenProject ? 'https://maven.apache.org/guides/mini/guide-multiple-modules-4.html' : 'https://docs.gradle.org/current/userguide/intro_multi_project_builds.html'}`; |
| 96 | + } |
| 97 | + if ( |
| 98 | + (options.transformIntoMultiModule || options.addToExistingParentModule) && |
| 99 | + options.parentModuleName |
| 100 | + ) { |
| 101 | + const isMavenProject = options.buildSystem === 'MAVEN'; |
| 102 | + const helpComment = `For more information about ${buildSystemName} multi-modules projects, go to: ${ |
| 103 | + isMavenProject |
| 104 | + ? 'https://maven.apache.org/guides/mini/guide-multiple-modules-4.html' |
| 105 | + : 'https://docs.gradle.org/current/userguide/intro_multi_project_builds.html' |
| 106 | + }`; |
72 | 107 |
|
73 | | - const opts = await getAdjustedProjectAndModuleRoot(options, isMavenProject); |
| 108 | + const opts = await getAdjustedProjectAndModuleRoot(options, isMavenProject); |
74 | 109 |
|
75 | | - options.projectRoot = opts.projectRoot; |
76 | | - options.moduleRoot = opts.moduleRoot; |
| 110 | + options.projectRoot = opts.projectRoot; |
| 111 | + options.moduleRoot = opts.moduleRoot; |
77 | 112 |
|
78 | | - if (options.transformIntoMultiModule) { |
79 | | - // add the root module |
80 | | - if (isMavenProject) { |
81 | | - initMavenParentModule(tree, options.moduleRoot, options.groupId, options.parentModuleName, options.projectName, `<!-- ${helpComment} -->`); |
82 | | - } |
83 | | - else { |
84 | | - initGradleParentModule(tree, options.moduleRoot, options.groupId, options.parentModuleName, options.projectName, opts.offsetFromRoot, options.buildSystem === 'GRADLE_KTS', `// ${helpComment}`); |
85 | | - } |
86 | | - } |
87 | | - else if (options.addToExistingParentModule) { |
88 | | - // add to the chosen root module |
89 | | - if (isMavenProject) { |
90 | | - addMavenModule(tree, options.moduleRoot, options.projectName); |
91 | | - } |
92 | | - else { |
93 | | - addGradleModule(tree, options.moduleRoot, options.projectName, opts.offsetFromRoot, options.buildSystem === 'GRADLE_KTS'); |
94 | | - } |
95 | | - } |
| 113 | + if (options.transformIntoMultiModule) { |
| 114 | + // add the root module |
| 115 | + if (isMavenProject) { |
| 116 | + initMavenParentModule( |
| 117 | + tree, |
| 118 | + options.moduleRoot, |
| 119 | + options.groupId, |
| 120 | + options.parentModuleName, |
| 121 | + options.projectName, |
| 122 | + `<!-- ${helpComment} -->` |
| 123 | + ); |
| 124 | + } else { |
| 125 | + initGradleParentModule( |
| 126 | + tree, |
| 127 | + options.moduleRoot, |
| 128 | + options.groupId, |
| 129 | + options.parentModuleName, |
| 130 | + options.projectName, |
| 131 | + opts.offsetFromRoot, |
| 132 | + options.buildSystem === 'GRADLE_KTS', |
| 133 | + `// ${helpComment}` |
| 134 | + ); |
| 135 | + } |
| 136 | + } else if (options.addToExistingParentModule) { |
| 137 | + // add to the chosen root module |
| 138 | + if (isMavenProject) { |
| 139 | + addMavenModule(tree, options.moduleRoot, options.projectName); |
| 140 | + } else { |
| 141 | + addGradleModule( |
| 142 | + tree, |
| 143 | + options.moduleRoot, |
| 144 | + options.projectName, |
| 145 | + opts.offsetFromRoot, |
| 146 | + options.buildSystem === 'GRADLE_KTS' |
| 147 | + ); |
| 148 | + } |
96 | 149 | } |
| 150 | + } |
97 | 151 | } |
0 commit comments