(context, sourceFile, args, xcode)
| 231 | } |
| 232 | |
| 233 | async function reloadPreview(context, sourceFile, args, xcode) { |
| 234 | const started = Date.now(); |
| 235 | const timings = []; |
| 236 | const timed = (label, action) => { |
| 237 | const stageStarted = Date.now(); |
| 238 | const result = action(); |
| 239 | timings.push([label, Date.now() - stageStarted]); |
| 240 | return result; |
| 241 | }; |
| 242 | const timedAsync = async (label, action) => { |
| 243 | const stageStarted = Date.now(); |
| 244 | const result = await action(); |
| 245 | timings.push([label, Date.now() - stageStarted]); |
| 246 | return result; |
| 247 | }; |
| 248 | const source = timed("read source", () => |
| 249 | fs.readFileSync(sourceFile, "utf8"), |
| 250 | ); |
| 251 | const previews = timed("extract previews", () => extractPreviews(source)); |
| 252 | if (previews.length === 0) { |
| 253 | throw new Error(`No #Preview blocks found in ${sourceFile}.`); |
| 254 | } |
| 255 | const preview = timed("select preview", () => |
| 256 | selectPreview(previews, args.preview), |
| 257 | ); |
| 258 | const stamp = `${Date.now()}-${process.pid}`; |
| 259 | const payloadDir = path.join(context.buildRoot, "payloads", stamp); |
| 260 | timed("prepare payload dir", () => |
| 261 | fs.mkdirSync(payloadDir, { recursive: true }), |
| 262 | ); |
| 263 | |
| 264 | const sanitizedPath = path.join(payloadDir, "PreviewSource.swift"); |
| 265 | const wrapperPath = path.join(payloadDir, "SimDeckPreviewPayload.swift"); |
| 266 | const dylibPath = path.join( |
| 267 | payloadDir, |
| 268 | `SimDeckPreviewPayload-${stamp}.dylib`, |
| 269 | ); |
| 270 | const sanitizedSource = timed("sanitize source", () => |
| 271 | sanitizePreviewSource( |
| 272 | removeRanges( |
| 273 | source, |
| 274 | previews.map((item) => item.range), |
| 275 | ), |
| 276 | xcode?.moduleName, |
| 277 | ), |
| 278 | ); |
| 279 | const sourceModule = args.splitCompile |
| 280 | ? timed("resolve source cache", () => |
| 281 | previewSourceModule(context, sanitizedSource, args, xcode), |
| 282 | ) |
| 283 | : null; |
| 284 | timed("write generated swift", () => { |
| 285 | fs.writeFileSync(sanitizedPath, sanitizedSource); |
| 286 | fs.writeFileSync( |
| 287 | wrapperPath, |
| 288 | payloadWrapperSource( |
| 289 | preview.body, |
| 290 | xcode?.moduleName, |
no test coverage detected