Clean up exposed methods and added return types

pull/3094/head
ioedeveloper 5 years ago
parent 0b7e4066ee
commit ce027af6bb
  1. 192
      src/services/remixdClient.ts
  2. 15
      src/utils.ts
  3. 9
      types/index.ts

@ -1,35 +1,35 @@
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'
const isbinaryfile = require('isbinaryfile')
const fs = require('fs-extra')
export class RemixdClient extends PluginClient {
methods: ['folderIsReadOnly', 'resolveDirectory', 'get', 'exists', 'isFile']
trackDownStreamUpdate: TrackDownStreamUpdate
methods: ['folderIsReadOnly', 'resolveDirectory', 'get', 'exists', 'isFile', 'set']
trackDownStreamUpdate: TrackDownStreamUpdate = {}
websocket: WS
currentSharedFolder: string
readOnly: boolean
setWebSocket (websocket: WS) {
setWebSocket (websocket: WS): void {
this.websocket = websocket
}
sharedFolder (currentSharedFolder: string, readOnly: boolean) {
sharedFolder (currentSharedFolder: string, readOnly: boolean): void {
this.currentSharedFolder = currentSharedFolder
this.readOnly = readOnly
}
list (args: SharedFolderArgs, cb: Function) {
list (args: SharedFolderArgs): Filelist {
try {
cb(null, utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder))
return utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder)
} catch (e) {
cb(e.message)
throw new Error(e)
}
}
resolveDirectory (args: SharedFolderArgs) {
resolveDirectory (args: SharedFolderArgs): ResolveDirectory {
try {
const path = utils.absolutePath(args.path, this.currentSharedFolder)
const result = utils.resolveDirectory(path, this.currentSharedFolder)
@ -40,33 +40,37 @@ export class RemixdClient extends PluginClient {
}
}
folderIsReadOnly () {
folderIsReadOnly (): boolean {
return this.readOnly
}
get (args: SharedFolderArgs) {
return new Promise((resolve, reject) => {
const path = utils.absolutePath(args.path, this.currentSharedFolder)
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 })
})
get (args: SharedFolderArgs): Promise<FileContent> {
try {
return new Promise((resolve, reject) => {
const path = utils.absolutePath(args.path, this.currentSharedFolder)
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 })
})
}
})
})
})
} catch (error) {
throw new Error(error)
}
}
exists (args: SharedFolderArgs) {
exists (args: SharedFolderArgs): boolean {
try {
const path = utils.absolutePath(args.path, this.currentSharedFolder)
@ -76,69 +80,95 @@ export class RemixdClient extends PluginClient {
}
}
set (args: SharedFolderArgs, cb: Function) {
if (this.readOnly) return cb('Cannot write file: read-only mode selected')
const isFolder = args.path.endsWith('/')
const path = utils.absolutePath(args.path, this.currentSharedFolder)
if (fs.existsSync(path) && !isRealPath(path)) return
if (args.content === 'undefined') { // no !!!!!
console.log('trying to write "undefined" ! stopping.')
return
}
this.trackDownStreamUpdate[path] = path
if (isFolder) {
fs.mkdirp(path).then(() => cb()).catch((e: Error) => cb(e))
} else {
fs.ensureFile(path).then(() => {
fs.writeFile(path, args.content, 'utf8', (error: Error, data: string) => {
if (error) console.log(error)
cb(error, data)
})
}).catch((e: Error) => cb(e))
set (args: SharedFolderArgs): Promise<void> {
try {
return new Promise((resolve, reject) => {
if (this.readOnly) reject('Cannot write file: read-only mode selected')
const isFolder = args.path.endsWith('/')
const path = utils.absolutePath(args.path, this.currentSharedFolder)
if (fs.existsSync(path) && !isRealPath(path)) reject()
if (args.content === 'undefined') { // no !!!!!
console.log('trying to write "undefined" ! stopping.')
reject('trying to write "undefined" ! stopping.')
}
this.trackDownStreamUpdate[path] = path
if (isFolder) {
fs.mkdirp(path).then(() => resolve()).catch((e: Error) => reject(e))
} else {
fs.ensureFile(path).then(() => {
fs.writeFile(path, args.content, 'utf8', (error: Error, data: string) => {
if (error) {
console.log(error)
reject(error)
}
resolve()
})
}).catch((e: Error) => reject(e))
}
})
} catch (error) {
throw new Error(error)
}
}
rename (args: SharedFolderArgs, cb: Function) {
if (this.readOnly) return cb('Cannot rename file: read-only mode selected')
const oldpath = utils.absolutePath(args.oldPath, this.currentSharedFolder)
if (!fs.existsSync(oldpath)) {
return cb('File not found ' + oldpath)
rename (args: SharedFolderArgs): Promise<boolean> {
try {
return new Promise((resolve, reject) => {
if (this.readOnly) reject('Cannot rename file: read-only mode selected')
const oldpath = utils.absolutePath(args.oldPath, this.currentSharedFolder)
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) {
if (this.readOnly) return cb('Cannot remove file: read-only mode selected')
const path = utils.absolutePath(args.path, this.currentSharedFolder)
if (!fs.existsSync(path)) {
return cb('File not found ' + path)
remove (args: SharedFolderArgs): Promise<boolean> {
try {
return new Promise((resolve, reject) => {
if (this.readOnly) reject('Cannot remove file: read-only mode selected')
const path = utils.absolutePath(args.path, this.currentSharedFolder)
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) {
const path = utils.absolutePath(args.path, this.currentSharedFolder)
isDirectory (args: SharedFolderArgs): boolean {
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 {
const path = utils.absolutePath(args.path, this.currentSharedFolder)
@ -149,7 +179,7 @@ export class RemixdClient extends PluginClient {
}
}
function isRealPath (path: string) {
function isRealPath (path: string): boolean {
const realPath = fs.realpathSync(path)
const isRealPath = path === realPath
const mes = '[WARN] Symbolic link modification not allowed : ' + path + ' | ' + realPath

@ -1,4 +1,4 @@
import { ResolveDirectory } from '../types'
import { ResolveDirectory, Filelist } from '../types'
const fs = require('fs-extra')
const path = require('path')
@ -12,7 +12,7 @@ const pathModule = require('path')
* @param {String} sharedFolder - absolute shared path. platform dependent representation.
* @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)
if (path.indexOf(sharedFolder) !== 0) {
path = pathModule.resolve(sharedFolder, path)
@ -28,7 +28,8 @@ function absolutePath (path: string, sharedFolder:string) {
* @return {String} relative path (Unix style which is the one used by Remix IDE)
*/
function relativePath (path: string, sharedFolder: string): string {
const relative = <string>pathModule.relative(sharedFolder, path)
const relative: string = pathModule.relative(sharedFolder, path)
return normalizePath(relative)
}
@ -39,9 +40,7 @@ function normalizePath (path: string): string {
return path
}
function walkSync (dir: string, filelist: {
[key: string]: string
}, sharedFolder: string) {
function walkSync (dir: string, filelist: Filelist, sharedFolder: string): Filelist {
const files: string[] = fs.readdirSync(dir)
filelist = filelist || {}
@ -65,9 +64,11 @@ function resolveDirectory (dir: string, sharedFolder: string): ResolveDirectory
files.forEach(function (file) {
const subElement = path.join(dir, file)
if (!fs.lstatSync(subElement).isSymbolicLink()) {
const relative: string = relativePath(subElement, sharedFolder)
ret[relative] = { isDirectory: <boolean>fs.statSync(subElement).isDirectory() }
ret[relative] = { isDirectory: fs.statSync(subElement).isDirectory() }
}
})
return ret

@ -25,8 +25,15 @@ export type ResolveDirectory = {
}
}
export type FileContent = {
content: string
readonly: boolean
}
export type TrackDownStreamUpdate = KeyPairString
export type SharedFolderArgs = FolderArgs & KeyPairString
export type WS = typeof Websocket
export type WS = typeof Websocket
export type Filelist = KeyPairString
Loading…
Cancel
Save