|
1 | | -# mfr |
2 | | -MFR stands for Mass File Renamer. It's a tool to rename all files according to the provided pattern in selected directory at once. |
| 1 | +# What is MFR? |
| 2 | + |
| 3 | +MFR stands for Mass File Rename. It's a Node.js tool to rename all files inside the selected directory. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +The simplest usage of MFR looks as in the code sample: |
| 8 | + |
| 9 | +```javascript |
| 10 | +import MFR from 'mfr'; |
| 11 | + |
| 12 | +const mfr = new MFR(); |
| 13 | + |
| 14 | +await mfr.run( options ); |
| 15 | +``` |
| 16 | + |
| 17 | +MFR also supports TypeScript: |
| 18 | + |
| 19 | +```typescript |
| 20 | + |
| 21 | +import MFR from 'mfr'; |
| 22 | + |
| 23 | +const mfr: MFR = new MFR(); |
| 24 | + |
| 25 | +await mfr.run( options ); |
| 26 | +``` |
| 27 | + |
| 28 | +## Options |
| 29 | + |
| 30 | +MFR needs to be configured to work properly: |
| 31 | + |
| 32 | +* **REQUIRED**: `mode: string` - determines the library mode. Basically, there are 2 modes: |
| 33 | + * `random` - file names are [uuid]( https://en.wikipedia.org/wiki/Universally_unique_identifier )s, |
| 34 | + * `string` - file names base on the provided `pattern`. |
| 35 | +* **REQUIRED**: `sourcePath: string` - the path to the source directory. |
| 36 | +* **REQUIRED**: `destinationPath: string` - the destination path. The directory will be created if not exists. |
| 37 | +* **OPTIONAL**: `formats: string | string[]` - the file format or array of formats. Only files with provided formats will be renamed. If not provided, all files will be renamed. |
| 38 | +* **OPTIONAL**: `pattern: string` - the pattern for the `string` mode. **REQUIRED** if the `string` is selected. |
| 39 | +* **OPTIONAL**: `appendix: 'prefix' | 'postfix' | 'all' | 'none'` - determines if generated names should have prefixes and postfixes. Basically set to `none`. |
| 40 | +* **OPTIONAL**: `prefixPattern: string | function` - pattern for the prefix. **REQUIRED** if the `appendix` option is set to `prefix` or `all`. |
| 41 | +* **OPTIONAL**: `postfixPattern: string | function` - pattern for the postfix. **REQUIRED** if the `appendix` option is set to `postfix` or `all`. |
| 42 | + |
| 43 | +## Custom drivers and generators |
| 44 | + |
| 45 | +To create an additional MFR behavior or modify the existing one, you can use drivers and generators. |
| 46 | + |
| 47 | +Generators are responsible for returning only the core file name, without prefixes and postfixes. |
| 48 | + |
| 49 | +```typescript |
| 50 | +import { IGenerator, IOptions } from 'mfr'; |
| 51 | + |
| 52 | +class CustomGenerator implements IGenerator { |
| 53 | + // Options are available as a constructor parameter. |
| 54 | + public constructor( private readonly _options: IOptions ) {} |
| 55 | + |
| 56 | + public generate(): string {} |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Drivers should use generators for creating the full file name ( without extension ) with a prefix and a postfix. |
| 61 | +Also, additional input/options validation should be added here. |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { Driver, IGenerator, IOptions, IDriver } from 'mfr'; |
| 65 | + |
| 66 | +// If you'd like to keep the original appendix validation mechanism extend the Driver class. |
| 67 | +// Otherwise, extend the IDriver interface. |
| 68 | +export default class CustomDriver extends Driver { |
| 69 | + // Specify a unique driver name. |
| 70 | + public static driverName: string = 'custom'; |
| 71 | + |
| 72 | + public constructor( |
| 73 | + // Generatoris available as the first constructor parameter. |
| 74 | + generator: IGenerator, |
| 75 | + // Options are available as the second constructor parameter. |
| 76 | + options: IOptions |
| 77 | + ) { |
| 78 | + super( generator, options ); |
| 79 | + } |
| 80 | + |
| 81 | + // The method should return the full file name without the extension. |
| 82 | + public getFilename(): string {} |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +You can also add custom properties to the provided `options` if you need. |
| 87 | + |
| 88 | +After creating your custom driver and generator, you need to provide it to MFR. |
| 89 | + |
| 90 | +```typescript |
| 91 | +import MFR from 'mfr'; |
| 92 | + |
| 93 | +import { CustomGenerator, CustomDriver } from './some-destination'; |
| 94 | + |
| 95 | +const mfr: MFR = new MFR( [ { driver: CustomDriver, generator: CustomGenerator } ] ); |
| 96 | + |
| 97 | +await mfr.run( { mode: 'custom' } ); |
| 98 | +``` |
| 99 | + |
| 100 | +You can also use custom file system instead of the native Node.js module. |
| 101 | + |
| 102 | +```typescript |
| 103 | +import MFR from 'mfr'; |
| 104 | + |
| 105 | +import customFileSystem from './some-destination'; |
| 106 | + |
| 107 | +const mfr: MFR = new MFR( [], customFileSystem ); |
| 108 | +``` |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +Noticed a bug or a problem? Report it on [GitHub repository](https://github.com/elszczepano/mfr/issues). |
| 113 | +Before contribute please check the `CONTRIBUTING.md` file. |
0 commit comments