11import fs from 'fs' ;
2+ import readline from 'node:readline' ;
23
34
45//************************************ Write you dir here ************************************
56
67//Please write the "workspace/data/plugins" directory here
78//请在这里填写你的 "workspace/data/plugins" 目录
8- const targetDir = '' ;
9+ let targetDir = '' ;
910//Like this
1011// const targetDir = `H:\\SiYuanDevSpace\\data\\plugins`;
1112//********************************************************************************************
1213
14+ const log = console . log ;
1315
16+ async function getSiYuanDir ( ) {
17+ let url = 'http://127.0.0.1:6806/api/system/getWorkspaces' ;
18+ let header = {
19+ // "Authorization": `Token ${token}`,
20+ "Content-Type" : "application/json" ,
21+ }
22+ let conf = { } ;
23+ try {
24+ let response = await fetch ( url , {
25+ method : 'POST' ,
26+ headers : header
27+ } ) ;
28+ if ( response . ok ) {
29+ conf = await response . json ( ) ;
30+ } else {
31+ log ( `HTTP-Error: ${ response . status } ` ) ;
32+ return null ;
33+ }
34+ } catch ( e ) {
35+ log ( "Error:" , e ) ;
36+ log ( "Please make sure SiYuan is running!!!" ) ;
37+ return null ;
38+ }
39+ return conf . data ;
40+ }
41+
42+ async function chooseTarget ( workspaces ) {
43+ let count = workspaces . length ;
44+ log ( `Got ${ count } SiYuan ${ count > 1 ? 'workspaces' : 'workspace' } ` )
45+ for ( let i = 0 ; i < workspaces . length ; i ++ ) {
46+ log ( `[${ i } ] ${ workspaces [ i ] . path } ` ) ;
47+ }
48+
49+ if ( count == 1 ) {
50+ return `${ workspaces [ 0 ] . path } /data/plugins` ;
51+ } else {
52+ const rl = readline . createInterface ( {
53+ input : process . stdin ,
54+ output : process . stdout
55+ } ) ;
56+ let index = await new Promise ( ( resolve , reject ) => {
57+ rl . question ( `Please select a workspace[0-${ count - 1 } ]: ` , ( answer ) => {
58+ resolve ( answer ) ;
59+ } ) ;
60+ } ) ;
61+ rl . close ( ) ;
62+ return `${ workspaces [ index ] . path } /data/plugins` ;
63+ }
64+ }
1465
66+ if ( targetDir === '' ) {
67+ log ( '"targetDir" is empty, try to get SiYuan directory automatically....' )
68+ let res = await getSiYuanDir ( ) ;
69+
70+ if ( res === null ) {
71+ log ( 'Failed! You can set the plugin directory in scripts/make_dev_link.js and try again' ) ;
72+ process . exit ( 1 ) ;
73+ }
74+
75+ targetDir = await chooseTarget ( res ) ;
76+ log ( `Got target directory: ${ targetDir } ` ) ;
77+ }
1578
1679//Check
1780if ( ! fs . existsSync ( targetDir ) ) {
18- console . log ( `Failed! plugin directory not exists: "${ targetDir } "` ) ;
19- console . log ( `Please set the plugin directory in scripts/make_dev_link.js` ) ;
81+ log ( `Failed! plugin directory not exists: "${ targetDir } "` ) ;
82+ log ( `Please set the plugin directory in scripts/make_dev_link.js` ) ;
2083 process . exit ( 1 ) ;
2184}
2285
@@ -31,7 +94,7 @@ if (!fs.existsSync('./plugin.json')) {
3194const plugin = JSON . parse ( fs . readFileSync ( './plugin.json' , 'utf8' ) ) ;
3295const name = plugin ?. name ;
3396if ( ! name || name === '' ) {
34- console . log ( 'Failed! Please set plugin name in plugin.json' ) ;
97+ log ( 'Failed! Please set plugin name in plugin.json' ) ;
3598 process . exit ( 1 ) ;
3699}
37100
@@ -45,11 +108,10 @@ if (!fs.existsSync(devDir)) {
45108const targetPath = `${ targetDir } /${ name } ` ;
46109//如果已经存在,就退出
47110if ( fs . existsSync ( targetPath ) ) {
48- console . log ( 'Failed! Target directory already exists' ) ;
49- process . exit ( 1 ) ;
111+ log ( `Failed! Target directory ${ targetPath } already exists` ) ;
112+ } else {
113+ //创建软链接
114+ fs . symlinkSync ( `${ process . cwd ( ) } /dev` , targetPath , 'junction' ) ;
115+ log ( `Done! Created symlink ${ targetPath } ` ) ;
50116}
51117
52- //创建软链接
53- fs . symlinkSync ( `${ process . cwd ( ) } /dev` , targetPath , 'junction' ) ;
54- console . log ( `Done! Created symlink ${ targetPath } ` ) ;
55-
0 commit comments