Star

Created With

linkFiles

One of the most common use cases of task pipelines is doing something with several files. RxLine comes with utility functions in rxline/fs to make that super convenient.

Assume we have the following file structure:

1./

2-| bavaria/

3-|-------| munich/

4-|-------|-------| index.js

5-|-------|-------| schwabing.js

6-|-------| garching.js

7-|-------| augsburg.ts

8-| index.js

9-| berlin.js

You can get all files like this:

1import { files } from 'rxline/fs';

2

3files('.')

4 .peek(f => console.log(f.path))

5 .process();

6

7// Result:

8// > index.js

9// > berlin.js

10// > bavaria/augsburg.ts

11// > bavaria/garching.js

12// > bavaria/munich/index.js

13// > bavaria/munich/schwabing.js

files() creates a line with File objects representing all files in given address.

You can turn off the recursive scan:

1files('.', { recursive: false })

2 .peek(f => console.log(f.path))

3 .process();

4

5// Result:

6// > index.js

7// > berlin.js

You can change scan root:

1files('.', { root: 'bavaria' })

2 .peek(f => console.log(f.path))

3 .process();

4

5// Result:

6// > garching.js

7// > augsburg.ts

8// > munich/index.js

9// > munich/schwabing.js

Or specify search directory:

1files('munich', { root: 'bavaria' })

2 .peek(f => console.log(f.path))

3 .process();

4

5// Result:

6// > munich/index.js

7// > munich/schwabing.js


linkPath Filtering

1import { files, pathMatch } from 'rxline/fs';

2

3files('bavaria') // --> scan files in `bavaria`

4 .pick(pathMatch(/\.js$/)) // --> pick those with `.js` extension

5 .peek(f => console.log(f.path))

6 .process();

7

8// Result:

9// > bavaria/garching.js

10// > bavaria/munich/index.js

11// > bavaria/munich/schwabing.js

1files('.')

2 .drop(pathMatch(/\/munich\//)) // --> drop files in `munich` folder

3 .peek(f => console.log(f.path))

4 .process();

5

6// Result:

7// > index.js

8// > berlin.js

9// > bavaria/garching.js

10// > bavaria/augsburg.ts


linkReading Files

By default, the content of the files are not loaded:

1files('munich', { root: 'bavaria')

2 .peek(f => console.log(f.path + ' : ' + f.content))

3 .process();

4

5// Result:

6// > munich/index.js : undefined

7// > munich/schwabing.js : undefined

You can load file contents using readFile():

1import { files, readFile } from 'rxline/fs';

2

3files('munich', { root: 'bavaria')

4 .pipe(readFile())

5 .peek(f => console.log(f.path + ' : ' + f.content.split('\n').length + ' lines'))

6 .process();

7

8// Result:

9// > munich/index.js : 56 lines

10// > munich/schwabing.js : 215 lines


linkModifying and Saving

You can use mapContent() to modify file contents, and writeFile() to save the files:

1import { concurrently } from 'rxline';

2import { files, pathMatch, readFile, writeFile, mapContent } from 'rxline/fs';

3

4files('.', { root: 'bavaria' }) // --> all files with root: `bavaria/`

5 .pick(pathMatch(/\.js$/)) // --> pick `.js` files

6 .pipe(readFile(), // --> read'em

7 mapContent(

8 (content, path) => `/** file: ${path} **/\n` + content // --> modify the content (in memory)

9 ),

10 writeFile()) // --> save them

11 .process(concurrently); // --> all in parallel.

12

13// Adds first line `/** file: garching.js **/` to `bavaria/garching.js`

14// Adds first line `/** file: munich/index.js **/ to `bavaria/munich/index.js`

15// Adds first line `/** file: munich/schwabing.js **/ to `bavaria/munich/schwabing.js`


linkModifying Path & Extension

mapPath() modifies each file's path:

1import { files, readFile, writeFile, mapPath, pathMatch } from 'rxline/fs';

2

3files('.')

4 .drop(pathMatch(/\.js$/)) // --> drop all `.js` files

5 .pipe(readFile(), // --> read the rest

6 mapPath(path => path + '.bak'), // --> add `.bak` to their path

7 writeFile()) // --> save them.

8 .process();

9

10// Creates `bavaria/augsburg.ts.bak`, a copy of `bavaria/augsburg.ts`.

mapExt() modifies each file's extension:

1import { files, readFile, writeFile, mapExt } from 'rxline/fs';

2

3files('.', { root : 'bavaria' })

4 .pipe(readFile(), // --> read the files

5 mapExt(ext => 'bak' + ext), // --> suffix `bak` to their extension

6 writeFile()) // --> save them.

7 .process();

8

9// Creates `bavaria/augsburg.bak.ts`, a copy of `bavaria/augsburg.ts`.

10// Creates `bavaria/garching.bak.js`, a copy of `bavaria/garching.js`.

11// Creates `bavaria/munich/index.bak.js`, a copy of `bavaria/munich/index.js`.

12// Creates `bavaria/munich/schwabing.bak.js`, a copy of `bavaria/munich/schwabing.js`.

mapRoot() modifies the root folder (so you can easily move files somewhere else):

1import { files, readFile, writeFile, mapExt, mapRoot } from 'rxline/fs';

2

3files('munich', { root : 'bavaria' })

4 .pipe(readFile(), // --> read the files

5 mapRoot(() => 'backup'), // --> change their root to `backup/`

6 writeFile()) // --> save them.

7 .process();

8

9// Creates `backup/munich/index.js`, a copy of `bavaria/munich/index.js`.

10// Creates `backup/munich/schwabing.js`, a copy of `bavaria/munich/schwabing.js`.

FilesPath FilteringReading FilesModifying and SavingModifying Path & Extension

Home

Basicschevron_right
In Depthchevron_right