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
58 lines (51 loc) · 1.49 KB
/
resultHelper.ts
File metadata and controls
58 lines (51 loc) · 1.49 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
import { ResultFile, ResultDir } from '@alilc/lowcode-types';
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: `${cwd ? `${cwd}/` : ''}${file.name}${file.ext ? `.${file.ext}` : ''}`,
content: file.content,
}),
),
].concat(
...dir.dirs.map((subDir) =>
flattenResult(subDir, [cwd, subDir.name].filter((x) => x !== '' && x !== '.').join('/'))),
);
}