Skip to content

Commit

Permalink
Merge pull request #7 from czfadmin/feat/terminal
Browse files Browse the repository at this point in the history
Feat/terminal: 更新vscode插件需要版本, 修复相关BUG
  • Loading branch information
czfadmin authored Sep 7, 2024
2 parents bb157d9 + 65d98c5 commit 703b9e9
Show file tree
Hide file tree
Showing 6 changed files with 827 additions and 1,822 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## 0.1.5

### Patch Changes

- - fix(modules): 修复直接在资源管理器上创建文件, 选中选项丢失问题.
- fix(module): 修复存在多个应用情况下, 在apps目录上创建文件或者模块是时, 下拉模块出现其他应用的现象

## 0.1.4

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"displayName": "NestJS Tool",
"publisher": "czfadmin",
"description": "Quickly create NestJS Files tool",
"version": "0.1.4",
"version": "0.1.5",
"engines": {
"vscode": "^1.82.0"
"vscode": "^1.88.0"
},
"categories": [
"Other"
Expand Down Expand Up @@ -313,7 +313,7 @@
"@types/mocha": "^8.0.4",
"@types/mustache": "^4.1.0",
"@types/node": "^20.11.30",
"@types/vscode": "^1.52.0",
"@types/vscode": "^1.88.0",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"@vscode/l10n-dev": "^0.0.34",
Expand Down Expand Up @@ -345,4 +345,4 @@
"prettier --write"
]
}
}
}
69 changes: 56 additions & 13 deletions src/services/command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ExtensionContext,
l10n,
Terminal,
TerminalShellExecution,
Uri,
window,
} from 'vscode';
Expand Down Expand Up @@ -39,9 +40,13 @@ import {LoggerService} from './logger-service';
export default class CommandService implements Disposable {
private _context: ExtensionContext;

private _terminal: Terminal;
private _terminal?: Terminal;

private _execution: TerminalShellExecution | null = null;

private _logger: LoggerService;

private _disposers: Disposable[] = [];
private get configuration() {
return this.sm.configService.configuration;
}
Expand All @@ -52,12 +57,25 @@ export default class CommandService implements Disposable {
this._context = this.sm.context;
this._logger = LoggerService.createFactory(CommandService);

this._terminal =
window.terminals.find(it => it.name == EXTENSION_TERMINAL_NAME) ||
window.createTerminal({
this._terminal = window.terminals.find(
it => it.name == EXTENSION_TERMINAL_NAME,
);

if (!this._terminal) {
this._terminal = window.createTerminal({
name: EXTENSION_TERMINAL_NAME,
hideFromUser: shouldExecute,
});
}

// 判断当前的是否为之前打开的nestjstool的terminal
this._disposers.push(
window.onDidCloseTerminal(t => {
if (t.name === EXTENSION_TERMINAL_NAME) {
this._terminal = undefined;
}
}),
);

this._initial();
}
Expand Down Expand Up @@ -99,6 +117,7 @@ export default class CommandService implements Disposable {
application = await getApplicationFromUri(fileUri);

if (!isConfigCmd) {
// 当有多个子项目的时候, 选中其中一个Project然后继续操作
project = await getProjectFromUri(fileUri, application);

if (!project && application) {
Expand All @@ -120,6 +139,7 @@ export default class CommandService implements Disposable {

// 获取从当前文件夹中创建的是否存在多个模块, 如果是模块, 直接在此某块块中创建, 反之选择模块
const modules = await checkoutFolderIsModule(
project,
'fsPath' in args[0] ? args[0] : undefined,
);

Expand Down Expand Up @@ -179,6 +199,8 @@ export default class CommandService implements Disposable {

/**
* 判断用户的输入内容,并结合用户的配置,生成最终的命令
*
*
* @param userInput 用户输入的内容
* @returns 处理后的用户输入
*/
Expand Down Expand Up @@ -262,33 +284,54 @@ export default class CommandService implements Disposable {
...args: any[]
) {
const {showTerminal, shouldExecute} = this.sm.configService.configuration;
if (
this._terminal.exitStatus &&
this._terminal.exitStatus.code === undefined
) {
if (!this._terminal) {
this._terminal = window.createTerminal({
name: EXTENSION_TERMINAL_NAME,
hideFromUser: shouldExecute,
});
}
let sendText = `nest g ${alias} ${args.join(' ')}`;

let commandLine = `nest g ${alias} ${args.join(' ')}`;
if (application) {
const distPath = resolve(
application.workspace.uri.fsPath,
application.name === 'root' ? '' : application.name,
);
sendText = `cd ${distPath} && ${sendText}`;
commandLine = `cd ${distPath} && ${commandLine}`;
}

if (this._terminal.shellIntegration) {
this._execution =
this._terminal.shellIntegration.executeCommand(commandLine);

this._disposers.push(
window.onDidEndTerminalShellExecution(e => {
if (e.execution === this._execution) {
this._logger.info(
`Command executed successfully!`,
e.execution.commandLine,
);
window.showInformationMessage(
`The \`${commandLine}\` was executed successfully!`,
);
}
}),
);
} else {
this._terminal.sendText(commandLine);
}
this._terminal.sendText(sendText);

if (showTerminal) {
this._terminal.show();
}
this._logger.info(`execute command: ${sendText}`);
this._logger.info(`execute command: ${commandLine}`);
}

dispose() {
this._terminal.dispose();
for (const d of this._disposers) {
d.dispose();
}
this._terminal?.dispose();
this._context.subscriptions.forEach(it => it.dispose());
}
}
25 changes: 18 additions & 7 deletions src/utils/modules.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'node:fs';
import {l10n, QuickPickItem, Uri, window} from 'vscode';
import {l10n, QuickPickItem, Uri, window, workspace} 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';
import path from 'node:path';

/**
* 列出指定项目(存在)的所有的模块
Expand Down Expand Up @@ -112,20 +113,31 @@ export async function getModulesQuickPick2(modules: IModule[]) {
return modules.find(module => module.name === selectedModule.label);
}

export async function checkoutFolderIsModule(p?: Uri) {
/**
*
* @param project 用户选择的子项目
* @param p 选择的文件上下文, 当直接在资源管理器视图中右键操作为undefined
* @returns
*/
export async function checkoutFolderIsModule(project?: INestProject, p?: Uri) {
if (!p) {
return;
return [];
}

if (fs.statSync(p.fsPath).isFile()) {
return;
return [];
}

let _modules: IModule[] = [];

// 从当前的项目中的sourceRoot下选择对应的module出来
const cwd = !project
? p.fsPath
: Uri.joinPath(project.workspace.uri, project.root || '').fsPath;

const entries = fg.globSync([`**/*.module.(t|j)s`], {
cwd: p.fsPath,
deep: 2,
cwd: cwd,
deep: 4,
dot: false,
absolute: true,
});
Expand All @@ -144,6 +156,5 @@ export async function checkoutFolderIsModule(p?: Uri) {
});
}
}
console.log(_modules);
return _modules;
}
4 changes: 2 additions & 2 deletions src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {getAllNestOptions} from './options';
import {resolve} from './path';

/**
* 通过读取nest-cli.json文件判断工作文件中是否存在多个project
* @returns {[boolean, any[]]}
* 通过读取nest-cli.json文件, 判断工作文件中是否存在并返回对饮的多个project
* @returns {[boolean, INestProject[]]}
*/
export async function getAllNestProjects(
application?: INestApplication,
Expand Down
Loading

0 comments on commit 703b9e9

Please sign in to comment.