Skip to content

Commit 45519d8

Browse files
authored
PHP: Add prependPath option to listFiles method (#462)
## What? Add `prependPath` option to `PHP.listFiles` method, which will prepend given folder path to every file found in it. ## Why? It's a common need to iterate over a list of files with each file path being an accessible path instead of only the file name. Idea mentioned in: - #427 (comment) For example, this is a common pattern: ```ts const files = await playground.listFiles( folderPath ) for (const file of files) { const filePath = `${folderPath}/${file}`; ... } ``` Also expressed as: ```ts const filePaths = (await playground.listFiles( folderPath )).map( (name: string) => `${folderPath}/${name}`) ) ``` With the new option, the above can be simplified as: ```ts const filePaths = await playground.listFiles(folderPath, { prependPath: true }) ``` ## How? - [x] Add `prependPath` option to `BasePHP.listFiles` method - [x] Document the option and what it does - [x] Add test ## Testing Instructions <!-- Please include step by step instructions on how to test this PR. --> 1. Check out the branch. 2. Run `nx test playground-blueprints`
1 parent 069930c commit 45519d8

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

packages/php-wasm/node/src/test/php.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ describe.each(SupportedPHPVersions)('PHP %s', (phpVersion) => {
152152
'test2.txt',
153153
]);
154154
});
155+
156+
it('listFiles() option prependPath should prepend given path to all files returned', () => {
157+
php.mkdir(testDirPath);
158+
php.writeFile(testDirPath + '/test.txt', 'Hello World!');
159+
php.writeFile(testDirPath + '/test2.txt', 'Hello World!');
160+
expect(php.listFiles(testDirPath, { prependPath: true })).toEqual([
161+
testDirPath + '/test.txt',
162+
testDirPath + '/test2.txt',
163+
]);
164+
});
155165
});
156166

157167
describe('Stdio', () => {

packages/php-wasm/universal/src/lib/base-php.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
PHPRequestHeaders,
1515
PHPRunOptions,
1616
RmDirOptions,
17+
ListFilesOptions,
1718
} from './universal-php';
1819
import {
1920
getFunctionsMaybeMissingFromAsyncify,
@@ -507,14 +508,22 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
507508

508509
/** @inheritDoc */
509510
@rethrowFileSystemError('Could not list files in "{path}"')
510-
listFiles(path: string): string[] {
511+
listFiles(
512+
path: string,
513+
options: ListFilesOptions = { prependPath: false }
514+
): string[] {
511515
if (!this.fileExists(path)) {
512516
return [];
513517
}
514518
try {
515-
return this[__private__dont__use].FS.readdir(path).filter(
519+
const files = this[__private__dont__use].FS.readdir(path).filter(
516520
(name: string) => name !== '.' && name !== '..'
517521
);
522+
if (options.prependPath) {
523+
const prepend = path.replace(/\/$/, '');
524+
return files.map((name: string) => `${prepend}/${name}`);
525+
}
526+
return files;
518527
} catch (e) {
519528
console.error(e, { path });
520529
return [];

packages/php-wasm/universal/src/lib/universal-php.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,11 @@ export interface RmDirOptions {
439439
*/
440440
recursive?: boolean;
441441
}
442+
443+
export interface ListFilesOptions {
444+
/**
445+
* If true, prepend given folder path to all file names.
446+
* Default: false.
447+
*/
448+
prependPath: boolean;
449+
}

0 commit comments

Comments
 (0)