Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref(command): 优化在模块中创建对应的资源操作命令 #6

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 0.1.4

### Patch Changes

- 优化模块选择逻辑

## 0.1.3

### Patch Changes
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "NestJS Tool",
"publisher": "czfadmin",
"description": "Quickly create NestJS Files tool",
"version": "0.1.3",
"version": "0.1.4",
"engines": {
"vscode": "^1.82.0"
},
Expand Down Expand Up @@ -283,7 +283,7 @@
]
},
"scripts": {
"vscode:prepublish": "yarn compile",
"vscode:prepublish": "yarn build",
"build": "webpack",
"watch": "webpack --watch",
"package": "cp -r src/templates ./out &&webpack --mode production --devtool hidden-source-map",
Expand Down 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
Loading