瀏覽代碼

Switching from a pure css file to a dynamically generated sass -> css. Server will watch for sass file changes and update as needed. Path app/css/nespaint.css is now ignored as it's generated at start if missing.

dev-bank
Bryan Miller 5 年之前
父節點
當前提交
dda6f3032a
共有 6 個文件被更改,包括 1723 次插入27 次删除
  1. +1
    -0
      .gitignore
  2. +1596
    -0
      package-lock.json
  3. +3
    -1
      package.json
  4. +0
    -0
      sass/nespaint.scss
  5. +121
    -26
      server.js
  6. +2
    -0
      start.sh

+ 1
- 0
.gitignore 查看文件

@@ -1,3 +1,4 @@
node_modules/*
app/css/nespaint.css
*.swp
doc/*

+ 1596
- 0
package-lock.json
文件差異過大導致無法顯示
查看文件


+ 3
- 1
package.json 查看文件

@@ -21,7 +21,9 @@
"author": "Bryan \"ObsidianBlk\" Miller",
"license": "ISC",
"dependencies": {
"chokidar": "^2.1.2",
"ejs": "^2.6.1",
"express": "^4.16.4"
"express": "^4.16.4",
"sass": "^1.17.2"
}
}

app/css/nespaint.css → sass/nespaint.scss 查看文件


+ 121
- 26
server.js 查看文件

@@ -10,13 +10,55 @@ const app = express();
const http = require("http");
const https = require("https");

const sass = require("sass");
const SASS_PATH = path.join(__dirname, "sass");
const CSS_PATH = path.join(__dirname, "app/css");

const watcher = require("chokidar").watch(SASS_PATH, {ignored: /[\/\\]\./, persistent: true});

// --------------------------------------------------------
// Environment options for the server.
var production = process.env.PRODUCTION || false;
var forceCSSRegen = process.env.FORCECSSREGEN || false;
// NOTE: The default ports are blocked by default on linux without some hocus pocus.
var port = process.env.NESPORT || 80;
var portSSL = process.env.NESPORTSSL || 443;
var sslKeyPath = process.env.SSLKEYPATH || null;
var sslCertPath = process.env.SSLCERTPATH || null;
var sslCaPath = process.env.SSLCAPATH || null;


// -------------------------------------------------------
// Simple helper function
function debounce(func, delay, scope){
var timeout = null;
return function(){
var context = scope || this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function(){
func.apply(context, args);
}, delay);
};
}

var generateCSS = debounce(function(src, dst, cb){
sass.render({file: src}, (err, res)=>{
if (err){
cb("Failed to generate css - " + err.toString());
} else {
fs.writeFile(path.join(CSS_PATH, "nespaint.css"), res.css.toString(), (err) => {
if (err)
cb("Failed to write file '" + path.join(CSS_PATH, "nespaint.css") + "' - " + err.toString());
else
cb();
});
}
});
}, 1000);

// -------------------------------------------------------
// Configuring the current version of the application.
function GenVersion(){
var v = package.version;
// Testing for a GIT repo... if not in a production environment.
@@ -38,6 +80,9 @@ function GenVersion(){
}
var version = GenVersion();


// ---------------------------------------------------
// Configuring the express server.
app.set('views', path.join(__dirname, "/views"));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
@@ -47,38 +92,88 @@ app.get('/', function(req, res){
});


console.log("NESPaint (v" + version + ") Server");

if (sslKeyPath !== null && sslCertPath !== null){
try {
var options = {
key: fs.readFileSync(sslKeyPath),
cert: fs.readFileSync(sslCertPath)
}
if (sslCaPath !== null)
options.ca = fs.readFileSync(sslCaPath);

app.use(function(req, res, next){
if (req.secure){
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
// ----------------------------------------------------
// Watching for any needed file updates (to minimize the need to restart the server.
watcher.on('ready', () => {
// console.log("Watching path " + SASS_PATH);
var dst = path.join(CSS_PATH, "nespaint.css");
fs.access(dst, fs.constants.F_OK, (err) => {
// Only try generating a new CSS if one doesn't already exists, or FORCECSSREGEN is true
if (err || forceCSSRegen){
if (!fs.existsSync(CSS_PATH)){
try {
fs.mkdirSync(CSS_PATH);
} catch (e) {
conosle.log("ERROR: " + e.toString());
exit();
}
}
});
https.createServer(options, app).listen(portSSL, () => {
console.log("HTTPS Listening on port " + portSSL + "!");
});
} catch (e) {
console.log("WARNING: Failed to initialize HTTPS server. \"" + e.toString() + "\"");
}
}

http.createServer(app).listen(port, () => {
console.log("HTTP Listening on port " + port + "!");
generateCSS(path.join(SASS_PATH, "nespaint.scss"), path.join(CSS_PATH, "nespaint.css"), (err) => {
if (err){
console.log("ERROR: " + err);
exit();
} else {
startServer();
}
});
} else {
startServer();
}
});
});
watcher.on('change', (fpath) => {
// console.log("File " + fpath + " changed!");
generateCSS(path.join(SASS_PATH, "nespaint.scss"), path.join(CSS_PATH, "nespaint.css"), (err) => {
if (err)
console.log("WARNING: " + err);
});
});


// --------------------------------------------------
// Announce app version!
console.log("NESPaint (v" + version + ") Server");


// --------------------------------------------------
// KICK THE PIG!

function startServer(){
// Check if given SSL key and cert(s). If so, attempt to start an HTTPS server and
// reroute HTTP requests to HTTPS.
if (sslKeyPath !== null && sslCertPath !== null){
try {
var options = {
key: fs.readFileSync(sslKeyPath),
cert: fs.readFileSync(sslCertPath)
}
if (sslCaPath !== null)
options.ca = fs.readFileSync(sslCaPath);

app.use(function(req, res, next){
if (req.secure){
next();
} else {
res.redirect('https://' + req.headers.host + req.url);
}
});
https.createServer(options, app).listen(portSSL, () => {
console.log("HTTPS Listening on port " + portSSL + "!");
});
} catch (e) {
console.log("WARNING: Failed to initialize HTTPS server. \"" + e.toString() + "\"");
}
}

// Start the HTTP server.
http.createServer(app).listen(port, () => {
console.log("HTTP Listening on port " + port + "!");
});
}





+ 2
- 0
start.sh 查看文件

@@ -1,4 +1,6 @@
#!/bin/bash

export FORCECSSREGEN=true

export NESPORT=8000
node .

Loading…
取消
儲存