forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateHelper.ts
More file actions
30 lines (26 loc) · 847 Bytes
/
templateHelper.ts
File metadata and controls
30 lines (26 loc) · 847 Bytes
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
import { ResultDir, ResultFile } from '@alilc/lowcode-types';
import { createResultDir, addDirectory, addFile } from './resultHelper';
type FuncFileGenerator = () => [string[], ResultFile];
export function insertFile(root: ResultDir, path: string[], file: ResultFile) {
let current: ResultDir = root;
path.forEach((pathNode) => {
const dir = current.dirs.find((d) => d.name === pathNode);
if (dir) {
current = dir;
} else {
const newDir = createResultDir(pathNode);
addDirectory(current, newDir);
current = newDir;
}
});
addFile(current, file);
}
export function runFileGenerator(root: ResultDir, fun: FuncFileGenerator) {
try {
const result = fun();
const [path, file] = result;
insertFile(root, path, file);
} catch (error) {
throw new Error(`Error: ${typeof fun}`);
}
}