diff --git a/packages/php-wasm/node/src/test/php.spec.ts b/packages/php-wasm/node/src/test/php.spec.ts index 9e74c87b..dbf7fa1c 100644 --- a/packages/php-wasm/node/src/test/php.spec.ts +++ b/packages/php-wasm/node/src/test/php.spec.ts @@ -152,6 +152,16 @@ describe.each(SupportedPHPVersions)('PHP %s', (phpVersion) => { 'test2.txt', ]); }); + + it('listFiles() option prependPath should prepend given path to all files returned', () => { + php.mkdir(testDirPath); + php.writeFile(testDirPath + '/test.txt', 'Hello World!'); + php.writeFile(testDirPath + '/test2.txt', 'Hello World!'); + expect(php.listFiles(testDirPath, { prependPath: true })).toEqual([ + testDirPath + '/test.txt', + testDirPath + '/test2.txt', + ]); + }); }); describe('Stdio', () => { diff --git a/packages/php-wasm/universal/src/lib/base-php.ts b/packages/php-wasm/universal/src/lib/base-php.ts index 19e35d2f..706cd4ec 100644 --- a/packages/php-wasm/universal/src/lib/base-php.ts +++ b/packages/php-wasm/universal/src/lib/base-php.ts @@ -14,6 +14,7 @@ import { PHPRequestHeaders, PHPRunOptions, RmDirOptions, + ListFilesOptions, } from './universal-php'; import { getFunctionsMaybeMissingFromAsyncify, @@ -507,14 +508,22 @@ export abstract class BasePHP implements IsomorphicLocalPHP { /** @inheritDoc */ @rethrowFileSystemError('Could not list files in "{path}"') - listFiles(path: string): string[] { + listFiles( + path: string, + options: ListFilesOptions = { prependPath: false } + ): string[] { if (!this.fileExists(path)) { return []; } try { - return this[__private__dont__use].FS.readdir(path).filter( + const files = this[__private__dont__use].FS.readdir(path).filter( (name: string) => name !== '.' && name !== '..' ); + if (options.prependPath) { + const prepend = path.replace(/\/$/, ''); + return files.map((name: string) => `${prepend}/${name}`); + } + return files; } catch (e) { console.error(e, { path }); return []; diff --git a/packages/php-wasm/universal/src/lib/universal-php.ts b/packages/php-wasm/universal/src/lib/universal-php.ts index 4a598a7c..5be4f29d 100644 --- a/packages/php-wasm/universal/src/lib/universal-php.ts +++ b/packages/php-wasm/universal/src/lib/universal-php.ts @@ -439,3 +439,11 @@ export interface RmDirOptions { */ recursive?: boolean; } + +export interface ListFilesOptions { + /** + * If true, prepend given folder path to all file names. + * Default: false. + */ + prependPath: boolean; +}