4brains-node-generator
Version:
A CLI tool to generate a Node.js app by 4Brains Technologies
131 lines (112 loc) • 3.64 kB
JavaScript
import fs from "fs";
import path from "path";
import readline from "readline";
import { execSync } from "child_process";
import chalk from "chalk";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function updateEnvFile(filePath, serialPortPath, baudRate) {
const serialPortPathLine = `SERIALPORT_PATH=${serialPortPath}\n`;
const baudRateLine = `BAUDRATE=${baudRate}\n`;
if (fs.existsSync(filePath)) {
let envContent = fs.readFileSync(filePath, "utf8");
if (envContent.includes("SERIALPORT_PATH=")) {
envContent = envContent.replace(
/SERIALPORT_PATH=.*/,
serialPortPathLine.trim()
);
} else {
envContent = envContent.trim() + "\n" + serialPortPathLine;
}
if (envContent.includes("BAUDRATE=")) {
envContent = envContent.replace(/BAUDRATE=.*/, baudRateLine.trim());
} else {
envContent = envContent.trim() + "\n" + baudRateLine;
}
fs.writeFileSync(filePath, envContent, "utf8");
} else {
fs.writeFileSync(filePath, serialPortPathLine + baudRateLine, "utf8");
}
}
function addSerialPort(serialPortPath, baudRate) {
const projectRoot = process.cwd();
const indexPath = path.join(projectRoot, "index.js");
const serialportImport = `
import { SerialPort } from 'serialport';
import { ReadlineParser } from '@serialport/parser-readline';
`;
const serialportSetup = `
try {
const serialPort = new SerialPort({
baudRate: parseInt(process.env.BAUDRATE, 10) || ${baudRate},
path: process.env.SERIALPORT_PATH || '${serialPortPath}',
});
const parser = serialPort.pipe(new ReadlineParser({ delimiter: "\\r\\n" }));
parser.on("data", async (data) => {
console.log('Received data:', data);
});
serialPort.on("error", function (err) {
console.error("SerialPort Error: ", err.message);
});
} catch (error) {
console.error("Error initializing SerialPort or Parser:", error);
}
`;
let indexContent = fs.readFileSync(indexPath, "utf8");
// Add imports if they don't already exist
if (!indexContent.includes("import { SerialPort }")) {
indexContent = serialportImport + indexContent;
}
// Ensure SerialPort setup is added just before app.listen or server.listen
const listenMatch = indexContent.match(/(app|server)\.listen\(.*\);/);
if (listenMatch) {
indexContent = indexContent.replace(
listenMatch[0],
serialportSetup + "\n" + listenMatch[0]
);
} else {
indexContent += serialportSetup;
}
fs.writeFileSync(indexPath, indexContent, "utf8");
// Update .env files
updateEnvFile(
path.join(projectRoot, ".env.development"),
serialPortPath,
baudRate
);
updateEnvFile(
path.join(projectRoot, ".env.production"),
serialPortPath,
baudRate
);
// Install serialport and @serialport/parser-readline
execSync("npm install serialport @serialport/parser-readline", {
stdio: "inherit",
});
console.log(chalk.green("SerialPort integration added successfully."));
}
rl.question(
chalk.yellow("Enter the serial port path (e.g., /dev/tty-usbserial1): "),
(serialPortPath) => {
if (!serialPortPath.trim()) {
console.error(chalk.red("Serial port path is required."));
rl.close();
process.exit(1);
}
rl.question(
chalk.yellow("Enter the baud rate (e.g., 9600): "),
(baudRate) => {
if (!baudRate.trim()) {
console.error(chalk.red("Baud rate is required."));
rl.close();
process.exit(1);
}
addSerialPort(serialPortPath, baudRate);
rl.close();
}
);
}
);