From 75cd042bd531a1fbe3e82514b296ac80e49486d9 Mon Sep 17 00:00:00 2001
From: dragonwocky <thedragonring.bod@gmail.com>
Date: Thu, 16 Jul 2020 23:48:09 +1000
Subject: [PATCH] add store to loader, bugfix homedir with wsl

---
 DOCUMENTATION.md |  2 +-
 mods/core/mod.js |  9 +++++++++
 pkg/apply.js     |  4 +++-
 pkg/helpers.js   | 24 ++++++++++++++++++++----
 pkg/helpers.md   | 14 --------------
 pkg/loader.js    |  5 ++++-
 6 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md
index 5c5100d..2a9a742 100644
--- a/DOCUMENTATION.md
+++ b/DOCUMENTATION.md
@@ -14,7 +14,7 @@ _and explore the contents of your local extracted `app.asar`._
 _explore [the existing modules](https://github.com/dragonwocky/notion-enhancer/tree/js/mods/)_
 _for examples of implementing what's described below._
 
-each directory in the `mods` folder is considered a module, with the entry points `mod.js` and `styles.css`
+each directory in the `mods` folder is considered a module, with the entry points `mod.js` and `styles.css`.
 
 | file         | description                                                                                          |
 | ------------ | ---------------------------------------------------------------------------------------------------- |
diff --git a/mods/core/mod.js b/mods/core/mod.js
index 61f9573..1b3d9cc 100644
--- a/mods/core/mod.js
+++ b/mods/core/mod.js
@@ -8,9 +8,18 @@ module.exports = {
   id: '0f0bf8b6-eae6-4273-b307-8fc43f2ee082',
   type: 'core',
   name: 'notion-enhancer core',
+  desc:
+    'the modloader itself, including: the CLI, the menu, and enabling/disabling/insertion/updating of mods.',
   version: require('../../package.json').version,
   author: 'dragonwocky',
   thumb:
     'https://camo.githubusercontent.com/5c5bca9e987d986b8cc7e51066f90c6f8a84af08/68747470733a2f2f63646e2e646973636f72646170702e636f6d2f6174746163686d656e74732f3733313634373938343332333931393933332f3733313732373235393239353032333132342f494d475f323137302e6a7067',
   options: [],
+  hacks: {
+    'renderer/preload.js': function (store) {
+      const data = store({ name: 'dragonwocky' });
+      console.log(data.name);
+      data.name = 'tom';
+    },
+  },
 };
diff --git a/pkg/apply.js b/pkg/apply.js
index f8b1464..fa8bb8a 100644
--- a/pkg/apply.js
+++ b/pkg/apply.js
@@ -8,6 +8,8 @@
 const fs = require('fs-extra'),
   path = require('path'),
   { readdirIterator } = require('readdir-enhanced'),
+  { promisify } = require('util'),
+  { exec } = require('child_process'),
   helpers = require('./helpers.js'),
   { version } = require('../package.json');
 
@@ -64,7 +66,7 @@ module.exports = async function ({ overwrite_version } = {}) {
         'bin',
         'asar.js'
       );
-    await helpers.exec(
+    await promisify(exec)(
       `"${asar_exec}" extract "${asar_app}" "${path.resolve(__notion, 'app')}"`
     );
     fs.move(asar_app, path.resolve(__notion, 'app.asar.bak'));
diff --git a/pkg/helpers.js b/pkg/helpers.js
index f1262e8..b7aa4f2 100644
--- a/pkg/helpers.js
+++ b/pkg/helpers.js
@@ -8,7 +8,8 @@
 const os = require('os'),
   path = require('path'),
   fs = require('fs-extra'),
-  exec = require('util').promisify(require('child_process').exec);
+  { exec, execSync } = require('child_process'),
+  { promisify } = require('util');
 
 // used to differentiate between "enhancer failed" and "code broken" errors.
 class EnhancerError extends Error {
@@ -24,7 +25,21 @@ const is_wsl =
     process.platform === 'linux' &&
     os.release().toLowerCase().includes('microsoft'),
   // ~/.notion-enhancer absolute path.
-  data_folder = path.resolve(os.homedir(), '.notion-enhancer');
+  data_folder = path.resolve(
+    is_wsl
+      ? (() => {
+          const stdout = execSync('cmd.exe /c echo %systemdrive%%homepath%', {
+              encoding: 'utf8',
+            }),
+            drive = stdout[0];
+          return `/mnt/${drive.toLowerCase()}${stdout
+            .replace(/\\/g, '/')
+            .slice(2)
+            .trim()}`;
+        })()
+      : os.homedir(),
+    '.notion-enhancer'
+  );
 
 // transform a wsl filepath to its relative windows filepath if necessary.
 // every file path inserted by hack.js should be put through this.
@@ -49,7 +64,9 @@ async function getNotion() {
       break;
     case 'linux':
       if (is_wsl) {
-        const { stdout } = await exec('cmd.exe /c echo %localappdata%'),
+        const { stdout } = await promisify(exec)(
+            'cmd.exe /c echo %localappdata%'
+          ),
           drive = stdout[0];
         folder = `/mnt/${drive.toLowerCase()}${stdout
           .replace(/\\/g, '/')
@@ -121,5 +138,4 @@ module.exports = {
   getNotion,
   getJSON,
   readline,
-  exec,
 };
diff --git a/pkg/helpers.md b/pkg/helpers.md
index a48dfa7..0acc263 100644
--- a/pkg/helpers.md
+++ b/pkg/helpers.md
@@ -107,17 +107,3 @@ if (overwrite) {
   // do stuff
 } else console.info(' -- keeping file: skipping step.');
 ```
-
----
-
-```js
-async function exec(command[, options]) {
- return child_process;
-}
-```
-
-use `helpers.exec()` to execute shell commands. it is a promisified version of node.js's
-[child_process.exec(command[, options][, callback])](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback).
-
-primarily used for internal processes (e.g. unpacking asar, fetching windows app path from the wsl).
-for security reasons this should not be used by modules.
diff --git a/pkg/loader.js b/pkg/loader.js
index 9605e3e..be23efc 100644
--- a/pkg/loader.js
+++ b/pkg/loader.js
@@ -7,7 +7,8 @@
 'use strict';
 const fs = require('fs-extra'),
   path = require('path'),
-  helpers = require('./helpers.js');
+  helpers = require('./helpers.js'),
+  store = require('./store.js');
 
 let __notion = helpers.getNotion();
 module.exports = async function (__file) {
@@ -30,6 +31,8 @@ module.exports = async function (__file) {
         !['extension', 'theme', 'core'].includes(mod.type)
       )
         throw Error;
+      if (mod.hacks && mod.hacks[__file])
+        mod.hacks[__file]((defaults) => store(mod.id, defaults));
       loaded_mods.push(mod.name);
     } catch (err) {
       invalid_mods.push(dir);