Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
baas-ide
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guxukai
baas-ide
Commits
473c9e89
Commit
473c9e89
authored
Jun 10, 2020
by
ioedeveloper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up exposed methods and added return types
parent
c9203f54
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
128 additions
and
90 deletions
+128
-90
remixdClient.ts
src/services/remixdClient.ts
+111
-81
utils.ts
src/utils.ts
+8
-7
index.ts
types/index.ts
+9
-2
No files found.
src/services/remixdClient.ts
View file @
473c9e89
import
{
PluginClient
}
from
'@remixproject/plugin'
import
{
PluginClient
}
from
'@remixproject/plugin'
import
{
SharedFolderArgs
,
TrackDownStreamUpdate
,
WS
}
from
'../../types'
import
{
SharedFolderArgs
,
TrackDownStreamUpdate
,
WS
,
Filelist
,
ResolveDirectory
,
FileContent
}
from
'../../types'
import
*
as
utils
from
'../utils'
import
*
as
utils
from
'../utils'
const
isbinaryfile
=
require
(
'isbinaryfile'
)
const
isbinaryfile
=
require
(
'isbinaryfile'
)
const
fs
=
require
(
'fs-extra'
)
const
fs
=
require
(
'fs-extra'
)
export
class
RemixdClient
extends
PluginClient
{
export
class
RemixdClient
extends
PluginClient
{
methods
:
[
'folderIsReadOnly'
,
'resolveDirectory'
,
'get'
,
'exists'
,
'isFile'
]
methods
:
[
'folderIsReadOnly'
,
'resolveDirectory'
,
'get'
,
'exists'
,
'isFile'
,
'set'
]
trackDownStreamUpdate
:
TrackDownStreamUpdate
trackDownStreamUpdate
:
TrackDownStreamUpdate
=
{}
websocket
:
WS
websocket
:
WS
currentSharedFolder
:
string
currentSharedFolder
:
string
readOnly
:
boolean
readOnly
:
boolean
setWebSocket
(
websocket
:
WS
)
{
setWebSocket
(
websocket
:
WS
)
:
void
{
this
.
websocket
=
websocket
this
.
websocket
=
websocket
}
}
sharedFolder
(
currentSharedFolder
:
string
,
readOnly
:
boolean
)
{
sharedFolder
(
currentSharedFolder
:
string
,
readOnly
:
boolean
)
:
void
{
this
.
currentSharedFolder
=
currentSharedFolder
this
.
currentSharedFolder
=
currentSharedFolder
this
.
readOnly
=
readOnly
this
.
readOnly
=
readOnly
}
}
list
(
args
:
SharedFolderArgs
,
cb
:
Function
)
{
list
(
args
:
SharedFolderArgs
):
Filelist
{
try
{
try
{
cb
(
null
,
utils
.
walkSync
(
this
.
currentSharedFolder
,
{},
this
.
currentSharedFolder
)
)
return
utils
.
walkSync
(
this
.
currentSharedFolder
,
{},
this
.
currentSharedFolder
)
}
catch
(
e
)
{
}
catch
(
e
)
{
cb
(
e
.
messag
e
)
throw
new
Error
(
e
)
}
}
}
}
resolveDirectory
(
args
:
SharedFolderArgs
)
{
resolveDirectory
(
args
:
SharedFolderArgs
)
:
ResolveDirectory
{
try
{
try
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
const
result
=
utils
.
resolveDirectory
(
path
,
this
.
currentSharedFolder
)
const
result
=
utils
.
resolveDirectory
(
path
,
this
.
currentSharedFolder
)
...
@@ -40,33 +40,37 @@ export class RemixdClient extends PluginClient {
...
@@ -40,33 +40,37 @@ export class RemixdClient extends PluginClient {
}
}
}
}
folderIsReadOnly
()
{
folderIsReadOnly
()
:
boolean
{
return
this
.
readOnly
return
this
.
readOnly
}
}
get
(
args
:
SharedFolderArgs
)
{
get
(
args
:
SharedFolderArgs
):
Promise
<
FileContent
>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
return
new
Promise
((
resolve
,
reject
)
=>
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
if
(
!
fs
.
existsSync
(
path
))
{
reject
(
'File not found '
+
path
)
if
(
!
fs
.
existsSync
(
path
))
{
}
reject
(
'File not found '
+
path
)
if
(
!
isRealPath
(
path
))
return
isbinaryfile
(
path
,
(
error
:
Error
,
isBinary
:
boolean
)
=>
{
if
(
error
)
console
.
log
(
error
)
if
(
isBinary
)
{
resolve
({
content
:
'<binary content not displayed>'
,
readonly
:
true
})
}
else
{
fs
.
readFile
(
path
,
'utf8'
,
(
error
:
Error
,
data
:
string
)
=>
{
if
(
error
)
console
.
log
(
error
)
resolve
({
content
:
data
,
readonly
:
false
})
})
}
}
if
(
!
isRealPath
(
path
))
return
isbinaryfile
(
path
,
(
error
:
Error
,
isBinary
:
boolean
)
=>
{
if
(
error
)
console
.
log
(
error
)
if
(
isBinary
)
{
resolve
({
content
:
'<binary content not displayed>'
,
readonly
:
true
})
}
else
{
fs
.
readFile
(
path
,
'utf8'
,
(
error
:
Error
,
data
:
string
)
=>
{
if
(
error
)
console
.
log
(
error
)
resolve
({
content
:
data
,
readonly
:
false
})
})
}
})
})
})
})
}
catch
(
error
)
{
throw
new
Error
(
error
)
}
}
}
exists
(
args
:
SharedFolderArgs
)
{
exists
(
args
:
SharedFolderArgs
)
:
boolean
{
try
{
try
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
...
@@ -76,69 +80,95 @@ export class RemixdClient extends PluginClient {
...
@@ -76,69 +80,95 @@ export class RemixdClient extends PluginClient {
}
}
}
}
set
(
args
:
SharedFolderArgs
,
cb
:
Function
)
{
set
(
args
:
SharedFolderArgs
):
Promise
<
void
>
{
if
(
this
.
readOnly
)
return
cb
(
'Cannot write file: read-only mode selected'
)
try
{
const
isFolder
=
args
.
path
.
endsWith
(
'/'
)
return
new
Promise
((
resolve
,
reject
)
=>
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
if
(
this
.
readOnly
)
reject
(
'Cannot write file: read-only mode selected'
)
const
isFolder
=
args
.
path
.
endsWith
(
'/'
)
if
(
fs
.
existsSync
(
path
)
&&
!
isRealPath
(
path
))
return
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
if
(
args
.
content
===
'undefined'
)
{
// no !!!!!
console
.
log
(
'trying to write "undefined" ! stopping.'
)
if
(
fs
.
existsSync
(
path
)
&&
!
isRealPath
(
path
))
reject
()
return
if
(
args
.
content
===
'undefined'
)
{
// no !!!!!
}
console
.
log
(
'trying to write "undefined" ! stopping.'
)
this
.
trackDownStreamUpdate
[
path
]
=
path
reject
(
'trying to write "undefined" ! stopping.'
)
if
(
isFolder
)
{
}
fs
.
mkdirp
(
path
).
then
(()
=>
cb
()).
catch
((
e
:
Error
)
=>
cb
(
e
))
this
.
trackDownStreamUpdate
[
path
]
=
path
}
else
{
if
(
isFolder
)
{
fs
.
ensureFile
(
path
).
then
(()
=>
{
fs
.
mkdirp
(
path
).
then
(()
=>
resolve
()).
catch
((
e
:
Error
)
=>
reject
(
e
))
fs
.
writeFile
(
path
,
args
.
content
,
'utf8'
,
(
error
:
Error
,
data
:
string
)
=>
{
}
else
{
if
(
error
)
console
.
log
(
error
)
fs
.
ensureFile
(
path
).
then
(()
=>
{
cb
(
error
,
data
)
fs
.
writeFile
(
path
,
args
.
content
,
'utf8'
,
(
error
:
Error
,
data
:
string
)
=>
{
})
if
(
error
)
{
}).
catch
((
e
:
Error
)
=>
cb
(
e
))
console
.
log
(
error
)
reject
(
error
)
}
resolve
()
})
}).
catch
((
e
:
Error
)
=>
reject
(
e
))
}
})
}
catch
(
error
)
{
throw
new
Error
(
error
)
}
}
}
}
rename
(
args
:
SharedFolderArgs
,
cb
:
Function
)
{
rename
(
args
:
SharedFolderArgs
):
Promise
<
boolean
>
{
if
(
this
.
readOnly
)
return
cb
(
'Cannot rename file: read-only mode selected'
)
try
{
const
oldpath
=
utils
.
absolutePath
(
args
.
oldPath
,
this
.
currentSharedFolder
)
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
this
.
readOnly
)
reject
(
'Cannot rename file: read-only mode selected'
)
if
(
!
fs
.
existsSync
(
oldpath
))
{
const
oldpath
=
utils
.
absolutePath
(
args
.
oldPath
,
this
.
currentSharedFolder
)
return
cb
(
'File not found '
+
oldpath
)
if
(
!
fs
.
existsSync
(
oldpath
))
{
reject
(
'File not found '
+
oldpath
)
}
const
newpath
=
utils
.
absolutePath
(
args
.
newPath
,
this
.
currentSharedFolder
)
if
(
!
isRealPath
(
oldpath
))
return
fs
.
move
(
oldpath
,
newpath
,
(
error
:
Error
,
data
:
string
)
=>
{
if
(
error
)
{
console
.
log
(
error
)
reject
(
error
.
message
)
}
resolve
(
true
)
})
})
}
catch
(
error
)
{
throw
new
Error
(
error
)
}
}
const
newpath
=
utils
.
absolutePath
(
args
.
newPath
,
this
.
currentSharedFolder
)
if
(
!
isRealPath
(
oldpath
))
return
fs
.
move
(
oldpath
,
newpath
,
(
error
:
Error
,
data
:
string
)
=>
{
if
(
error
)
console
.
log
(
error
)
cb
(
error
,
data
)
})
}
}
remove
(
args
:
SharedFolderArgs
,
cb
:
Function
)
{
remove
(
args
:
SharedFolderArgs
):
Promise
<
boolean
>
{
if
(
this
.
readOnly
)
return
cb
(
'Cannot remove file: read-only mode selected'
)
try
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
this
.
readOnly
)
reject
(
'Cannot remove file: read-only mode selected'
)
if
(
!
fs
.
existsSync
(
path
))
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
return
cb
(
'File not found '
+
path
)
if
(
!
fs
.
existsSync
(
path
))
reject
(
'File not found '
+
path
)
if
(
!
isRealPath
(
path
))
return
return
fs
.
remove
(
path
,
(
error
:
Error
,
data
:
string
)
=>
{
if
(
error
)
{
console
.
log
(
error
)
reject
(
'Failed to remove file/directory: '
+
error
)
}
resolve
(
true
)
})
})
}
catch
(
error
)
{
throw
new
Error
(
error
)
}
}
if
(
!
isRealPath
(
path
))
return
fs
.
remove
(
path
,
(
error
:
Error
)
=>
{
if
(
error
)
{
console
.
log
(
error
)
return
cb
(
'Failed to remove file/directory: '
+
error
)
}
cb
(
error
,
true
)
})
}
}
isDirectory
(
args
:
SharedFolderArgs
,
cb
:
Function
)
{
isDirectory
(
args
:
SharedFolderArgs
):
boolean
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
try
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
cb
(
null
,
fs
.
statSync
(
path
).
isDirectory
())
return
fs
.
statSync
(
path
).
isDirectory
()
}
catch
(
error
)
{
throw
new
Error
(
error
)
}
}
}
isFile
(
args
:
SharedFolderArgs
)
{
isFile
(
args
:
SharedFolderArgs
)
:
boolean
{
try
{
try
{
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
const
path
=
utils
.
absolutePath
(
args
.
path
,
this
.
currentSharedFolder
)
...
@@ -149,7 +179,7 @@ export class RemixdClient extends PluginClient {
...
@@ -149,7 +179,7 @@ export class RemixdClient extends PluginClient {
}
}
}
}
function
isRealPath
(
path
:
string
)
{
function
isRealPath
(
path
:
string
)
:
boolean
{
const
realPath
=
fs
.
realpathSync
(
path
)
const
realPath
=
fs
.
realpathSync
(
path
)
const
isRealPath
=
path
===
realPath
const
isRealPath
=
path
===
realPath
const
mes
=
'[WARN] Symbolic link modification not allowed : '
+
path
+
' | '
+
realPath
const
mes
=
'[WARN] Symbolic link modification not allowed : '
+
path
+
' | '
+
realPath
...
...
src/utils.ts
View file @
473c9e89
import
{
ResolveDirectory
}
from
'../types'
import
{
ResolveDirectory
,
Filelist
}
from
'../types'
const
fs
=
require
(
'fs-extra'
)
const
fs
=
require
(
'fs-extra'
)
const
path
=
require
(
'path'
)
const
path
=
require
(
'path'
)
...
@@ -12,7 +12,7 @@ const pathModule = require('path')
...
@@ -12,7 +12,7 @@ const pathModule = require('path')
* @param {String} sharedFolder - absolute shared path. platform dependent representation.
* @param {String} sharedFolder - absolute shared path. platform dependent representation.
* @return {String} platform dependent absolute path (/home/user1/.../... for unix, c:\user\...\... for windows)
* @return {String} platform dependent absolute path (/home/user1/.../... for unix, c:\user\...\... for windows)
*/
*/
function
absolutePath
(
path
:
string
,
sharedFolder
:
string
)
{
function
absolutePath
(
path
:
string
,
sharedFolder
:
string
)
:
string
{
path
=
normalizePath
(
path
)
path
=
normalizePath
(
path
)
if
(
path
.
indexOf
(
sharedFolder
)
!==
0
)
{
if
(
path
.
indexOf
(
sharedFolder
)
!==
0
)
{
path
=
pathModule
.
resolve
(
sharedFolder
,
path
)
path
=
pathModule
.
resolve
(
sharedFolder
,
path
)
...
@@ -28,7 +28,8 @@ function absolutePath (path: string, sharedFolder:string) {
...
@@ -28,7 +28,8 @@ function absolutePath (path: string, sharedFolder:string) {
* @return {String} relative path (Unix style which is the one used by Remix IDE)
* @return {String} relative path (Unix style which is the one used by Remix IDE)
*/
*/
function
relativePath
(
path
:
string
,
sharedFolder
:
string
):
string
{
function
relativePath
(
path
:
string
,
sharedFolder
:
string
):
string
{
const
relative
=
<
string
>
pathModule
.
relative
(
sharedFolder
,
path
)
const
relative
:
string
=
pathModule
.
relative
(
sharedFolder
,
path
)
return
normalizePath
(
relative
)
return
normalizePath
(
relative
)
}
}
...
@@ -39,9 +40,7 @@ function normalizePath (path: string): string {
...
@@ -39,9 +40,7 @@ function normalizePath (path: string): string {
return
path
return
path
}
}
function
walkSync
(
dir
:
string
,
filelist
:
{
function
walkSync
(
dir
:
string
,
filelist
:
Filelist
,
sharedFolder
:
string
):
Filelist
{
[
key
:
string
]:
string
},
sharedFolder
:
string
)
{
const
files
:
string
[]
=
fs
.
readdirSync
(
dir
)
const
files
:
string
[]
=
fs
.
readdirSync
(
dir
)
filelist
=
filelist
||
{}
filelist
=
filelist
||
{}
...
@@ -65,9 +64,11 @@ function resolveDirectory (dir: string, sharedFolder: string): ResolveDirectory
...
@@ -65,9 +64,11 @@ function resolveDirectory (dir: string, sharedFolder: string): ResolveDirectory
files
.
forEach
(
function
(
file
)
{
files
.
forEach
(
function
(
file
)
{
const
subElement
=
path
.
join
(
dir
,
file
)
const
subElement
=
path
.
join
(
dir
,
file
)
if
(
!
fs
.
lstatSync
(
subElement
).
isSymbolicLink
())
{
if
(
!
fs
.
lstatSync
(
subElement
).
isSymbolicLink
())
{
const
relative
:
string
=
relativePath
(
subElement
,
sharedFolder
)
const
relative
:
string
=
relativePath
(
subElement
,
sharedFolder
)
ret
[
relative
]
=
{
isDirectory
:
<
boolean
>
fs
.
statSync
(
subElement
).
isDirectory
()
}
ret
[
relative
]
=
{
isDirectory
:
fs
.
statSync
(
subElement
).
isDirectory
()
}
}
}
})
})
return
ret
return
ret
...
...
types/index.ts
View file @
473c9e89
...
@@ -25,8 +25,15 @@ export type ResolveDirectory = {
...
@@ -25,8 +25,15 @@ export type ResolveDirectory = {
}
}
}
}
export
type
FileContent
=
{
content
:
string
readonly
:
boolean
}
export
type
TrackDownStreamUpdate
=
KeyPairString
export
type
TrackDownStreamUpdate
=
KeyPairString
export
type
SharedFolderArgs
=
FolderArgs
&
KeyPairString
export
type
SharedFolderArgs
=
FolderArgs
&
KeyPairString
export
type
WS
=
typeof
Websocket
export
type
WS
=
typeof
Websocket
\ No newline at end of file
export
type
Filelist
=
KeyPairString
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment