Repository navigation
Expand file tree
/
Copy pathtransform_core.js
More file actions
131 lines (112 loc) · 3.43 KB
/
Copy pathtransform_core.js
File metadata and controls
131 lines (112 loc) · 3.43 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
const PLATFORM = Deno.args[0] ?? "ios";
function checkFile(path) {
try {
return Deno.lstatSync(path).isFile;
} catch (_e) {
return false;
}
}
function readDir(path) {
for (
const { name, isDirectory, isFile } of Deno.readDirSync(
new URL(`./${path}`, import.meta.url),
)
) {
if (isDirectory) {
readDir(`${path}/${name}`);
} else if (isFile) {
if (!name.endsWith(".js")) {
continue;
}
if (name.match(/\.[a-z]+\.js$/) && !name.endsWith(`.${PLATFORM}.js`)) {
continue;
}
const url = new URL(`./${path}/${name}`, import.meta.url);
let code = Deno.readTextFileSync(url);
const transformModuleSpec = (path, baseURL = url) => {
if (path.startsWith("~/")) {
path = path.slice(2);
path = `${new URL("./", import.meta.url).pathname}${path}`;
return path;
} else if (path.startsWith("@nativescript/core")) {
path = path.slice("@nativescript/core".length);
path = `${new URL("./core", import.meta.url).pathname}${path}`;
return transformModuleSpec(path, baseURL);
}
const pathURL = new URL(path, baseURL);
let stat;
try {
stat = Deno.statSync(pathURL);
} catch (_e) {
stat = {};
}
if (stat.isDirectory) {
const pathAsFile = new URL(pathURL);
pathAsFile.pathname += `/index.${PLATFORM}.js`;
if (checkFile(pathAsFile)) {
path += `/index.${PLATFORM}.js`;
} else {
pathAsFile.pathname = pathURL.pathname + `/index.js`;
if (checkFile(pathAsFile)) {
path += `/index.js`;
} else {
pathAsFile.pathname = pathURL.pathname + "/package.json";
if (checkFile(pathAsFile)) {
const pkg = JSON.parse(
Deno.readTextFileSync(pathAsFile),
);
if (pkg.main) {
path += transformModuleSpec(
`./${pkg.main}`,
new URL(path + "/", baseURL),
).slice(
1,
);
}
}
}
}
}
const pathAsFile = new URL(pathURL);
pathAsFile.pathname += `.${PLATFORM}.js`;
if (checkFile(pathAsFile)) {
path += `.${PLATFORM}.js`;
} else {
pathAsFile.pathname = pathURL.pathname + `.js`;
if (checkFile(pathAsFile)) {
path += `.js`;
}
}
return path;
};
for (const match of code.matchAll(/from '([a-zA-Z\/\.0-9\-]+)';/g)) {
const path = match[1];
const newPath = transformModuleSpec(path);
if (path === newPath) {
continue;
}
code = code.replace(
match[0],
`from '${newPath}';`,
);
// console.log(`Updated ESM spec ${match[0]} to ${path}`);
}
for (
const match of code.matchAll(/require\('([\@a-zA-Z\/\.0-9\-~]+)'\)/g)
) {
const path = match[1];
const newPath = transformModuleSpec(path);
if (path === newPath) {
continue;
}
code = code.replace(
match[0],
`require('${newPath}')`,
);
// console.log(`Updated CommonJS spec ${match[0]} to ${newPath}`);
}
Deno.writeTextFileSync(url, code);
}
}
}
readDir("core");