Skip to content

Commit

Permalink
allow to creates files within hidden holders (#15)
Browse files Browse the repository at this point in the history
NiklasGollenstede committed Aug 8, 2018
1 parent 2ae47c3 commit fa09c22
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion background/chrome/index.js
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ const prefix = `\n/* Do not edit this section of this file (outside the Browser
// The media query seems to "reset" the parser (and doesn't do anything itself).
// At the same time it serves as split point when the changes to the files are applied to the local edit files.
const infix = `\n/*"*//*'*/;};};};};};}@media not all {} /* reset sequence, do not edit this line */ /*NEXT:${uuid}*/\n`;
const suffix = `/*END:${uuid}*/\n`;
const suffix = `\n/*END:${uuid}*/\n`;
// extracts reStyles section from the files. This allows other content to coexist with reStyles managed code
const rExtract = RegExpX`
(?:^|\n) .* \/\*START:${uuid}\*\/ .*\n ([^]*) \n.*\/\*END:${uuid}\*\/ (?:\n|$)
7 changes: 5 additions & 2 deletions background/local/index.js
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ async function enable(force) {
let files; (await Native.on((onSpawn = async process => {
native = (await process.require(require.resolve('./native')));

files = (await native.readStyles(options.local.children.folder.value, onCange, options.local.children.folder.values.isSet));
files = (await native.readStyles(options.local.children.folder.value, onCange, options.local.children.folder.values.isSet || force === 'create'));

if (options.local.children.chrome.value) { debounceIdle(() => {
files && active && native && native.watchChrome(onChromeChange);
@@ -93,7 +93,10 @@ async function enable(force) {

if (files === null) { {
Native.on.removeListener(onSpawn); active = false; letRemoteProceed();
notify.error(`Can't read local dir`, `The folder "${options.local.children.folder.value}" does not exist or can not be read. To use local styles, create the folder or change it in the options.`);
notify.error(`Can't read local dir`,
`The folder "${options.local.children.folder.value}" does not exist or can not be read.`,
`To use local styles, change the location in the options, create the folder manually or click here to do it automatically.`
).then(_=>_ && options.local.value && enable('create'));
} return; }

// console.log('got local styles', files);
12 changes: 6 additions & 6 deletions background/local/native.js
Original file line number Diff line number Diff line change
@@ -34,8 +34,8 @@ module.exports = {
*/
async writeStyle(path, css) {
path = normalize(path); if (
!(/\.css$/).test(path) || (/\/\./).test(path)
|| !(await get(FS.access, path, FS.constants.W_OK).then(_=>1,_=>0)) // writable
!(/\/[^.][^/\\]+\.css$/).test(path)
|| (await get(FS.access, path, FS.constants.W_OK).then(_=>0/*OK*/,_=>1/*not OK*/))
) { throw new Error(`Can't write to "${path}"`); }
(await get(FS.writeFile, path, css.replace(/\n/g, EOL), 'utf-8'));
},
@@ -47,8 +47,8 @@ module.exports = {
*/
async createStyle(path, css) {
path = normalize(path); if (
!(/\.css$/).test(path) || (/\/\./).test(path)
|| !(await get(FS.access, path).catch(_=>true)) // exists
!(/\/[^.][^/\\]+\.css$/).test(path)
|| (await get(FS.access, path).then(_=>1/*exists*/, _=>_.code !== 'ENOENT'/*can't write*/))
) { throw new Error(`Can't create file "${path}"`); }
(await get(FS.writeFile, path, css.replace(/\n/g, EOL), { encoding: 'utf-8', flags: 'wx', }));
},
@@ -59,8 +59,8 @@ module.exports = {
*/
async openStyle(path) {
path = normalize(path); if (
!(/\.css$/).test(path) || (/\/\./).test(path)
|| (await get(FS.access, path).catch(_=>true)) // !exists
!(/\/[^.][^/\\]+\.css$/).test(path)
|| (await get(FS.access, path).then(_=>0/*OK*/,_=>1/*not OK*/))
) { throw new Error(`Can only open existing non-hidden .css files`); }
switch (process.platform) {
case 'win32': (await
3 changes: 3 additions & 0 deletions views/panel.js
Original file line number Diff line number Diff line change
@@ -112,6 +112,9 @@ create.addEventListener('click', async event => { try {
name = name.slice(0, -4) +'-'+ Math.random().toString(16).slice(2) +'.css';
path = (await LocalStyle.createStyle(name, file));
}
// notify.success( // clicking the notification doesn't work if it is directly replaced by the restart notification
// `Created new Style at`, path, `Click here to open it!`,
// ).then(_=>_ && LocalStyle.openStyle(name).catch(notify.error));
notify.success(`Created new Style at`, path);
LocalStyle.openStyle(name).catch(e => console.error(e));
} catch (error) { notify.error(error); } });

0 comments on commit fa09c22

Please sign in to comment.