forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathHelper.ts
More file actions
41 lines (39 loc) · 1.21 KB
/
pathHelper.ts
File metadata and controls
41 lines (39 loc) · 1.21 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
import { IContextData } from '../types';
function relativePath(from: string[], to: string[]): string[] {
const length = Math.min(from.length, to.length);
let samePartsLength = length;
for (let i = 0; i < length; i++) {
if (from[i] !== to[i]) {
samePartsLength = i;
break;
}
}
if (samePartsLength === 0) {
return to;
}
let outputParts = [];
for (let i = samePartsLength; i < from.length; i++) {
outputParts.push('..');
}
outputParts = [...outputParts, ...to.slice(samePartsLength)];
if (outputParts[0] !== '..') {
outputParts.unshift('.');
}
return outputParts;
}
export function getSlotRelativePath(options: {
contextData: IContextData;
from: string;
to: string;
}) {
const { contextData, from, to } = options;
const isSingleComponent = contextData?.extraContextData?.projectRemark?.isSingleComponent;
const template = contextData?.extraContextData?.template;
let toPath = template.slots[to].path;
toPath = [...toPath, template.slots[to].fileName!];
let fromPath = template.slots[from].path;
if (!isSingleComponent && ['components', 'pages'].indexOf(from) !== -1) {
fromPath = [...fromPath, 'pageName'];
}
return relativePath(fromPath, toPath).join('/');
}