Просмотр исходного кода

Change revision string if the files are in a dirty state

jherve 1 год назад
Родитель
Сommit
96b27831fe
1 измененных файлов с 19 добавлено и 4 удалено
  1. 19 4
      to_pdf.js

+ 19 - 4
to_pdf.js

@@ -3,7 +3,7 @@
 const fs = require("fs");
 const path = require("path");
 const yaml = require("yaml");
-const { exec } = require("child_process");
+const { exec, execSync } = require("child_process");
 
 const puppeteer = require("puppeteer");
 const pug = require("pug");
@@ -61,19 +61,34 @@ function exportRenderToPdf(dataFilePath, templatePath, assetsPath, outputPath, o
 }
 
 function writeGitRevisionFile(dataFilePath, templatePath, assetsPath, revPath) {
-  const cmd = `git log --format="%H" ${dataFilePath} ${templatePath} ${assetsPath} | head -1`;
+  const allFiles = `${dataFilePath} ${templatePath} ${assetsPath}`;
+  const dirty = areDirty(allFiles);
+
+  const cmd = `git log --format="%H" ${allFiles} | head -1`;
   exec(cmd, (err, stdout, stderr) => {
     if (err) {
       return;
     }
 
-    fs.appendFile(revPath, stdout, (err) => {
+    const newRev = dirty ? `#${stdout}` : stdout;
+    fs.appendFile(revPath, newRev, (err) => {
       if (err) throw err;
     });
-    console.log(`Wrote file revision data to ${revPath}`);
+    console.log(`Wrote ${dirty ? "dirty" : "clean"} revision data to ${revPath}`);
   });
 }
 
+function areDirty(fileList) {
+  const dirtyStatusCmd = `git diff --quiet ${fileList}`;
+
+  try {
+    execSync(dirtyStatusCmd);
+    return false;
+  } catch {
+    return true;
+  }
+}
+
 const [inputPath, variantName, outputPath] = process.argv.slice(2);
 if (!inputPath || !variantName || !outputPath) {
   console.error(`The script should be called like :\n${process.argv[1]} input_path variant_name output_dir`);