diff --git a/package.json b/package.json index b3c8a41..8d46398 100644 --- a/package.json +++ b/package.json @@ -333,6 +333,7 @@ "webpack-cli": "^5.1.4" }, "dependencies": { + "fast-glob": "^3.3.2", "fs-extra": "^9.0.1", "mustache": "^4.1.0" }, @@ -344,4 +345,4 @@ "prettier --write" ] } -} \ No newline at end of file +} diff --git a/src/services/command-service.ts b/src/services/command-service.ts index a7d629b..61a096c 100644 --- a/src/services/command-service.ts +++ b/src/services/command-service.ts @@ -15,7 +15,9 @@ import { } from '../constants'; import {COMMANDS} from '../constants'; import { + checkoutFolderIsModule, getModulesQuickPick, + getModulesQuickPick2, getProjectFromUri, resolve, showCommandsQuickPick, @@ -95,7 +97,7 @@ export default class CommandService implements Disposable { } application = await getApplicationFromUri(fileUri); - + if (!isConfigCmd) { project = await getProjectFromUri(fileUri, application); @@ -116,10 +118,22 @@ export default class CommandService implements Disposable { } } - if (!isConfigCmd) { - // 从app中选取模块进行将生成的文件添加到指定的模块中 - selectedModule = await getModulesQuickPick(application!, project); + // 获取从当前文件夹中创建的是否存在多个模块, 如果是模块, 直接在此某块块中创建, 反之选择模块 + const modules = await checkoutFolderIsModule( + 'fsPath' in args[0] ? args[0] : undefined, + ); + + if ((modules && modules.length) || (!isConfigCmd && !modules)) { + if (modules && modules.length) { + selectedModule = await getModulesQuickPick2(modules); + } else { + // 从app中选取模块进行将生成的文件添加到指定的模块中 + selectedModule = await getModulesQuickPick(application!, project); + } + if (!selectedModule) { + return; + } userInput = this._buildCommand( cmd, userInput, @@ -191,7 +205,8 @@ export default class CommandService implements Disposable { !_userInput.startsWith('-') && module && module.name && - module.name !== 'None' + module.name !== 'None' && + module.name !== 'app' ) { _userInput = `${module.name}/${_userInput}`; } @@ -265,6 +280,7 @@ export default class CommandService implements Disposable { sendText = `cd ${distPath} && ${sendText}`; } this._terminal.sendText(sendText); + if (showTerminal) { this._terminal.show(); } diff --git a/src/utils/modules.ts b/src/utils/modules.ts index 9d17f94..48fc6a4 100644 --- a/src/utils/modules.ts +++ b/src/utils/modules.ts @@ -1,8 +1,11 @@ import fs from 'node:fs'; -import {IModule, INestApplication, INestProject} from '../types/nest-cli'; import {l10n, QuickPickItem, Uri, window} from 'vscode'; +import fg from 'fast-glob'; +import {IModule, INestApplication, INestProject} from '../types/nest-cli'; import {joinPath, resolve} from './path'; import {statSync} from './fs'; +import {getApplicationFromUri} from './application'; +import {getProjectFromUri} from './project'; /** * 列出指定项目(存在)的所有的模块 @@ -19,11 +22,21 @@ export function getAllModules( project ? project.root! : '', 'src', ); + const allFiles = fs.readdirSync(targetPath); // 遍历目录下方的文件夹中是否存在module.ts/js 文件 for (const file of allFiles) { const filePath = resolve(targetPath, file); + if (file === 'app.module.ts') { + modules.push({ + name: 'app', + moduleRoot: project ? `${project.sourceRoot}/app` : 'app', + moduleEntry: `${project ? project.sourceRoot + '/' : ''}app.module.ts`, + project: project, + }); + continue; + } if (statSync(filePath).isFile()) { continue; } @@ -71,8 +84,19 @@ export async function getModulesQuickPick( return; } + return await getModulesQuickPick2(modules); +} + +export async function getModulesQuickPick2(modules: IModule[]) { + if (modules.length === 0) { + return; + } + if (modules.length === 1) { + return modules[0]; + } + const moduleQuickItems = getModulePickItems(modules); - // 如果选择了一个module 将会在生成命令的时候追加到指定的module中 + // 如果选择了一个module将会在生成命令的时候追加到指定的module中 const selectedModule = await window.showQuickPick(moduleQuickItems, { placeHolder: l10n.t('Please select a module'), matchOnDescription: true, @@ -81,5 +105,45 @@ export async function getModulesQuickPick( if (!selectedModule) { return; } + + if (selectedModule.label === 'None') { + return modules[0]; + } return modules.find(module => module.name === selectedModule.label); } + +export async function checkoutFolderIsModule(p?: Uri) { + if (!p) { + return; + } + + if (fs.statSync(p.fsPath).isFile()) { + return; + } + + let _modules: IModule[] = []; + + const entries = fg.globSync([`**/*.module.(t|j)s`], { + cwd: p.fsPath, + deep: 2, + dot: false, + absolute: true, + }); + + if (entries.length) { + const application = await getApplicationFromUri(p); + const project = await getProjectFromUri(p, application); + for (let entry of entries) { + const arr = entry.split('/'); + const name = arr[arr.length - 1].split('.')[0]; + _modules.push({ + name, + moduleRoot: p.fsPath, + moduleEntry: entry, + project: project, + }); + } + } + console.log(_modules); + return _modules; +} diff --git a/src/utils/project.ts b/src/utils/project.ts index ba75773..03b4dfd 100644 --- a/src/utils/project.ts +++ b/src/utils/project.ts @@ -84,8 +84,12 @@ export async function showProjectQuickPick(application: INestApplication) { const projects = await getAllNestProjects(application); const pickitems = getAllNestProjectPickItems(projects); + if (!pickitems.length) { + return; + } + const selectedItem = await window.showQuickPick(pickitems, { - placeHolder:l10n.t("Please select a project"), + placeHolder: l10n.t('Please select a project'), matchOnDescription: true, matchOnDetail: true, });