chore(fs): reformat code

This commit is contained in:
Julien Fontanet 2018-11-28 13:34:12 +01:00
parent 05fa76dad3
commit 30d3701ab1
5 changed files with 72 additions and 72 deletions

View File

@ -24,7 +24,7 @@ export default class RemoteHandlerAbstract {
_remote: Object
_timeout: number
constructor (remote: any, options: Object = {}) {
constructor(remote: any, options: Object = {}) {
if (remote.url === 'test://') {
this._remote = remote
} else {
@ -36,33 +36,33 @@ export default class RemoteHandlerAbstract {
;({ timeout: this._timeout = DEFAULT_TIMEOUT } = options)
}
get type (): string {
get type(): string {
throw new Error('Not implemented')
}
/**
* Asks the handler to sync the state of the effective remote with its' metadata
*/
async sync (): Promise<mixed> {
async sync(): Promise<mixed> {
return this._sync()
}
async _sync (): Promise<mixed> {
async _sync(): Promise<mixed> {
throw new Error('Not implemented')
}
/**
* Free the resources possibly dedicated to put the remote at work, when it is no more needed
*/
async forget (): Promise<void> {
async forget(): Promise<void> {
await this._forget()
}
async _forget (): Promise<void> {
async _forget(): Promise<void> {
throw new Error('Not implemented')
}
async test (): Promise<Object> {
async test(): Promise<Object> {
const testFileName = `${Date.now()}.test`
const data = await fromCallback(cb => randomBytes(1024 * 1024, cb))
let step = 'write'
@ -88,21 +88,21 @@ export default class RemoteHandlerAbstract {
}
}
async outputFile (file: string, data: Data, options?: Object): Promise<void> {
async outputFile(file: string, data: Data, options?: Object): Promise<void> {
return this._outputFile(file, data, {
flags: 'wx',
...options,
})
}
async _outputFile (file: string, data: Data, options?: Object): Promise<void> {
async _outputFile(file: string, data: Data, options?: Object): Promise<void> {
const stream = await this.createOutputStream(file, options)
const promise = fromEvent(stream, 'finish')
stream.end(data)
await promise
}
async read (
async read(
file: File,
buffer: Buffer,
position?: number
@ -110,7 +110,7 @@ export default class RemoteHandlerAbstract {
return this._read(file, buffer, position)
}
_read (
_read(
file: File,
buffer: Buffer,
position?: number
@ -118,15 +118,15 @@ export default class RemoteHandlerAbstract {
throw new Error('Not implemented')
}
async readFile (file: string, options?: Object): Promise<Buffer> {
async readFile(file: string, options?: Object): Promise<Buffer> {
return this._readFile(file, options)
}
_readFile (file: string, options?: Object): Promise<Buffer> {
_readFile(file: string, options?: Object): Promise<Buffer> {
return this.createReadStream(file, options).then(getStream.buffer)
}
async rename (
async rename(
oldPath: string,
newPath: string,
{ checksum = false }: Object = {}
@ -141,11 +141,11 @@ export default class RemoteHandlerAbstract {
return p
}
async _rename (oldPath: string, newPath: string) {
async _rename(oldPath: string, newPath: string) {
throw new Error('Not implemented')
}
async list (
async list(
dir: string = '.',
{
filter,
@ -166,11 +166,11 @@ export default class RemoteHandlerAbstract {
return entries
}
async _list (dir: string): Promise<string[]> {
async _list(dir: string): Promise<string[]> {
throw new Error('Not implemented')
}
createReadStream (
createReadStream(
file: string,
{ checksum = false, ignoreMissingChecksum = false, ...options }: Object = {}
): Promise<LaxReadable> {
@ -228,33 +228,33 @@ export default class RemoteHandlerAbstract {
)
}
async _createReadStream (
async _createReadStream(
file: string,
options?: Object
): Promise<LaxReadable> {
throw new Error('Not implemented')
}
async openFile (path: string, flags?: string): Promise<FileDescriptor> {
async openFile(path: string, flags?: string): Promise<FileDescriptor> {
return {
fd: await timeout.call(this._openFile(path, flags), this._timeout),
path,
}
}
async _openFile (path: string, flags?: string): Promise<mixed> {
async _openFile(path: string, flags?: string): Promise<mixed> {
throw new Error('Not implemented')
}
async closeFile (fd: FileDescriptor): Promise<void> {
async closeFile(fd: FileDescriptor): Promise<void> {
await timeout.call(this._closeFile(fd.fd), this._timeout)
}
async _closeFile (fd: mixed): Promise<void> {
async _closeFile(fd: mixed): Promise<void> {
throw new Error('Not implemented')
}
async refreshChecksum (path: string): Promise<void> {
async refreshChecksum(path: string): Promise<void> {
const stream = (await this.createReadStream(path)).pipe(
createChecksumStream()
)
@ -262,7 +262,7 @@ export default class RemoteHandlerAbstract {
await this.outputFile(checksumFile(path), await stream.checksum)
}
async createOutputStream (
async createOutputStream(
file: File,
{ checksum = false, ...options }: Object = {}
): Promise<LaxWritable> {
@ -296,14 +296,14 @@ export default class RemoteHandlerAbstract {
return checksumStream
}
async _createOutputStream (
async _createOutputStream(
file: mixed,
options?: Object
): Promise<LaxWritable> {
throw new Error('Not implemented')
}
async unlink (file: string, { checksum = true }: Object = {}): Promise<void> {
async unlink(file: string, { checksum = true }: Object = {}): Promise<void> {
if (checksum) {
ignoreErrors.call(this._unlink(checksumFile(file)))
}
@ -311,15 +311,15 @@ export default class RemoteHandlerAbstract {
await timeout.call(this._unlink(file), this._timeout)
}
async _unlink (file: mixed): Promise<void> {
async _unlink(file: mixed): Promise<void> {
throw new Error('Not implemented')
}
async getSize (file: mixed): Promise<number> {
async getSize(file: mixed): Promise<number> {
return timeout.call(this._getSize(file), this._timeout)
}
async _getSize (file: mixed): Promise<number> {
async _getSize(file: mixed): Promise<number> {
throw new Error('Not implemented')
}
}

View File

@ -7,7 +7,7 @@ import AbstractHandler from './abstract'
const TIMEOUT = 10e3
class TestHandler extends AbstractHandler {
constructor (impl) {
constructor(impl) {
super({ url: 'test://' }, { timeout: TIMEOUT })
Object.keys(impl).forEach(method => {

View File

@ -5,15 +5,15 @@ import { noop, startsWith } from 'lodash'
import RemoteHandlerAbstract from './abstract'
export default class LocalHandler extends RemoteHandlerAbstract {
get type () {
get type() {
return 'file'
}
_getRealPath () {
_getRealPath() {
return this._remote.path
}
_getFilePath (file) {
_getFilePath(file) {
const realPath = this._getRealPath()
const parts = [realPath]
if (file) {
@ -26,7 +26,7 @@ export default class LocalHandler extends RemoteHandlerAbstract {
return path
}
async _sync () {
async _sync() {
const path = this._getRealPath()
await fs.ensureDir(path)
await fs.access(path, fs.R_OK | fs.W_OK)
@ -34,17 +34,17 @@ export default class LocalHandler extends RemoteHandlerAbstract {
return this._remote
}
async _forget () {
async _forget() {
return noop()
}
async _outputFile (file, data, options) {
async _outputFile(file, data, options) {
const path = this._getFilePath(file)
await fs.ensureDir(dirname(path))
await fs.writeFile(path, data, options)
}
async _read (file, buffer, position) {
async _read(file, buffer, position) {
const needsClose = typeof file === 'string'
file = needsClose ? await fs.open(this._getFilePath(file), 'r') : file.fd
try {
@ -62,19 +62,19 @@ export default class LocalHandler extends RemoteHandlerAbstract {
}
}
async _readFile (file, options) {
async _readFile(file, options) {
return fs.readFile(this._getFilePath(file), options)
}
async _rename (oldPath, newPath) {
async _rename(oldPath, newPath) {
return fs.rename(this._getFilePath(oldPath), this._getFilePath(newPath))
}
async _list (dir = '.') {
async _list(dir = '.') {
return fs.readdir(this._getFilePath(dir))
}
async _createReadStream (file, options) {
async _createReadStream(file, options) {
return typeof file === 'string'
? fs.createReadStream(this._getFilePath(file), options)
: fs.createReadStream('', {
@ -84,7 +84,7 @@ export default class LocalHandler extends RemoteHandlerAbstract {
})
}
async _createOutputStream (file, options) {
async _createOutputStream(file, options) {
if (typeof file === 'string') {
const path = this._getFilePath(file)
await fs.ensureDir(dirname(path))
@ -97,7 +97,7 @@ export default class LocalHandler extends RemoteHandlerAbstract {
})
}
async _unlink (file) {
async _unlink(file) {
return fs.unlink(this._getFilePath(file)).catch(error => {
// do not throw if the file did not exist
if (error == null || error.code !== 'ENOENT') {
@ -106,18 +106,18 @@ export default class LocalHandler extends RemoteHandlerAbstract {
})
}
async _getSize (file) {
async _getSize(file) {
const stats = await fs.stat(
this._getFilePath(typeof file === 'string' ? file : file.path)
)
return stats.size
}
async _openFile (path, flags) {
async _openFile(path, flags) {
return fs.open(this._getFilePath(path), flags)
}
async _closeFile (fd) {
async _closeFile(fd) {
return fs.close(fd)
}
}

View File

@ -8,7 +8,7 @@ import LocalHandler from './local'
const DEFAULT_NFS_OPTIONS = 'vers=3'
export default class NfsHandler extends LocalHandler {
constructor (
constructor(
remote,
{ mountsDir = join(tmpdir(), 'xo-fs-mounts'), ...opts } = {}
) {
@ -17,15 +17,15 @@ export default class NfsHandler extends LocalHandler {
this._realPath = join(mountsDir, remote.id)
}
get type () {
get type() {
return 'nfs'
}
_getRealPath () {
_getRealPath() {
return this._realPath
}
async _mount () {
async _mount() {
await fs.ensureDir(this._getRealPath())
const { host, path, port, options } = this._remote
return execa(
@ -54,13 +54,13 @@ export default class NfsHandler extends LocalHandler {
})
}
async _sync () {
async _sync() {
await this._mount()
return this._remote
}
async _forget () {
async _forget() {
try {
await this._umount(this._remote)
} catch (_) {
@ -68,7 +68,7 @@ export default class NfsHandler extends LocalHandler {
}
}
async _umount () {
async _umount() {
await execa('umount', ['--force', this._getRealPath()], {
env: {
LANG: 'C',

View File

@ -7,7 +7,7 @@ const noop = () => {}
// Normalize the error code for file not found.
class ErrorWrapper extends Error {
constructor (error, newCode) {
constructor(error, newCode) {
super(error.message)
this.cause = error
this.code = newCode
@ -25,16 +25,16 @@ const normalizeError = (error, shouldBeDirectory) => {
}
export default class SmbHandler extends RemoteHandlerAbstract {
constructor (remote, opts) {
constructor(remote, opts) {
super(remote, opts)
this._forget = noop
}
get type () {
get type() {
return 'smb'
}
_getClient () {
_getClient() {
const remote = this._remote
return new Smb2({
@ -46,7 +46,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
})
}
_getFilePath (file) {
_getFilePath(file) {
if (file === '.') {
file = undefined
}
@ -65,20 +65,20 @@ export default class SmbHandler extends RemoteHandlerAbstract {
return path
}
_dirname (file) {
_dirname(file) {
const parts = file.split('\\')
parts.pop()
return parts.join('\\')
}
async _sync () {
async _sync() {
// Check access (smb2 does not expose connect in public so far...)
await this.list()
return this._remote
}
async _outputFile (file, data, options = {}) {
async _outputFile(file, data, options = {}) {
const client = this._getClient()
const path = this._getFilePath(file)
const dir = this._dirname(path)
@ -92,7 +92,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
})
}
async _read (file, buffer, position) {
async _read(file, buffer, position) {
const needsClose = typeof file === 'string'
let client
@ -113,7 +113,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
}
}
async _readFile (file, options = {}) {
async _readFile(file, options = {}) {
const client = this._getClient()
let content
@ -130,7 +130,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
return content
}
async _rename (oldPath, newPath) {
async _rename(oldPath, newPath) {
const client = this._getClient()
try {
@ -146,7 +146,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
}
}
async _list (dir = '.') {
async _list(dir = '.') {
const client = this._getClient()
let list
@ -161,7 +161,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
return list
}
async _createReadStream (file, options = {}) {
async _createReadStream(file, options = {}) {
if (typeof file !== 'string') {
file = file.path
}
@ -179,7 +179,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
return stream
}
async _createOutputStream (file, options = {}) {
async _createOutputStream(file, options = {}) {
if (typeof file !== 'string') {
file = file.path
}
@ -200,7 +200,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
return stream
}
async _unlink (file) {
async _unlink(file) {
const client = this._getClient()
try {
@ -212,7 +212,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
}
}
async _getSize (file) {
async _getSize(file) {
const client = await this._getClient()
let size
@ -230,7 +230,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
}
// TODO: add flags
async _openFile (path) {
async _openFile(path) {
const client = this._getClient()
return {
client,
@ -238,7 +238,7 @@ export default class SmbHandler extends RemoteHandlerAbstract {
}
}
async _closeFile ({ client, file }) {
async _closeFile({ client, file }) {
await client.close(file)
client.disconnect()
}