Browse Source

Move __version__ to a script

Peter deHaan 7 years ago
parent
commit
314e756ef1
4 changed files with 28 additions and 13 deletions
  1. 1 0
      .gitignore
  2. 3 3
      package.json
  3. 20 0
      scripts/version.js
  4. 4 10
      server/portal_server.js

+ 1 - 0
.gitignore

@@ -1,5 +1,6 @@
 .DS_Store
 node_modules
 public/bundle.js
+public/version.json
 static/*
 !static/info.txt

+ 3 - 3
package.json

@@ -12,7 +12,7 @@
     "cross-env": "^5.0.1",
     "express": "^4.15.3",
     "express-handlebars": "^3.0.0",
-    "git-rev-sync": "1.9.1",
+    "git-rev-sync": "^1.9.1",
     "helmet": "^3.6.1",
     "jquery": "^3.2.1",
     "mozlog": "^2.1.1",
@@ -42,9 +42,9 @@
   "license": "MPL-2.0",
   "repository": "mozilla/something-awesome",
   "scripts": {
-    "bundle": "browserify frontend/src/main.js | uglifyjs > public/bundle.js",
+    "bundle": "browserify frontend/src/main.js | uglifyjs > public/bundle.js && node scripts/version",
     "dev": "watchify frontend/src/main.js -o public/bundle.js -d | node server/portal_server",
-    "format": "prettier 'frontend/src/*.js' 'public/*.css' 'server/*.js' 'test/*.js' --single-quote --write",
+    "format": "prettier '{frontend/src/,scripts/,server/,test/}*.js' 'public/*.css' --single-quote --write",
     "lint": "npm-run-all lint:*",
     "lint:css": "stylelint 'public/*.css'",
     "lint:js": "eslint .",

+ 20 - 0
scripts/version.js

@@ -0,0 +1,20 @@
+const fs = require('fs');
+const path = require('path');
+const pkg = require('../package.json');
+
+let commit;
+
+try {
+  commit = require('git-rev-sync').short();
+} catch (err) {
+  // Whatever...
+}
+
+const filename = path.join(__dirname, '..', 'public', 'version.json');
+const filedata = {
+  commit,
+  source: pkg.homepage,
+  version: pkg.version
+};
+
+fs.writeFileSync(filename, JSON.stringify(filedata, null, 2) + '\n');

+ 4 - 10
server/portal_server.js

@@ -7,9 +7,6 @@ const helmet = require('helmet');
 const bytes = require('bytes');
 const conf = require('./config.js');
 const storage = require('./storage.js');
-const pkg = require('../package.json');
-
-const gitSHA = require('git-rev-sync').short();
 
 const notLocalHost = conf.notLocalHost;
 
@@ -17,6 +14,8 @@ const mozlog = require('./log.js');
 
 const log = mozlog('portal.server');
 
+const STATIC_PATH = path.join(__dirname, '../public');
+
 const app = express();
 
 app.engine('handlebars', exphbs({ 
@@ -28,8 +27,7 @@ app.set('view engine', 'handlebars');
 app.use(helmet());
 app.use(busboy());
 app.use(bodyParser.json());
-app.use(express.static(path.join(__dirname, '../public')));
-
+app.use(express.static(STATIC_PATH));
 
 app.get('/', (req, res) => {
   res.render('index', {
@@ -154,11 +152,7 @@ app.get('/__lbheartbeat__', (req, res) => {
 });
 
 app.get('/__version__', (req, res) => {
-  res.json({
-    commit: gitSHA,
-    source: pkg.homepage,
-    version: pkg.version
-  });
+  res.sendFile(path.join(STATIC_PATH, 'version.json'));
 });
 
 app.listen(conf.listen_port, () => {