node-zwave-js/packages/config/maintenance/codegen.ts

46 lines
1.3 KiB
TypeScript

import { formatWithDprint } from "@zwave-js/maintenance";
import * as fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import peggy from "peggy";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// @ts-expect-error ts-pegjs has incorrect types
const pegts = (await import("ts-pegjs")).default;
//
// Generate logic parser
//
const sourceDir = path.join(__dirname, "../src");
const grammarFilename = path.join(sourceDir, "logic.pegjs");
const grammar = fs.readFileSync(grammarFilename, "utf8");
const parserCode = peggy.generate(grammar, {
output: "source",
plugins: [pegts],
}) as any as string;
let code = `// THIS FILE WAS AUTO GENERATED
/* eslint-disable */
// @ts-nocheck
${parserCode}`;
const logicParserFilename = path.join(sourceDir, "LogicParser.ts");
code = formatWithDprint(logicParserFilename, code);
fs.writeFileSync(logicParserFilename, code, "utf8");
//
// Generate PACKAGE_VERSION module
//
const { version: PACKAGE_VERSION } = JSON.parse(
fs.readFileSync(path.join(__dirname, "../package.json"), "utf8"),
);
const versionFileName = path.join(sourceDir, "_version.ts");
code = `// This file is auto-generated by the codegen maintenance script
export const PACKAGE_VERSION = "${PACKAGE_VERSION}";
`;
fs.writeFileSync(versionFileName, code, "utf8");