forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresultHelper.ts
More file actions
251 lines (219 loc) · 6.61 KB
/
resultHelper.ts
File metadata and controls
251 lines (219 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { ResultFile, ResultDir } from '@alilc/lowcode-types';
import nm from 'nanomatch';
import { CodeGeneratorError } from '../types/error';
import { FlattenFile } from '../types/file';
export function createResultFile(name: string, ext = 'jsx', content = ''): ResultFile {
return {
name,
ext,
content,
};
}
export function createResultDir(name: string): ResultDir {
return {
name,
dirs: [],
files: [],
};
}
export function addDirectory(target: ResultDir, dir: ResultDir): void {
if (target.dirs.findIndex((d) => d.name === dir.name) < 0) {
target.dirs.push(dir);
} else {
throw new CodeGeneratorError(
`Adding same directory to one directory: ${dir.name} -> ${target.name}`,
);
}
}
export function addFile(target: ResultDir, file: ResultFile): void {
if (target.files.findIndex((f) => f.name === file.name && f.ext === file.ext) < 0) {
target.files.push(file);
} else {
throw new CodeGeneratorError(
`Adding same file to one directory: ${file.name} -> ${target.name}`,
);
}
}
export function flattenResult(dir: ResultDir, cwd = ''): FlattenFile[] {
if (!dir.files.length && !dir.dirs.length) {
return [];
}
return [
...dir.files.map(
(file): FlattenFile => ({
pathName: joinPath(cwd, `${file.name}${file.ext ? `.${file.ext}` : ''}`),
content: file.content,
}),
),
].concat(...dir.dirs.map((subDir) => flattenResult(subDir, joinPath(cwd, subDir.name))));
}
export type GlobOptions = {
/** 是否查找 ".xxx" 文件, 默认: 否 */
dot?: boolean;
};
/**
* 查找文件
* @param result 出码结果
* @param fileGlobExpr 文件名匹配表达式
* @param resultDirPath 出码结果的路径(默认是 '.')
* @returns 匹配的第一个文件或 null (找不到)
*/
export function findFile(
result: ResultDir,
fileGlobExpr: string,
options: GlobOptions = {},
resultDirPath = getResultNameOrDefault(result, ''),
): ResultFile | null {
const maxDepth = !/\/|\*\*/.test(fileGlobExpr) ? 1 : undefined; // 如果 glob 表达式里面压根不会匹配子目录,则深度限制为 1
const files = scanFiles(result, resultDirPath, maxDepth);
for (let [filePath, file] of files) {
if (nm.isMatch(filePath, fileGlobExpr, options)) {
return file;
}
}
return null;
}
/**
* 使用 glob 语法查找多个文件
* @param result 出码结果
* @param fileGlobExpr 文件名匹配表达式
* @param resultDirPath 出码结果的路径(默认是 '.')
* @returns 找到的文件列表的迭代器 [ [文件路径, 文件信息], ... ]
*/
export function* globFiles(
result: ResultDir,
fileGlobExpr: string,
options: GlobOptions = {},
resultDirPath = getResultNameOrDefault(result, ''),
): IterableIterator<[string, ResultFile]> {
const files = scanFiles(result, resultDirPath);
for (let [filePath, file] of files) {
if (nm.isMatch(filePath, fileGlobExpr, options)) {
yield [filePath, file];
}
}
}
/**
* 遍历所有的文件
*/
export function* scanFiles(
result: ResultDir,
resultDirPath = getResultNameOrDefault(result, ''),
maxDepth = 10000,
): IterableIterator<[string, ResultFile]> {
for (let file of result.files) {
const fileName = getFileNameWithExt(file);
yield [joinPath(resultDirPath, fileName), file];
}
for (let subDir of result.dirs) {
yield* scanFiles(subDir, joinPath(resultDirPath, subDir.name), maxDepth - 1);
}
}
export function getFileNameWithExt(file: ResultFile) {
return `${file.name}${file.ext ? `.${file.ext}` : ''}`;
}
function getResultNameOrDefault(result: ResultDir, defaultDir = '/') {
return result.name && result.name !== '.' ? result.name : defaultDir;
}
function joinPath(...pathParts: string[]): string {
return pathParts
.filter((x) => x !== '' && x !== '.')
.join('/')
.replace(/\\+/g, '/')
.replace(/\/+/g, '/');
}
export function* scanDirs(
result: ResultDir,
resultDirPath = getResultNameOrDefault(result, ''),
maxDepth = 10000,
): IterableIterator<[string, ResultDir]> {
yield [resultDirPath, result];
for (let subDir of result.dirs) {
yield* scanDirs(subDir, joinPath(resultDirPath, subDir.name), maxDepth - 1);
}
}
export function* globDirs(
result: ResultDir,
dirGlobExpr: string,
options: GlobOptions = {},
resultDirPath = getResultNameOrDefault(result, ''),
): IterableIterator<[string, ResultDir]> {
const dirs = scanDirs(result, resultDirPath);
for (let [dirPath, dir] of dirs) {
if (nm.isMatch(dirPath, dirGlobExpr, options)) {
yield [dirPath, dir];
}
}
}
export function findDir(
result: ResultDir,
dirGlobExpr: string,
options: GlobOptions = {},
resultDirPath = getResultNameOrDefault(result, ''),
): ResultDir | null {
const dirs = scanDirs(result, resultDirPath);
for (let [dirPath, dir] of dirs) {
if (nm.isMatch(dirPath, dirGlobExpr, options)) {
return dir;
}
}
return null;
}
/**
* 从结果中移除一些文件
* @param result 出码结果目录
* @param filePathGlobExpr 要移除的文件路径(glob 表达式)
* @param globOptions glob 参数
* @returns 移除了多少文件
*/
export function removeFilesFromResult(
result: ResultDir,
filePathGlobExpr: string,
globOptions: GlobOptions = {},
): number {
let removedCount = 0;
const [dirPath, fileName] = splitPath(filePathGlobExpr);
const dirs = dirPath ? globDirs(result, dirPath) : [['', result] as const];
for (let [, dir] of dirs) {
const files = globFiles(dir, fileName, globOptions, '.');
for (let [, file] of files) {
dir.files.splice(dir.files.indexOf(file), 1);
removedCount += 1;
}
}
return removedCount;
}
/**
* 从结果中移除一些目录
* @param result 出码结果目录
* @param dirPathGlobExpr 要移除的目录路径(glob 表达式)
* @param globOptions glob 参数
* @returns 移除了多少文件
*/
export function removeDirsFromResult(
result: ResultDir,
dirPathGlobExpr: string,
globOptions: GlobOptions = {},
): number {
let removedCount = 0;
const [dirPath, fileName] = splitPath(dirPathGlobExpr);
const dirs = dirPath ? globDirs(result, dirPath) : [['', result] as const];
for (let [, dir] of dirs) {
const foundDirs = globDirs(dir, fileName, globOptions, '.');
for (let [, foundDir] of foundDirs) {
dir.dirs.splice(dir.dirs.indexOf(foundDir), 1);
removedCount += 1;
}
}
return removedCount;
}
/**
* 将文件路径拆分为目录路径和文件名
* @param filePath
* @returns [fileDirPath, fileName]
*/
function splitPath(filePath: string) {
const parts = filePath.split('/');
const fileName = parts.pop() || '';
return [joinPath(...parts), fileName];
}