fix env fs, electron js init, handle search/hash in scheme

This commit is contained in:
dragonwocky 2021-11-07 23:40:44 +11:00
parent 4df4235ad2
commit 3f57f01f55
Signed by: dragonwocky
GPG Key ID: 86DFC3C312A56010
7 changed files with 40 additions and 16 deletions

View File

@ -36,6 +36,7 @@ a complete rework of the enhancer, with new features and a port to the browser a
with an option to truncate timeline item titles. with an option to truncate timeline item titles.
- renamed "notion icons" to "icon sets" with new support for uploading/reusing custom icons - renamed "notion icons" to "icon sets" with new support for uploading/reusing custom icons
directly within the icon picker. directly within the icon picker.
- cli can now detect and apply to user-only installations on macOS.
#### removed #### removed

@ -1 +1 @@
Subproject commit 0ff69abb37c32146de4d93060b5c233a0ac2df6d Subproject commit 7b488677b7e97dc80befc4281058f51ff3d7cd07

19
insert/env/fs.cjs vendored
View File

@ -12,6 +12,9 @@ module.exports = {};
* @module notion-enhancer/api/fs * @module notion-enhancer/api/fs
*/ */
const fs = require('fs'),
{ resolve: resolvePath } = require('path');
/** /**
* transform a path relative to the enhancer root directory into an absolute path * transform a path relative to the enhancer root directory into an absolute path
* @param {string} path - a url or within-the-enhancer filepath * @param {string} path - a url or within-the-enhancer filepath
@ -25,8 +28,10 @@ module.exports.localPath = (path) => `notion://www.notion.so/__notion-enhancer/$
* @param {object} [opts] - the second argument of a fetch() request * @param {object} [opts] - the second argument of a fetch() request
* @returns {object} the json value of the requested file as a js object * @returns {object} the json value of the requested file as a js object
*/ */
module.exports.getJSON = (path, opts = {}) => module.exports.getJSON = (path, opts = {}) => {
fetch(path.startsWith('http') ? path : localPath(path), opts).then((res) => res.json()); if (path.startsWith('http')) return fetch(path, opts).then((res) => res.json());
return require(`notion-enhancer/${path}`);
};
/** /**
* fetch a text file's contents * fetch a text file's contents
@ -34,8 +39,10 @@ module.exports.getJSON = (path, opts = {}) =>
* @param {object} [opts] - the second argument of a fetch() request * @param {object} [opts] - the second argument of a fetch() request
* @returns {string} the text content of the requested file * @returns {string} the text content of the requested file
*/ */
module.exports.getText = (path, opts = {}) => module.exports.getText = (path, opts = {}) => {
fetch(path.startsWith('http') ? path : localPath(path), opts).then((res) => res.text()); if (path.startsWith('http')) return fetch(path, opts).then((res) => res.text());
return fs.readFileSync(resolvePath(`${__dirname}/../../${path}`));
};
/** /**
* check if a file exists * check if a file exists
@ -44,7 +51,9 @@ module.exports.getText = (path, opts = {}) =>
*/ */
module.exports.isFile = async (path) => { module.exports.isFile = async (path) => {
try { try {
await fetch(path.startsWith('http') ? path : localPath(path)); if (path.startsWith('http')) {
await fetch(path);
} else fs.existsSync(resolvePath(`${__dirname}/../../${path}`));
return true; return true;
} catch { } catch {
return false; return false;

View File

@ -100,7 +100,11 @@ module.exports.set = (path, value) => {
* @param {function} [set] - the storage set function to be wrapped * @param {function} [set] - the storage set function to be wrapped
* @returns {object} an object with the wrapped get/set functions * @returns {object} an object with the wrapped get/set functions
*/ */
module.exports.db = (namespace, getFunc = get, setFunc = set) => { module.exports.db = (
namespace,
getFunc = module.exports.get,
setFunc = module.exports.set
) => {
if (typeof namespace === 'string') namespace = [namespace]; if (typeof namespace === 'string') namespace = [namespace];
return { return {
get: (path = [], fallback = undefined) => getFunc([...namespace, ...path], fallback), get: (path = [], fallback = undefined) => getFunc([...namespace, ...path], fallback),

View File

@ -6,8 +6,6 @@
'use strict'; 'use strict';
const api = require('notion-enhancer/api/_.cjs');
module.exports = async function (target, __exports) { module.exports = async function (target, __exports) {
if (target === 'renderer/preload') { if (target === 'renderer/preload') {
require('notion-enhancer/electronApi.cjs'); require('notion-enhancer/electronApi.cjs');
@ -24,4 +22,14 @@ module.exports = async function (target, __exports) {
const { app } = require('electron'); const { app } = require('electron');
app.whenReady().then(require('notion-enhancer/worker.cjs').listen); app.whenReady().then(require('notion-enhancer/worker.cjs').listen);
} }
const api = require('notion-enhancer/api/_.cjs'),
{ registry } = api;
for (const mod of await registry.list((mod) => registry.enabled(mod.id))) {
for (const { source, target: scriptTarget } of (mod.js ? mod.js.electron : []) || []) {
if (`${target}.js` !== scriptTarget) continue;
const script = require(`notion-enhancer/repo/${mod._dir}/${source}`);
script(api, await registry.db(mod.id), __exports);
}
}
}; };

View File

@ -75,7 +75,7 @@ export default async function (
} }
await fsp.appendFile( await fsp.appendFile(
file, file,
`\n\n//notion-enhancer\nrequire('notion-enhancer')('${target}', exports))` `\n\n//notion-enhancer\nrequire('notion-enhancer')('${target}', exports)`
); );
} }
} }

View File

@ -21,17 +21,19 @@ export default async function (filepath) {
// notion-enhancer // notion-enhancer
const schemePrefix = 'notion://www.notion.so/__notion-enhancer/'; const schemePrefix = 'notion://www.notion.so/__notion-enhancer/';
if (req.url.startsWith(schemePrefix)) { if (req.url.startsWith(schemePrefix)) {
const resolvePath = (path) => require('path').resolve(\`\${__dirname}/\${path}\`), const { search, hash, pathname } = new URL(req.url),
fileExt = req.url.split('.').reverse()[0], resolvePath = (path) => require('path').resolve(\`\${__dirname}/\${path}\`),
filePath = resolvePath( fileExt = pathname.split('.').reverse()[0],
\`../node_modules/notion-enhancer/\${req.url.slice(schemePrefix.length)}\`
),
mimeDB = Object.entries(require('notion-enhancer/dep/mime-db.json')), mimeDB = Object.entries(require('notion-enhancer/dep/mime-db.json')),
mimeType = mimeDB mimeType = mimeDB
.filter(([mime, data]) => data.extensions) .filter(([mime, data]) => data.extensions)
.find(([mime, data]) => data.extensions.includes(fileExt)); .find(([mime, data]) => data.extensions.includes(fileExt));
let filePath = '../node_modules/notion-enhancer/';
filePath += req.url.slice(schemePrefix.length);
if (search) filePath = filePath.slice(0, -search.length);
if (hash) filePath = filePath.slice(0, -hash.length);
callback({ callback({
data: require('fs').createReadStream(filePath), data: require('fs').createReadStream(resolvePath(filePath)),
headers: { 'content-type': mimeType }, headers: { 'content-type': mimeType },
}); });
} }