de-async-ifying stuff = tray fixed!

This commit is contained in:
dragonwocky 2020-07-18 23:50:56 +10:00
parent b13fe98d40
commit d423bdbfd0
Signed by: dragonwocky
GPG Key ID: C7A48B7846AA706D
11 changed files with 135 additions and 160 deletions

View File

@ -23,7 +23,7 @@ module.exports = {
'https://camo.githubusercontent.com/5c5bca9e987d986b8cc7e51066f90c6f8a84af08/68747470733a2f2f63646e2e646973636f72646170702e636f6d2f6174746163686d656e74732f3733313634373938343332333931393933332f3733313732373235393239353032333132342f494d475f323137302e6a7067',
options: [],
hacks: {
// 'main/main.js': require('./tray.js')(defaults),
'main/main.js': require('./tray.js')(defaults),
'renderer/preload.js': function (store) {
const data = store({ name: 'dragonwocky' });
console.log(data.name);

View File

@ -6,76 +6,80 @@
*/
let tray;
const electron = require('electron'),
path = require('path'),
is_mac = process.platform === 'darwin',
is_win = process.platform === 'win32',
settings = {};
electron.app.on('ready', () => {
tray = new electron.Tray(
is_win
? path.resolve(__dirname, 'windows.ico')
: new electron.nativeImage.createFromPath(
path.resolve(__dirname, 'mac+linux.png')
).resize({
width: 16,
height: 16,
})
);
module.exports = (defaults) =>
function (store) {
const electron = require('electron'),
path = require('path'),
is_mac = process.platform === 'darwin',
is_win = process.platform === 'win32',
settings = store(defaults);
const contextMenu = electron.Menu.buildFromTemplate([
{
type: 'normal',
label: 'Bug Report',
},
{
type: 'normal',
label: 'Feature Request',
},
{
type: 'separator',
},
{
type: 'normal',
label: 'Docs',
},
{
type: 'normal',
label: 'Enhancements',
},
{
type: 'separator',
},
{
label: 'Quit',
role: 'quit',
},
]);
tray.setContextMenu(contextMenu);
tray.setToolTip('Notion');
electron.app.on('ready', () => {
tray = new electron.Tray(
is_win
? path.normalize(`${__dirname}/windows.ico`)
: new electron.nativeImage.createFromPath(
path.normalize(`${__dirname}/mac+linux.png`)
).resize({
width: 16,
height: 16,
})
);
function showWindows() {
const windows = electron.BrowserWindow.getAllWindows();
if (is_mac) electron.app.show();
if (settings.maximized) windows.forEach((win) => [win.maximize()]);
else windows.forEach((win) => win.show());
electron.app.focus({ steal: true });
}
function hideWindows() {
const windows = electron.BrowserWindow.getAllWindows();
windows.forEach((win) => [win.isFocused() && win.blur(), win.hide()]);
if (is_mac) electron.app.hide();
}
tray.on('click', () => {
const windows = electron.BrowserWindow.getAllWindows();
if (windows.some((win) => win.isVisible())) hideWindows();
else showWindows();
});
electron.globalShortcut.register(settings.hotkey, () => {
const windows = electron.BrowserWindow.getAllWindows();
if (windows.some((win) => win.isFocused() && win.isVisible()))
hideWindows();
else showWindows();
});
});
const contextMenu = electron.Menu.buildFromTemplate([
{
type: 'normal',
label: 'Bug Report',
},
{
type: 'normal',
label: 'Feature Request',
},
{
type: 'separator',
},
{
type: 'normal',
label: 'Docs',
},
{
type: 'normal',
label: 'Enhancements',
},
{
type: 'separator',
},
{
label: 'Quit',
role: 'quit',
},
]);
tray.setContextMenu(contextMenu);
tray.setToolTip('Notion');
function showWindows() {
const windows = electron.BrowserWindow.getAllWindows();
if (is_mac) electron.app.show();
if (settings.maximized) windows.forEach((win) => [win.maximize()]);
else windows.forEach((win) => win.show());
electron.app.focus({ steal: true });
}
function hideWindows() {
const windows = electron.BrowserWindow.getAllWindows();
windows.forEach((win) => [win.isFocused() && win.blur(), win.hide()]);
if (is_mac) electron.app.hide();
}
tray.on('click', () => {
const windows = electron.BrowserWindow.getAllWindows();
if (windows.some((win) => win.isVisible())) hideWindows();
else showWindows();
});
electron.globalShortcut.register(settings.hotkey, () => {
const windows = electron.BrowserWindow.getAllWindows();
if (windows.some((win) => win.isFocused() && win.isVisible()))
hideWindows();
else showWindows();
});
});
};

View File

@ -33,7 +33,6 @@
"dependencies": {
"asar": "^3.0.3",
"cac": "^6.5.12",
"caller-path": "^3.0.0",
"fs-extra": "^9.0.1",
"readdir-enhanced": "^6.0.3"
}

View File

@ -24,7 +24,6 @@ const fs = require('fs-extra'),
let __notion = helpers.getNotion();
module.exports = async function ({ overwrite_version } = {}) {
try {
__notion = await __notion;
await fs.ensureDir(helpers.data_folder);
// handle pre-existing installations: app.asar present? version set in data folder? overwrite?
@ -57,19 +56,16 @@ module.exports = async function ({ overwrite_version } = {}) {
});
}
console.info(' ...unpacking app.asar');
const asar_app = path.resolve(__notion, 'app.asar'),
asar_exec = path.resolve(
__dirname,
'..',
'node_modules',
'asar',
'bin',
'asar.js'
const asar_app = path.normalize(`${__notion}/app.asar`),
asar_exec = path.normalize(
`${__dirname}/../node_modules/asar/bin/asar.js`
);
await promisify(exec)(
`"${asar_exec}" extract "${asar_app}" "${path.resolve(__notion, 'app')}"`
`"${asar_exec}" extract "${asar_app}" "${path.normalize(
`${__notion}/app`
)}"`
);
fs.move(asar_app, path.resolve(__notion, 'app.asar.bak'));
fs.move(asar_app, path.normalize(`${__notion}/app.asar.bak`));
// patching launch script target of custom wrappers
if (
@ -98,28 +94,31 @@ module.exports = async function ({ overwrite_version } = {}) {
}
for await (let insertion_target of readdirIterator(
path.resolve(__notion, 'app'),
path.normalize(`${__notion}/app`),
{
deep: (stats) => stats.path.indexOf('node_modules') === -1,
filter: (stats) => stats.isFile() && stats.path.endsWith('.js'),
}
)) {
insertion_target = path.resolve(__notion, 'app', insertion_target);
insertion_target = path.normalize(`${__notion}/app/${insertion_target}`);
fs.appendFile(
insertion_target,
`\n\n//notion-enhancer\nrequire('${helpers.realpath(
__dirname
)}/loader.js')();`
)}/loader.js')(__filename);`
);
}
// not resolved, nothing depends on it so it's just a "let it do its thing"
console.info(' ...recording enhancement version.');
fs.outputFile(
path.resolve(__notion, 'app', 'ENHANCER_VERSION.txt'),
path.normalize(`${__notion}/app/ENHANCER_VERSION.txt`),
version
);
fs.outputFile(
path.normalize(`${helpers.data_folder}/version.txt`),
version
);
fs.outputFile(path.resolve(helpers.data_folder, 'version.txt'), version);
console.info(' ~~ success.');
return true;

View File

@ -14,13 +14,11 @@ const fs = require('fs-extra'),
let __notion = helpers.getNotion();
module.exports = async function () {
__notion = await __notion;
const version_path = path.resolve(__notion, 'app', 'ENHANCER_VERSION.txt'),
const version_path = path.normalize(`${__notion}/app/ENHANCER_VERSION.txt`),
installed_version = (await fs.pathExists(version_path))
? await fs.readFile(version_path, 'utf8')
: '?.?.?';
if (await fs.pathExists(path.resolve(__notion, 'app.asar'))) {
if (await fs.pathExists(path.normalize(`${__notion}/app.asar`))) {
return {
msg: `notion-enhancer has not been applied.`,
code: 0,

View File

@ -25,20 +25,21 @@ const is_wsl =
process.platform === 'linux' &&
os.release().toLowerCase().includes('microsoft'),
// ~/.notion-enhancer absolute path.
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'
data_folder = path.normalize(
`${
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.
@ -53,7 +54,7 @@ function realpath(hack_path) {
}
// gets possible system notion app filepaths.
async function getNotion() {
function getNotion() {
let folder = '';
switch (process.platform) {
case 'darwin':
@ -64,9 +65,9 @@ async function getNotion() {
break;
case 'linux':
if (is_wsl) {
const { stdout } = await promisify(exec)(
'cmd.exe /c echo %localappdata%'
),
const stdout = execSync('cmd.exe /c echo %localappdata%', {
encoding: 'utf8',
}),
drive = stdout[0];
folder = `/mnt/${drive.toLowerCase()}${stdout
.replace(/\\/g, '/')
@ -78,7 +79,7 @@ async function getNotion() {
'/opt/notion-app', // https://aur.archlinux.org/packages/notion-app/
'/opt/notion', // https://github.com/jaredallard/notion-app
]) {
if (await fs.pathExists(loc)) folder = loc;
if (fs.pathExistsSync(loc)) folder = loc;
}
}
}
@ -89,17 +90,17 @@ async function getNotion() {
);
// check if actual app files are present.
// if app/app.asar are missing but app.asar.bak present it will be moved to app.asar
const app_asar = path.resolve(folder, 'app.asar');
const app_asar = path.normalize(`${folder}/app.asar`);
if (
!(
(await fs.pathExists(folder)) &&
((await fs.pathExists(app_asar)) ||
(await fs.pathExists(path.resolve(folder, 'app'))))
fs.pathExistsSync(folder) &&
(fs.pathExistsSync(app_asar) ||
fs.pathExistsSync(path.normalize(`${folder}/app`)))
)
) {
const asar_bak = path.resolve(folder, 'app.asar.bak');
if (await fs.pathExists(asar_bak)) {
await fs.move(asar_bak, app_asar);
const asar_bak = path.normalize(`${__notion}/app.asar.bak`);
if (fs.pathExistsSync(asar_bak)) {
fs.moveSync(asar_bak, app_asar);
} else
throw new EnhancerError(
'nothing found: notion installation is either corrupted or non-existent.'

View File

@ -55,7 +55,7 @@ primarily used for internal handling of filepaths (e.g. for the modloader).
---
```js
async function getNotion() {
function getNotion() {
return notion_app_path;
}
```

View File

@ -8,19 +8,16 @@
const fs = require('fs-extra'),
path = require('path'),
helpers = require('./helpers.js'),
store = require('./store.js'),
caller = require('caller-path');
store = require('./store.js');
let __notion = helpers.getNotion();
module.exports = async function () {
let __file = caller();
__notion = await __notion;
module.exports = function (__file) {
__file = __file
.slice(path.resolve(__notion, 'app').length + 1)
.slice(path.normalize(`${__notion}/app`).length + 1)
.replace(/\\/g, '/');
const modules = {
source: await fs.readdir(path.resolve(__dirname, '..', 'mods')),
source: fs.readdirSync(path.normalize(`${__dirname}/../mods`)),
invalid: [],
loaded: [],
};
@ -42,9 +39,9 @@ module.exports = async function () {
);
if (
__file === 'renderer/preload.js' &&
(await fs.pathExists(
path.resolve(__dirname, '..', 'mods', dir, 'styles.css')
))
fs.pathExistsSync(
path.normalize(`${__dirname}/../mods/${dir}/styles.css`)
)
) {
document.addEventListener('readystatechange', (event) => {
if (document.readyState !== 'complete') return false;
@ -66,11 +63,8 @@ module.exports = async function () {
.session.fromPartition('persist:notion')
.protocol.registerFileProtocol('enhancement', (req, callback) => {
callback({
path: path.resolve(
__dirname,
'..',
'mods',
req.url.slice('enhancement://'.length)
path: path.normalize(
`${__dirname}/../mods/${req.url.slice('enhancement://'.length)}`
),
});
});

View File

@ -21,21 +21,20 @@ let __notion = helpers.getNotion();
module.exports = async function ({ overwrite_asar, delete_data } = {}) {
try {
const file_operations = [];
__notion = await __notion;
// extracted asar: modded
const app_folder = path.resolve(__notion, 'app');
const app_folder = path.normalize(`${__notion}/app`);
if (await fs.pathExists(app_folder)) {
console.info(` ...removing folder ${app_folder}`);
file_operations.push(fs.remove(app_folder));
} else console.warn(` * ${app_folder} not found: step skipped.`);
// restoring original asar
const asar_bak = path.resolve(__notion, 'app.asar.bak');
const asar_bak = path.normalize(`${__notion}/app.asar.bak`);
if (await fs.pathExists(asar_bak)) {
console.info(' ...moving asar.app.bak to app.asar');
if (await fs.pathExists(path.resolve(__notion, 'app.asar'))) {
if (await fs.pathExists(path.normalize(`${__notion}/app.asar`))) {
console.warn(' * app.asar already exists!');
if (overwrite_asar === undefined) {
do {
@ -57,7 +56,7 @@ module.exports = async function ({ overwrite_asar, delete_data } = {}) {
file_operations.push(
overwrite_asar || overwrite_asar === undefined
? fs.move(asar_bak, path.resolve(__notion, 'app.asar'), {
? fs.move(asar_bak, path.normalize(`${__notion}/app.asar`), {
overwrite: true,
})
: fs.remove(asar_bak)
@ -84,7 +83,7 @@ module.exports = async function ({ overwrite_asar, delete_data } = {}) {
);
if (delete_data) {
file_operations.push(fs.remove(helpers.data_folder));
} else fs.remove(path.resolve(helpers.data_folder, 'version.txt'));
} else fs.remove(path.normalize(`${helpers.data_folder}/version.txt`));
} else console.warn(` * ${helpers.data_folder} not found: step skipped.`);
await Promise.all(file_operations);

View File

@ -11,7 +11,7 @@ const path = require('path'),
// a wrapper for accessing data stored in a JSON file.
module.exports = (namespace, defaults = {}) => {
namespace = path.resolve(data_folder, namespace + '.json');
namespace = path.normalize(`${data_folder}/${namespace}.json`);
fs.ensureDirSync(data_folder);
const getData = () => ({ ...defaults, ...getJSON(namespace) });

View File

@ -69,25 +69,6 @@ cac@^6.5.12:
resolved "https://registry.yarnpkg.com/cac/-/cac-6.6.1.tgz#3dde3f6943f45d42a56729ea3573c08b3e7b6a6d"
integrity sha512-uhki4T3Ax68hw7Dufi0bATVAF8ayBSwOKUEJHjObPrUN4tlQ8Lf7oljpTje/mArLxYN0D743c2zJt4C1bVTCqg==
caller-callsite@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/caller-callsite/-/caller-callsite-4.1.0.tgz#3e33cb1d910e7b09332d59a3503b9af7462f7295"
integrity sha512-99nnnGlJexTc41xwQTr+mWl15OI5PPczUJzM4YRE7QjkefMKCXGa5gfQjCOuVrD+1TjI/fevIDHg2nz3iYN5Ig==
dependencies:
callsites "^3.1.0"
caller-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-3.0.0.tgz#a13cbec75eea1b355e3f92dc0df4288b7eebdc5f"
integrity sha512-8nvMBmBgTmEfAXywJf27jbPZlURi5xUWLwkTXa3hZFcEZa5iRnMP4sF0p00CstWNHKBENVyYWDRSByNRdshdgQ==
dependencies:
caller-callsite "^4.1.0"
callsites@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==
chromium-pickle-js@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/chromium-pickle-js/-/chromium-pickle-js-0.2.0.tgz#04a106672c18b085ab774d983dfa3ea138f22205"