ソースを参照

Переписан скрипт, чтобы использовать args

Ryan Wright 5 ヶ月 前
コミット
96f7f47ff9
1 ファイル変更17 行追加38 行削除
  1. 17 38
      script.js

+ 17 - 38
script.js

@@ -5,16 +5,10 @@ const path = require("path");
 const CsvReadableStream = require("csv-reader");
 const { Transform } = require("stream");
 
-const template = fs.readFileSync(
-  path.resolve(__dirname, "template.docx"),
-  "binary"
-);
-const templateEmpty = fs.readFileSync(
-  path.resolve(__dirname, "template-empty.docx"),
-  "binary"
-);
+const [dataFilePath, templateFilePath] = process.argv.slice(2);
 
-const inputStream = fs.createReadStream("data.csv", "utf-8");
+const template = fs.readFileSync(templateFilePath, "binary");
+const inputStream = fs.createReadStream(dataFilePath, "utf-8");
 
 fs.rmSync("./docx", { recursive: true, force: true });
 fs.mkdirSync("./docx", { recursive: true });
@@ -22,65 +16,50 @@ fs.mkdirSync("./docx", { recursive: true });
 fs.rmSync("./result", { recursive: true, force: true });
 fs.mkdirSync("./result", { recursive: true });
 
-console.log("START");
+console.log("ЭТАП ГЕНЕРАЦИИ ПО ШАБЛОНУ");
 
 // converting docx to pdf stream
-
-const COUNT = 507;
 let counter = 0;
 
 // Transform stream that takes csv data and converts it to docx
 // and then to pdf
 const pdfStream = new Transform({
   objectMode: true,
-  transform: async (row, encoding, callback) => {
+  transform: async (row, _encoding, callback) => {
     const templateZip = new PizZip(template);
-    const templateEmptyZip = new PizZip(templateEmpty);
-
-    const [NAME, PLACE, NOMINATOIN, TEACHER] = row;
 
-    const isEmpty = !NOMINATOIN || !TEACHER;
-    const doc = new Docxtemplater(isEmpty ? templateEmptyZip : templateZip, {
+    const doc = new Docxtemplater(templateZip, {
       paragraphLoop: true,
       linebreaks: true,
       errorLogging: true,
     });
 
-    doc.render(
-      isEmpty
-        ? { NAME, PLACE }
-        : {
-            NAME,
-            PLACE,
-            NOMINATOIN,
-            TEACHER,
-          }
-    );
+    doc.render(row);
 
     const buffer = doc.getZip().generate({
       type: "nodebuffer",
     });
-    const fileName = `${isEmpty ? `e-${counter}` : counter}-${NAME.split(" ")
-      .join("-")
-      .toLowerCase()}`;
-
-    // const html = await mammoth.convertToHtml({ buffer });
+    const fileName = `${row.NOMINATION.replace(
+      "Изобразительное искусство",
+      "ИЗО"
+    ).replace("Декоративно-прикладное искусство", "ДПИ")} ${row.NAME}`;
 
     fs.writeFileSync(
-      path.resolve(__dirname, "result", `${fileName}.docx`),
+      path.resolve(__dirname, "docx", `${fileName}.docx`),
       buffer
     );
 
     counter += 1;
-    console.log(`PROCESS ${counter}/${COUNT}`);
+    console.log(`ГЕНЕРИРОВАНО ${counter}`);
 
     callback(null, null);
   },
 });
 
 inputStream
-  .pipe(new CsvReadableStream({ trim: true, delimiter: ";", skipHeader: true }))
+  .pipe(new CsvReadableStream({ trim: true, delimiter: ";", asObject: true }))
   .pipe(pdfStream)
-  .on("end", () => {
-    console.log("DONE");
+  .on("finish", () => {
+    console.log("ГЕНЕРАЦИЯ ЗАВЕРШЕНА");
+    console.log("--------------------------------------------");
   });