Skip to content

Commit

Permalink
Add Usabilla feedback/tracking option (hyperledger-archives#3340)
Browse files Browse the repository at this point in the history
Add Composer config option to specify a Usabilla code template to
include at the end of the playground index page

Contributes to #3334

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti authored Feb 6, 2018
1 parent 3ab7b49 commit c242e04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/composer-playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ npm-debug.log
# IDE #
.idea/
*.swp

# Usabilla code template
usabilla.html.template
20 changes: 18 additions & 2 deletions packages/composer-playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

'use strict';

const cheerio = require('cheerio');
const express = require('express');
const fs = require('fs');
const path = require('path');
const playgroundAPI = require('composer-playground-api');

Expand All @@ -31,13 +33,27 @@ function createServer (port, testMode, config) {
// Create the playground API server.
const app = playgroundAPI(port, testMode);

// Parse and load the configuration, if any specified.
const dist = path.resolve(__dirname, 'dist');

if (config) {
// Parse and load the configuration, if any specified.
app.get('/config.json', (req, res, next) => {
res.json(config);
});

// Optionally add a Usabilla feedback/tracking script to index.html
if (config.usabillaTemplate) {
const indexFile = path.resolve(dist, 'index.html');
const indexHTML = fs.readFileSync(indexFile, 'utf8');
const usabillaTemplateFile = path.resolve(__dirname, config.usabillaTemplate);
const usabillaTemplate = fs.readFileSync(usabillaTemplateFile, 'utf8');
const $ = cheerio.load(indexHTML);
$('body').append(usabillaTemplate);
const modifiedIndexHTML = $.html();
fs.writeFileSync(indexFile, modifiedIndexHTML, 'utf8');
}
}
const dist = path.resolve(__dirname, 'dist');

app.use(express.static(dist));
app.all('/*', (req, res, next) => {
res.sendFile('index.html', { root: dist });
Expand Down
1 change: 1 addition & 0 deletions packages/composer-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
},
"dependencies": {
"@ng-bootstrap/ng-bootstrap": "1.0.0-beta.2",
"cheerio": "0.22.0",
"composer-common": "0.17.4",
"composer-playground-api": "0.17.4",
"express": "4.15.2",
Expand Down

0 comments on commit c242e04

Please sign in to comment.