Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/php-wasm/node/src/test/php.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PHPRequestHeaders,
PHPRunOptions,
RmDirOptions,
ListFilesOptions,
} from './universal-php';
import {
getFunctionsMaybeMissingFromAsyncify,
Expand Down Expand Up @@ -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 [];
Expand Down
8 changes: 8 additions & 0 deletions packages/php-wasm/universal/src/lib/universal-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}