Created
July 20, 2017 04:28
-
-
Save RhinoLu/90462271be076c0d9617a4fb8459ce94 to your computer and use it in GitHub Desktop.
Node Render Pug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path="../node_modules/@types/node/index.d.ts" /> | |
/// <reference path="../node_modules/@types/pug/index.d.ts" /> | |
/** | |
* | |
* node js/render // 使用測試站設置(預設) | |
* node js/render prod // 使用正式站設置 | |
*/ | |
let pug = require("pug"); | |
let path = require("path"); | |
let fs = require("fs"); | |
interface IOption { | |
pretty: boolean; | |
meta: { url: string }; | |
og: { app_id: string }; | |
} | |
// 正式站環境 | |
let prod_option: IOption = { | |
meta: { | |
url: "https://www.example.com.tw/", | |
}, | |
og: { | |
app_id: "", | |
}, | |
pretty: true, | |
}; | |
// 測試站環境 | |
let dev_option: IOption = { | |
meta: { | |
url: "https://test.example.com.tw/", | |
}, | |
og: { | |
app_id: "", | |
}, | |
pretty: true, | |
}; | |
/*process.argv.forEach((val, index) => { | |
console.log(`${index}: ${val}`); | |
});*/ | |
if(process.argv.length > 2 && process.argv[2] === "prod"){ | |
console.log("\x1b[33m %s \x1b[0m", "production mode"); | |
createFullHTML(prod_option); | |
} else { | |
console.log("\x1b[37m %s \x1b[0m:", "dev mode"); | |
createFullHTML(dev_option); | |
} | |
function createFullHTML(target: IOption): void { | |
// console.log(__dirname); | |
// console.log(__dirname.replace("/js", "/")); | |
let _path: string = __dirname.replace(path.sep + "js", path.sep); | |
// Desktop | |
let pug_path: string = _path + "pug" + path.sep; | |
let output_path: string = _path; | |
// console.log(pug_path); | |
// console.log(output_path); | |
// 首頁 | |
outputHTML(pug.renderFile(pug_path + "index.pug", target), output_path + "index.html"); | |
// 內頁 | |
outputHTML(pug.renderFile(pug_path + "page.pug", target), output_path + "page.html"); | |
// Mobile | |
pug_path = _path + "m/pug" + path.sep; | |
output_path = _path + "m/"; | |
// console.log(pug_path); | |
// console.log(output_path); | |
// 首頁 | |
outputHTML(pug.renderFile(pug_path + "index.pug", target), output_path + "index.html"); | |
// 內頁 | |
outputHTML(pug.renderFile(pug_path + "page.pug", target), output_path + "page.html"); | |
} | |
// save output html | |
function outputHTML(res: any, outputPath: string): void { | |
// console.log(res); | |
fs.writeFile(outputPath, res, function(err) { | |
if(err) { return console.log(err); } | |
console.log(outputPath + " was saved!"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment