Skip to content

Commit f05b03e

Browse files
committed
autostart + clean
1 parent a253891 commit f05b03e

File tree

10 files changed

+141
-75
lines changed

10 files changed

+141
-75
lines changed

PackageCreator.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
const path = require('./lib/path2');
22
const fs = require('fs');
33
const kleur = require('kleur');
4-
const { EXT_CONFIGS_DIR, DATA_CONF_PATH } = require('./lib/Constants');
4+
const { EXT_CONFIGS_DIR, DATA_CONF_PATH, LINUX_AUTOSTART_DIR } = require('./lib/Constants');
55

66
class PackageCreator
77
{
88
static CONF_FILE_PATH = path.join(__dirname, 'config.json');
99

1010

11-
constructor()
11+
constructor(conf)
1212
{
1313
console.log("### CONFIGURING PACKAGE ###");
1414

1515
this.createAppDirectories();
16-
try {this.conf = JSON.parse(fs.readFileSync(PackageCreator.CONF_FILE_PATH));}
17-
catch (e) {return console.log('Could not load config file:', e);}
16+
this.conf = conf;
1817

1918
// console.log(conf);
2019

@@ -29,7 +28,7 @@ class PackageCreator
2928

3029
createAppDirectories()
3130
{
32-
[EXT_CONFIGS_DIR].forEach(dir => {
31+
[EXT_CONFIGS_DIR, LINUX_AUTOSTART_DIR].forEach(dir => {
3332
dir = path.joinAppData(dir);
3433
if (!fs.existsSync(dir))
3534
{

config.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
},
99
"version": "0.0.1",
1010
"autoupdate": false,
11-
"electron_package": "package.json"
11+
"electron_package": "package.json",
12+
"app_name": "Webpage Accessor",
13+
"app_executable": "webpage_accessor"
1214
},
1315

1416
"log":
@@ -79,6 +81,8 @@
7981
},
8082
"extra_extensions":
8183
{
84+
"enable_shortcuts": true,
85+
"hide_toolbar": true,
8286
"extension": "window-events"
8387
},
8488
"startup_actions": {},
@@ -87,14 +91,19 @@
8791
"enabled": true,
8892
"extension": "keys-checker",
8993
"custom_id": false
94+
},
95+
"autostart":
96+
{
97+
"desktop_filename": "webpage-accessor.desktop",
98+
"extension": "autostart"
9099
}
91100
}
92101
}
93102
},
94103

95104
"default_data":
96105
{
97-
"fullscreen": false,
106+
"fullscreen": true,
98107
"autostart": true,
99108
"autorestart": true,
100109
"autoreload": false,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Desktop Entry]
2+
Name=webpage-accessor
3+
Comment=webpage accessor autostart script
4+
Type=Application
5+
Exec=webpage-accessor

extensions_main/autostart.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
const { app } = require('electron');
21
const fs = require('fs');
32
const path = require('../lib/path2');
3+
const { notStrictEqual } = require('assert');
4+
const { LINUX_AUTOSTART_DIR } = require('../lib/Constants')
45

5-
// Sample Module. Plase copy-paste this file into new module's main folder
6+
// WARNING: THIS SCRIPT WILL CHANGE THE MACHINE'S STARTUP BEHAVIOUR.
7+
// TO DISABLE REMOVE .desktop FILE
8+
// WILL TAKE EFFECT ONLY ON BUILD MODE, NOT ON DEV MODE
69
class Autostarter extends require('../lib/BaseModule')
710
{
8-
// unspecified: loads data.json
9-
// MODULE_NAME = "autostarter";
10-
11+
MODULE_NAME = "autostart";
12+
1113
setup()
1214
{
13-
this.log("setting autostart to", this.__conf.autostart == true);
14-
app.on('ready', () => {
15-
app.setLoginItemSettings({
16-
openAtLogin: (this.__conf.autostart == true), // autostart at login
17-
openAsHidden: false, // can hide window if desired
18-
});
19-
});
20-
21-
let filel = null;
22-
const logfile = path.joinAppData("lel.txt");
23-
if (fs.existsSync(logfile))
24-
filel = fs.readFileSync(logfile)
25-
else
26-
filel = ""
27-
filel = filel + "\ntried to open at " + Date.now().toString();
28-
fs.writeFileSync(logfile, filel);
15+
// LINUX SETUP]
16+
this.log("setting autostart to", this.getAppData().autostart);
17+
if (this.getAppData().autostart == true)
18+
{
19+
const file_content = `[Desktop Entry]
20+
Name=` + this.getAppConfig().app_info.app_name + `
21+
Comment=webpage accessor autostart script
22+
Type=Application
23+
Exec=` + this.getAppConfig().app_info.app_executable;
24+
// debug location
25+
// const desktop_file_dir = this.joinData(this.__conf.desktop_filename);
26+
const desktop_file_dir = path.join(LINUX_AUTOSTART_DIR, this.__conf.desktop_filename);
27+
fs.writeFileSync(desktop_file_dir, file_content);
28+
this.log("desktop file created");
29+
}
2930
}
3031
}
3132

extensions_main/window-events.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ class WindowSetup extends require('../lib/BaseModule')
1212
// Setup code here. This function is called in BaseModule's constructor.
1313
setup()
1414
{
15-
this.requireDataConf();
16-
1715
this.window.on('resize', () => {
1816
if (this.window.isFullScreen()) return ;
1917
this.tab.setBounds({x: 0, y: 0 , height: this.window.getContentBounds().height, width: this.window.getContentBounds().width});
@@ -27,18 +25,21 @@ class WindowSetup extends require('../lib/BaseModule')
2725
this.tab.setBounds({x: 0, y: 0 , height: this.window.getContentBounds().height, width: this.window.getContentBounds().width});
2826
this.logWindowDimensions('leave fullscreen');
2927
});
30-
31-
globalShortcut.register('f', () => {
32-
this.window.setFullScreen(!this.window.isFullScreen());
33-
});
34-
globalShortcut.register('d', () => {
35-
this.tab.webContents.toggleDevTools();
36-
});
37-
globalShortcut.register('c', () => {
38-
checkActiveModules();
39-
});
40-
41-
if (this.__data.fullscreen == true) this.window.setFullScreen(true);
28+
29+
if (this.__conf.enable_shortcuts == true)
30+
{
31+
globalShortcut.register('f', () => {
32+
this.window.setFullScreen(!this.window.isFullScreen());
33+
});
34+
globalShortcut.register('d', () => {
35+
this.tab.webContents.toggleDevTools();
36+
});
37+
globalShortcut.register('c', () => {
38+
checkActiveModules();
39+
});true
40+
}
41+
42+
if (this.getAppData().fullscreen == true) this.window.setFullScreen(true);
4243
}
4344

4445
logWindowDimensions(operation = '')

lib/BaseModule.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ const path = require('./path2')
33
const kleur = require('kleur');
44
const Env = require('../env');
55
const { DATA_CONF_PATH } = require('./Constants');
6+
const { app } = require('electron');
67

78
class BaseModule
89
{
10+
// extensions's configuration file content. Different from app.conf
911
__conf = null;
12+
1013
window = null;
1114
tab = null;
1215
module_name = "";
1316
is_active = false;
1417

1518
constructor() {}
1619

20+
// extension's constructors are called after window creation and after app.ready().
1721
__start(window, tab)
1822
{
1923
this.window = window;
@@ -25,10 +29,11 @@ class BaseModule
2529
else
2630
this.conf_file_path = path.joinConfigDir(this.module_name + '.json');
2731

28-
try { this.__conf = JSON.parse(fs.readFileSync(this.conf_file_path, 'utf-8')); }
29-
catch (e) { console.log('Could not load conf file for', this.module_name, ':', e, '.'); }
32+
if (fs.existsSync(this.conf_file_path))
33+
try { this.__conf = JSON.parse(fs.readFileSync(this.conf_file_path, 'utf-8')); }
34+
catch (e) { this.warn('Could not load conf file for', this.module_name, ':', e, '.'); }
3035

31-
if (!this.__conf) console.log('__conf not defined.');
36+
if (!this.__conf) this.warn('__conf not defined.');
3237
else if (this.__conf.enabled == false) return;
3338

3439
// this.log('Setting up', this.module_name, 'config:', this.__conf, '...');
@@ -38,7 +43,7 @@ class BaseModule
3843
this.log('... done');
3944
}
4045

41-
// Colored logging!
46+
// Colored logging! Use this instead of console.log()
4247
log(...message)
4348
{
4449
if (Env.DEBUG_MODE) console.log('[', kleur.green(this.module_name), ']:', ...message);
@@ -52,15 +57,44 @@ class BaseModule
5257
if (Env.DEBUG_MODE) console.log('[', kleur.red(this.module_name), ']:', ...message);
5358
}
5459

60+
// sets active status
5561
setActive(active = true) {this.is_active = active;}
5662

63+
// returns active status
5764
isActive() {return this.is_active;}
5865

59-
requireDataConf()
66+
// returns module's private folder
67+
appData()
68+
{
69+
return path.joinAppData(this.module_name);
70+
}
71+
72+
// returns module's data folder
73+
joinData(...dir)
74+
{
75+
return path.joinDataDir(this.module_name, ...dir);
76+
}
77+
78+
// returns module's conf folder
79+
joinConf(...dir)
6080
{
61-
try { this.__data = JSON.parse(fs.readFileSync(path.joinAppData(DATA_CONF_PATH))); }
62-
catch (e) { console.log('Could not load conf file for', this.module_name, ':', e, '.'); }
81+
return path.joinConfigDir(this.module_name, ...dir);
6382
}
83+
84+
// returns app.conf (config.json) content. Maybe not assigned? check main.js
85+
getAppConfig()
86+
{
87+
return app.conf;
88+
}
89+
90+
//returns app.data (data.json) content
91+
static getDataFile = false;
92+
getAppData()
93+
{
94+
return app.data;
95+
}
96+
97+
// setup Windows??
6498
}
6599

66100
module.exports = BaseModule;

lib/Constants.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
const { app } = require('electron')
2+
const path = require('path')
3+
14
EXT_CONFIGS_DIR = 'extensions_conf';
2-
APPDATA_DIRNAME = 'Webpage_Accessor';
5+
EXT_DATA_DIR = 'extensions_data'
6+
APPDATA_DIRNAME = 'webpage-accessor';
37
DATA_CONF_PATH = 'extensions_conf/data.json';
8+
LINUX_AUTOSTART_DIR = path.join(app.getPath('home'), '.config', 'autostart')
49
// APPDATA_SAFE_OVERRIDE = '';
510

6-
module.exports = { EXT_CONFIGS_DIR, APPDATA_DIRNAME, DATA_CONF_PATH };
11+
module.exports = { EXT_CONFIGS_DIR, APPDATA_DIRNAME, DATA_CONF_PATH, EXT_DATA_DIR, LINUX_AUTOSTART_DIR };

lib/path2.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
const path = require('path');
22
const fs = require('fs');
33
const { app } = require('electron')
4-
const { EXT_CONFIGS_DIR, APPDATA_DIRNAME } = require('./Constants');
4+
const { EXT_CONFIGS_DIR, APPDATA_DIRNAME, EXT_DATA_DIR } = require('./Constants');
55
const { findAppArg } = require('./utils')
66

77

88
class path2
99
{
1010
// static __dirname = path.join(__dirname, APPDATA_DIRNAME);
11-
static __dirname = path.join(app.getPath('appData'), APPDATA_DIRNAME);
11+
static appData = path.join(app.getPath('appData'), APPDATA_DIRNAME);
12+
static appRoot = path.join(__dirname, '..');
1213

13-
static config_location = path.join(path2.__dirname, EXT_CONFIGS_DIR);
14+
static config_location = path.join(path2.appData, EXT_CONFIGS_DIR);
1415

15-
static joinAppData(...dir) {return path.join(path2.__dirname, ...dir);}
16-
static joinConfigDir(...dir) {return path.join(path2.__dirname, EXT_CONFIGS_DIR, ...dir);}
16+
// returns location inside app's private folder in AppData (location is system-specific)
17+
static joinAppData(...dir) {return path.join(path2.appData, ...dir);}
18+
19+
// retuns location inside extensions_conf in AppData
20+
static joinConfigDir(...dir) {return path.join(path2.appData, EXT_CONFIGS_DIR, ...dir);}
21+
22+
// returns location inside extensions_data in app's root -- READONLY
23+
static joinDataDir(...dir) {return path.join(path2.appRoot, EXT_DATA_DIR, ...dir);}
24+
25+
// regular old join
1726
static join(...dir) {return path.join(...dir);}
1827

1928
static already_required = false;
2029
}
2130

22-
31+
// this section creates a link to extensions_conf inside appdata at the root of the app. Developer option
2332
if (!path2.already_required)
2433
{
2534
app.whenReady().then(function ()
2635
{
2736
console.log('creating dir',path2.config_location )
2837
if (!fs.existsSync(path2.config_location))
2938
try {fs.mkdirSync(path2.config_location, { recursive: true });}
30-
catch (e) {console.log("could not create path to", EXT_CONFIGS_DIR, "in", path2.__dirname, ".", )}
39+
catch (e) {console.log("could not create path to", EXT_CONFIGS_DIR, "in", path2.appData, ".", )}
3140
if (findAppArg('create_config_link') && !fs.existsSync(path.join(__dirname, '..', EXT_CONFIGS_DIR)))
32-
fs.symlinkSync(path2.config_location, path.join(__dirname, '..', EXT_CONFIGS_DIR));
41+
fs.symlinkSync(path2.config_location, path.join(appData, '..', EXT_CONFIGS_DIR));
3342
});
3443
path2.already_required = true;
3544
}

main.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { DATA_CONF_PATH } = require('./lib/Constants');
1010

1111
const DATA_FILE_PATH = path.joinAppData(DATA_CONF_PATH);
1212
const LOAD_DIR = path.join(__dirname, 'extensions_main');
13-
let PAGE_URL = url.format({
13+
const PAGE_URL = url.format({
1414
pathname: path.join(__dirname, "index.html"),
1515
// pathname: path.join("reception.parchotels.it"),
1616
// protocol: 'http'
@@ -19,24 +19,27 @@ let PAGE_URL = url.format({
1919

2020
const enabled_modules = [];
2121

22-
async function createMainWindow()
23-
{
24-
// During first execution create all config files
25-
app.args = process.argv.slice(3);
26-
app.conf = {};
27-
app.data = {};
28-
22+
// During first execution create all config files
23+
app.args = process.argv.slice(3);
24+
app.conf = {};
25+
app.data = {};
26+
27+
// loads data file, if present
28+
if (fs.existsSync(DATA_FILE_PATH))
2929
try {app.data = JSON.parse(fs.readFileSync(DATA_FILE_PATH));}
30-
catch {console.log('Main: could not load data file'); } // new pc(); return ;}
31-
30+
catch {console.log('Main: could not load data file'); }
31+
// loads config file
32+
try {app.conf = JSON.parse(fs.readFileSync(pc.CONF_FILE_PATH));}
33+
catch {console.log('Main: could not load data file'); } // new pc(); return ;}
34+
3235

36+
async function createMainWindow()
37+
{
3338
if (!app.data.is_configured)
34-
try {new pc()}
39+
try {new pc(app.conf)}
3540
catch (e) {console.log("FAILED:", e)}
3641
finally {console.log("### CONFIGURATION FINISHED ###")};
3742

38-
39-
4043
if (Env.DEBUG_MODE && app.data && app.data.version)
4144
console.log("### WELCOME TO VERSION", app.data.version, "###");
4245

0 commit comments

Comments
 (0)