switch to fs/promises

This commit is contained in:
2022-07-06 12:12:11 +02:00
parent cc10877207
commit 06adacd0ce
+45 -39
View File
@@ -1,4 +1,5 @@
const fs = require('fs'); //const fs = require('fs');
const fs = require('node:fs/promises');
const path = require('path'); const path = require('path');
const HTMLparser = require('node-html-parser'); const HTMLparser = require('node-html-parser');
const exec = require("child_process").execSync; const exec = require("child_process").execSync;
@@ -24,32 +25,31 @@ function cleanPath(pathToClean) {
return cleanPath; return cleanPath;
} }
function writeFile(dir, file, data) { async function mkDir(dirPath) {
try {
await fs.access(cleanPath(dirPath));
}
catch (err)
{
await fs.mkdir(dirPath, { recursive: true});
}
await fs.access(cleanPath(dirPath));
}
async function writeFile(dir, file, data) {
let cleanDir = cleanPath(dir); let cleanDir = cleanPath(dir);
let cleanFile = cleanPath(file); let cleanFile = cleanPath(file);
if (!fs.existsSync(cleanDir)) { await mkDir(dir);
fs.mkdirSync(cleanDir, { recursive: true });
} return fs.writeFile(`${cleanDir}/${cleanFile}`, data);
fs.writeFileSync(`${cleanDir}/${cleanFile}`, data, function (err) {
if (err != null) console.log(err);
return err != null;
});
} }
function readFile(file) { async function readFile(file, {format = 'utf8'} = {}) {
let fileContent; return fs.readFile(cleanPath(file), format);
try {
fileContent = fs.readFileSync(cleanPath(file), 'utf8');
}
catch (err) {
fileContent = false;
}
return fileContent;
} }
function loadConfig() { async function loadConfig() {
let novelConfigDefault = { let novelConfigDefault = {
"downloadLocation": "", "downloadLocation": "",
"converterPath": "ebook-convert.exe", "converterPath": "ebook-convert.exe",
@@ -83,10 +83,8 @@ function loadConfig() {
"novels": [] "novels": []
}; };
let configRead = readFile('./novelConfig.conf'); try {
config = JSON.parse(await readFile('./novelConfig.conf'));
if (configRead) {
config = JSON.parse(configRead);
transporter = nodemailer.createTransport({ transporter = nodemailer.createTransport({
service: config['emailProvider'], service: config['emailProvider'],
auth: { auth: {
@@ -94,17 +92,25 @@ function loadConfig() {
pass: config['emailPassword'] pass: config['emailPassword']
} }
}); });
writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4)); await writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4));
writeFile('.', 'novelConfig.bak.conf', JSON.stringify(config, null, 4)); await writeFile('.', 'novelConfig.bak.conf', JSON.stringify(config, null, 4));
} }
else { catch (err) {
writeFile('.', 'novelConfig.conf', JSON.stringify(novelConfigDefault, null, 4)); await writeFile('.', 'novelConfig.conf', JSON.stringify(novelConfigDefault, null, 4));
config = novelConfigDefault; config = novelConfigDefault;
} }
transporter = nodemailer.createTransport({
service: config['emailProvider'],
auth: {
user: config['emailUsername'],
pass: config['emailPassword']
}
});
} }
function saveConfig() { async function saveConfig() {
writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4)); await writeFile('.', 'novelConfig.conf', JSON.stringify(config, null, 4));
} }
async function convertEbook(dir, file, params = { "cover": false, "authors": false, "title": false }, format = 'html') { async function convertEbook(dir, file, params = { "cover": false, "authors": false, "title": false }, format = 'html') {
@@ -131,7 +137,7 @@ async function convertEbook(dir, file, params = { "cover": false, "authors": fal
} }
async function sendEbook(subject, ebookAttachments) { function sendEbook(subject, ebookAttachments) {
if (config['sendEmail']) { if (config['sendEmail']) {
let splicedAttachments = []; let splicedAttachments = [];
while (ebookAttachments.length > config['emailAttachments']) { while (ebookAttachments.length > config['emailAttachments']) {
@@ -150,7 +156,7 @@ async function sendEbook(subject, ebookAttachments) {
attachments: splicedAttachments[i] attachments: splicedAttachments[i]
} }
await transporter.sendMail(message, (err) => { transporter.sendMail(message, (err) => {
if (err) if (err)
console.log(err); console.log(err);
@@ -280,7 +286,7 @@ function getChapterContent(response, hosting) {
} }
async function main() { async function main() {
loadConfig(); await loadConfig();
for (let i = 0; i < config['novels'].length; ++i) { for (let i = 0; i < config['novels'].length; ++i) {
let novel = clone(config['novels'][i]); let novel = clone(config['novels'][i]);
@@ -296,7 +302,7 @@ async function main() {
novel['coverURL'] = novelInfo[4]; novel['coverURL'] = novelInfo[4];
config['novels'][i] = clone(novel); config['novels'][i] = clone(novel);
saveConfig(); await saveConfig();
if (!novel['lastChapterURL']) { if (!novel['lastChapterURL']) {
novel['lastChapterURL'] = novelInfo[3]; novel['lastChapterURL'] = novelInfo[3];
@@ -337,11 +343,11 @@ async function main() {
novel['lastChapterURL'] = chapters[(chap - 2 < 0 ? 0 : chap - 2)][1]; novel['lastChapterURL'] = chapters[(chap - 2 < 0 ? 0 : chap - 2)][1];
novel['lastVolume'] = vol + 1; novel['lastVolume'] = vol + 1;
config['novels'][i] = clone(novel); config['novels'][i] = clone(novel);
saveConfig(); await saveConfig();
let novelFileName = `${(vol + 1) < 10 ? '0' + (vol + 1) : (vol + 1)}. ${novel['title']}; ${novel['author']}`; let novelFileName = `${(vol + 1) < 10 ? '0' + (vol + 1) : (vol + 1)}. ${novel['title']}; ${novel['author']}`;
writeFile(novelDir, `${novelFileName}.html`, volContent); await writeFile(novelDir, `${novelFileName}.html`, volContent);
console.log(`Saved volume: ${novelFileName}`); console.log(`Saved volume: ${novelFileName}`);
await convertEbook(novelDir, novelFileName, { await convertEbook(novelDir, novelFileName, {
@@ -372,11 +378,11 @@ async function main() {
novel['lastChapterURL'] = chapters[(chap - 2 < 0 ? 0 : chap - 2)][1]; novel['lastChapterURL'] = chapters[(chap - 2 < 0 ? 0 : chap - 2)][1];
novel['lastVolume'] = vol + 1; novel['lastVolume'] = vol + 1;
config['novels'][i] = clone(novel); config['novels'][i] = clone(novel);
saveConfig(); await saveConfig();
let novelFileName = `${(vol + 1) < 10 ? '0' + (vol + 1) : (vol + 1)}. ${novel['title']}; ${novel['author']}`; let novelFileName = `${(vol + 1) < 10 ? '0' + (vol + 1) : (vol + 1)}. ${novel['title']}; ${novel['author']}`;
writeFile(novelDir, `${novelFileName}.html`, volContent); await writeFile(novelDir, `${novelFileName}.html`, volContent);
console.log(`Saved volume: ${novelFileName}`); console.log(`Saved volume: ${novelFileName}`);
await convertEbook(novelDir, novelFileName, { await convertEbook(novelDir, novelFileName, {
@@ -392,7 +398,7 @@ async function main() {
chapters.splice(0, chap); chapters.splice(0, chap);
} }
await sendEbook(novel['title'], ebookAttachments); sendEbook(novel['title'], ebookAttachments);
} }
} }
} }