Skip to content

Commit 048d348

Browse files
authored
Merge pull request #7 from quinkennedy/flags-and-fullscreen
Flags and fullscreen
2 parents cb93e7f + 39f20f5 commit 048d348

File tree

6 files changed

+109
-23
lines changed

6 files changed

+109
-23
lines changed

.eslintrc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ env:
7373
commonjs: true
7474
mocha: true
7575

76-
76+
parserOptions:
77+
ecmaVersion: 6
7778

7879

7980
globals:

extension.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
'use strict';
12
var pjson = require('./package.json'),
23
Extension = require('openframe-extension'),
3-
execSync = require('child_process').execSync;
4+
execSync = require('child_process').execSync,
5+
debug = require('debug')('openframe-website');
46

57
/**
68
* Extensions should expose an instance of the Extension class.
@@ -16,17 +18,50 @@ module.exports = new Extension({
1618
'download': false,
1719
'start_command': function(args, tokens) {
1820
// 1. clone template .xinitrc
19-
var filePath = _cloneTemplate(this.xinitrcTplPath);
20-
// 1. replace tokens in .xinitrc
21-
_replaceTokens(filePath, tokens);
22-
// 2. return xinit
21+
var filePath = _cloneTemplate(this.xinitrcTplPath),
22+
// 2. parse options from args into tokens
23+
_tokens = _extendTokens(args, tokens);
24+
// 3. replace tokens in .xinitrc
25+
_replaceTokens(filePath, _tokens);
26+
// 4. return xinit
2327
return 'xinit ' + filePath;
2428
},
2529
'end_command': 'pkill -f X',
2630
xinitrcTplPath: __dirname + '/scripts/.xinitrc.tpl'
2731
},
2832
});
2933

34+
/**
35+
* extend the tokens with expected values from args
36+
*
37+
* @param {object} args Arguments provided to this extension
38+
* @param {object} tokens Original tokens for this extension
39+
*/
40+
function _extendTokens(args, tokens) {
41+
var _tokens = {},
42+
_args = args,
43+
expectedKeys = ['flags'];
44+
45+
// if args is not an object, we'll just use an empty one
46+
if (typeof(args) !== 'object') {
47+
_args = {};
48+
}
49+
50+
// shallow-copy the original tokens object
51+
for (let key in tokens) {
52+
_tokens[key] = tokens[key];
53+
}
54+
55+
// copy expected arguments from args to the new tokens object
56+
// defaulting to an emptystring
57+
for (let key of expectedKeys) {
58+
// prepend keys with a dollar-sign for template-replacement
59+
_tokens['$'+key] = _args[key] || '';
60+
}
61+
62+
return _tokens;
63+
}
64+
3065
/**
3166
* Replace tokens in a file.
3267
*
@@ -35,12 +70,12 @@ module.exports = new Extension({
3570
* @return {string} The string with tokens replaced.
3671
*/
3772
function _replaceTokens(filePath, tokens) {
38-
console.log(_replaceTokens, filePath, tokens);
73+
debug(_replaceTokens, filePath, tokens);
3974

4075
function replace(token, value) {
4176
// tokens start with a $ which needs to be escaped, oops
4277
var _token = '\\' + token,
43-
// any '&' character needs to be escaped in the value,
78+
// any '&' character needs to be escaped in the value,
4479
// otherwise it is used as a backreference
4580
_value = value.replace(/&/g, '\\&'),
4681
// use commas as delims so that we don't need to escape value, which might be a URL

install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if [ $os == "Linux" ]; then
2323
# same for any debian disto (untested), including rpi (tested)
2424
sudo apt-get install chromium
2525
sudo apt-get install unclutter
26+
sudo apt-get install matchbox-window-manager
2627

2728
if [ $arq == "armv7l" ] || [ $arq == "armv6l" ]; then
2829
# on RaspberryPi or other arm 6/7 device

scripts/.xinitrc.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ xset -dpms
55
xset s noblank
66

77
unclutter -idle 0.1 &
8+
matchbox-window-manager -use_cursor no &
89

9-
exec /usr/bin/chromium --noerrdialogs --kiosk --incognito "$url"
10+
exec /usr/bin/chromium --noerrdialogs --kiosk --incognito $flags "$url"

test/.xinitrc.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
exec /usr/bin/chromium --noerrdialogs --kiosk --incognito $url
1+
exec /usr/bin/chromium --noerrdialogs --kiosk --incognito $flags "$url"

test/extension.spec.js

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ describe('instantiation', function() {
1111
});
1212

1313
describe('properties', function() {
14-
after(function(done) {
15-
exec('rm ' + __dirname + '/.xinitrc', done);
16-
});
17-
1814
it('should include all required format properties', function() {
1915
var format = WebsiteExtension.props.format;
2016

@@ -37,29 +33,81 @@ describe('properties', function() {
3733
assert(format.end_command);
3834
assert(typeof format.end_command === 'string');
3935
});
36+
});
4037

41-
it('start_command should update .xinitrc file with supplied token', function(done) {
42-
var format = WebsiteExtension.props.format,
43-
command,
44-
expected = 'exec /usr/bin/chromium --noerrdialogs --kiosk --incognito http://test.com';
38+
describe('start_command', function() {
39+
var expectedDefault = 'exec /usr/bin/chromium --noerrdialogs --kiosk --incognito "http://test.com"';
4540

46-
// use test .xinitrc
47-
format.xinitrcTplPath = __dirname + '/.xinitrc.tpl';
48-
format.xinitrcFinalPath = format.xinitrcTplPath.replace('.tpl', '');
41+
after(function(done) {
42+
exec('rm ' + __dirname + '/.xinitrc', done);
43+
});
44+
45+
it('should update .xinitrc file with supplied token', function(done) {
46+
var format = getTestFormat(),
47+
command;
4948

5049
// replace $url token with url string
5150
command = format.start_command({}, {
5251
$url: 'http://test.com'
5352
});
5453

5554
assert(typeof command === 'string');
55+
checkXinitrc(format.xinitrcFinalPath, expectedDefault, done);
56+
});
57+
it('should not accept undefined args parameter', function(done) {
58+
var format = getTestFormat(),
59+
command;
60+
61+
// replace $url token with url string
62+
command = format.start_command(undefined, {
63+
$url: 'http://test.com'
64+
});
65+
66+
assert(typeof command === 'string');
67+
checkXinitrc(format.xinitrcFinalPath, expectedDefault, done);
68+
});
69+
it('should update .xinitrc with optional args', function(done) {
70+
var format = getTestFormat(),
71+
command,
72+
expected = 'exec /usr/bin/chromium --noerrdialogs --kiosk --incognito --allow-insecure-localhost "http://test.com"';
5673

57-
fs.readFile(format.xinitrcFinalPath, 'utf8', function(err, data) {
74+
// replace $url token with url string
75+
command = format.start_command({flags:'--allow-insecure-localhost'}, {
76+
$url: 'http://test.com'
77+
});
78+
79+
assert(typeof command === 'string');
80+
checkXinitrc(format.xinitrcFinalPath, expected, done);
81+
});
82+
it('should ignore unsupported args', function(done) {
83+
var format = getTestFormat(),
84+
command;
85+
86+
// replace $url token with url string
87+
command = format.start_command({random:'args'}, {
88+
$url: 'http://test.com'
89+
});
90+
91+
assert(typeof command === 'string');
92+
checkXinitrc(format.xinitrcFinalPath, expectedDefault, done);
93+
});
94+
95+
function checkXinitrc(path, expected, done) {
96+
fs.readFile(path, 'utf8', function(err, data) {
5897
if (err) {
5998
throw err;
6099
}
61100
assert.equal(data, expected);
62101
done();
63102
});
64-
});
103+
}
104+
function getTestFormat() {
105+
var format = WebsiteExtension.props.format;
106+
107+
// set test .xinitrc.tpl
108+
format.xinitrcTplPath = __dirname + '/.xinitrc.tpl';
109+
format.xinitrcFinalPath = format.xinitrcTplPath.replace('.tpl', '');
110+
111+
return format;
112+
}
65113
});

0 commit comments

Comments
 (0)