UNPKG

automate-electron-ipc

Version:

Node library for automating the generation of IPC components for Electron apps.

77 lines (76 loc) 2.77 kB
/* * Apache License 2.0 * * Copyright (c) 2024, Mattias Aabmets * * The contents of this file are subject to the terms and conditions defined in the License. * You may not use, modify, or distribute this file except in compliance with the License. * * SPDX-License-Identifier: Apache-2.0 */ import fsp from "node:fs/promises"; import path from "node:path"; import cfg from "./config.js"; import logger from "./logger.js"; import parser from "./parser.js"; import writer from "./writer/index.js"; export async function ipcAutomation() { const config = await cfg.getResolvedConfig(); const pfsArray = []; if (!config.ipcSchema.stats) { await fsp.mkdir(path.dirname(config.ipcSchema.path), { recursive: true }); logger.nonExistentSchemaPath(config.ipcSchema.path); return; } else if (config.ipcSchema.stats.isFile()) { const contents = await fsp.readFile(config.ipcSchema.path); const fileData = { fullPath: config.ipcSchema.path, relativePath: config?.ipcDataDir || "src/ipc", }; const specs = parser.parseSpecs({ contents: contents.toString(), ...fileData, }); if (specs.channelSpecArray.length > 0) { pfsArray.push({ specs: specs, ...fileData }); } } else if (config.ipcSchema.stats.isDirectory()) { const files = await fsp.readdir(config.ipcSchema.path, { recursive: true }); const rawFileContents = []; await Promise.all(files.map(async (file) => { const fullPath = path.join(config.ipcSchema.path, file); const stat = await fsp.stat(fullPath); if (stat.isFile() && (await fsp.exists(fullPath))) { const contents = await fsp.readFile(fullPath); rawFileContents.push({ fullPath, relativePath: file, contents: contents.toString(), }); } })); for (const item of rawFileContents) { const specs = parser.parseSpecs(item); if (specs.channelSpecArray.length > 0) { pfsArray.push({ fullPath: item.fullPath, relativePath: item.relativePath, specs: specs, }); } } } await Promise.all([ new writer.MainBindingsWriter(config, pfsArray).write(), new writer.PreloadBindingsWriter(config, pfsArray).write(), new writer.RendererTypesWriter(config, pfsArray).write(), ]); if (pfsArray.length === 0) { logger.noChannelExpressions(config.ipcSchema.path); } else { logger.reportSuccess(pfsArray); } }