UNPKG

automate-electron-ipc

Version:

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

102 lines (101 loc) 3.59 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 utils from "../utils.js"; import { ImportsGenerator } from "./imports-generator.js"; export class BaseWriter { config; pfsArray; importsGenerator; indents; notice = utils.dedent(` // NOTICE: THIS FILE WAS GENERATED BY AUTOMATE-ELECTRON-IPC PLUGIN. // ANY CHANGES TO THIS FILE WILL NOT PERSIST BETWEEN GENERATIONS. `); constructor(config, pfsArray) { if (this.constructor === BaseWriter) { throw new Error(`Cannot instantiate abstract base class '${this.constructor.name}'`); } this.config = config; this.pfsArray = pfsArray; this.importsGenerator = new ImportsGenerator(config.projectUsesNodeNext, this.getTargetFilePath()); this.indents = this.getCodeIndents(); } throwAbstractError(methodName) { throw new Error(`Must implement method '${methodName}' in '${this.constructor.name}' class`); } getTargetFilePath() { this.throwAbstractError("getTargetFilePath"); return null; } renderEmptyFileContents() { this.throwAbstractError("renderEmptyFileContents"); return null; } renderFileContents() { this.throwAbstractError("generateFileContents"); return null; } getCodeIndents() { return [1, 2, 3, 4, 5].map((value) => { return " ".repeat(this.config.codeIndent).repeat(value); }); } injectEventTypehint(sigDef) { return sigDef.replace("(", "(event: IpcMainEvent, "); } getOriginalParams(spec, onlyNames) { return spec.signature.params .map((param) => { const typeHint = onlyNames ? "" : `: ${param.type || "any"}`; const optional = !onlyNames && param.optional ? "?" : ""; const rest = !onlyNames && param.rest ? "..." : ""; return `${rest}${param.name}${optional}${typeHint}`; }) .join(", "); } sortCallablesArray(callablesArray) { return callablesArray.sort((a, b) => { const prefixOrder = ["on", "send"]; const getPrefixRank = (value) => { for (let i = 0; i < prefixOrder.length; i++) { if (value.startsWith(prefixOrder[i])) { return i; } } return prefixOrder.length; }; const prefixRankA = getPrefixRank(a); const prefixRankB = getPrefixRank(b); if (prefixRankA === prefixRankB) { return a.localeCompare(b); } return prefixRankA - prefixRankB; }); } async write(withNotice = true) { const targetFilePath = this.getTargetFilePath(); const fileDirectory = path.dirname(targetFilePath); await fsp.mkdir(fileDirectory, { recursive: true }); let contents; if (this.pfsArray.length === 0) { contents = this.renderEmptyFileContents(); } else { contents = this.renderFileContents(); } if (withNotice) { contents = `${this.notice}\n${contents}`; } await fsp.writeFile(targetFilePath, contents); } }