-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
58 lines (48 loc) · 1.42 KB
/
preload.js
File metadata and controls
58 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* The below source code is licensed under MIT. */
const fs = require('fs');
const path = require('path');
const electron = require('electron');
const sass = require('sass');
const sucrase = require('sucrase');
const compile = (code, extension) => {
let compiled;
switch (extension) {
case 'sass':
case 'scss':
compiled = sass.compileString(code).css;
break;
case 'ts':
case 'jsx':
case 'tsx':
compiled = sucrase.transform(code, {
transforms: ['jsx', 'typescript'],
}).code;
break;
default: return code;
}
return compiled;
};
const pathJoin = (_path) => {
return path.join(__dirname, _path);
};
const fileManager = {
writeFile: (_path, data, errorHandler) => {
if (!fs.existsSync(pathJoin(_path)))
fs.mkdirSync(path.dirname(pathJoin(_path)), { recursive: true });
return fs.writeFileSync(pathJoin(_path), data, errorHandler);
},
readFile: (_path, base64) => {
if (!fs.existsSync(pathJoin(_path))) return;
return fs.readFileSync(pathJoin(_path), base64 ? { encoding: 'base64' } : 'utf-8');
},
readDir: (_path) => {
if (!fs.existsSync(pathJoin(_path))) return;
return fs.readdirSync(pathJoin(_path), 'utf-8');
}
};
const openPath = (_path) => {
try { electron.shell.openPath(pathJoin(_path)); } catch { }
};
electron.contextBridge.exposeInMainWorld('CodeSnippetsNative', Object.assign({
compile, pathJoin, openPath
}, fileManager));