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

Make the script installable and accept parameters

theenglishway (time) 2 лет назад
Родитель
Сommit
77f5c08538
4 измененных файлов с 51 добавлено и 28 удалено
  1. 1 2
      .gitignore
  2. 14 7
      package-lock.json
  3. 4 0
      package.json
  4. 32 19
      to_pdf.js

+ 1 - 2
.gitignore

@@ -1,3 +1,2 @@
 node_modules/
 node_modules/
-resume.html
-resume.pdf
+output/

+ 14 - 7
package-lock.json

@@ -1,9 +1,10 @@
 {
 {
-  "name": "CV",
+  "name": "@jherve/cv-builder",
   "lockfileVersion": 2,
   "lockfileVersion": 2,
   "requires": true,
   "requires": true,
   "packages": {
   "packages": {
     "": {
     "": {
+      "name": "@jherve/cv-builder",
       "dependencies": {
       "dependencies": {
         "@anduh/pug-cli": "^1.0.0-alpha8",
         "@anduh/pug-cli": "^1.0.0-alpha8",
         "html2pug": "^4.0.0",
         "html2pug": "^4.0.0",
@@ -11,6 +12,9 @@
         "pug": "^3.0.2",
         "pug": "^3.0.2",
         "puppeteer": "^20.8.3",
         "puppeteer": "^20.8.3",
         "yaml": "^2.3.4"
         "yaml": "^2.3.4"
+      },
+      "bin": {
+        "pdf_resume": "to_pdf.js"
       }
       }
     },
     },
     "node_modules/@anduh/pug-cli": {
     "node_modules/@anduh/pug-cli": {
@@ -570,9 +574,12 @@
       }
       }
     },
     },
     "node_modules/function-bind": {
     "node_modules/function-bind": {
-      "version": "1.1.1",
-      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
-      "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
+      "version": "1.1.2",
+      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
+      "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
+      "funding": {
+        "url": "https://github.com/sponsors/ljharb"
+      }
     },
     },
     "node_modules/get-caller-file": {
     "node_modules/get-caller-file": {
       "version": "2.0.5",
       "version": "2.0.5",
@@ -1969,9 +1976,9 @@
       }
       }
     },
     },
     "function-bind": {
     "function-bind": {
-      "version": "1.1.1",
-      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
-      "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
+      "version": "1.1.2",
+      "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
+      "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA=="
     },
     },
     "get-caller-file": {
     "get-caller-file": {
       "version": "2.0.5",
       "version": "2.0.5",

+ 4 - 0
package.json

@@ -1,4 +1,5 @@
 {
 {
+  "name": "@jherve/cv-builder",
   "dependencies": {
   "dependencies": {
     "@anduh/pug-cli": "^1.0.0-alpha8",
     "@anduh/pug-cli": "^1.0.0-alpha8",
     "html2pug": "^4.0.0",
     "html2pug": "^4.0.0",
@@ -6,5 +7,8 @@
     "pug": "^3.0.2",
     "pug": "^3.0.2",
     "puppeteer": "^20.8.3",
     "puppeteer": "^20.8.3",
     "yaml": "^2.3.4"
     "yaml": "^2.3.4"
+  },
+  "bin": {
+    "pdf_resume": "./to_pdf.js"
   }
   }
 }
 }

+ 32 - 19
to_pdf.js

@@ -7,22 +7,16 @@ const yaml = require("yaml");
 const puppeteer = require("puppeteer");
 const puppeteer = require("puppeteer");
 const pug = require("pug");
 const pug = require("pug");
 
 
-const cv = yaml.parse(fs.readFileSync("./cv.yaml", { encoding: "utf8" }));
-
-function changeExt(filePath, new_) {
-  parsed = path.parse(filePath);
-  return path.join(parsed.dir, `${path.basename(parsed.name)}.${new_}`);
+function parseResume(resumePath) {
+  return yaml.parse(fs.readFileSync(resumePath, { encoding: "utf8" }));
 }
 }
 
 
-function createHtml(pug_file) {
-  const rendered = pug.renderFile(pug_file, cv);
-  const html_path = changeExt(pug_file, "html");
+function createHtml(pugFile, resumeData, outputPath) {
+  const rendered = pug.renderFile(pugFile, resumeData);
 
 
-  fs.writeFile(html_path, rendered, (err) => {
+  fs.writeFile(outputPath, rendered, (err) => {
     if (err) throw err;
     if (err) throw err;
   });
   });
-  console.log(`Wrote HTML file to ${html_path}`);
-  return html_path;
 }
 }
 
 
 async function printPDF(url) {
 async function printPDF(url) {
@@ -35,20 +29,39 @@ async function printPDF(url) {
   return pdf;
   return pdf;
 }
 }
 
 
-function exportToPdf(htmlFile) {
+function exportToPdf(htmlFile, pdfPath) {
   url = `file://${path.resolve(htmlFile)}`;
   url = `file://${path.resolve(htmlFile)}`;
-  pdf_path = changeExt(htmlFile, "pdf");
 
 
   printPDF(url).then((pdf) => {
   printPDF(url).then((pdf) => {
-    fs.writeFile(pdf_path, pdf, (err) => {
+    fs.writeFile(pdfPath, pdf, (err) => {
       if (err) throw err;
       if (err) throw err;
     });
     });
-    console.log(`Wrote PDF file to ${pdf_path}`);
   });
   });
 }
 }
 
 
-local_pug_path = "./resume.pug";
-local_pdf_path = changeExt(local_pug_path, "pdf");
+function exportResumeToPdf(resumeDataPath, localPugPath, assetsPath, outputPath) {
+  const resumeData = parseResume(resumeDataPath);
+  const htmlPath = path.join(outputPath, "resume.html");
+  const pdfPath = path.join(outputPath, "resume.pdf");
+  const newAssetsPath = path.join(outputPath, "assets");
+
+  fs.mkdirSync(outputPath, { recursive: true });
+  fs.mkdirSync(newAssetsPath, { recursive: true });
+  fs.cpSync(assetsPath, newAssetsPath, { recursive: true });
+
+  createHtml(localPugPath, resumeData, htmlPath);
+  console.log(`Wrote HTML file to ${htmlPath}`);
+
+  exportToPdf(htmlPath, pdfPath);
+  console.log(`Wrote PDF file to ${pdfPath}`);
+}
+
+const [resumeDataPath, resumeTemplatePath, assetsPath, outputPath] = process.argv.slice(2);
+if (!resumeDataPath || !resumeTemplatePath || !assetsPath || !outputPath) {
+  console.error(
+    `The script should be called like :\n${process.argv[1]} resume_path template_path assets_dir output_dir`
+  );
+  process.exit(1);
+}
 
 
-local_html_path = createHtml(local_pug_path);
-exportToPdf(local_html_path);
+exportResumeToPdf(resumeDataPath, resumeTemplatePath, assetsPath, outputPath);