11var pjson = require ( './package.json' ) ,
2- Extension = require ( 'openframe-extension' ) ;
2+ Extension = require ( 'openframe-extension' ) ,
3+ execSync = require ( 'child_process' ) . execSync ;
34
45/**
56 * Extensions should expose an instance of the Extension class.
@@ -13,7 +14,54 @@ module.exports = new Extension({
1314 // this is what might get displayed to users (not currently used)
1415 'display_name' : 'Website' ,
1516 'download' : false ,
16- 'start_command' : 'xinit /usr/bin/chromium --kiosk $url' ,
17- 'end_command' : 'sudo pkill -f chromium'
18- }
17+ 'start_command' : function ( args , tokens ) {
18+ // 1. clone template .xinitrc
19+ var filePath = _cloneTemplate ( this . xinitrcTplPath ) ;
20+ // 1. replace tokens in .xinitrc
21+ _replaceTokens ( filePath , tokens ) ;
22+ // 2. return xinit
23+ return 'xinit ' + filePath ;
24+ } ,
25+ 'end_command' : 'pkill -f X' ,
26+ xinitrcTplPath : __dirname + '/scripts/.xinitrc.tpl'
27+ } ,
1928} ) ;
29+
30+ /**
31+ * Replace tokens in a file.
32+ *
33+ * @param {string } _str
34+ * @param {object } tokens
35+ * @return {string } The string with tokens replaced.
36+ */
37+ function _replaceTokens ( filePath , tokens ) {
38+ console . log ( _replaceTokens , filePath , tokens ) ;
39+
40+ function replace ( token , value ) {
41+ // tokens start with a $ which needs to be escaped, oops
42+ var _token = '\\' + token ,
43+ // use commas as delims so that we don't need to escape value, which might be a URL
44+ cmd = 'sed -i "s,' + _token + ',' + value + ',g" ' + filePath ;
45+ execSync ( cmd ) ;
46+ }
47+
48+ var key ;
49+ for ( key in tokens ) {
50+ // TODO: better token replacement (global replacement?
51+ replace ( key , tokens [ key ] ) ;
52+ }
53+ }
54+
55+ /**
56+ * Clone xinitrc
57+ *
58+ * @return {string } The string with tokens replaced.
59+ */
60+ function _cloneTemplate ( filePath ) {
61+ var newFilePath = filePath . replace ( '.tpl' , '' ) ,
62+ cmd = 'cp -f ' + filePath + ' ' + newFilePath ;
63+
64+ execSync ( cmd ) ;
65+
66+ return newFilePath ;
67+ }
0 commit comments