From 3f57f01f5555e9a8332b7d1af8978ac8dee443ca Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Sun, 7 Nov 2021 23:40:44 +1100 Subject: [PATCH] fix env fs, electron js init, handle search/hash in scheme --- CHANGELOG.md | 1 + insert/api | 2 +- insert/env/fs.cjs | 19 ++++++++++++++----- insert/env/storage.cjs | 6 +++++- insert/init.cjs | 12 ++++++++++-- pkg/apply.mjs | 2 +- pkg/replacers/main/schemeHandler.mjs | 14 ++++++++------ 7 files changed, 40 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0ceb00..a189168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. - renamed "notion icons" to "icon sets" with new support for uploading/reusing custom icons directly within the icon picker. +- cli can now detect and apply to user-only installations on macOS. #### removed diff --git a/insert/api b/insert/api index 0ff69ab..7b48867 160000 --- a/insert/api +++ b/insert/api @@ -1 +1 @@ -Subproject commit 0ff69abb37c32146de4d93060b5c233a0ac2df6d +Subproject commit 7b488677b7e97dc80befc4281058f51ff3d7cd07 diff --git a/insert/env/fs.cjs b/insert/env/fs.cjs index 66f7478..01982e7 100644 --- a/insert/env/fs.cjs +++ b/insert/env/fs.cjs @@ -12,6 +12,9 @@ module.exports = {}; * @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 * @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 * @returns {object} the json value of the requested file as a js object */ -module.exports.getJSON = (path, opts = {}) => - fetch(path.startsWith('http') ? path : localPath(path), opts).then((res) => res.json()); +module.exports.getJSON = (path, opts = {}) => { + if (path.startsWith('http')) return fetch(path, opts).then((res) => res.json()); + return require(`notion-enhancer/${path}`); +}; /** * 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 * @returns {string} the text content of the requested file */ -module.exports.getText = (path, opts = {}) => - fetch(path.startsWith('http') ? path : localPath(path), opts).then((res) => res.text()); +module.exports.getText = (path, opts = {}) => { + if (path.startsWith('http')) return fetch(path, opts).then((res) => res.text()); + return fs.readFileSync(resolvePath(`${__dirname}/../../${path}`)); +}; /** * check if a file exists @@ -44,7 +51,9 @@ module.exports.getText = (path, opts = {}) => */ module.exports.isFile = async (path) => { try { - await fetch(path.startsWith('http') ? path : localPath(path)); + if (path.startsWith('http')) { + await fetch(path); + } else fs.existsSync(resolvePath(`${__dirname}/../../${path}`)); return true; } catch { return false; diff --git a/insert/env/storage.cjs b/insert/env/storage.cjs index e0e2b96..2a78f6e 100644 --- a/insert/env/storage.cjs +++ b/insert/env/storage.cjs @@ -100,7 +100,11 @@ module.exports.set = (path, value) => { * @param {function} [set] - the storage set function to be wrapped * @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]; return { get: (path = [], fallback = undefined) => getFunc([...namespace, ...path], fallback), diff --git a/insert/init.cjs b/insert/init.cjs index d6ba5d8..ed29a6c 100644 --- a/insert/init.cjs +++ b/insert/init.cjs @@ -6,8 +6,6 @@ 'use strict'; -const api = require('notion-enhancer/api/_.cjs'); - module.exports = async function (target, __exports) { if (target === 'renderer/preload') { require('notion-enhancer/electronApi.cjs'); @@ -24,4 +22,14 @@ module.exports = async function (target, __exports) { const { app } = require('electron'); 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); + } + } }; diff --git a/pkg/apply.mjs b/pkg/apply.mjs index 9293684..05100b3 100644 --- a/pkg/apply.mjs +++ b/pkg/apply.mjs @@ -75,7 +75,7 @@ export default async function ( } await fsp.appendFile( file, - `\n\n//notion-enhancer\nrequire('notion-enhancer')('${target}', exports))` + `\n\n//notion-enhancer\nrequire('notion-enhancer')('${target}', exports)` ); } } diff --git a/pkg/replacers/main/schemeHandler.mjs b/pkg/replacers/main/schemeHandler.mjs index 5f21cfe..5340c8d 100644 --- a/pkg/replacers/main/schemeHandler.mjs +++ b/pkg/replacers/main/schemeHandler.mjs @@ -21,17 +21,19 @@ export default async function (filepath) { // notion-enhancer const schemePrefix = 'notion://www.notion.so/__notion-enhancer/'; if (req.url.startsWith(schemePrefix)) { - const resolvePath = (path) => require('path').resolve(\`\${__dirname}/\${path}\`), - fileExt = req.url.split('.').reverse()[0], - filePath = resolvePath( - \`../node_modules/notion-enhancer/\${req.url.slice(schemePrefix.length)}\` - ), + const { search, hash, pathname } = new URL(req.url), + resolvePath = (path) => require('path').resolve(\`\${__dirname}/\${path}\`), + fileExt = pathname.split('.').reverse()[0], mimeDB = Object.entries(require('notion-enhancer/dep/mime-db.json')), mimeType = mimeDB .filter(([mime, data]) => data.extensions) .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({ - data: require('fs').createReadStream(filePath), + data: require('fs').createReadStream(resolvePath(filePath)), headers: { 'content-type': mimeType }, }); }