Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,19 +1,10 @@
|
||||
import * as nodePath from 'path'
|
||||
import * as fs from 'fs'
|
||||
import * as mkdirp from 'mkdirp'
|
||||
import { Filesystem } from "./path/Filesystem"
|
||||
import { Filesystem } from './path/Filesystem'
|
||||
import ReadableStream = NodeJS.ReadableStream;
|
||||
import WritableStream = NodeJS.WritableStream;
|
||||
|
||||
/**
|
||||
* Possible prefixes for files referenced by a UniversalPath.
|
||||
*/
|
||||
export enum UniversalPathPrefix {
|
||||
HTTP = 'http://',
|
||||
HTTPS = 'https://',
|
||||
Local = 'file://',
|
||||
}
|
||||
|
||||
/**
|
||||
* An item that could represent a path.
|
||||
*/
|
||||
@@ -24,8 +15,10 @@ export type PathLike = string | UniversalPath
|
||||
* @param parts
|
||||
*/
|
||||
export function universalPath(...parts: PathLike[]): UniversalPath {
|
||||
let [main, ...concats] = parts
|
||||
if ( !(main instanceof UniversalPath) ) main = new UniversalPath(main)
|
||||
let [main, ...concats] = parts // eslint-disable-line prefer-const
|
||||
if ( !(main instanceof UniversalPath) ) {
|
||||
main = new UniversalPath(main)
|
||||
}
|
||||
return main.concat(...concats)
|
||||
}
|
||||
|
||||
@@ -42,8 +35,11 @@ export function universalPath(...parts: PathLike[]): UniversalPath {
|
||||
export async function* walk(dir: string): any {
|
||||
for await (const sub of await fs.promises.opendir(dir) ) {
|
||||
const entry = nodePath.join(dir, sub.name)
|
||||
if ( sub.isDirectory() ) yield* walk(entry)
|
||||
else if ( sub.isFile() ) yield entry
|
||||
if ( sub.isDirectory() ) {
|
||||
yield* walk(entry)
|
||||
} else if ( sub.isFile() ) {
|
||||
yield entry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,8 +47,9 @@ export async function* walk(dir: string): any {
|
||||
* Class representing some kind of filesystem resource.
|
||||
*/
|
||||
export class UniversalPath {
|
||||
protected _prefix!: string
|
||||
protected _local!: string
|
||||
protected resourcePrefix!: string
|
||||
|
||||
protected resourceLocalPath!: string
|
||||
|
||||
constructor(
|
||||
/**
|
||||
@@ -72,15 +69,15 @@ export class UniversalPath {
|
||||
* Determine the correct prefix for this path.
|
||||
* @protected
|
||||
*/
|
||||
protected setPrefix() {
|
||||
protected setPrefix(): void {
|
||||
if ( this.initial.toLowerCase().startsWith('http://') ) {
|
||||
this._prefix = UniversalPathPrefix.HTTP
|
||||
this.resourcePrefix = 'http://'
|
||||
} else if ( this.initial.toLowerCase().startsWith('https://') ) {
|
||||
this._prefix = UniversalPathPrefix.HTTPS
|
||||
this.resourcePrefix = 'https://'
|
||||
} else if ( this.filesystem ) {
|
||||
this._prefix = this.filesystem.getPrefix()
|
||||
this.resourcePrefix = this.filesystem.getPrefix()
|
||||
} else {
|
||||
this._prefix = UniversalPathPrefix.Local
|
||||
this.resourcePrefix = 'file://'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,68 +91,68 @@ export class UniversalPath {
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected setLocal() {
|
||||
this._local = this.initial
|
||||
if ( this.initial.toLowerCase().startsWith(this._prefix) ) {
|
||||
this._local = this._local.slice(this._prefix.length)
|
||||
protected setLocal(): void {
|
||||
this.resourceLocalPath = this.initial
|
||||
if ( this.initial.toLowerCase().startsWith(this.resourcePrefix) ) {
|
||||
this.resourceLocalPath = this.resourceLocalPath.slice(this.resourcePrefix.length)
|
||||
}
|
||||
|
||||
if ( this._prefix === UniversalPathPrefix.Local && !this._local.startsWith('/') && !this.filesystem ) {
|
||||
this._local = nodePath.resolve(this._local)
|
||||
if ( this.resourcePrefix === 'file://' && !this.resourceLocalPath.startsWith('/') && !this.filesystem ) {
|
||||
this.resourceLocalPath = nodePath.resolve(this.resourceLocalPath)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new copy of this UniversalPath instance.
|
||||
*/
|
||||
clone() {
|
||||
clone(): UniversalPath {
|
||||
return new UniversalPath(this.initial)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the UniversalPathPrefix of this resource.
|
||||
* Get the string of this resource.
|
||||
*/
|
||||
get prefix() {
|
||||
return this._prefix
|
||||
get prefix(): string {
|
||||
return this.resourcePrefix
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this resource refers to a file on the local filesystem.
|
||||
*/
|
||||
get isLocal() {
|
||||
return this._prefix === UniversalPathPrefix.Local && !this.filesystem
|
||||
get isLocal(): boolean {
|
||||
return this.resourcePrefix === 'file://' && !this.filesystem
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this resource refers to a file on a remote filesystem.
|
||||
*/
|
||||
get isRemote() {
|
||||
return this._prefix !== UniversalPathPrefix.Local || this.filesystem
|
||||
get isRemote(): boolean {
|
||||
return Boolean(this.resourcePrefix !== 'file://' || this.filesystem)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the non-prefixed path to this resource.
|
||||
*/
|
||||
get unqualified() {
|
||||
return this._local
|
||||
get unqualified(): string {
|
||||
return this.resourceLocalPath
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to this resource as it would be accessed from the current filesystem.
|
||||
*/
|
||||
get toLocal() {
|
||||
get toLocal(): string {
|
||||
if ( this.isLocal ) {
|
||||
return this._local
|
||||
return this.resourceLocalPath
|
||||
} else {
|
||||
return `${this.prefix}${this._local}`
|
||||
return `${this.prefix}${this.resourceLocalPath}`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fully-prefixed path to this resource.
|
||||
*/
|
||||
get toRemote() {
|
||||
return `${this.prefix}${this._local}`
|
||||
get toRemote(): string {
|
||||
return `${this.prefix}${this.resourceLocalPath}`
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,15 +180,15 @@ export class UniversalPath {
|
||||
* @param path
|
||||
*/
|
||||
public append(path: PathLike): this {
|
||||
this._local += String(path)
|
||||
this.resourceLocalPath += String(path)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast the path to a string (fully-prefixed).
|
||||
*/
|
||||
toString() {
|
||||
return `${this.prefix}${this._local}`
|
||||
toString(): string {
|
||||
return `${this.prefix}${this.resourceLocalPath}`
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,8 +201,8 @@ export class UniversalPath {
|
||||
* myFile.ext // => 'txt'
|
||||
* ```
|
||||
*/
|
||||
get ext() {
|
||||
return nodePath.extname(this._local)
|
||||
get ext(): string {
|
||||
return nodePath.extname(this.resourceLocalPath)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,26 +220,26 @@ export class UniversalPath {
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
walk() {
|
||||
return walk(this._local)
|
||||
walk(): any {
|
||||
return walk(this.resourceLocalPath)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given resource exists at the path.
|
||||
*/
|
||||
async exists() {
|
||||
async exists(): Promise<boolean> {
|
||||
if ( this.filesystem ) {
|
||||
const stat = await this.filesystem.stat({
|
||||
storePath: this._local
|
||||
storePath: this.resourceLocalPath,
|
||||
})
|
||||
|
||||
return stat.exists
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.promises.stat(this._local)
|
||||
await fs.promises.stat(this.resourceLocalPath)
|
||||
return true
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -250,13 +247,13 @@ export class UniversalPath {
|
||||
/**
|
||||
* Recursively create this path as a directory. Equivalent to `mkdir -p` on Linux.
|
||||
*/
|
||||
async mkdir() {
|
||||
async mkdir(): Promise<void> {
|
||||
if ( this.filesystem ) {
|
||||
await this.filesystem.mkdir({
|
||||
storePath: this._local
|
||||
storePath: this.resourceLocalPath,
|
||||
})
|
||||
} else {
|
||||
await mkdirp(this._local)
|
||||
await mkdirp(this.resourceLocalPath)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,22 +261,27 @@ export class UniversalPath {
|
||||
* Write the given data to this resource as a file.
|
||||
* @param data
|
||||
*/
|
||||
async write(data: string | Buffer) {
|
||||
if ( typeof data === 'string' ) data = Buffer.from(data, 'utf8')
|
||||
async write(data: string | Buffer): Promise<void> {
|
||||
if ( typeof data === 'string' ) {
|
||||
data = Buffer.from(data, 'utf8')
|
||||
}
|
||||
|
||||
if ( this.filesystem ) {
|
||||
const stream = await this.filesystem.putStoreFileAsStream({
|
||||
storePath: this._local
|
||||
storePath: this.resourceLocalPath,
|
||||
})
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
stream.write(data, err => {
|
||||
if ( err ) rej(err)
|
||||
else res()
|
||||
if ( err ) {
|
||||
rej(err)
|
||||
} else {
|
||||
res()
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
const fd = await fs.promises.open(this._local, 'w')
|
||||
const fd = await fs.promises.open(this.resourceLocalPath, 'w')
|
||||
await fd.write(data)
|
||||
await fd.close()
|
||||
}
|
||||
@@ -291,24 +293,24 @@ export class UniversalPath {
|
||||
async writeStream(): Promise<WritableStream> {
|
||||
if ( this.filesystem ) {
|
||||
return this.filesystem.putStoreFileAsStream({
|
||||
storePath: this._local
|
||||
storePath: this.resourceLocalPath,
|
||||
})
|
||||
} else {
|
||||
return fs.createWriteStream(this._local)
|
||||
return fs.createWriteStream(this.resourceLocalPath)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the data from this resource's file as a string.
|
||||
*/
|
||||
async read() {
|
||||
async read(): Promise<string> {
|
||||
let stream: ReadableStream
|
||||
if ( this.filesystem ) {
|
||||
stream = await this.filesystem.getStoreFileAsStream({
|
||||
storePath: this._local
|
||||
storePath: this.resourceLocalPath,
|
||||
})
|
||||
} else {
|
||||
stream = fs.createReadStream(this._local)
|
||||
stream = fs.createReadStream(this.resourceLocalPath)
|
||||
}
|
||||
|
||||
const chunks: any[] = []
|
||||
@@ -325,14 +327,14 @@ export class UniversalPath {
|
||||
async readStream(): Promise<ReadableStream> {
|
||||
if ( this.filesystem ) {
|
||||
return this.filesystem.getStoreFileAsStream({
|
||||
storePath: this._local
|
||||
storePath: this.resourceLocalPath,
|
||||
})
|
||||
} else {
|
||||
return fs.createReadStream(this._local)
|
||||
return fs.createReadStream(this.resourceLocalPath)
|
||||
}
|
||||
}
|
||||
|
||||
/*get mime_type() {
|
||||
/* get mime_type() {
|
||||
return Mime.lookup(this.ext)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user