Skip to content

Commit 344f18e

Browse files
committed
appdata fix
1 parent 2042d82 commit 344f18e

File tree

11 files changed

+50
-22
lines changed

11 files changed

+50
-22
lines changed

PackageCreator.js

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

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

1011
constructor()
1112
{
1213
console.log("### CONFIGURING PACKAGE ###");
14+
15+
this.createAppDirectories();
1316
try {this.conf = JSON.parse(fs.readFileSync(PackageCreator.CONF_FILE_PATH));}
1417
catch (e) {return console.log('Could not load config file:', e);}
1518

1619
// console.log(conf);
1720

1821
this.createConfigurations();
1922

20-
try { fs.writeFileSync(path.join(__dirname, 'data.json'), JSON.stringify({...conf.default_data, is_configured: false}, null, 2)); }
21-
catch { console.log('Could not create data.json file'); }
23+
try { fs.writeFileSync(path.joinAppData('data.json'), JSON.stringify({...this.conf.default_data, is_configured: false}, null, 2)); }
24+
catch (e) { console.log('Could not create data.json file:', e); }
2225
// this.conf.is_configured = true;
2326
// fs.writeFileSync(PackageCreator.CONF_FILE_PATH, JSON.stringify(this.conf, null, 2));
2427
console.log("### PACKAGE CONFIGURATION CONCLUDED ###");
2528
}
2629

27-
30+
createAppDirectories()
31+
{
32+
[EXT_CONFIGS_DIR].forEach(dir => {
33+
dir = path.joinAppData(dir);
34+
if (!fs.existsSync(dir))
35+
{
36+
console.log("@ Creating", dir);
37+
fs.mkdirSync(dir, { recursive: true });
38+
}
39+
});
40+
}
2841
createConfigurations()
2942
{
3043
this.conf_files_dir = this.conf.configuration.conf_files_dir;
@@ -68,7 +81,7 @@ class PackageCreator
6881
this.betterLog(depth + 1, 'configuring', kleur.green(name));
6982
const ext_name = ext.extension;
7083
Reflect.deleteProperty(ext, 'extension');
71-
fs.writeFileSync(path.join(__dirname, this.conf_files_dir, ext_name) + '.json', JSON.stringify(ext, null, 2));
84+
fs.writeFileSync(path.joinConfigDir(ext_name + '.json'), JSON.stringify(ext, null, 2));
7285
}
7386

7487
betterLog(depth, ...msg)

config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
"configuration":
2727
{
28-
"conf_files_dir": "extensions_conf",
2928
"keyboard_config":
3029
{
3130
"enabled": false,

extensions_main/double-click.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const path = require('path');
2-
31
// radial double-click
42
class DoubleClick extends require('../lib/BaseModule')
53
{

extensions_main/electronAPI.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { app, ipcMain } = require('electron');
22
const fs = require('fs');
3-
const path = require('path')
43

54
// Sample Module. Plase copy-paste this file into new module's main folder
65
class ElectronAPI extends require('../lib/BaseModule')

extensions_main/window-events.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { app, globalShortcut } = require('electron');
2-
const path = require('path')
32

43
// Sample Module. Plase copy-paste this file into new module's main folder
54
class WindowSetup extends require('../lib/BaseModule')

lib/BaseModule.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const fs = require('fs');
2-
const path = require('path')
2+
const path = require('./path2')
33
const kleur = require('kleur');
44
const Env = require('../env');
55

6-
const CONF_DIR = 'extensions_conf';
6+
const { EXT_CONFIGS_DIR } = require('./Constants');
77

88
class BaseModule
99
{
@@ -20,9 +20,9 @@ class BaseModule
2020
this.window = window;
2121
this.tab = tab;
2222
this.module_name = this.MODULE_NAME || 'BaseModule';
23-
this.conf_file_path = path.join(__dirname, '..', CONF_DIR, this.module_name + '.json');
23+
this.conf_file_path = path.joinConfigDir(this.module_name + '.json');
2424
try { this.__conf = JSON.parse(fs.readFileSync(this.conf_file_path, 'utf-8')); }
25-
catch (e) { console.log('Could not load conf file for', this.module_name, ':', e.message, '.'); }
25+
catch (e) { console.log('Could not load conf file for', this.module_name, ':', e, '.'); }
2626

2727
if (!this.__conf) console.log('__conf not defined.');
2828
else if (this.__conf.enabled == false) return;

lib/Constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
EXT_CONFIGS_DIR = "extensions_conf";
2+
APPDATA_DIRNAME = 'Webpage_Accessor';
3+
4+
module.exports = { EXT_CONFIGS_DIR, APPDATA_DIRNAME };

lib/SAMPLE_MODULE.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require('path')
1+
const path = require('./path2')
22

33
// Sample Module. Plase copy-paste this file into new module's main folder
44
class SAMPLEMODULE extends require('./BaseModule')

lib/path2.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path');
2+
const { app } = require('electron')
3+
const { EXT_CONFIGS_DIR, APPDATA_DIRNAME } = require('./Constants');
4+
5+
class path2
6+
{
7+
static __dirname = path.join(app.getPath('appData'), '');
8+
9+
static joinAppData(...dir) {return path.join(path2.__dirname, APPDATA_DIRNAME, ...dir);}
10+
static joinConfigDir(...dir) {return path.join(path2.__dirname, APPDATA_DIRNAME, EXT_CONFIGS_DIR, ...dir);}
11+
static join(...dir) {return path.join(...dir);}
12+
}
13+
14+
module.exports = path2;

main.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
const path = require('path');
2+
const path = require('./lib/path2');
33
const {app, WebContentsView, BaseWindow, screen } = require('electron');
44
const url = require('url');
55
const fs = require('fs');
66
const Env = require('./env');
77
const pc = require('./PackageCreator');
88

9-
const DATA_FILE_PATH = path.join(__dirname, 'data.json');
9+
const DATA_FILE_PATH = path.joinAppData('data.json');
1010
const LOAD_DIR = path.join(__dirname, 'extensions_main');
1111
const PAGE_URL = url.format({
1212
pathname: path.join(__dirname, "index.html"),
@@ -25,8 +25,9 @@ async function createMainWindow()
2525
// try {app.conf = JSON.parse(fs.readFileSync(pc.CONF_FILE_PATH));}
2626
// catch {console.log('Could not load config file');}
2727
try {app.data = JSON.parse(fs.readFileSync(DATA_FILE_PATH));}
28-
catch {console.log('Could not load data file');}
29-
if (!app.data.is_configured) new pc();
28+
catch {console.log('Could not load data file'); } // new pc(); return ;}
29+
30+
if (!app.data.is_configured) try {new pc()} catch (e) {console.log("### CONFIGURATION FAILED:", e, "###")};
3031

3132
const {height, width} = screen.getPrimaryDisplay().workAreaSize;
3233
app.displaySize = {height: height, width: width}
@@ -64,13 +65,14 @@ async function createMainWindow()
6465
try
6566
{
6667
const ModuleClass = require(fullpath);
68+
6769
const t = new ModuleClass()
6870
enabled_modules.push(t);
6971
t.__start(mainWindow, mainTab);
7072
}
7173
catch (e)
7274
{
73-
console.log("Module not loaded:", e.message);
75+
console.log("Module not loaded:", e);
7476
}
7577
});
7678

0 commit comments

Comments
 (0)