Skip to content

Commit 07a28f6

Browse files
authored
Introduce MFR
Deliver basic library functionality. For more details see README.md.
1 parent b7d7e38 commit 07a28f6

34 files changed

+1223
-4
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = tab
6+
tab_width = 4
7+
charset = utf-8
8+
end_of_line = lf
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.json]
13+
indent_style = space
14+
indent_size = 2
15+
tab_width = 2

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
dist-tests

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require( '@cksource-cs/eslint-config-cs-module/eslint-patch-dependency-loader' );
2+
3+
module.exports = {
4+
'root': true,
5+
'extends': '@cksource-cs/eslint-config-cs-module/typescript'
6+
};

.gitignore

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
.idea
1+
# IDE files
2+
.idea
3+
4+
# External packages
5+
node_modules
6+
package-lock.json
7+
8+
# Build files
9+
dist
10+
dist-tests
11+
tsconfig.tsbuildinfo
12+
13+
# Tests output
14+
coverage
15+
tests/assets/destination-*

.nycrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"exclude": [
3+
"dist/index.js",
4+
"dist/**/*.d.ts"
5+
],
6+
"include": [
7+
"dist/**"
8+
],
9+
"reporter": [
10+
"lcov",
11+
"text",
12+
"text-summary"
13+
],
14+
"all": true,
15+
"exclude-after-remap": false,
16+
"temp-directory": "coverage/.nyc_output"
17+
}

CONTRIBUTING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Reporting a bug
2+
3+
1. Before reporting a bug, make sure the bug is on the library side.
4+
2. To the bug report please attach a description, and the reproduction steps. Expected and actual results are welcome.
5+
6+
## Contributing
7+
8+
1. Every added change should be covered by a unit test.
9+
2. Every test **has to** pass.
10+
3. The code should not have any linting errors.
11+
4. The TypeScript code should not have any errors.
12+
5. Every new feature has to be documented.
13+
6. Update the project version according to the [Semantic Versioning](https://semver.org/).
14+
7. Do not forget to update the `CONTRIBUTORS.md` file.

CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# MFR Contributors
2+
- [Dominik Szczepaniak](https://github.com/elszczepano)

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Dominik Szczepaniak
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,113 @@
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.

ava.config.cjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
process.env.TS_NODE_PROJECT = './tests/tsconfig.build.json';
2+
3+
const os = require( 'os' );
4+
const cpuCount = os.cpus().length;
5+
6+
module.exports = {
7+
concurrency: cpuCount * 2,
8+
extensions: [
9+
'js'
10+
],
11+
files: [
12+
'dist-tests/**/*'
13+
],
14+
require: [
15+
'tsconfig-paths/register'
16+
],
17+
timeout: '30s'
18+
};

0 commit comments

Comments
 (0)