Clean up exposed methods and added return types

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

@ -1,35 +1,35 @@
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.message) 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,11 +40,12 @@ export class RemixdClient extends PluginClient {
} }
} }
folderIsReadOnly () { folderIsReadOnly (): boolean {
return this.readOnly return this.readOnly
} }
get (args: SharedFolderArgs) { get (args: SharedFolderArgs): Promise<FileContent> {
try {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const path = utils.absolutePath(args.path, this.currentSharedFolder) const path = utils.absolutePath(args.path, this.currentSharedFolder)
@ -64,9 +65,12 @@ export class RemixdClient extends PluginClient {
} }
}) })
}) })
} 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 {
} }
} }
set (args: SharedFolderArgs, cb: Function) { set (args: SharedFolderArgs): Promise<void> {
if (this.readOnly) return cb('Cannot write file: read-only mode selected') try {
return new Promise((resolve, reject) => {
if (this.readOnly) reject('Cannot write file: read-only mode selected')
const isFolder = args.path.endsWith('/') const isFolder = args.path.endsWith('/')
const path = utils.absolutePath(args.path, this.currentSharedFolder) const path = utils.absolutePath(args.path, this.currentSharedFolder)
if (fs.existsSync(path) && !isRealPath(path)) return if (fs.existsSync(path) && !isRealPath(path)) reject()
if (args.content === 'undefined') { // no !!!!! if (args.content === 'undefined') { // no !!!!!
console.log('trying to write "undefined" ! stopping.') console.log('trying to write "undefined" ! stopping.')
return reject('trying to write "undefined" ! stopping.')
} }
this.trackDownStreamUpdate[path] = path this.trackDownStreamUpdate[path] = path
if (isFolder) { if (isFolder) {
fs.mkdirp(path).then(() => cb()).catch((e: Error) => cb(e)) fs.mkdirp(path).then(() => resolve()).catch((e: Error) => reject(e))
} else { } else {
fs.ensureFile(path).then(() => { fs.ensureFile(path).then(() => {
fs.writeFile(path, args.content, 'utf8', (error: Error, data: string) => { fs.writeFile(path, args.content, 'utf8', (error: Error, data: string) => {
if (error) console.log(error) if (error) {
cb(error, data) console.log(error)
reject(error)
}
resolve()
})
}).catch((e: Error) => reject(e))
}
}) })
}).catch((e: Error) => cb(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 {
return new Promise((resolve, reject) => {
if (this.readOnly) reject('Cannot rename file: read-only mode selected')
const oldpath = utils.absolutePath(args.oldPath, this.currentSharedFolder) const oldpath = utils.absolutePath(args.oldPath, this.currentSharedFolder)
if (!fs.existsSync(oldpath)) { if (!fs.existsSync(oldpath)) {
return cb('File not found ' + oldpath) reject('File not found ' + oldpath)
} }
const newpath = utils.absolutePath(args.newPath, this.currentSharedFolder) const newpath = utils.absolutePath(args.newPath, this.currentSharedFolder)
if (!isRealPath(oldpath)) return if (!isRealPath(oldpath)) return
fs.move(oldpath, newpath, (error: Error, data: string) => { fs.move(oldpath, newpath, (error: Error, data: string) => {
if (error) console.log(error) if (error) {
cb(error, data) console.log(error)
reject(error.message)
}
resolve(true)
})
}) })
} catch (error) {
throw new Error(error)
}
} }
remove (args: SharedFolderArgs, cb: Function) { remove (args: SharedFolderArgs): Promise<boolean> {
if (this.readOnly) return cb('Cannot remove file: read-only mode selected') 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) const path = utils.absolutePath(args.path, this.currentSharedFolder)
if (!fs.existsSync(path)) { if (!fs.existsSync(path)) reject('File not found ' + path)
return cb('File not found ' + path)
}
if (!isRealPath(path)) return if (!isRealPath(path)) return
fs.remove(path, (error: Error) => { return fs.remove(path, (error: Error, data: string) => {
if (error) { if (error) {
console.log(error) console.log(error)
return cb('Failed to remove file/directory: ' + error) reject('Failed to remove file/directory: ' + error)
} }
cb(error, true) resolve(true)
})
}) })
} catch (error) {
throw new Error(error)
}
} }
isDirectory (args: SharedFolderArgs, cb: Function) { isDirectory (args: SharedFolderArgs): boolean {
try {
const path = utils.absolutePath(args.path, this.currentSharedFolder) 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 {
} }
} }
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

@ -1,4 +1,4 @@
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')
* @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) {
* @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 {
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
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

@ -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
export type Filelist = KeyPairString
Loading…
Cancel
Save