Skip to content

Commit

Permalink
chore(filesystem): move several path* utility functions to deepkit/core
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Oct 17, 2023
1 parent 6e48f8c commit 4c084dd
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 51 deletions.
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './src/reflection.js';
export * from './src/url.js';
export * from './src/array.js';
export * from './src/types.js';
export * from './src/path.js';
43 changes: 43 additions & 0 deletions packages/core/src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/**
* Normalizes the given path.
* Removes duplicate slashes, removes trailing slashes, adds a leading slash.
*/
export function pathNormalize(path: string): string {
path = path[0] !== '/' ? '/' + path : path;
path = path.length > 1 && path[path.length - 1] === '/' ? path.slice(0, -1) : path;
return path.replace(/\/+/g, '/');
}

/**
* Returns the directory (dirname) of the given path.
*/
export function pathDirectory(path: string): string {
if (path === '/') return '/';
const lastSlash = path.lastIndexOf('/');
return lastSlash <= 0 ? '/' : path.slice(0, lastSlash);
}

/**
* Returns the basename of the given path.
*/
export function pathBasename(path: string): string {
const lastSlash = path.lastIndexOf('/');
return lastSlash === -1 ? path : path.slice(lastSlash + 1);
}

/**
* Returns the extension of the given path.
*/
export function pathExtension(path: string): string {
const basename = pathBasename(path);
const lastDot = basename.lastIndexOf('.');
return lastDot === -1 ? '' : basename.slice(lastDot + 1);
}

export function pathJoin(...paths: string[]): string {
return '/' + paths
.map(v => pathNormalize(v).slice(1))
.filter(v => !!v)
.join('/');
}
3 changes: 2 additions & 1 deletion packages/filesystem-aws-s3/src/s3-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FilesystemAdapter, FilesystemError, FilesystemFile, FileVisibility, pathDirectory, Reporter } from '@deepkit/filesystem';
import { FilesystemAdapter, FilesystemError, FilesystemFile, FileVisibility, Reporter } from '@deepkit/filesystem';
import { pathDirectory } from '@deepkit/core';
import {
CopyObjectCommand,
DeleteObjectsCommand,
Expand Down
3 changes: 2 additions & 1 deletion packages/filesystem-ftp/src/ftp-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FilesystemAdapter, FilesystemFile, FileType, FileVisibility, pathBasename, pathDirectory, Reporter, resolveFilesystemPath } from '@deepkit/filesystem';
import { FilesystemAdapter, FilesystemFile, FileType, FileVisibility, Reporter, resolveFilesystemPath } from '@deepkit/filesystem';
import { pathDirectory, pathBasename } from '@deepkit/core';
import { Client, FileInfo, UnixPermissions } from 'basic-ftp';
import type { ConnectionOptions as TLSConnectionOptions } from 'tls';
import { Readable, Writable } from 'stream';
Expand Down
3 changes: 2 additions & 1 deletion packages/filesystem-google/src/google-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FilesystemAdapter, FilesystemFile, FileVisibility, pathDirectory, pathNormalize, Reporter, resolveFilesystemPath } from '@deepkit/filesystem';
import { FilesystemAdapter, FilesystemFile, FileVisibility, Reporter, resolveFilesystemPath } from '@deepkit/filesystem';
import { pathDirectory, pathNormalize } from '@deepkit/core';
import { Bucket, File, Storage, StorageOptions } from '@google-cloud/storage';
import { fixAsyncOperation } from '@deepkit/core';

Expand Down
3 changes: 2 additions & 1 deletion packages/filesystem-sftp/src/sftp-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileType, FileVisibility, pathBasename, pathDirectory, Reporter, resolveFilesystemPath, FilesystemAdapter, FilesystemFile } from '@deepkit/filesystem';
import { FileType, FileVisibility, Reporter, resolveFilesystemPath, FilesystemAdapter, FilesystemFile } from '@deepkit/filesystem';
import { pathDirectory, pathBasename } from '@deepkit/core';
import Client, { ConnectOptions, FileInfo } from 'ssh2-sftp-client';
import { Readable } from 'stream';
import { asyncOperation } from '@deepkit/core';
Expand Down
49 changes: 5 additions & 44 deletions packages/filesystem/src/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { asyncOperation, Inject, isFunction } from '@deepkit/core';
import { asyncOperation, Inject, isFunction, pathBasename, pathExtension, pathJoin, pathNormalize } from '@deepkit/core';
import { readFile } from 'fs/promises';

export type FileType = 'file' | 'directory';
Expand Down Expand Up @@ -305,13 +305,6 @@ export function resolveFilesystemPath(path: FilesystemPath): string {
return path.path;
}

export function pathJoin(...paths: string[]): string {
return '/' + paths
.map(v => pathNormalize(v).slice(1))
.filter(v => !!v)
.join('/');
}

export interface FilesystemOptions {
/**
* Default visibility for new files.
Expand Down Expand Up @@ -565,6 +558,8 @@ export class Filesystem {
* Reads the contents of the given path as binary.
*
* Returns a Progress object that can be used to track the progress of the operation.
*
* @throws Error if the file does not exist.
*/
read(path: FilesystemPath): Operation<Uint8Array> {
path = resolveFilesystemPath(path);
Expand All @@ -577,6 +572,8 @@ export class Filesystem {
* Reads the contents of the given path as string.
*
* Returns a Progress object that can be used to track the progress of the operation.
*
* @throws Error if the file does not exist.
*/
readAsText(path: FilesystemPath): Operation<string> {
path = resolveFilesystemPath(path);
Expand Down Expand Up @@ -729,42 +726,6 @@ export class Filesystem {
}
}

/**
* Normalizes the given path.
* Removes duplicate slashes, removes trailing slashes, adds a leading slash.
*/
export function pathNormalize(path: string): string {
path = path[0] !== '/' ? '/' + path : path;
path = path.length > 1 && path[path.length - 1] === '/' ? path.slice(0, -1) : path;
return path.replace(/\/+/g, '/');
}

/**
* Returns the directory (dirname) of the given path.
*/
export function pathDirectory(path: string): string {
if (path === '/') return '/';
const lastSlash = path.lastIndexOf('/');
return lastSlash <= 0 ? '/' : path.slice(0, lastSlash);
}

/**
* Returns the basename of the given path.
*/
export function pathBasename(path: string): string {
const lastSlash = path.lastIndexOf('/');
return lastSlash === -1 ? path : path.slice(lastSlash + 1);
}

/**
* Returns the extension of the given path.
*/
export function pathExtension(path: string): string {
const basename = pathBasename(path);
const lastDot = basename.lastIndexOf('.');
return lastDot === -1 ? '' : basename.slice(lastDot + 1);
}

/**
* A sorting comparator for FilesystemFile that sorts directories first, then by path.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/filesystem/src/local-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileType, FileVisibility, pathDirectory, pathNormalize, Reporter, resolveFilesystemPath, FilesystemAdapter, FilesystemFile } from './filesystem.js';
import { FileType, FileVisibility, Reporter, FilesystemAdapter, FilesystemFile } from './filesystem.js';
import { pathDirectory, pathNormalize } from '@deepkit/core';
import type * as fs from 'fs/promises';

export interface FilesystemLocalAdapterOptions {
Expand Down
3 changes: 2 additions & 1 deletion packages/filesystem/src/memory-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FilesystemAdapter, FilesystemFile, FilesystemFileNotFound, FileVisibility, pathDirectories, pathDirectory, Reporter } from './filesystem.js';
import { pathDirectory } from '@deepkit/core';
import { FilesystemAdapter, FilesystemFile, FilesystemFileNotFound, FileVisibility, pathDirectories, Reporter } from './filesystem.js';

export interface FilesystemMemoryAdapterOptions {
}
Expand Down
2 changes: 1 addition & 1 deletion packages/filesystem/tests/filesystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('url', async () => {
}

//this test is about URL mapping feature from Filesystem
const url = await filesystem.publicUrl('/file1.txt');
const url = filesystem.publicUrl('/file1.txt');
expect(url).toBe('http://localhost/assets/file1.txt');

await filesystem.close();
Expand Down

0 comments on commit 4c084dd

Please sign in to comment.