Skip to content

Commit

Permalink
chore(command): 优化在模块中创建对应的资源操作命令
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed Apr 25, 2024
1 parent e15b139 commit 22c5342
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"fast-glob": "^3.3.2",
"fs-extra": "^9.0.1",
"mustache": "^4.1.0"
},
Expand All @@ -344,4 +345,4 @@
"prettier --write"
]
}
}
}
26 changes: 21 additions & 5 deletions src/services/command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
} from '../constants';
import {COMMANDS} from '../constants';
import {
checkoutFolderIsModule,
getModulesQuickPick,
getModulesQuickPick2,
getProjectFromUri,
resolve,
showCommandsQuickPick,
Expand Down Expand Up @@ -95,7 +97,7 @@ export default class CommandService implements Disposable {
}

application = await getApplicationFromUri(fileUri);

if (!isConfigCmd) {
project = await getProjectFromUri(fileUri, application);

Expand All @@ -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,
Expand Down Expand Up @@ -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}`;
}
Expand Down Expand Up @@ -265,6 +280,7 @@ export default class CommandService implements Disposable {
sendText = `cd ${distPath} && ${sendText}`;
}
this._terminal.sendText(sendText);

if (showTerminal) {
this._terminal.show();
}
Expand Down
68 changes: 66 additions & 2 deletions src/utils/modules.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
* 列出指定项目(存在)的所有的模块
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
6 changes: 5 additions & 1 deletion src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 22c5342

Please sign in to comment.