nexushub-commands 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/shared-commands/ParserSharedCommands.service.d.ts +18 -0
- package/lib/shared-commands/ParserSharedCommands.service.js +87 -15
- package/lib/test-imports.d.ts +1 -0
- package/lib/test-imports.js +29 -0
- package/package.json +1 -1
- package/src/shared-commands/ParserSharedCommands.service.ts +101 -18
- package/test-imports.d.ts +1 -0
- package/test.d.ts +5 -0
|
@@ -11,6 +11,24 @@ export declare class ParserSharedCommandsService {
|
|
|
11
11
|
* @returns преобразованный код команды
|
|
12
12
|
*/
|
|
13
13
|
static transformCommand(commandContent: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Проверяет, является ли импорт локальным (относительным путем)
|
|
16
|
+
* @param importPath - путь импорта
|
|
17
|
+
* @returns true, если импорт локальный
|
|
18
|
+
*/
|
|
19
|
+
private static isLocalImport;
|
|
20
|
+
/**
|
|
21
|
+
* Удаляет только локальные импорты, сохраняя нелокальные
|
|
22
|
+
* @param content - исходный код
|
|
23
|
+
* @returns код с удаленными локальными импортами
|
|
24
|
+
*/
|
|
25
|
+
private static removeLocalImports;
|
|
26
|
+
/**
|
|
27
|
+
* Удаляет все оставшиеся приватные методы с умным поиском
|
|
28
|
+
* @param content - код для очистки
|
|
29
|
+
* @returns код с удаленными приватными методами
|
|
30
|
+
*/
|
|
31
|
+
private static removeRemainingPrivateMethods;
|
|
14
32
|
/**
|
|
15
33
|
* Создает API вызов для метода
|
|
16
34
|
* @param methodName - название метода
|
|
@@ -25,8 +25,8 @@ class ParserSharedCommandsService {
|
|
|
25
25
|
* @returns преобразованный код команды
|
|
26
26
|
*/
|
|
27
27
|
static transformCommand(commandContent) {
|
|
28
|
-
// Убираем
|
|
29
|
-
let transformedContent =
|
|
28
|
+
// Убираем только локальные импорты (относительные пути), сохраняем нелокальные (npm пакеты)
|
|
29
|
+
let transformedContent = this.removeLocalImports(commandContent);
|
|
30
30
|
// Заменяем extends на SharedCommand
|
|
31
31
|
transformedContent = transformedContent.replace(/extends\s+\w+/g, 'extends SharedCommand');
|
|
32
32
|
// Находим все приватные методы (включая статические)
|
|
@@ -40,13 +40,7 @@ class ParserSharedCommandsService {
|
|
|
40
40
|
const className = this.parseClassName(commandContent);
|
|
41
41
|
// Заменяем вызовы приватных методов на API вызовы
|
|
42
42
|
privateMethods.forEach((methodName) => {
|
|
43
|
-
// Паттерн для поиска вызовов this.methodName(...) и ClassName.methodName(...)
|
|
44
|
-
const thisMethodCallRegex = new RegExp(`this\\.${methodName}\\s*\\(([^)]*)\\)`, 'g');
|
|
45
43
|
const staticMethodCallRegex = new RegExp(`${className}\\.${methodName}\\s*\\(([^)]*)\\)`, 'g');
|
|
46
|
-
// Заменяем вызовы this.methodName(...)
|
|
47
|
-
transformedContent = transformedContent.replace(thisMethodCallRegex, (match, args) => {
|
|
48
|
-
return this.createApiCall(methodName, args);
|
|
49
|
-
});
|
|
50
44
|
// Заменяем вызовы ClassName.methodName(...)
|
|
51
45
|
if (className) {
|
|
52
46
|
transformedContent = transformedContent.replace(staticMethodCallRegex, (match, args) => {
|
|
@@ -54,17 +48,94 @@ class ParserSharedCommandsService {
|
|
|
54
48
|
});
|
|
55
49
|
}
|
|
56
50
|
});
|
|
57
|
-
// Удаляем все приватные методы по одному
|
|
51
|
+
// Удаляем все приватные методы по одному - разбиваем на линии и ищем границы
|
|
58
52
|
privateMethods.forEach((methodName) => {
|
|
59
|
-
const
|
|
60
|
-
|
|
53
|
+
const lines = transformedContent.split('\n');
|
|
54
|
+
let methodStartIndex = -1;
|
|
55
|
+
let methodEndIndex = -1;
|
|
56
|
+
// Ищем начало метода
|
|
57
|
+
for (let i = 0; i < lines.length; i++) {
|
|
58
|
+
const line = lines[i];
|
|
59
|
+
if (line.includes(`private`) && line.includes(`${methodName}(`) && line.includes('{')) {
|
|
60
|
+
methodStartIndex = i;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Если нашли начало, ищем конец (строку с \t})
|
|
65
|
+
if (methodStartIndex !== -1) {
|
|
66
|
+
for (let i = methodStartIndex + 1; i < lines.length; i++) {
|
|
67
|
+
const line = lines[i];
|
|
68
|
+
if (line.trim() === '}') {
|
|
69
|
+
methodEndIndex = i;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Если нашли конец, удаляем метод
|
|
74
|
+
if (methodEndIndex !== -1) {
|
|
75
|
+
lines.splice(methodStartIndex, methodEndIndex - methodStartIndex + 1);
|
|
76
|
+
transformedContent = lines.join('\n');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
61
79
|
});
|
|
62
|
-
//
|
|
63
|
-
transformedContent =
|
|
64
|
-
// Убираем лишние пустые строки
|
|
65
|
-
transformedContent = transformedContent.replace(/\n\s*\n\s*\n/g, '\n\n');
|
|
80
|
+
// Дополнительная очистка - убираем все оставшиеся приватные методы с умным поиском
|
|
81
|
+
transformedContent = this.removeRemainingPrivateMethods(transformedContent);
|
|
66
82
|
return transformedContent;
|
|
67
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Проверяет, является ли импорт локальным (относительным путем)
|
|
86
|
+
* @param importPath - путь импорта
|
|
87
|
+
* @returns true, если импорт локальный
|
|
88
|
+
*/
|
|
89
|
+
static isLocalImport(importPath) {
|
|
90
|
+
return importPath.startsWith('./') || importPath.startsWith('../') || importPath.startsWith('/');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Удаляет только локальные импорты, сохраняя нелокальные
|
|
94
|
+
* @param content - исходный код
|
|
95
|
+
* @returns код с удаленными локальными импортами
|
|
96
|
+
*/
|
|
97
|
+
static removeLocalImports(content) {
|
|
98
|
+
// Убираем импорты с относительными путями: './file', '../folder/file'
|
|
99
|
+
let result = content.replace(/import\s+.*?from\s+['"]\.\.?\/[^'"]*['"];?\n?/g, '');
|
|
100
|
+
// Убираем импорты с абсолютными путями к локальным файлам: '/path/to/file'
|
|
101
|
+
result = result.replace(/import\s+.*?from\s+['"]\/[^'"]*['"];?\n?/g, '');
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Удаляет все оставшиеся приватные методы с умным поиском
|
|
106
|
+
* @param content - код для очистки
|
|
107
|
+
* @returns код с удаленными приватными методами
|
|
108
|
+
*/
|
|
109
|
+
static removeRemainingPrivateMethods(content) {
|
|
110
|
+
let result = content;
|
|
111
|
+
const privateMethodRegex = /private\s+(?:static\s+)?(?:async\s+)?\w+\s*\([^)]*\)\s*\{/g;
|
|
112
|
+
let match;
|
|
113
|
+
while ((match = privateMethodRegex.exec(result)) !== null) {
|
|
114
|
+
const startIndex = match.index;
|
|
115
|
+
const startBraceIndex = result.indexOf('{', startIndex);
|
|
116
|
+
if (startBraceIndex !== -1) {
|
|
117
|
+
// Находим конец метода, подсчитывая скобки
|
|
118
|
+
let braceCount = 1;
|
|
119
|
+
let endIndex = startBraceIndex + 1;
|
|
120
|
+
while (braceCount > 0 && endIndex < result.length) {
|
|
121
|
+
const char = result[endIndex];
|
|
122
|
+
if (char === '{')
|
|
123
|
+
braceCount++;
|
|
124
|
+
else if (char === '}')
|
|
125
|
+
braceCount--;
|
|
126
|
+
endIndex++;
|
|
127
|
+
}
|
|
128
|
+
if (braceCount === 0) {
|
|
129
|
+
// Удаляем весь метод от начала до конца
|
|
130
|
+
const methodEndIndex = endIndex;
|
|
131
|
+
result = result.substring(0, startIndex) + result.substring(methodEndIndex);
|
|
132
|
+
// Сбрасываем регулярное выражение, так как индексы изменились
|
|
133
|
+
privateMethodRegex.lastIndex = 0;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
68
139
|
/**
|
|
69
140
|
* Создает API вызов для метода
|
|
70
141
|
* @param methodName - название метода
|
|
@@ -183,3 +254,4 @@ class ParserSharedCommandsService {
|
|
|
183
254
|
}
|
|
184
255
|
}
|
|
185
256
|
exports.ParserSharedCommandsService = ParserSharedCommandsService;
|
|
257
|
+
fs.writeFileSync('test.ts', ParserSharedCommandsService.transformCommand(fs.readFileSync('../escort/src/commands/shared/models/CreateModel.srdcmd.ts', 'utf-8')));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const ParserSharedCommands_service_1 = require("./src/shared-commands/ParserSharedCommands.service");
|
|
4
|
+
// Тестовый код с разными типами импортов
|
|
5
|
+
const testCode = `
|
|
6
|
+
import * as fs from 'fs'
|
|
7
|
+
import * as path from 'path'
|
|
8
|
+
import { Injectable } from '@nestjs/common'
|
|
9
|
+
import { Command } from './Command'
|
|
10
|
+
import { BaseService } from '../services/BaseService'
|
|
11
|
+
import { Utils } from '/src/utils/Utils'
|
|
12
|
+
import { Config } from './config/Config'
|
|
13
|
+
|
|
14
|
+
class TestCommand extends Command {
|
|
15
|
+
private async processData() {
|
|
16
|
+
return fs.readFileSync('test.txt', 'utf-8')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private static validateInput(input: string) {
|
|
20
|
+
return path.isAbsolute(input)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
`;
|
|
24
|
+
console.log('Исходный код:');
|
|
25
|
+
console.log(testCode);
|
|
26
|
+
console.log('\n' + '='.repeat(50) + '\n');
|
|
27
|
+
console.log('Преобразованный код:');
|
|
28
|
+
const transformed = ParserSharedCommands_service_1.ParserSharedCommandsService.transformCommand(testCode);
|
|
29
|
+
console.log(transformed);
|
package/package.json
CHANGED
|
@@ -22,8 +22,8 @@ export class ParserSharedCommandsService {
|
|
|
22
22
|
* @returns преобразованный код команды
|
|
23
23
|
*/
|
|
24
24
|
static transformCommand(commandContent: string): string {
|
|
25
|
-
// Убираем
|
|
26
|
-
let transformedContent =
|
|
25
|
+
// Убираем только локальные импорты (относительные пути), сохраняем нелокальные (npm пакеты)
|
|
26
|
+
let transformedContent = this.removeLocalImports(commandContent)
|
|
27
27
|
|
|
28
28
|
// Заменяем extends на SharedCommand
|
|
29
29
|
transformedContent = transformedContent.replace(/extends\s+\w+/g, 'extends SharedCommand')
|
|
@@ -42,38 +42,119 @@ export class ParserSharedCommandsService {
|
|
|
42
42
|
|
|
43
43
|
// Заменяем вызовы приватных методов на API вызовы
|
|
44
44
|
privateMethods.forEach((methodName) => {
|
|
45
|
-
// Паттерн для поиска вызовов this.methodName(...) и ClassName.methodName(...)
|
|
46
|
-
const thisMethodCallRegex = new RegExp(`this\\.${methodName}\\s*\\(([^)]*)\\)`, 'g')
|
|
47
45
|
const staticMethodCallRegex = new RegExp(`${className}\\.${methodName}\\s*\\(([^)]*)\\)`, 'g')
|
|
48
46
|
|
|
49
|
-
// Заменяем вызовы this.methodName(...)
|
|
50
|
-
transformedContent = transformedContent.replace(thisMethodCallRegex, (match, args) => {
|
|
51
|
-
return this.createApiCall(methodName, args)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
47
|
// Заменяем вызовы ClassName.methodName(...)
|
|
55
48
|
if (className) {
|
|
56
|
-
transformedContent = transformedContent.replace(staticMethodCallRegex, (match, args) => {
|
|
49
|
+
transformedContent = transformedContent.replace(staticMethodCallRegex, (match: string, args: string) => {
|
|
57
50
|
return this.createApiCall(methodName, args)
|
|
58
51
|
})
|
|
59
52
|
}
|
|
60
53
|
})
|
|
61
54
|
|
|
62
|
-
// Удаляем все приватные методы по одному
|
|
55
|
+
// Удаляем все приватные методы по одному - разбиваем на линии и ищем границы
|
|
63
56
|
privateMethods.forEach((methodName) => {
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
57
|
+
const lines = transformedContent.split('\n')
|
|
58
|
+
let methodStartIndex = -1
|
|
59
|
+
let methodEndIndex = -1
|
|
67
60
|
|
|
68
|
-
|
|
69
|
-
|
|
61
|
+
// Ищем начало метода
|
|
62
|
+
for (let i = 0; i < lines.length; i++) {
|
|
63
|
+
const line = lines[i]
|
|
64
|
+
if (line.includes(`private`) && line.includes(`${methodName}(`) && line.includes('{')) {
|
|
65
|
+
methodStartIndex = i
|
|
66
|
+
break
|
|
67
|
+
}
|
|
68
|
+
}
|
|
70
69
|
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
// Если нашли начало, ищем конец (строку с \t})
|
|
71
|
+
if (methodStartIndex !== -1) {
|
|
72
|
+
for (let i = methodStartIndex + 1; i < lines.length; i++) {
|
|
73
|
+
const line = lines[i]
|
|
74
|
+
if (line.trim() === '}') {
|
|
75
|
+
methodEndIndex = i
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Если нашли конец, удаляем метод
|
|
81
|
+
if (methodEndIndex !== -1) {
|
|
82
|
+
lines.splice(methodStartIndex, methodEndIndex - methodStartIndex + 1)
|
|
83
|
+
transformedContent = lines.join('\n')
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
// Дополнительная очистка - убираем все оставшиеся приватные методы с умным поиском
|
|
89
|
+
transformedContent = this.removeRemainingPrivateMethods(transformedContent)
|
|
73
90
|
|
|
74
91
|
return transformedContent
|
|
75
92
|
}
|
|
76
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Проверяет, является ли импорт локальным (относительным путем)
|
|
96
|
+
* @param importPath - путь импорта
|
|
97
|
+
* @returns true, если импорт локальный
|
|
98
|
+
*/
|
|
99
|
+
private static isLocalImport(importPath: string) {
|
|
100
|
+
return importPath.startsWith('./') || importPath.startsWith('../') || importPath.startsWith('/')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Удаляет только локальные импорты, сохраняя нелокальные
|
|
105
|
+
* @param content - исходный код
|
|
106
|
+
* @returns код с удаленными локальными импортами
|
|
107
|
+
*/
|
|
108
|
+
private static removeLocalImports(content: string): string {
|
|
109
|
+
// Убираем импорты с относительными путями: './file', '../folder/file'
|
|
110
|
+
let result = content.replace(/import\s+.*?from\s+['"]\.\.?\/[^'"]*['"];?\n?/g, '')
|
|
111
|
+
|
|
112
|
+
// Убираем импорты с абсолютными путями к локальным файлам: '/path/to/file'
|
|
113
|
+
result = result.replace(/import\s+.*?from\s+['"]\/[^'"]*['"];?\n?/g, '')
|
|
114
|
+
|
|
115
|
+
return result
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Удаляет все оставшиеся приватные методы с умным поиском
|
|
120
|
+
* @param content - код для очистки
|
|
121
|
+
* @returns код с удаленными приватными методами
|
|
122
|
+
*/
|
|
123
|
+
private static removeRemainingPrivateMethods(content: string): string {
|
|
124
|
+
let result = content
|
|
125
|
+
const privateMethodRegex = /private\s+(?:static\s+)?(?:async\s+)?\w+\s*\([^)]*\)\s*\{/g
|
|
126
|
+
let match
|
|
127
|
+
|
|
128
|
+
while ((match = privateMethodRegex.exec(result)) !== null) {
|
|
129
|
+
const startIndex = match.index
|
|
130
|
+
const startBraceIndex = result.indexOf('{', startIndex)
|
|
131
|
+
|
|
132
|
+
if (startBraceIndex !== -1) {
|
|
133
|
+
// Находим конец метода, подсчитывая скобки
|
|
134
|
+
let braceCount = 1
|
|
135
|
+
let endIndex = startBraceIndex + 1
|
|
136
|
+
|
|
137
|
+
while (braceCount > 0 && endIndex < result.length) {
|
|
138
|
+
const char = result[endIndex]
|
|
139
|
+
if (char === '{') braceCount++
|
|
140
|
+
else if (char === '}') braceCount--
|
|
141
|
+
endIndex++
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (braceCount === 0) {
|
|
145
|
+
// Удаляем весь метод от начала до конца
|
|
146
|
+
const methodEndIndex = endIndex
|
|
147
|
+
result = result.substring(0, startIndex) + result.substring(methodEndIndex)
|
|
148
|
+
|
|
149
|
+
// Сбрасываем регулярное выражение, так как индексы изменились
|
|
150
|
+
privateMethodRegex.lastIndex = 0
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return result
|
|
156
|
+
}
|
|
157
|
+
|
|
77
158
|
/**
|
|
78
159
|
* Создает API вызов для метода
|
|
79
160
|
* @param methodName - название метода
|
|
@@ -198,3 +279,5 @@ export class ParserSharedCommandsService {
|
|
|
198
279
|
return results
|
|
199
280
|
}
|
|
200
281
|
}
|
|
282
|
+
|
|
283
|
+
fs.writeFileSync('test.ts', ParserSharedCommandsService.transformCommand(fs.readFileSync('../escort/src/commands/shared/models/CreateModel.srdcmd.ts', 'utf-8')))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/test.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { InlineQueryContext } from 'evogram/lib/migrated';
|
|
2
|
+
export declare class EscortCreateModelCommand extends SharedCommand {
|
|
3
|
+
execute(context: CommandContext, name: string, age: number, about: string, services: string[], rating: number, specifications: string[], _: true): Promise<any>;
|
|
4
|
+
inlineExecute(context: InlineQueryContext, ...args: any): Promise<void>;
|
|
5
|
+
}
|