-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom.js
More file actions
182 lines (157 loc) · 5.23 KB
/
custom.js
File metadata and controls
182 lines (157 loc) · 5.23 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
const fs = require("fs");
const path = require("path");
const defaultConf = `
import ExpoModulesCore
import SwiftUI
struct CustomView: View {
let viewKey: String
let material: ViewMaterial
let onEvent: EventDispatcher
@ViewBuilder
var body: some View {
switch viewKey {
default:
ErrorMessage(message: "Native View not found")
}
}
}
`;
// Function to extract key, material, and struct name information from content
function extractKeyAndMaterial(content) {
const result = [];
const lines = content.split("\n");
const eventImport = content.includes("import ExpoModulesCore") ? true : false;
let inStruct = false;
let material = false;
let event = false;
let structName = "";
lines.forEach((line) => {
if (/\/\/\s*key:/.test(line)) {
const key = line.split(":").pop().trim();
material = false;
inStruct = false;
event = false;
structName = "";
// Process the next lines to find the struct and material
for (let i = lines.indexOf(line) + 1; i < lines.length; i++) {
const currentLine = lines[i];
if (/struct\s+(\w+)\s*:\s*View\s*{/.test(currentLine)) {
inStruct = true;
structName = currentLine.match(/struct\s+(\w+)\s*:/)[1];
material = false;
event = false;
}
if (inStruct) {
if (/var\s+material:\s*ViewMaterial/.test(currentLine)) {
material = true;
}
if (
/var\s+onEvent:\s*EventDispatcher/.test(currentLine) &&
eventImport
) {
event = true;
}
if (/}/.test(currentLine)) {
inStruct = false;
}
}
if (!inStruct && /}/.test(currentLine)) {
break;
}
}
console.log(
`\u001b[1;32m \n Extracted informations: \n key: ${key}, material: ${material}, name: ${structName}; event: ${event} \n`
);
result.push({
key: key,
material: material,
structName: structName,
event: event,
});
}
});
return result;
}
// Function to insert a new case into the switch statement
function insertConfig(content, newCase) {
const switchPattern = /switch viewKey {[^}]*default:/;
if (switchPattern.test(content)) {
return content.replace(switchPattern, (match) => {
return match.replace(/default:/, `${newCase};\ndefault:`);
});
}
return content;
}
// Function to prepare the file content
function prepareFile(content, destDir) {
const views = extractKeyAndMaterial(content);
const indexFile = path.join(destDir, "index.swift");
let config = fs.readFileSync(indexFile, "utf8");
views.forEach((view) => {
const key = view.key;
const material = view.material ? "material: material" : "";
const event = view.event ? "onEvent: onEvent" : "";
const structName = view.structName || key;
const newCase = `case ".$${key}": ${structName}(${material} ${(event && material) !== "" ? "," : ""} ${event})`;
config = insertConfig(config, newCase);
});
fs.writeFileSync(indexFile, config, "utf8");
console.log("\u001b[1;32m \n ✓ Prepared file");
}
// Function to clean the destination directory except index.swift
async function cleanDirectory(destDir) {
const indexFile = await path.join(destDir, "index.swift");
if (fs.existsSync(destDir) && fs.existsSync(indexFile)) {
await console.log("\u001b[1;32m \n \n Cleaning directory...");
await fs.writeFileSync(indexFile, defaultConf, "utf8");
await fs.readdir(destDir, (err, files) => {
if (err) {
return;
}
});
console.log("\u001b[1;32m \n ✓ Directory cleaned");
} else {
console.error("\u001b[1;31m \n ✕ Destination directory does not exist");
}
await console.log("\u001b[1;32m \n Searching files");
await copyFiles(srcDir, destDir);
}
// Function to copy files from source to destination
function copyFiles(srcDir, destDir) {
if (!fs.existsSync(srcDir)) {
fs.mkdirSync(srcDir, { recursive: true });
}
if (!fs.existsSync(destDir)) {
console.error("\u001b[1;31m ✕ \n Cannot locate destination directory");
return;
}
fs.readdirSync(srcDir).forEach((file) => {
console.log("\u001b[1;32m Found file " + file);
const srcFile = path.join(srcDir, file);
const destFile = path.join(destDir, file);
if (fs.statSync(srcFile).isFile() && file.endsWith(".swift")) {
const data = fs.readFileSync(srcFile, "utf8");
prepareFile(data, destDir);
fs.copyFileSync(srcFile, destFile);
} else {
console.log(`\u001b[1;33m \n⚠ Skipping non-swift-file: ${srcFile} \n`);
}
});
}
const args = process.argv.slice(2);
const customPath =
args[0] !== "--no-clean" && args[0] !== undefined ? args[0] : null;
const srcDir =
customPath !== null && customPath !== "undefined"
? path.resolve(customPath)
: path.resolve("./swiftui");
const destDir = args.includes("--example")
? path.resolve("../ios/CustomViews")
: path.resolve("./node_modules/react-native-render-swift-ui/ios/CustomViews");
if (args.includes("--no-clean")) {
console.log(`\u001b[1;33m \n⚠ Cleaning skipped \n`);
console.log("\u001b[1;32m \n Searching files... \n");
copyFiles(srcDir, destDir);
} else {
cleanDirectory(destDir);
}