A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

164 行
4.5KB

  1. const package = require("./package.json");
  2. const exec = require('child_process').execSync;
  3. const fs = require("fs");
  4. const path = require("path");
  5. const express = require("express");
  6. const app = express();
  7. const http = require("http");
  8. const https = require("https");
  9. const sass = require("sass");
  10. const SASS_PATH = path.join(__dirname, "sass");
  11. const SASS_FILE = "style.scss";
  12. const watcher = require("chokidar").watch(SASS_PATH, {ignored: /[\/\\]\./, persistent: true});
  13. // --------------------------------------------------------
  14. // Environment options for the server.
  15. var production = process.env.PRODUCTION || false;
  16. var forceCSSRegen = process.env.FORCECSSREGEN || false;
  17. // NOTE: The default ports are blocked by default on linux without some hocus pocus.
  18. var port = process.env.NESPORT || 80;
  19. var portSSL = process.env.NESPORTSSL || 443;
  20. var sslKeyPath = process.env.SSLKEYPATH || null;
  21. var sslCertPath = process.env.SSLCERTPATH || null;
  22. var sslCaPath = process.env.SSLCAPATH || null;
  23. var css_output = ""; // Used to hold dynamic css.
  24. // -------------------------------------------------------
  25. // Simple helper function
  26. function debounce(func, delay, scope){
  27. var timeout = null;
  28. return function(){
  29. var context = scope || this;
  30. var args = arguments;
  31. clearTimeout(timeout);
  32. timeout = setTimeout(function(){
  33. func.apply(context, args);
  34. }, delay);
  35. };
  36. }
  37. var generateCSS = debounce(function(src, cb){
  38. sass.render({file: src}, (err, res)=>{
  39. if (err){
  40. cb("Failed to generate css - " + err.toString());
  41. } else {
  42. css_output = res.css.toString();
  43. cb();
  44. }
  45. });
  46. }, 1000);
  47. // -------------------------------------------------------
  48. // Configuring the current version of the application.
  49. function GenVersion(){
  50. var v = package.version;
  51. // Testing for a GIT repo... if not in a production environment.
  52. if (production === false){
  53. try{
  54. var res = exec("git rev-parse --abbrev-ref HEAD").toString();
  55. v += "-[" + res.trim();
  56. res = exec("git rev-parse HEAD").toString();
  57. v += ":" + res.substring(0, 5) + "]";
  58. } catch(e) {
  59. if (v !== package.version){
  60. v += "]"; // If v doesn't match package.version, then assume that the first git call worked.
  61. }
  62. }
  63. }
  64. return v;
  65. }
  66. var version = GenVersion();
  67. // ---------------------------------------------------
  68. // Configuring the express server.
  69. app.set('views', path.join(__dirname, "/views"));
  70. app.engine('html', require('ejs').renderFile);
  71. app.set('view engine', 'html');
  72. app.use("/app", express.static(path.join(__dirname, "/app")));
  73. app.use("/app/css/nespaint.css", function(req, res){
  74. res.set("Content-Type", "text/css");
  75. res.send(new Buffer.from(css_output));
  76. });
  77. app.get('/', function(req, res){
  78. res.render('index.html', {version:version, author:package.author});
  79. });
  80. // ----------------------------------------------------
  81. // Watching for any needed file updates (to minimize the need to restart the server.
  82. watcher.on('ready', () => {
  83. generateCSS(path.join(SASS_PATH, SASS_FILE), (err) => {
  84. if (err){
  85. console.log("ERROR: " + err);
  86. exit();
  87. } else {
  88. startServer();
  89. }
  90. });
  91. });
  92. watcher.on('change', (fpath) => {
  93. generateCSS(path.join(SASS_PATH, SASS_FILE), (err) => {
  94. if (err)
  95. console.log("WARNING: " + err);
  96. });
  97. });
  98. // --------------------------------------------------
  99. // Announce app version!
  100. console.log("NESPaint (v" + version + ") Server");
  101. // --------------------------------------------------
  102. // KICK THE PIG!
  103. function startServer(){
  104. // Check if given SSL key and cert(s). If so, attempt to start an HTTPS server and
  105. // reroute HTTP requests to HTTPS.
  106. if (sslKeyPath !== null && sslCertPath !== null){
  107. try {
  108. var options = {
  109. key: fs.readFileSync(sslKeyPath),
  110. cert: fs.readFileSync(sslCertPath)
  111. }
  112. if (sslCaPath !== null)
  113. options.ca = fs.readFileSync(sslCaPath);
  114. app.use(function(req, res, next){
  115. if (req.secure){
  116. next();
  117. } else {
  118. res.redirect('https://' + req.headers.host + req.url);
  119. }
  120. });
  121. https.createServer(options, app).listen(portSSL, () => {
  122. console.log("HTTPS Listening on port " + portSSL + "!");
  123. });
  124. } catch (e) {
  125. console.log("WARNING: Failed to initialize HTTPS server. \"" + e.toString() + "\"");
  126. }
  127. }
  128. // Start the HTTP server.
  129. http.createServer(app).listen(port, () => {
  130. console.log("HTTP Listening on port " + port + "!");
  131. });
  132. }