@@ -8,67 +8,67 @@ const rpc = require("node-json-rpc")
88const typoraPath = process . env . TYPORA_PATH
99
1010const getSettings = async ( ) => {
11- const isObject = value => {
12- const type = typeof value
13- return value !== null && ( type === "object" || type === "function" )
11+ const isObject = value => {
12+ const type = typeof value
13+ return value !== null && ( type === "object" || type === "function" )
14+ }
15+ const merge = ( source , other ) => {
16+ if ( ! isObject ( source ) || ! isObject ( other ) ) {
17+ return other === undefined ? source : other
1418 }
15- const merge = ( source , other ) => {
16- if ( ! isObject ( source ) || ! isObject ( other ) ) {
17- return other === undefined ? source : other
18- }
19- return Object . keys ( { ...source , ...other } ) . reduce ( ( obj , key ) => {
20- const isArray = Array . isArray ( source [ key ] ) && Array . isArray ( other [ key ] )
21- obj [ key ] = isArray ? other [ key ] : merge ( source [ key ] , other [ key ] )
22- return obj
23- } , Array . isArray ( source ) ? [ ] : { } )
24- }
25- const readTOML = async ( p ) => {
26- try {
27- const content = await fs . readFile ( p , "utf-8" )
28- return toml . parse ( content )
29- } catch ( e ) {
30- }
31- return { }
19+ return Object . keys ( { ...source , ...other } ) . reduce ( ( obj , key ) => {
20+ const isArray = Array . isArray ( source [ key ] ) && Array . isArray ( other [ key ] )
21+ obj [ key ] = isArray ? other [ key ] : merge ( source [ key ] , other [ key ] )
22+ return obj
23+ } , Array . isArray ( source ) ? [ ] : { } )
24+ }
25+ const readTOML = async ( p ) => {
26+ try {
27+ const content = await fs . readFile ( p , "utf-8" )
28+ return toml . parse ( content )
29+ } catch ( e ) {
3230 }
31+ return { }
32+ }
3333
34- const userTOML = "settings.user.toml"
35- const defaultTOML = "settings.default.toml"
36- const originSettingsPath = path . resolve ( "../plugin/global/settings/" )
37- const homeSettingsPath = path . join ( os . homedir ( ) , ".config" , "typora_plugin" )
38- const paths = [
39- path . join ( originSettingsPath , defaultTOML ) ,
40- path . join ( originSettingsPath , userTOML ) ,
41- path . join ( homeSettingsPath , userTOML ) ,
42- ]
43- const files = await Promise . all ( paths . map ( readTOML ) )
44- return files . reduce ( merge )
34+ const userTOML = "settings.user.toml"
35+ const defaultTOML = "settings.default.toml"
36+ const originSettingsPath = path . resolve ( "../plugin/global/settings/" )
37+ const homeSettingsPath = path . join ( os . homedir ( ) , ".config" , "typora_plugin" )
38+ const paths = [
39+ path . join ( originSettingsPath , defaultTOML ) ,
40+ path . join ( originSettingsPath , userTOML ) ,
41+ path . join ( homeSettingsPath , userTOML ) ,
42+ ]
43+ const files = await Promise . all ( paths . map ( readTOML ) )
44+ return files . reduce ( merge )
4545}
4646
4747const initRPC = ( options ) => {
48- const client = new rpc . Client ( options )
49- const cli = {
50- self : client ,
51- call : async ( method , params ) => new Promise ( ( resolve , reject ) => {
52- client . call ( { method, params } , ( err , resp ) => {
53- if ( err ) reject ( err )
54- else resolve ( resp )
55- } )
56- } ) ,
57- eval : async ( x ) => cli . call ( "eval" , [ x ] ) ,
58- invoke : async ( plugin , fn , ...args ) => cli . call ( "invokePlugin" , [ plugin , fn , ...args ] ) ,
59- startTypora : ( ) => child_process . exec ( typoraPath ) ,
60- closeTypora : ( ) => cli . eval ( "JSBridge.invoke('window.close')" ) ,
61- }
62- return cli
48+ const client = new rpc . Client ( options )
49+ const cli = {
50+ self : client ,
51+ call : async ( method , params ) => new Promise ( ( resolve , reject ) => {
52+ client . call ( { method, params } , ( err , resp ) => {
53+ if ( err ) reject ( err )
54+ else resolve ( resp )
55+ } )
56+ } ) ,
57+ eval : async ( x ) => cli . call ( "eval" , [ x ] ) ,
58+ invoke : async ( plugin , fn , ...args ) => cli . call ( "invokePlugin" , [ plugin , fn , ...args ] ) ,
59+ startTypora : ( ) => child_process . exec ( typoraPath ) ,
60+ closeTypora : ( ) => cli . eval ( "JSBridge.invoke('window.close')" ) ,
61+ }
62+ return cli
6363}
6464
6565const run = async ( ) => {
66- const settings = await getSettings ( )
67- const jsonRPC = settings . json_rpc
68- if ( ! jsonRPC || jsonRPC . ENABLE !== true ) {
69- throw new Error ( "plugin 'json_rpc' must be enabled" )
70- }
71- return initRPC ( jsonRPC . SERVER_OPTIONS )
66+ const settings = await getSettings ( )
67+ const jsonRPC = settings . json_rpc
68+ if ( ! jsonRPC || jsonRPC . ENABLE !== true ) {
69+ throw new Error ( "plugin 'json_rpc' must be enabled" )
70+ }
71+ return initRPC ( jsonRPC . SERVER_OPTIONS )
7272}
7373
7474module . exports = run
0 commit comments